NC50958. Cow Acrobats
描述
输入描述
* Line 1: A single line with the integer N.
* Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, and .
输出描述
* Line 1: A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.
示例1
输入:
3 10 3 2 5 3 3
输出:
2
说明:
OUTPUT DETAILS:C++14(g++5.4) 解法, 执行用时: 32ms, 内存消耗: 1772K, 提交时间: 2020-05-18 11:02:19
#include<bits/stdc++.h> using namespace std; const int N=5e4+10; struct node { int x,y; }a[N]; int sum[N],mx,n; bool cmp(node a,node b) { return a.x+a.y<b.x+b.y; } int main() { mx=INT_MIN; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y); sort(a+1,a+1+n,cmp); for(int i=1;i<=n;i++) sum[i]+=sum[i-1]+a[i].x; for(int i=1;i<=n;i++) mx=max(mx,sum[i-1]-a[i].y); printf("%d\n",mx); return 0; }
C++ 解法, 执行用时: 32ms, 内存消耗: 716K, 提交时间: 2021-11-23 21:16:53
#include<bits/stdc++.h> using namespace std; struct node{int w,s;}a[100001]; int n,ans=INT_MIN,sum; bool cmp(node x,node y){return x.w+x.s<y.w+y.s;} 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++)ans=max(ans,sum-a[i].s),sum+=a[i].w; cout<<ans; return 0; }