POJ1010 STAMPS(DFS+剪枝)


STAMPS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 15892 Accepted: 4486

Description

Have you done any Philately lately?

You have been hired by the Ruritanian Postal Service (RPS) to design their new postage software. The software allocates stamps to customers based on customer needs and the denominations that are currently in stock.

Ruritania is filled with people who correspond with stamp collectors. As a service to these people, the RPS asks that all stamp allocations have the maximum number of different types of stamps in it. In fact, the RPS has been known to issue several stamps of the same denomination in order to please customers (these count as different types, even though they are the same denomination). The maximum number of different types of stamps issued at any time is twenty-five.

To save money, the RPS would like to issue as few duplicate stamps as possible (given the constraint that they want to issue as many different types). Further, the RPS won't sell more than four stamps at a time.

Input

The input for your program will be pairs of positive integer sequences, consisting of two lines, alternating until end-of-file. The first sequence are the available values of stamps, while the second sequence is a series of customer requests. For example:

1 2 3 0 ; three different stamp types
7 4 0 ; two customers
1 1 0 ; a new set of stamps (two of the same type)
6 2 3 0 ; three customers

Note: the comments in this example are *not* part of the data file; data files contain only integers.

Output

For each customer, you should print the "best" combination that is exactly equal to the customer's needs, with a maximum of four stamps. If no such combination exists, print "none".
The "best" combination is defined as the maximum number of different stamp types. In case of a tie, the combination with the fewest total stamps is best. If still tied, the set with the highest single-value stamp is best. If there is still a tie, print "tie".

For the sample input file, the output should be:

7 (3): 1 1 2 3
4 (2): 1 3
6 ---- none
2 (2): 1 1
3 (2): tie

That is, you should print the customer request, the number of types sold and the actual stamps. In case of no legal allocation, the line should look like it does in the example, with four hyphens after a space. In the case of a tie, still print the number of types but do not print the allocation (again, as in the example).Don't print extra blank at the end of each line.

Sample Input

1 2 3 0	; three different stamp types
7 4 0		; two customers
1 1 0		; a new set of stamps (two of the same type)
6 2 3 0	; three customers

Sample Output

7 (3): 1 1 2 3 
4 (2): 1 3 
6 ---- none
2 (2): 1 1
3 (2): tie

#include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <cstring>
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
#define LL long long 
#define mod 1000000007
#define inf 0x3f3f3f3f
#define lson id<<1,l,mid  
#define rson id<<1|1,mid+1,r
const int maxn=30;
int value[maxn],time[maxn],countt[maxn],Bsolve[maxn],solve[maxn];
int kind,num,cnt;
int flag;
int max4(int *a)
{
	int ans=-1;
	ans=max(a[1],a[2]);
	ans=max(ans,a[3]);
	ans=max(ans,a[4]);
	return ans;
}
void bestsolve()
{
	for(int i=1;i<=4;i++)
	{
		Bsolve[i]=solve[i];
	}
}
void dfs(int cnum,int ctotal,int ckind,int chose )
{
	if(cnum>4)
		return ;
	if(ctotal==0)
	{
		if(ckind==kind)
		{
			if(cnum==num)
			{
				int cmaxp=max4(solve);
				int maxp=max4(Bsolve);
				if(cmaxp>maxp)
				{
					flag=0;
					maxp=cmaxp;
					bestsolve();
				}
				else if(cmaxp==maxp)
				{
					flag=1;
				}
			}
			else if(cnum<num)
			{
				flag=0;
				num=cnum;
				kind=ckind;
				bestsolve();
			}
		}
		else if(ckind>kind)
		{
			flag=0;
			kind=ckind;
			num=cnum;
			bestsolve();
		}
	}
	for(int i=chose;i<cnt;i++)//主要剪枝,搜索过某个价值的邮票后就只从大于等于它的值搜(之前已排序)
	{
		if(ctotal>=value[i])
		{
			solve[cnum+1]=value[i];
			if(time[i]==0)
			{
				time[i]++;
				dfs(cnum+1,ctotal-value[i],ckind+1,i);
			}
			else if(time[i]!=0)
			{
				time[i]++;
				dfs(cnum+1,ctotal-value[i],ckind,i);
			}
			time[i]--;
			solve[cnum+1]=0;
		}
		else 
			return ;
	}
	return;
}
int main()
{
	int price;
	while(scanf("%d",&price)!=EOF&&price)
	{
		memset(countt,0,sizeof(countt));
		cnt=1;
		value[cnt++]=price;
		countt[price]++;
		while(scanf("%d",&price)&&price)
		{
			countt[price]++;
			if(countt[price]>5) continue;
			else value[cnt++]=price;
		}
		sort(value+1,value+cnt);
		int cc;
		while(scanf("%d",&cc)&&cc)
		{
			memset(Bsolve,0,sizeof(Bsolve));
			memset(time,0,sizeof(time));
			memset(solve,0,sizeof(solve));
			kind=0,num=30;
			flag=0;
			dfs(0,cc,0,1);
			if(kind==0)
			{
				printf("%d ---- none\n",cc);
			}
			else if(flag==1)
			{
				printf("%d (%d): tie\n",cc,kind);
			}
			else
			{
				printf("%d (%d):",cc,kind);
				for(int i=1;i<=4;i++)
				{
					if(Bsolve[i]==0) continue;
					printf(" %d",Bsolve[i]);
				}
				printf("\n");
			}
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值