1. 程式人生 > >2017Google Codejam round2 Problem A. Fresh Chocolate

2017Google Codejam round2 Problem A. Fresh Chocolate

Problem

You are the public relations manager for a chocolate manufacturer. Unfortunately, the company's image has suffered because customers think the owner is cheap and miserly. You hope to undo that impression by offering a free factory tour and chocolate tasting.

Soon after starting the new project, you realized that the company owner's reputation is well-deserved: he only agreed to give away free chocolate if you would minimize the cost. The chocolate to be given away comes in packs of P

 pieces. You would like to open new packs for each tour group, but the owner insists that if there are leftover pieces from one group, they must be used with the next tour group before opening up any new packs.

For instance, suppose that each pack contains P=3 pieces, and that a tour group with 5 people comes. You will open two packs to give one piece to each person, and you will have one piece left over. Suppose that after that, another tour group with 6 people comes. They will receive the leftover piece, and then you will open two more packs to finish giving them their samples, and so you will have one piece left over again. If two groups with 4 people each come right after, the first of those will get the leftover piece plus a full pack, and the last 4 person group will get their pieces from two newly opened packs. Notice that you cannot open new packs until all leftovers have been used up, even if you plan on using all of the newly opened pack immediately.

In the example above, 2 out of the 4 groups (the first and last groups) got all of their chocolate from freshly opened packs. The other 2 groups got some fresh chocolate and some leftovers. You know that giving out leftovers is not the best way to undo the owner's miserly image, but you had to accept this system in order to get your cheap boss to agree to the project. Despite the unfavorable context, you are committed to doing a good job.

You have requests from N groups, and each group has specified the number of people that will come into the factory. Groups will come in one at a time. You want to bring them in in an order that maximizes the number of groups that get only fresh chocolate and no leftovers. You cannot reject groups, nor have a group get chocolate more than once, and you need to give exactly one piece to each person in each group.

In the example above, if instead of 5, 6, 4, 4, the order were 4, 5, 6, 4, a total of 3 groups (all but the 5 person group) would get only fresh chocolate. For that set of groups, it is not possible to do better, as no arrangement would cause all groups to get only fresh chocolate.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case consists of two lines. The first line contains two integers N, the number of groups coming for a tour, and P, the number of pieces of chocolate per pack. The second line contains N integers G1G2, ..., GN, the number of people in each of the groups.

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 number of groups that will receive only fresh chocolate if you bring them in in an order that maximizes that number.

Limits

1 ≤ T ≤ 100.
1 ≤ N ≤ 100.
1 ≤ Gi ≤ 100, for all i.

Small dataset

2 ≤ P ≤ 3.

Large dataset

2 ≤ P ≤ 4.

Sample


Input 
 

Output 
 
3
4 3
4 5 6 4
4 2
4 5 6 4
3 3
1 1 1

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

Sample Case #1 is the one explained in the statement. Besides the possible optimal order given above, other orders like 6, 5, 4, 4 also maximize the number of groups with only fresh chocolate, although the groups that get the fresh chocolate are not necesarily the same. Notice that we only care about the number of groups that get the best experience, not the total number of people in them.

In Sample Case #2, the groups are the same as in Case #1, but the packs contain two pieces each. In this case, several ways of ordering them — for instance, 4, 4, 6, 5 — make all groups get only fresh chocolate.

In Sample Case #3, all groups are single individuals, and they will all eat from the same pack. Of course, only the first one to come in is going to get a freshly opened pack.


根據P的值 分情況討論  首先根據輸入 預處理模p的值 如果值為0  就是一個  然後剩下的 再根據p的情況進行兩兩組合 使得組合後的和模p的值為0
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("A-large.in","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int n,p;
int g[110];

void solve(int t)
{
    int ans=0;
    if(p==2)
    {
        int one=0;
        for(int i=0;i<n;i++)
            g[i]=g[i]%2;
        for(int i=0;i<n;i++)
        {
            if(g[i]==0)
                ans++;
            if(g[i]==1)
                one++;
        }
        ans+=(one/2+(one%2!=0));
    }
    else if(p==3)
    {
        int one=0,two=0;
        for(int i=0;i<n;i++)
            g[i]=g[i]%3;
        for(int i=0;i<n;i++)
        {
            if(g[i]==0)
                ans++;
            if(g[i]==1)
                one++;
            if(g[i]==2)
                two++;
        }
        if(one>=two)
        {
            ans+=two;
            one-=two;
            //one/=3;
            ans+=one/3+(one%3!=0);
        }
        else if(one<two)
        {
            ans+=one;
            two-=one;
            //two/=3;
            ans+=two/3+(two%3!=0);
        }
    }
    else
    {
        int one=0,two=0,three=0;
        for(int i=0;i<n;i++)
            g[i]=g[i]%4;
        for(int i=0;i<n;i++)
        {
            if(g[i]==0)
                ans++;
            if(g[i]==1)
                one++;
            if(g[i]==2)
                two++;
            if(g[i]==3)
                three++;
        }
        ans+=(two/2);
        two%=2;
        if(one>=three)
        {
            ans+=three;
            one-=three;
            if(two==0)
            {
                ans+=one/4+(one%4!=0);
            }
            else
            {
                if(one<=2)
                    ans++;
                else
                {
                    ans++;
                    one-=2;
                    ans+=one/4+(one%4!=0);
                }
            }
        }
        else
        {
            ans+=one;
            three-=one;
            if(two==0)
            {
                ans+=three/4+(three%4!=0);
            }
            else
            {
                if(three<=2)
                    ans++;
                else
                {
                    ans++;
                    three-=2;
                    ans+=three/4+(three%4!=0);
                }
            }
        }
        // int flag=1;
        // if(one>=two&&two>=three&&flag)
        // {
        //     ans+=three;
        //     one-=three;
        //     ans+=(two/2);
        //     two=two%2;
        //     if(two==0)
        //     {
        //         ans+=one/4+(one%4!=0);
        //     }
        //     else
        //     {
        //         if(one)
        //     }
        // }
    }

    printf("Case #%d: %d\n",t,ans);
}


int main()
{
    //fread;
    //fwrite;
    int tc;
    scanf("%d",&tc);
    //cout<<tc<<endl;
    for(int t=1;t<=tc;t++)
    {
        cin>>n>>p;
        for(int i=0;i<n;i++)
        {
            cin>>g[i];

        }
        solve(t);

    }
    return 0;
}


相關推薦

2017Google Codejam round2 Problem A. Fresh Chocolate

Problem You are the public relations manager for a chocolate manufacturer. Unfortunately, the company's image has suffered because cust

Codeforces Round#413 Problem A - C

false font vector for循環 type als 不存在 pac 鏈接 [寫在前面感(亂)嘆(七)人(八)生(糟)的話]   本想借此機會一口氣玩到藍名,結果,A題寫炸(少判了一種情況),C題寫炸(辜負了我5分鐘狂敲出來的線段樹),結果又掉Rating..

【二分】Petrozavodsk Winter Training Camp 2017 Day 1: Jagiellonian U Contest, Monday, January 30, 2017 Problem A. The Catcher in the Rye

什麽 不同 stdin n) clas sqrt ios 這份 std 一個區域,垂直分成三塊,每塊有一個速度限制,問你從左下角跑到右上角的最短時間。 將區域看作三塊折射率不同的介質,可以證明,按照光路跑時間最短。 於是可以二分第一個入射角,此時可以推出射到最右側邊界上的位

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B

initial index 技術 ble continue efi whole ret rem Pronlem A In a small restaurant there are a tables for one person and b tables for t

ZCMU Problem A: Good Joke!

his mda tin discuss pid ble weak cpp std Problem A: Good Joke! Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 25 Solved: 16[Subm

【找規律】【DFS】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem A. Arithmetic Derivative

blog r+ clas 可能 .cn can typedef pro 找規律 假設一個數有n個質因子a1,a2,..,an,那麽n‘=Σ(a1*a2*...*an)/ai。 打個表出來,發現一個數x,如果x‘=Kx,那麽x一定由K個“基礎因子”組成。 這些基礎因子是2^

[HDU5685]2016"百度之星" - 資格賽 Problem A

++i 字符 pre fine 字符串 scan div true 擴展歐幾裏得 題目大意:給你一個字符串,和一些問題,每個問題問你[l,r]子串的哈希值是多少。 哈希值計算方法為:$H(s)=\prod _{i=1} ^{i\leq len(s)}(s_i-28)(mo

[HAOI2011]problem a

根據 min 輸入 pac 代碼實現 problem const 兩個 ron 題目描述 一次考試共有n個人參加,第i個人說:“有ai個人分數比我高,bi個人分數比我低。”問最少有幾個人沒有說真話(可能有相同的分數) 輸入輸出格式 輸入格式

【BZOJ2298】[HAOI2011]problem a DP

最大 一起 class min rip ret esc output 並且 【BZOJ2298】[HAOI2011]problem a Description 一次考試共有n個人參加,第i個人說:“有ai個人分數比我高,bi個人分數比我低。”問最

BZOJ 2298: [HAOI2011]problem a

mes sca ret des shu insert define page bsp 2298: [HAOI2011]problem a Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1523 Solved: 756

2017 ACM-ICPC Asia Xi'an Problem A XOR(異或線性基 )

problem 線段樹 all gpo efi printf 異或 bre %d 題目鏈接 2017西安賽區 Problem A 題意 給定一個數列,和$q$個詢問,每個詢問中我們可以在區間$[L, R]$中選出一些數。    假設我們選出來的這個數列為$A[i_{

【BZOJ2998】Problem A(動態規劃)

gpo pre com space main ostream 最大 == while 【BZOJ2998】Problem A(動態規劃) 題面 BZOJ 題解 一個人的成績範圍可以確定為一個區間 這樣就變成了 選擇若幹區間,不重合, 每個區間有個權值,求最大權值和 這樣就可

Problem A: 實現鏈表(線性表)

試用 stdio.h nbsp HR input type 結點 AR return Problem A: 實現鏈表(線性表) Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 443 Solved: 293[Submit]

Problem A: 文件操作--二進制文件讀入

math tput cannot clu break pen esc 數學 個學生 Problem A: 文件操作--二進制文件讀入 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2232 Solved: 631[Subm

Problem A: 數組類(I)

支持 for http sin ret IT RR tor == Problem A: 數組類(I) Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3295 Solved: 2418[Submit][Status][

Problem A: 求個最大值

public lang TP problem inpu mem sam 數據 Go Problem A: 求個最大值 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1635 Solved: 1339[Submit][

2298: [HAOI2011]problem a

端點 for clu ctype com www ble algo pre 2298: [HAOI2011]problem a 鏈接 分析:   每個人說的話,可以轉化成區間[l,r]的人的排名是一樣的,於是就轉化成了區間帶權覆蓋問題。   f[i]表示到第i個人,

BZOJ2298: [HAOI2011]problem a(帶權區間覆蓋DP)

div fin get LV cor 不同 script enter ble Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1747 Solved: 876[Submit][Status][Discuss] Descri

HDU 6319 Problem A. Ascending Rating(單調隊列)

type typedef ++ ems 區間 tor gif i++ lose 要求一個區間內的最大值和每次數過去最大值更新的次數,然後求每次的這個值異或 i 的總和。 這個序列一共有n個數,前k個直接給出來,從k+1到n個數用公式計算出來。 因為要最大值,所以就要用到

hdu 6319 Problem A. Ascending Rating (2018 Multi-University Training Contest 3 A)

while ble con -- test ++ nbsp ini for 鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=6319 思路: 單調隊列倒著維護,隊列裏面剩下的值的數量就是這一段區間的count值,如樣例