【解题报告】 耍杂技的牛
解题思路:
排序+贪心
这个牛啊实际上就是大臣,约翰那就是国王,你有没有想到一道题目,没错,那就是《国王游戏》,但是没错,那道题需要高精度,这道题目不需要高精度,所以这道题目就简单多了,这道题就是按(w+s)把牛牛们从小到大排序一下,然后计算出它们的风险值,找出最大的就可以了,还是比较简单的
AC代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int maxn=50010; long long n; long long d[maxn]; long long res=-0x3f3f3f3f,sum=0; struct cow { long long w; long long s; }a[maxn]; bool cmp(cow a,cow b) { return a.s+a.w<b.s+b.w; } int main() { cin>>n; for(int i=1;i<=n;i++) cin>>a[i].w>>a[i].s; sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++) { res=max(res,sum-a[i].s); sum+=a[i].w; } cout<<res<<endl; return 0; }
|