B. No Time for Dragons 贪心

http://codeforces.com/gym/101149/problem/B

这是很基本的贪心题。

B题,考虑样例
1 1
1 1
1 1
1 1
999999 1
这里应该是999999就够了
如果是按伤亡排序的话,快排是不稳定的,如果先杀了前面那些,答案不是最优。
应该是把人数 - 伤亡最大的优先,因为这样留下来的人是最多的。

主要想写下怎么找最大值。

记ans为答案值,t为现在拥有的人数。 ans and t is zero before

如果t小于当前要拥有的人数,那么ans就应该加上他们的差值。

因为。ans表示满足前i个的最小值。然后拥有的人数不够的话,那是因为前面的扣费太多,ans加上差值,就会刚好满足第i个的扣费

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 2e5 + 20;
struct node {
    int val, kill;
    bool operator < (const struct node & rhs) const {
        return (val - kill) > (rhs.val - rhs.kill);
    }
}a[maxn];
int n;
void work() {
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i].val >> a[i].kill;
    }
    sort(a + 1, a + 1 + n);
    LL t = 0;
    LL ans = 0;
    for (int i = 1; i <= n; ++i) {
        if (t < a[i].val) {
            ans += (a[i].val - t);
            t = a[i].val;
        }
        t -= a[i].kill;
    }
    cout << ans << endl;
}

int main() {
#ifdef local
    freopen("data.txt","r",stdin);
#endif
    IOS;
    work();
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6038318.html