1 条题解
-
0xuyifeng LV 10 MOD @ 2017-08-23 14:25:14
---------------------------------------------AC code---------------------------------------------
#include<cstdio> #include<algorithm> #ifdef WIN32 # define outl "%I64d" #else # define outl "%lld" #endif using namespace std; inline int read(){ int x = 0; bool f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = 0; ch = getchar(); } while(ch >= '0' && ch <= '9'){ x = x * 10 + ch - '0'; ch = getchar(); } return f ? x : -x; } typedef long long LL; const int MAXN = 100005; int CASES, n; LL ans, remain; struct P{ int a, b, c;//a:die; b:involve; c:survive; }p[MAXN]; bool cmp(P x, P y){ return x.c > y.c; } int main(){ // freopen("army.in", "r", stdin); // freopen("army.out", "w", stdout); CASES = read(); while(CASES--){ n = read(); for(int i = 1; i <= n; i++) p[i].a = read(), p[i].b = read(), p[i].c = p[i].b - p[i].a; sort(p+1, p+n+1, cmp); ans = 0, remain = 0; for(int i = 1; i <= n; i++){ if(remain < p[i].b){ ans += p[i].b - remain; remain = (LL)p[i].c; } else{ remain -= (LL)p[i].b; remain += (LL)p[i].c; } } printf(outl"\n", ans); } return 0; }
- 1