让上面的w和s都尽可能小,即w+s升序排序,值小的放在上面。这样一来,不管是w很大s很小或者s很大w很小,都可以使得最大的PDV尽可能地小,可以用笔写写看。
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; typedef __int64 LL; const int MAXN = 100100; int n; struct Node { LL w, s; }t[MAXN]; bool cmp(const Node a, const Node b) { return a.w + a.s < b.w + b.s; } int main() { while(scanf("%d", &n) != EOF) { for(int i = 0; i < n; i ++) { scanf("%I64d%I64d", &t[i].w, &t[i].s); } sort(t, t + n, cmp); LL e = t[0].w; LL ans = - t[0].s; for(int i = 1; i < n; i ++) { LL tt = e - t[i].s; e += t[i].w; if(tt > ans) ans = tt; } if(ans < 0) puts("0"); else printf("%I64d\n", ans); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步