思路:
贪心。按照w+s排序。证明思路是从最优解在不损失最优性的前提下可以一步步变换到贪心解。
实现:
1 #include <cstdio> 2 #include <vector> 3 #include <algorithm> 4 using namespace std; 5 typedef long long ll; 6 typedef pair<ll, ll> pll; 7 bool cmp(pll x, pll y) 8 { 9 return x.first + x.second < y.first + y.second; 10 } 11 12 int main() 13 { 14 int n; 15 ll w, s; 16 scanf("%d", &n); 17 vector<pll> v(n); 18 for (int i = 0; i < n; i++) 19 { 20 scanf("%lld %lld", &v[i].first, &v[i].second); 21 } 22 sort(v.begin(), v.end(), cmp); 23 ll sum = 0, ans = -0x3f3f3f3f; 24 for (int i = 0; i < n; i++) 25 { 26 ans = max(ans, sum - v[i].second); 27 sum += v[i].first; 28 } 29 printf("%lld ", ans); 30 return 0; 31 }