码迷,mamicode.com
首页 > 其他好文 > 详细

GCJ 2015Q(Infinite House of Pancakes-贪心与枚举)

时间:2015-04-12 16:19:03      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

Problem

At the Infinite House of Pancakes, there are only finitely many pancakes, but there are infinitely many diners who would be willing to eat them! When the restaurant opens for breakfast, among the infinitely many diners, exactly D have non-empty plates; the ith of these has Pi pancakes on his or her plate. Everyone else has an empty plate.

Normally, every minute, every diner with a non-empty plate will eat one pancake from his or her plate. However, some minutes may be special. In a special minute, the head server asks for the diners‘ attention, chooses a diner with a non-empty plate, and carefully lifts some number of pancakes off of that diner‘s plate and moves those pancakes onto one other diner‘s (empty or non-empty) plate. No diners eat during a special minute, because it would be rude.

You are the head server on duty this morning, and it is your job to decide which minutes, if any, will be special, and which pancakes will move where. That is, every minute, you can decide to either do nothing and let the diners eat, or declare a special minute and interrupt the diners to make a single movement of one or more pancakes, as described above.

Breakfast ends when there are no more pancakes left to eat. How quickly can you make that happen?

Input

The first line of the input gives the number of test cases, TT test cases follow. Each consists of one line with D, the number of diners with non-empty plates, followed by another line with D space-separated integers representing the numbers of pancakes on those diners‘ plates.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the smallest number of minutes needed to finish the breakfast.

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ D ≤ 6.
1 ≤ Pi ≤ 9.

Large dataset

1 ≤ D ≤ 1000.
1 ≤ Pi ≤ 1000.

Sample


Input 
 

Output 
 
3
1
3
4
1 2 1 2
1
4

Case #1: 3
Case #2: 2
Case #3: 3

In Case #1, one diner starts with 3 pancakes and everyone else‘s plate is empty. One optimal strategy is:

Minute 1: Do nothing. The diner will eat one pancake.

Minute 2 (special): Interrupt and move one pancake from that diner‘s stack onto another diner‘s empty plate. (Remember that there are always infinitely many diners with empty plates available, no matter how many diners start off with pancakes.) No pancakes are eaten during an interruption.

Minute 3: Do nothing. Each of those two diners will eat one of the last two remaining pancakes.

In Case #2, it is optimal to let the diners eat for 2 minutes, with no interruptions, during which time they will finish all the pancakes.

In Case #3, one diner starts with 4 pancakes and everyone else‘s plate is empty. It is optimal to use the first minute as a special minute to move two pancakes from the diner‘s plate to another diner‘s empty plate, and then do nothing and let the diners eat for the second and third minutes.


一开始贪心把【最大煎饼数】那个折半

输在

2

3 9

显然sp_time放在最前,

之后时间只由最大的那个决定

枚举那个最大的,把a[i]拆成<k 的 最快不是折半是,直接拆成a[i]/k份(有余数+1) 


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<queue>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXT (100+10)
#define MAXD (2000+10)
#define MAXPi (2000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int T,n,a[MAXD];
int main()
{
	freopen("B-large.in","r",stdin);
	freopen("B-large.out","w",stdout);
	
	cin>>T;
	For(kcase,T)
	{
		
		scanf("%d",&n);
		For(i,n) scanf("%d",&a[i]);
		sort(a+1,a+1+n);
		
		int ans=a[n];
		
		For(k,1000)
		{
			int sp=0;
			For(i,n)
				if (a[i]>k)
				{
					int l=ceil((double)a[i]/(double)k);
					sp+=l-1; 
				}
			
			
			ans=min(ans,sp+k);
		}
		
		
		printf("Case #%d: %d\n",kcase,ans);
	}
	
	return 0;
}




GCJ 2015Q(Infinite House of Pancakes-贪心与枚举)

标签:

原文地址:http://blog.csdn.net/nike0good/article/details/45009943

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!