codeforces --- Round #244 (Div. 2) A. Police Recruits

6237 단어 codeforces
A. Police Recruits
The police department of your city has just started its journey. Initially, they don’t have any manpower. So, they started hiring new recruits in groups.
Meanwhile, crimes keeps occurring within the city. One member of the police force can investigate only one crime during his/her lifetime.
If there is no police officer free (isn't busy with crime) during the occurrence of a crime, it will go untreated.
Given the chronological order of crime occurrences and recruit hirings, find the number of crimes which will go untreated.
Input
The first line of input will contain an integer n (1 ≤ n ≤ 105), the number of events. The next line will contain n space-separated integers.
If the integer is -1 then it means a crime has occurred. Otherwise, the integer will be positive, the number of officers recruited together at that time. No more than 10 officers will be recruited at a time.
Output
Print a single integer, the number of crimes which will go untreated.
Sample test(s)
Input
3
-1 -1 1

Output
2

Input
8
1 -1 1 -1 -1 1 1 1

Output
1

Input
11
-1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1

Output
8

Note
Lets consider the second example:
  • Firstly one person is hired.
  • Then crime appears, the last hired person will investigate this crime.
  • One more person is hired.
  • One more crime appears, the last hired person will investigate this crime.
  • Crime appears. There is no free policeman at the time, so this crime will go untreated.
  • One more person is hired.
  • One more person is hired.
  • One more person is hired.

  • The answer is one, as one crime (on step 5) will go untreated.
    [제목 대의]
    파출소 안의 모든 경찰은 같은 시간에 한 사건만 처리할 수 있다. -1을 입력하면 한 사건이 발생했다는 것을 의미하고, 정수 a를 입력하면 a명의 경찰을 모집하여 들어왔다는 것을 의미한다. 그리고 얼마나 많은 사건이 제때에 처리되지 않았는지 통계해 보라고 한다.
    왜'적시에 빨간색을 처리합니까? 왜냐하면 이 문제의 사고 오류는 바로 여기에 있기 때문입니다. 만약에 제때성을 상관하지 않는다면 직접 통계를 하면 됩니다. 제때성은 모든 입력을 한 후에 사건은 0을 삭제해야 한다는 것을 의미합니다(sum1=0).
    source code:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int n;
        int i;
        int a;
        int sum1=0;
        int sum2=0;
        int cnt=0;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%d",&a);
            if(a<0)
                sum1++;
            else
            {
                if(a>10)
                    a=10;
                sum2+=a;
            }
            if(sum2>=sum1)   //
            {
                sum2-=sum1;
            }
            else             //        
            {
                sum1-=sum2;
                if(a<0)
                  cnt++;
            }
            sum1=0;         //      
        }
        printf("%d
    ",cnt); return 0; }

    another way:
    Maintain a variable, sum. Initially sum=0, it keeps the number of currently free police officers. With every recruitment operation, add the number of officers recruited at that time to sum. When a crime occurs, if sum > 0 then decrease the number of free officers by one, otherwise no officers are free so the crime will go untreated.

    좋은 웹페이지 즐겨찾기