C - Exam in BerSU (easy version)

 http://codeforces.com/problemset/problem/1185/C1

题目大意:

有n个学生排队依次进行考试(一次只能有一名同学进行考试,俺也不知道为啥,俺也不敢问),考试时间共计m,每个考生考试需要若干时间,考试时间结束后,没有时间考试的同学就不能考了。输入分两行,第一行为学生人数和考试时间,第二行输入每个考生考试需要多少时间。要去输出按照输入的考试时间来看,该同学如果想通过考试,他前面最少有几个同学不能参加考试(因为如果每个人都考的话,时间不够用)。题目保证数据正确(不会有某个学生通过考试的时间比考试总时间还长的情况)

这里用了前n求和数组。解决了我之前的如何将去除的同学的时间排除在排序外的难题;

#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <set>
using namespace std;
long long  a[10009] , vis[1000] , max1[10009];

int main()
{
    int n , m ;
    while(cin >> n >> m)
    {
        int  ans = 0 ;
        memset(max1, 0 , sizeof(max1));
        for(int i = 0 ; i < n ; i++)
        {
            cin >> a[i];
            if(i == 0)
                max1[i] += a[i];
            else
                max1[i] += a[i]+max1[i-1];
        }
        for(int i = 0 ; i < n ; i++)
        {
            if(i > 0)
            {
                if(max1[i] > m)
                {
                    sort(a , a + i);
                    for(int j = i - 1 ; j >= 0 ; j--)
                    {
                        max1[i] -= a[j];
                        ans++ ;
                        if(max1[i] <= m)
                            break ;
                    }
                }
            }
            vis[i] = ans ;
            ans = 0 ;
        }
        for(int i = 0 ; i < n - 1 ; i++)
            cout << vis[i] << " " ;
        cout << vis[n - 1] << endl ;


    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/nonames/p/11222081.html