Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr. Wolowitz, Dr. Blue.Mary has accomplished many great projects, one of which is the Guanghua Building.
The public opinion is that Guanghua Building is nothing more than one of hundreds of modern skyscrapers recently built in Shanghai, and sadly, they are all wrong. Blue.Mary the great civil engineer had try a completely new evolutionary building method in project of Guanghua Building. That is, to build all the floors at first, then stack them up forming a complete building.
Believe it or not, he did it (in secret manner). Now you are face the same problem Blue.Mary once stuck in: Place floors in a good way.
Each floor has its own weight w i and strength s i. When floors are stacked up, each floor has PDV(Potential Damage Value) equal to (Σw j)-s i, where (Σw j) stands for sum of weight of all floors above.
Blue.Mary, the great civil engineer, would like to minimize PDV of the whole building, denoted as the largest PDV of all floors.
Now, it’s up to you to calculate this value.
题意:
叠地板,每个地板都有它的重量w和强度s,
有一个概念pdv,pdv=Σw j -si;
Σw j代表当前这块地板上所有地板的重量,
要求我们最小化所有楼层中pdv的最大值。
输入:
每次输入N (1 <= N <= 105)
接下来的n行输入wi 和 si(0 <= w i, s i <= 100000)
输出:
每次输出最小化后的pdv
代码:
ll t,n,m;
string s;
struct node{
ll w,s;
}a[maxn];
bool cmp(node x,node y){
return x.w+x.s<y.w+y.s;
}
int main(){
while(~scanf("%lld",&n)){
for(int i=0;i<n;i++){
scanf("%lld %lld",&a[i].w,&a[i].s);
// cin>>a[i].w>>a[i].s;
}
sort(a,a+n,cmp);
ll sum=0,ans=0;
for(int i=0;i<n;i++){
ans=max(ans,sum-a[i].s);
sum+=a[i].w;
}
printf("%lld\n",ans);
// cout<<ans<<endl;
}
return 0;
}
这题也思考一下
①
假设第i块在下面,第 j 块在上面,
那么第 i 块 的 pdv 就是 ( wj + remain ) - si
那么第 j 块 的 pdv 就是 remain - sj
②
假设第 j 块在下面,第 i 块在上面,
那么第 i 块 的 pdv 就是 remain - si
那么第 j 块 的 pdv 就是 ( wi + remain ) - sj
假设第①种情况更优
__ 如果第 ① 种情况里 第 i 块 的 pdv 更大,
_____ 因为 remain - si 肯定小于 ( wj + remain ) - si
_____ 所以 第 ② 种情况里 ( wi + remain ) - sj 是最大的 pdv
_____ 又因为第 ① 种情况更优
_____ 所以 ( wj + remain ) - si < ( wi + remain ) - sj
_____ 即 wi + si < wj + sj
__ 如果第 ① 种情况里 第 j 块 的 pdv 更大,
_____ 因为第 ② 种情况里的 ( wi + remain ) - sj > remain - sj, remain - si < ( wj + remain ) - si
_____ 所以第 ② 种情况里 ( wi + remain ) - sj 是更大的pdv ,且必大于等于 remain - sj
所以只要使 wi + si < wj + sj ,就能使得 第 ① 种 情况更优