hdu 4296 Buildings 贪心算法 今日首A 详细解析 ,有些数据类型最好用long long

Buildings

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2278    Accepted Submission(s): 870

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.

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).

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.

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

这题算是简单的贪心,首先分析题目意思,就是有N块板,其中一块板的PDV = 在它上面的所有板重量-这个板的强度。然后求出这些板的PDV的最大值,问你怎么使这个最大值最小。

分析: 我们可以把公式写下来,pdv[i]=sum(w[i+1]->w[n])-s[i]
,这个式子等价于pdv[i]=sum(w[i]->w[n])-w[i]-s[i]
; 若使pdv最小,则即使需要w[i]+s[i]要达到最大 ,那好了,我们只需要按对w[i]+s[i]排序就可以,然后再求出最大的PDV。复杂度O(n*logn+n) ;n*logn是排序复杂度,我用的是STL里的sort排序。

还有一点就是有些数据类型需要是long long ,至少我的程序需要。

代码:

#include <cstdio>
#include <cstring>
#include <climits>
#include <algorithm>
#define MAX 100100
using namespace std ;
typedef long long ll ;

struct Floor{
	int w , s ;
}f[MAX];

bool cmp(const Floor &a , const Floor &b)
{
	return a.w+a.s>b.w+b.s ;
}

int main()
{
	int n ;
	while(~scanf("%d",&n))
	{
		ll sum = 0 ;
		for(int i = 0 ; i < n ; ++i)
		{
			scanf("%d%d",&f[i].w,&f[i].s) ;
			sum += f[i].w ;
		}
		sort(f,f+n,cmp) ;
		ll pdv = LLONG_MIN ;
		for(int i = 0 ; i < n ; ++i)
		{
			sum -= f[i].w ;
			if(sum-f[i].s>pdv)
			{
				pdv = sum-f[i].s ;
			}
		}
		if(pdv<=0)
		{
			puts("0") ;
		}
		else
		{
			printf("%I64d\n",pdv) ;
		}
	}
	return 0 ;
}

与君共勉

时间: 2024-10-25 22:53:42

hdu 4296 Buildings 贪心算法 今日首A 详细解析 ,有些数据类型最好用long long的相关文章

hdu 4296 Buildings(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the sto

hdu 1052(田忌赛马 贪心算法,sort排序)

Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18155    Accepted Submission(s): 5281 Problem Description Here is a famous story in Chinese history. "That was about

hdu 4296 Buildings(贪婪)

主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the st

HDU - 4296 Building (贪心)

题意:有n个板,每个板有重量和强度两个属性,把板叠在一起,对于每个板有个PDV值,计算方式为这个板上面的板的重量和减去这个板的强度, 然后求这些PDV中最大值(不同的排列方式会有不同的最大值)的最小值. 首先就是怎么叠放的问题,一般思路就是把轻的放上面,硬度大的放下面,一开始我就是这样写的,然后就一直wrong.(QAQ...) 换种思路,直接从题意中推导: 对于相邻放置的两块板,设两块板为i,j他们上面的重量为sum 1) a=sum-si;b=sum+wi-sj; 交换两个板的位置 2)a'

HDU 4950 Monster (贪心算法)

题目链接 题意:有一个怪物的血量为h,人攻击怪物,每回合可以杀掉a滴血,再回b滴血,k个回合之后人会休息一回合,即人不攻击而怪物回b滴血,问能否杀死.翻译过来就是给定一个数h,每轮可以先减a再加b,k轮后会有一轮只加b不减a,如果再这过程中存在h<=0就输出YES,否则输出NO. 题解:注意是先减a再加b,有可能不加b就已经h<=0,先分类讨论ab,如果a比b大那么只有可能第一回合杀死,一旦+b就不可能在杀死了,如果a比b小,讨论在经过k-1轮后第k轮只减不加能否<=0,再讨论k+1轮的

HDU 1009 FatMouse&#39; Trade (贪心算法)

题意:就是老鼠要用猫粮换粮食,第i个房间一些东西,要用东西去换,可以不全换.问给定的猫粮最多能换多少粮食. 析:贪心算法.我们先算出来每个房间物品的平均价格是多少,肯定越低越好,并且如果能全换就全换,如果不能, 肯定是最后一次了,就把剩下全部换了,看看能换多少.求和. 代码如下: #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <v

HDU 1009.FatMouse&#39; Trade【贪心算法】【8月16】

FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and req

hdu 1050(贪心算法)

Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19278    Accepted Submission(s): 6582 Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a

hdu 1789 Doing HomeWork Again (贪心算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 /*Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7903 Accepted Submission(s): 4680 Problem Description Ignatius has just c