GCJ 2015Q (Infinite House of pancakes-greed and enumeration)

Source: Internet
Author: User

Problem

At the Infinite House of Pancakes, there is only finitely many pancakes, but there is infinitely many diners who would b E Willing to eat them! When the restaurant opens for breakfast, among the infinitely many diners, exactlyDhave non-empty plates; TheITh of these hasPiPancakes on he or her plate. Everyone else has a empty plate.

Normally, every minute, every diner with a non-empty plate would eat one pancake from he or her plate. However, some minutes maySpecial. 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-emp TY) plate. No diners eat during a special minute, because it would be rude.

The head server on duty this morning, and it's your job to decide which minutes, if any, 'll be special, and WHI CH pancakes would move where. That's, every minute, can decide to either does nothing and let the diners eat, or declare a special minute and Interru PT the diners to make a single movement of one or more pancakes, as described above.

Breakfast ends when there is no more pancakes left to eat. How quickly can do that happen?

Input

The first line of the input gives the number of test cases, T. T test Cases follow. Each consists of a 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 th e 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  
 
31341 2 1 214
Case #1:3Case #2:2Case #3:3
In the case #1, the one diner starts with 3 pancakes and everyone else's plate is empty. One optimal strategy is:

Minute 1:do nothing. The diner would 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 is always infinitely many diners with empty plates available, no matter how many diners start off WI Th pancakes.) No pancakes is eaten during an interruption.

Minute 3:do nothing. Each of those diners would eat one of the last of the remaining pancakes.

In case #2, it's optimal to let the diners eat for 2 minutes, with no interruptions, during which time they would finish a ll the pancakes.

In the case #3, the 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, pancakes from the diner's plate to another diner ' s E Mpty plate, and then does nothing and let the diners eat for the second and third minutes.

At first, the greedy "max Pancake number" that binary

Lost in

2

3 9

On

Apparently Sp_time in the first place,

Then the time is determined only by the biggest one.

Enumerate the largest, a[i] into the <k of the fastest is not binary is, directly split into a[i]/k parts (surplus number +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)/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-greed and enumeration)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.