hdu 4296 贪心
Posted zzq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 4296 贪心相关的知识,希望对你有一定的参考价值。
Buildings
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4257 Accepted Submission(s): 1578
Problem Description
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 wi and strength si. When floors are stacked up, each floor has PDV(Potential Damage Value) equal to (Σwj)-si, where (Σwj) 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.
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 wi and strength si. When floors are stacked up, each floor has PDV(Potential Damage Value) equal to (Σwj)-si, where (Σwj) 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.
Input
There’re several test cases.
In each test case, in the first line is a single integer N (1 <= N <= 105) denoting the number of building’s floors. The following N lines specify the floors. Each of them contains two integers wi and si (0 <= wi, si <= 100000) separated by single spaces.
Please process until EOF (End Of File).
In each test case, in the first line is a single integer N (1 <= N <= 105) denoting the number of building’s floors. The following N lines specify the floors. Each of them contains two integers wi and si (0 <= wi, si <= 100000) separated by single spaces.
Please process until EOF (End Of File).
Output
For each test case, your program should output a single integer in a single line - the minimal PDV of the whole building.
If no floor would be damaged in a optimal configuration (that is, minimal PDV is non-positive) you should output 0.
If no floor would be damaged in a optimal configuration (that is, minimal PDV is non-positive) you should output 0.
Sample Input
3
10 6
2 3
5 4
2
2 2
2 2
3
10 3
2 5
3 3
Sample Output
1
0
2
题目大意是给出具有两个属性的物体,计算出如何堆放使得最大的PDV值越小,,,这个贪心不看题解,,真的想不到啊。。。
感觉贪心有点数学证明的意思,,蛤
我们不妨设有两个相邻的物体 a(wi,si),b(wj,sj),假设a在方案一中这个位置上方总w为SUM.显然只有两种情况 a上b下或者a下b上,我们就这个方案讨论最优解,
对于方案1,PDV(a)=SUM-si,PDV(b)=SUM+wi-sj;
对于方案二,PDV(b)=SUM-sj,PDV(a)=SUM+wj-si;
如果想要方案一更优的话,则有PDV(b)<PDV(a)-->wi-sj<wj-si-->wi+si<wj+sj
显然将w+s总和较小的放在上面更优!
#include<bits/stdc++.h>
using namespace std;
#define LL long long
struct node
{
LL w,s;
bool operator<(const node &chs)const{
return w+s<chs.w+chs.s;
}
}P[100005];
int main()
{
int n,m,i,j,k;
while(cin>>n){
for(i=0;i<n;++i) scanf("%lld%lld",&P[i].w,&P[i].s);
sort(P,P+n);
LL sum=P[0].w,ans=0;
for(i=1;i<n;sum+=P[i++].w){
ans=max(ans,sum-P[i].s);
}
if(ans<0) ans=0;
cout<<ans<<endl;
}
return 0;
}
using namespace std;
#define LL long long
struct node
{
LL w,s;
bool operator<(const node &chs)const{
return w+s<chs.w+chs.s;
}
}P[100005];
int main()
{
int n,m,i,j,k;
while(cin>>n){
for(i=0;i<n;++i) scanf("%lld%lld",&P[i].w,&P[i].s);
sort(P,P+n);
LL sum=P[0].w,ans=0;
for(i=1;i<n;sum+=P[i++].w){
ans=max(ans,sum-P[i].s);
}
if(ans<0) ans=0;
cout<<ans<<endl;
}
return 0;
}
以上是关于hdu 4296 贪心的主要内容,如果未能解决你的问题,请参考以下文章