https://vjudge.net/problem/POJ-3045
读题后提取到一点:例如对最底层的牛来说,它的崩溃风险=所有牛的重量-(底层牛的w+s),则w+s越大,越在底层。
注意范围lb=-INF。
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 #include<map> 8 #define lson l, m, rt<<1 9 #define rson m+1, r, rt<<1|1 10 #define INF 0x3f3f3f3f 11 typedef long long ll; 12 using namespace std; 13 typedef struct{ 14 ll w, s; 15 ll sum; 16 }Node; 17 Node node[100010]; 18 ll n, sum=0; 19 int C(int x) 20 { 21 ll maxm = -INF, _sum=sum; 22 for(int i = 0; i < n; i++){ 23 ll tmp = _sum-node[i].sum; 24 _sum -= node[i].w; 25 if(tmp>x) return 0; 26 } 27 return 1; 28 } 29 bool cmp(const Node a, const Node b) 30 { 31 if(a.sum != b.sum) 32 return a.sum>b.sum; 33 else return a.s>b.s; 34 } 35 int main() 36 { 37 scanf("%lld", &n); 38 for(int i = 0; i < n; i++){ 39 scanf("%lld%lld", &node[i].w, &node[i].s); 40 node[i].sum = node[i].w+node[i].s; 41 sum += node[i].w; 42 } 43 sort(node, node+n, cmp); 44 ll lb=-INF, ub=INF; 45 while(ub-lb>1){ 46 ll mid = (lb+ub)>>1; 47 if(C(mid)){ 48 ub = mid; 49 } 50 else lb = mid; 51 } 52 printf("%lld\n", ub); 53 return 0; 54 }