http://www.lydsy.com/JudgeOnline/problem.php?id=1629
这题我想了很久都没想出来啊。。。
其实任意两头相邻的牛交换顺序对其它牛是没有影响的。。
那么我们考虑哪个在前。。(假设现在是待交换的是 a和b,a<b)
当重b-力a < 重a-力b时,就不需交换,否则交换。。
这个贪心自己想想就懂了的。。
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #include <queue> using namespace std; #define rep(i, n) for(int i=0; i<(n); ++i) #define for1(i,a,n) for(int i=(a);i<=(n);++i) #define for2(i,a,n) for(int i=(a);i<(n);++i) #define for3(i,a,n) for(int i=(a);i>=(n);--i) #define for4(i,a,n) for(int i=(a);i>(n);--i) #define CC(i,a) memset(i,a,sizeof(i)) #define read(a) a=getint() #define print(a) printf("%d", a) #define dbg(x) cout << #x << " = " << x << endl #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; } inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } inline const int max(const int &a, const int &b) { return a>b?a:b; } inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=50005; int n; struct nod { int x, y; } p[N]; bool cmp(const nod &a, const nod &b) { return b.x-a.y<a.x-b.y; } int main() { read(n); for1(i, 1, n) read(p[i].x), read(p[i].y); sort(p+1, p+1+n, cmp); int sum=0, ans=(~0u>>1)+1; for3(i, n, 1) { ans=max(ans, sum-p[i].y); sum+=p[i].x; } print(ans); return 0; }
Description
Input
Output
Sample Input
10 3
2 5
3 3
Sample Output
OUTPUT DETAILS:
Put the cow with weight 10 on the bottom. She will carry the other
two cows, so the risk of her collapsing is 2+3-3=2. The other cows
have lower risk of collapsing.