Fork me on GitHub

poj3045 Cow Acrobats (思维,贪心)

题目:

poj3045 Cow Acrobats

解析:

贪心题,类似于国王游戏
考虑两个相邻的牛\(i\),\(j\)
设他们上面的牛的重量一共为\(sum\)
\(i\)放在上面,危险值分别为\(x_1=sum-s_i\),$ x_2=sum+w_i-s_j$
\(j\)放在上面,危险值分别为\(x_3=sum-s_j\), \(x_4=sum+w_j-s_i\)
若把j放在上面更优,则有\(max(x_3,x_4)<max(x_1,x_2)\)
有四种情况
\(x_3<x_1\)
\(x_3<x_2\)
\(x_4<x_1\)
\(x_4<x_2\)
显然的是\(x_3<x_2\)\(x_1>x_4\)

\(s_i\)\(s_j\)关系不确定
所以一定有\(w_i+s_i>w_j+w_j\)
\(w+s\)排序从小到大排序,大的在下面

代码:

很简单

#include <iostream>
#include <algorithm>
#include <cstdio>
#define int long long
using namespace std;

const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;

int n;

struct node {
    int w, s;
    bool operator <(const node &oth) const {
        return w + s < oth.w + oth.s;
    }
} e[N];

template<class T>inline void read(T &x) {
    x = 0; char ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
    return;
}

signed main() {
    read(n);
    for (int i = 1; i <= n; ++i) read(e[i].w), read(e[i].s);
    sort(e + 1, e + 1 + n);
    int sum = 0, ans = -INF;
    for (int i = 1; i <= n; ++i) ans = max(ans, sum - e[i].s), sum += e[i].w;
    cout << ans << endl;
}
posted @ 2019-10-11 14:14 Chrety 阅读(...) 评论(...) 编辑 收藏