程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> hdu 4296 Buildings 貪心算法 今日首A 詳細解析 ,有些數據類型最好用long long

hdu 4296 Buildings 貪心算法 今日首A 詳細解析 ,有些數據類型最好用long long

編輯:關於C++

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 
#include 
#include 
#include 
#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)
		{www.2cto.com
			sum -= f[i].w ;
			if(sum-f[i].s>pdv)
			{
				pdv = sum-f[i].s ;
			}
		}
		if(pdv<=0)
		{
			puts(0) ;
		}
		else
		{
			printf(%I64d
,pdv) ;
		}
	}
	return 0 ;
}
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved