NBUT 1583 贪心

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值