codeforces --- 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.

 

posted @   北岛知寒  阅读(291)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
主题色彩