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

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;
}
时间: 2024-10-04 20:26:25

GCJ 2015Q(Infinite House of Pancakes-贪心与枚举)的相关文章

Infinite House of Pancakes(贪心)

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-emp

GCJ 2015Q(Standing Ovation-贪心)

Problem It's opening night at the opera, and your friend is the prima donna (the lead female singer). You will not be in the audience, but you want to make sure she receives a standing ovation -- with every audience member standing up and clapping th

GCJ2015 Problem B. Infinite House of Pancakes

我这个弱B 一开始就认为对于一个数, 对半分肯定比其他的分配方案好,然后最终也没过 对半分不一定是最好的分配方案,比如, 9 可能分成3个3比对半分好 真不知道那些大牛是怎么思考问题的,我看了第一的代码,他枚举最终状态的最大值, 他是怎么思考问题的呢? 下面这个我改的蔡的暴力代码, 加上了枚举每一种分法,目前能过小数据, 大数据超时应该 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 5 usi

GCJ 2008 Round 1A A 排序贪心

题意:给你两个维数相同的向量,它们之中的元素可以任意交换位置,求它们内积的最大值. 思路:乍一看此题摸不着什么头脑,只是凭借直觉感觉一个升序一个降序求内积即可.这样的感觉有时是对的,有时是错的,如果实现复杂的话比赛中就不该冒这个险.最好简单的证明一下. 证明:先讨论二维向量的情况对于按升序排列的(x1,x2)按升序排列的(y1,y2),显然恰与思路想法相反,其内积减去思路想法的内积有: x1y1+x2y2?x1y2?x2y1=x1(y1?y2)+x2(y2?y1) 显然y1?y2≤0又x1?x2

POJ 2586 贪心+枚举

Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15626   Accepted: 7843 Description Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc

模拟,贪心,枚举(二)

#####例题二:高精度练习(没错,高精度也是模拟呀) 此处我就不给题目描述了,反正就是unsigned long long也存不下的数字搞运算,除法和取模比较困难,这里只讲加减乘. 题目评测的话可以参照codevs高精度练习系列. [分析] 高精度的核心实际上就是模拟人列竖式手算的过程.但是细节的处理才是写好高精度的关键. 首先就是数字的存储:既然没办法存在变量里面,我们可以用一个字符串来存储,毕竟我们看到一个超长的数字也是一位一位的读下来对吧.那么直接string,然后cin好了~ 然后是运

BZOJ 1293: [SCOI2009]生日礼物 贪心

1293: [SCOI2009]生日礼物 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2513  Solved: 1370[Submit][Status][Discuss] Description 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠,但多个彩珠也可以出现在同一个位置上. 小布生日快到了,于是小西打算剪一段彩带送给小

贪心思维 专题记录 2017-7-21

A.UVa 10382 - Watering Grass 题目大意: 有一块草坪,长为l,宽为w,在它的水平中心线上有n个位置可以安装喷水装置,各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆.求出最少需要的喷水装置个数. 思路 :转化一下 将二维降成一维      d = sqrt(1.0*r*r-w*w/4.0) 接着就是区间覆盖问题了 #include <bits/stdc++.h> using namespace std; const int maxn = 10000+10;

HDU 3697 Selecting courses(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3697 Problem Description A new Semester is coming and students are troubling for selecting courses. Students select their course on the web course system. There are n courses, the ith course is available