标签:style blog class code java tar
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.
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.
Print a single integer, the number of crimes which will go untreated.
3
-1 -1 1
2
8
1 -1 1 -1 -1 1 1 1
1
11
-1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1
8
Lets consider the second example:
The answer is one, as one crime (on step 5) will go untreated.
【题目大意】
派出所里的每个警察同一时间只能处理一个案子,输入-1表示发生了一起案子,输入一个正数a表示招募了a个警察进来,然后让你统计有多少案子是未及时处理的。
为什么”及时处理“标红色?因为这题的思维误区就在这,如果是不管及时性,那直接统计就行了,及时性就意味着每一个输入后,案子是要清零的(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\n",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.
Round #244 (Div. 2) A. Police Recruits,布布扣,bubuko.com
Round #244 (Div. 2) A. Police Recruits
标签:style blog class code java tar
原文地址:http://www.cnblogs.com/acmer-jsb/p/3705624.html