hdu 4422 The Little Girl who Picks Mushrooms【贪心+分类讨论】

来源:互联网 发布:源码屋 编辑:程序博客网 时间:2024/05/16 17:08

The Little Girl who Picks Mushrooms

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2631    Accepted Submission(s): 843

Problem Description

It's yet another festival season in Gensokyo. Little girl Alice planned to pick mushrooms in five mountains. She brought five bags with her and used different bags to collect mushrooms from different mountains. Each bag has a capacity of 2012 grams. Alice has finished picking mushrooms in 0 ≤ n ≤ 5 mountains. In the i-th mountain, she picked 0 ≤ wi ≤ 2012 grams of mushrooms. Now she is moving forward to the remained mountains. After finishing picking mushrooms in all the five mountains, she want to bring as much mushrooms as possible home to cook a delicious soup.
Alice lives in the forest of magic. At the entry of the forest of magic, live three mischievous fairies, Sunny, Lunar and Star. On Alice's way back home, to enter the forest, she must give them exactly three bags of mushrooms whose total weight must be of integral kilograms. If she cannot do so, she must leave all the five bags and enter the forest with no mushrooms.
Somewhere in the forest of magic near Alice's house, lives a magician, Marisa. Marisa will steal 1 kilogram of mushrooms repeatedly from Alice until she has no more than 1 kilogram mushrooms in total. So when Alice gets home, what's the maximum possible amount of mushrooms Alice has? Remember that our common sense doesn't always hold in Gensokyo. People in Gensokyo believe that 1 kilogram is equal to 1024 grams.

 

 

Input

There are about 8192 test cases. Proceed to the end of file.
The first line of each test case contains an integer 0 ≤ n ≤ 5, the number of mountains where Alice has picked mushrooms. The second line contains n integers 0 ≤ wi ≤ 2012, the amount of mushrooms picked in each mountain.

 

 

Output

For each test case, output the maximum possible amount of mushrooms Alice can bring home, modulo 20121014 (this is NOT a prime).

 

 

Sample Input

1

9

4

512 512 512 512

5

100 200 300 400 500

5

208 308 508 708 1108

 

 

Sample Output

1024

1024

0

792

Hint

 

In the second sample, if Alice doesn't pick any mushrooms from the 5-th mountain. She can give (512+512+0) =1024 grams of mushrooms to Sunny, Lunar and

Star. Marisa won't steal any mushrooms from her as she has exactly 1 kilogram of mushrooms in total.

In the third sample, there are no three bags whose total weight is of integral kilograms. So Alice must leave all the five bags and enter the forest with no mushrooms.

In the last sample:

1.Giving Sunny, Lunar and Star: (208+308+508)=1024

2.Stolen by Marisa: ((708+1108)-1024)=792

 

 

 

Source

2012 Asia ChangChun Regional Contest


题目大意:

小女孩爱丽丝有五个袋子,每个袋子有一个容量上限 ,只能装2012g的蘑菇,要去五个山上采蘑菇,其中输入有两行,第一行一个数n,第二行n个数,表示其中需要有n个袋子需要按照输入的数量来采蘑菇,不能多也不能少。其他的袋子可以采任意数量的蘑菇。

然后当爱丽丝采完蘑菇之后,回家的路上需要拿出三个袋子,并且要求这三个袋子中的蘑菇的重量和为1024的整数倍,作为过路费,如果拿不出来三个袋子的蘑菇重量和为1024的整数倍,那么爱丽丝就要抛弃所有的袋子才能回家。然而剩下的两个袋子还要被小偷偷,如果剩下的两个袋子重量和大于1024,小偷才会去偷,小偷每一次偷1024g的蘑菇,并且留下的重量和一定是小于等于1024g的.

问最多爱丽丝可能留下多少g的蘑菇。


思路:

1、分类讨论:

①如果n==0,那么代表我们随意的分配袋子中的蘑菇数量,显然分配成 512 512 512 1024 0即可,那么拿回家的蘑菇的数量就是1024.

②如果n==1.那么代表我们随意的分配4个袋子中的蘑菇数量,我们选择两个袋子使得其和已知重量的袋子的重量和为1024,然后分配最后三个袋子重量和和为1024的整数倍,那么拿回家的数量是1024。

③如果n==2,.那么代表我们随意的分配3个袋子中的蘑菇数量那么同理,我们随意分配一个袋子使得其和已知重量的袋子的重量和为1024,然后分配最后两个袋子重量和和为1024的整数倍,那么拿回家的数量是1024。

④如果n==3,.那么代表我们随意的分配2个袋子中的蘑菇数量那么同理,我们随意分配一个袋子使得其和两个已知重量的袋子的重量和为1024,然后分配最后一个袋子重量和另外一个已知重量的的袋子的重量和为1024的整数倍,那么拿回家的数量是1024。

以上,n==0||n==1||n==2||n==3,都能分配出1024的解。

⑤如果n==4,那么代表我们随意的分配一个袋子中的蘑菇的数量,如果在已知重量的四个袋子中能够拿出三个袋子重量和为1024的倍数,那么我们分配最后一个袋子和已知重量袋子使其重量和为1024,那么其能够拿回家的重量也是1024,否则,我们需要分配一个袋子和其中任意两个袋子使其重量和为1024的倍数,然后剩下两个袋子的重量和%1024就是一个可行解,那么我们其实就是相当于枚举两个袋子,作为拿回家的袋子,然后维护其解即可(注意这里如果sum%1024==0,如果sum!=0,那么其解就是1024而不是0)。

⑥如果n==5,那么我们枚举出三个袋子,判断其和是否为1024的倍数,如果是,那么剩下两个袋子的重量和%1024就是一个可行解,维护其解即可。


2、注意1024%1024==0,但是解是1024不是0.


Ac代码:

#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespace std;int a[10];int vis[10];int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        if(n==0||n==1||n==2||n==3)printf("1024\n");        if(n==4)        {            int ans=0;            for(int i=1;i<=4;i++)            {                for(int j=1;j<=4;j++)                {                    for(int k=1;k<=4;k++)                    {                        if(i==j||j==k||i==k)continue;                        if((a[i]+a[j]+a[k])%1024==0)                        {                            ans=1024;                        }                    }                }            }            int sum=0;            for(int i=1;i<=4;i++)            {                for(int j=1;j<=4;j++)                {                    if(i==j)continue;                    sum=a[i]+a[j];                    if(sum!=0&&sum%1024==0)ans=1024;                    else                    ans=max(ans,sum%1024);                }            }            printf("%d\n",ans);        }        if(n==5)        {            int ans=0;            for(int i=1;i<=5;i++)            {                for(int j=1;j<=5;j++)                {                    for(int k=1;k<=5;k++)                    {                        if(i==j||j==k||i==k)continue;                        if((a[i]+a[j]+a[k])%1024==0)                        {                            memset(vis,0,sizeof(vis));                            vis[i]=1;vis[j]=1;vis[k]=1;                            int sum=0;                            for(int l=1;l<=5;l++)                            {                                if(vis[l]==0)sum+=a[l];                            }                            if(sum!=0&&sum%1024==0)ans=1024;                            else ans=max(ans,sum%1024);                        }                    }                }            }            printf("%d\n",ans);        }    }}


0 0
原创粉丝点击