hdu 4422 The Little Girl who Picks Mushrooms

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4422


题目:

The Little Girl who Picks Mushrooms
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 136 Accepted Submission(s): 48


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


题意:

采蘑菇的小姑娘,要采5座山的蘑菇,每座山采的蘑菇 大于等于0 小于等于2012。5座山采的蘑菇 需要将其中三座山的蘑菇 剪掉 并且必须是1024的倍数,若不是则全部将所有蘑菇减掉,若符合,剩余两座山的蘑菇要不断地减去1024直到剩余量小于等于1024为止。问最后剩余蘑菇的最大值(1024)。(感觉小姑娘被压迫的好惨啊,其实不然……)


题解:

枚举+博弈。想法通过第一关 即三座山加起来是1024的倍数。然后后面的只要杂凑成1024就可以了。由于只有5座山,对其分类讨论:

已采蘑菇山的数量 <=3

必然是最大值1024,可以任意利用未知变化的山的蘑菇数 凑成1024的倍数。

已采蘑菇山的数量==4

分两种情况:1。   4座固定山中 其中3座正好凑成1024的倍数,那么剩余1座固定+1座变化  肯定能凑成1024,那么剩余蘑菇最大值肯定是1024了

2。   4座固定山中  任意3座山都不能凑成1024的倍数,那么就是2座固定+1座变化 凑1024的倍数   剩余2座固定不断减去1024后的结果即为剩余值,这个枚 举长度为2的子集就行了

已采蘑菇山的数量==5

枚举长度为3的子集,在剩余长度为2的集合中,选出最大的值就行了。


代码:


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
using namespace std;
int n=0;
int w[6]={0};
bool Vis[6]={false};
int Sum=0,MaxSum=0;
int flag=false;
int ans=0;
/*init the var*/
int InitVar()
{
	Sum=0;
	MaxSum=0;
	memset(Vis,false,sizeof(Vis));
	flag=false;
	ans=0;
	return(0);
}
/*DFS the w[i]*/
bool DFS3(int cur)
{
	if(flag)
	{
		return(flag);
	}
	if(cur==4)//have count 3
	{
		if(Sum%1024==0)
		{
			flag=true;
		}
		return(flag);
	}
	else
	{
		int i=0;
		for(i=1;i<=n;i++)
		{
			if(Vis[i]==false)
			{
				Vis[i]=true;
				Sum+=w[i];
				DFS3(cur+1);
				Vis[i]=false;
				Sum-=w[i];
			}
		}
		return(flag);
	}
}
/*DFSSub pass 1  and  2  entry*/
int DFSSub(int cur,int cnt)
{
	if(cur==cnt+1)
	{
		if(cnt==2)//n=4
		{
			int Sums=w[1]+w[2]+w[3]+w[4]-Sum;
			while(Sums>1024)
			{
				Sums-=1024;
			}
			if(Sums>MaxSum)
			{
				MaxSum=Sums;
			}
		}
		else//n=5
		{
			int Sums=0;
			if(Sum%1024!=0)
			{
				Sums=0;
			}
			else
			{
				Sums=w[1]+w[2]+w[3]+w[4]+w[5]-Sum;
				while(Sums>1024)
				{
					Sums-=1024;
				}
				if(Sums>MaxSum)
				{
					MaxSum=Sums;
				}
			}
		}
	}
	else
	{
		int i=0;
		for(i=1;i<=n;i++)
		{
			if(Vis[i]==false)
			{
				Vis[i]=true;
				Sum+=w[i];
				DFSSub(cur+1,cnt);
				Vis[i]=false;
				Sum-=w[i];
			}
		}
	}
	return(0);
}
/*for test*/
int test()
{
	return(0);
}
/*main process*/
int MainProc()
{
	while(scanf("%d",&n)!=EOF)
	{
		int i=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&w[i]);
		}
		InitVar();
		if(n<=3)
		{
			printf("1024\n");
		}
		else
		{
			if(n==4)
			{
				//C43
				if(DFS3(1))//could % 1024 ==0
				{
					printf("1024\n");
				}
				else
				{
					//C42=6,DFS get 2 bags randomly
					DFSSub(1,2);
					printf("%d\n",MaxSum);
				}
				
			}
			else
			{
				//get 3 bags randomly
				DFSSub(1,3);
				printf("%d\n",MaxSum);
			}
		}
	}
	return(0);
}
int main()
{
	MainProc();
	return(0);
}














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值