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

http://codeforces.com/problemset/problem/427/A

题意:
警察抓小偷。-1表示小偷,正数表示招募的警察。
警察只能处理一个小偷。如果当前没有警察有空,则小偷就会逃跑。
求有几个小偷会逃之夭夭。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;

int main() {
int n;
scanf("%d", &n);
int plice = 0, crime = 0;
while (n--) {
int x;
scanf("%d", &x);
if (x == -1 && !plice) {
// 小偷跑得条件是,出现小偷且没有警察有空。
crime++;
} else {
plice += x;
}
}
printf("%d\n", crime);
return 0;
};