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

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:

  1. Firstly one person is hired.

  2. Then crime appears, the last hired person will investigate this
    crime.

  3. One more person is hired.

  4. One more crime appears, the last hired person will investigate this
    crime.

  5. Crime appears. There is no free policeman at the time, so this crime will
    go untreated.

  6. One more person is hired.

  7. One more person is hired.

  8. One more person is hired.

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.

时间: 2024-08-05 14:40:17

Round #244 (Div. 2) A. Police Recruits的相关文章

Codeforces Round #244 (Div. 2) A. Police Recruits

题目的意思就是找出未能及时处理的犯罪数, #include <iostream> using namespace std; int main(){ int n; cin >> n; int a,recruit = 0, crimes = 0;; for(int i = 0 ; i < n; ++ i){ cin >> a; if(a > 0) recruit+=a; else recruit?recruit-- : crimes++; } cout<&

Codeforces Round #244 (Div. 2)

A. Police Recruits B. Prison Transfer A,B两个是水题. C. Checkposts DFS找出所有的环就行了. 每次搜索一个结点u时,给u加一个递增标号low[u],同时记录搜索u及u的子结点过程中遇到的最小标号minc,也就是当搜索u的子结点v时,minc = min(minc, low[v]).搜索完成后,如果minc < low[u],说明搜索u的子结点时又回到了u的父结点,也就是说u在一个环中,然后求出这个环的最小费用及取到最小费用的结点数. D.

Codeforces Round #244 (Div. 2)——Match &amp; Catch

题目链接 题意:给两个长度分别为n和m的序列,现在有两种操作:1.分别选择两个序列的一个非空前缀,切两个前缀的最后一位相同,删除之,得到1分(只累计),消耗e:2.直接删除两个序列,消耗值定于两个序列之前删除的元素个数之和,并且使得得到的分有效(之前没有有效分) 分析: 首先,问题其实就是转化成,进行若干次操作1,然后进行操作2 还要找到一个判别标准,来评判较优的状态(贪心) 每次的消耗值比较大,其实可以计算出最大的删除次数,这个值不是很大 状态表示: 简单的,一个状态可以表示为串A的位置.串B

Codeforces Round #244 (Div. 2)——Checkposts

题目链接 题意: 给定n个点,每个点有一个权值的有向图.现在需要选定一些点,使得这些点权值和最小,且满足:如果i能到达j且j能到达i,那么i.j可以只选一个 分析: 强联通模板题 //使用时只更新G完成构图 //scc_cnt从1开始计数 //pre[]表示点在DFS树中的先序时间戳 //lowlink[]表示当前点和后代能追溯到的最早祖先的pre值 //sccno[]表示点所在的双连通分量编号 //vector<int> G保存每个点相邻的下一个点序号 //stack<Edge>

Codeforces Round #244 (Div. 2) B. Prison Transfer

题目是选出c个连续的囚犯,而且囚犯的级别不能大于t #include <iostream> using namespace std; int main(){ int n,t,c; cin >> n >> t >> c; int a,cnt = 0, res =0;; for(int i = 0 ; i < n ; ++ i) { cin >> a; if(a > t ){ if(cnt > c-1) res+=cnt-c+1;

Codeforces Round #244 (Div. 2)D (后缀自动机)

Codeforces Round #244 (Div. 2)D (后缀自动机) (标号为0的节点一定是null节点,无论如何都不能拿来用,切记切记,以后不能再错了) 这题用后缀自动机的话,对后缀自动机的很多性质有足够深刻的理解.没想过后缀数组怎么做,因为不高兴敲.... 题意:给出两个长度均不超过5000的字符串s1,s2,求这两个串中,都只出现一次的最短公共子串. 解题思路:求的是公共子串,然后对出现的次数又有限制,第一想法就是后缀自动机啊,后缀自动机处理子串出现次数再合适不过了.做法是这样的

Codeforces Round #244 (Div. 2)D (后缀自己主动机)

Codeforces Round #244 (Div. 2)D (后缀自己主动机) (标号为0的节点一定是null节点,不管怎样都不能拿来用,切记切记,以后不能再错了) 这题用后缀自己主动机的话,对后缀自己主动机的非常多性质有足够深刻的理解. 没想过后缀数组怎么做.由于不高兴敲... . 题意:给出两个长度均不超过5000的字符串s1,s2,求这两个串中,都仅仅出现一次的最短公共子串. 解题思路:求的是公共子串,然后对出现的次数又有限制,第一想法就是后缀自己主动机啊,后缀自己主动机处理子串出现次

Codeforces Round #244 (Div. 2)B. Prison Transfer(想法题)

传送门 Description The prison of your city has n prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer c of the prisoners to a prison located in another city. For this reason, he made the n prisoners to stand in

Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/427/B Description The prison of your city has n prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer c