NBUT 1583 贪心
暑期集训实在太热了,机房里的空调有时还不给力。所以有些新生想出去买饮料,又有些新生出去上厕所,有些新生出去洗把脸等等等等…
Zyvas坐在自己的座位上就这样看着他们走进来走出去,他突然想知道这个机房里原先至少有多少新生。
输入
第一行输入一个数 N(0 < N < 10,000).
接下来N行,每行有2个数 P(P == 0 || P == 1)和 M(0 < M < 100).
当 P == 0时,说明Zyvas看到有M个人出去了;
当 P == 1时,说明Zyvas看到有M个人进来了.
输出
输出这个机房原先最少的新生人数(自己不算)。
样例输入
2
1 2
0 3
3
1 1
1 2
0 3
样例输出
1
0
这道题坑了好几次,是想的不太全面,最后才想到怎么贪心。
用in记录当前在教室的人,每次人出去的时候,优先在这里减去,不够的话只能是教室里本来就有人,ans加上不够的人数。
代码:
/*************************************************************************
> File Name: g.cpp
> Author: gwq
> Mail: gwq5210@qq.com
> Created Time: 2015年05月01日 星期五 13时45分42秒
************************************************************************/
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;
const double esp = 1e-5;
int main(int argc, char *argv[])
{
int n;
while (scanf("%d", &n) != EOF) {
int in = 0;
int ans = 0;
for (int i = 0; i < n; ++i) {
int p, x;
scanf("%d%d", &p, &x);
if (p) {
in += x;
} else {
if (in >= x) {
in -= x;
} else {
ans += x - in;
in = 0;
}
}
}
printf("%d\n", ans);
}
return 0;
}