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.

在有很多人,每个人手中都有一个盘子,n个人的盘子中有食物,给出n个盘子中的食物份数,两个操作

1,将盘子中的食物分给其他人,任意

2,盘中有食物的人都吃掉一份食物

每一天可以选择其中一个操作,问最快的时间要吃完所有的食物的时间。

因为人数只有1000,食物最多也是1000,也已枚举将食物分好后,盘中食物最多的为j(1 <= j <= max1 )这样可以计算出需要的时间,找出最小的。

因为跑数据的原因,竟然错过了提交时间,,,,,

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
int a[1200] ;
int main() {
    int t , step = 0 ;
    int n , i , j , max1 , min1 , sum ;
    freopen("1.in","r",stdin) ;
    freopen("2.out","w",stdout) ;
    scanf("%d", &t) ;
    while( t-- ) {
        scanf("%d", &n) ;
        for(i = 0 ; i < n ; i++) {
            scanf("%d", &a[i]) ;
            max1 = max(max1,a[i]) ;
        }
        min1 = max1 ;
        for(i = 1 ; i <= max1 ; i++) {
            sum = i ;
            for(j = 0 ; j < n ; j++) {
                if( a[j] > i ) {
                    if( a[j]%i == 0 )
                        sum += (a[j]/i-1) ;
                    else
                        sum += (a[j]/i) ;
                }
            }
            min1 = min(min1,sum) ;
        }
        printf("Case #%d: %d\n", ++step, min1) ;
    }
    return 0 ;
}
时间: 2024-10-08 10:29:35

Infinite House of Pancakes(贪心)的相关文章

GCJ2015 Problem B. Infinite House of Pancakes

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

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

codeforces - 1245 (div2)

A - Good ol' Numbers Coloring 直接判断两个数是否互质 #include <stdio.h> #include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #pragma GCC optimize(2) #define mm(i,v) m

Problem 1567 - D - Sloth&#39;s Angry ( 递归+贪心)

Problem 1567 - D - Sloth's Angry Time Limit: 1000MS Memory Limit: 65536KB Total Submit: 326 Accepted: 113 Special Judge: No Description A forest is full of sloths, they are so eager for tree leaves and as a result, very angry. We assume that the fore

poj1328Radar Installation(贪心—区间选点)

题目链接: 啊哈哈,点我点我 题目: Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52037   Accepted: 11682 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small isl

poj1328雷达设置 贪心

Radar Installation Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only

贪心--区间覆盖及区间选点问题

区间覆盖: 数轴上有若干区间,选用最少的线段覆盖指定区间. 区间选点:选用最少的区间使每个区间内至少有一个点 样题1: J - Muddy roads Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Farmer John has a problem: the dirt road from his farm to town has suffered in the re

poj1328Radar Installation 贪心

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64472   Accepted: 14497 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

poj 1328 Radar Installation (贪心)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52823   Accepted: 11883 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca