1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 5 const int maxn =100000+5; 6 struct node{ 7 int r, q; 8 }a[maxn]; 9 10 bool cmp(node x, node y){ 11 if(x.r - x.q != y.r - y.q) 12 return (x.r-x.q) > (y.r-y.q); 13 return x.r > y.r; 14 } 15 16 int main() 17 { 18 int n; 19 cin >> n; 20 for(int i =0 ;i<n;i++){ 21 cin >> a[i].r >> a[i].q; 22 } 23 sort(a,a+n,cmp); 24 int ans = 0; //总空间 25 int sum = 0; //剩余的空间 26 for(int i = 0;i < n;i++){ 27 if(sum < a[i].r){ 28 ans += a[i].r - sum; 29 sum = a[i].r; 30 } 31 sum -= a[i].q; //执行后剩下的空间 32 } 33 cout << ans << endl; 34 return 0; 35 }
原文地址:https://www.cnblogs.com/jaydenouyang/p/8995759.html
时间: 2024-11-12 07:29:15