【DFS】STAMPS

[Poj1010]STAMPS

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18867   Accepted: 5469

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

Source

题目大意:给你一些邮票,有一些个客户,你要用不超过4张邮票组成这个面值,优先级如下:

               ①种类最多者优先

               ②①一样的情况下票数最多者优先

               ③①②一样的情况下最大的面值最大优先

               ④以上都一样(最优答案超过一种),即输出tie

               ⑤如果没有方案:none

试题分析:据说数据有些坑人,大家开100的数组就足够了。

               直接加一些剪枝,判优,更新答案等乱七八糟的东西就够了

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
//#include<cmath>

using namespace std;
const int INF = 9999999;
#define LL long long

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘;
	return x*f;
}
int N,M;
int a[100001];
int Q;
int ans[5],tmp[5];
int anss,ansx,ansd;
bool tie=false,flag=false;

void copy(int st,int tmpx,int l){//复制到答案中
	anss=st;ansd=l;ansx=tmpx;
	for(int i=1;i<=st;i++) ans[i]=tmp[i];
	return ;
}

bool cmp(int st,int tmpx,int l){//比较优先
	if(l>ansd) {tie=false;return true;}
	if(l<ansd) {return false;}
	if(anss<st) {tie=false;return true;}
	if(anss<st) {return false;}
	if(ansx<tmpx) {tie=false;return true;}
	if(ansx>tmpx) {return false;}
	if(l==ansd&&anss==st&&ansx==tmpx) tie=true;
	return false;
}

void DFS(int sum,int step,int now,int tmpx,int l){
        //sum总面值 step步数 now现在可以访问的最小编号 tmpx搜索时选择的最大元素 l种数
	if(sum>Q||step>4||sum+(4-step)*a[N]<Q) return ;//剪枝
	if(sum==Q){
		flag=true;
		if(cmp(step,tmpx,l)) copy(step,tmpx,l);
		return ;
	}
	for(int i=now;i<=N;i++){
		if(i==0) continue;
    	tmp[step+1]=a[i];
	    if(i==now) DFS(sum+a[i],step+1,i,max(tmpx,a[i]),l);
	    else DFS(sum+a[i],step+1,i,max(tmpx,a[i]),l+1);
	}
	return ;
}

int p;

int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	while(scanf("%d",&p)!=EOF){
		a[++N]=p;
		while(a[N]!=0) a[++N]=read();
		N--;
		sort(a+1,a+N+1);//排序!
		Q=read();
		while(Q){
			memset(ans,0,sizeof(ans));
			memset(tmp,0,sizeof(tmp));
			tie=false;
			flag=false;
			anss=0,ansd=0,ansx=0;
			DFS(0,0,0,0,0);
			if(!flag) {cout<<Q,puts(" ---- none");Q=read();continue;}
			if(tie) {printf("%d (%d): ",Q,ansd);puts("tie");Q=read();continue;}
			printf("%d (%d): ",Q,ansd);
			for(int i=1;i<anss;i++)
		   		printf("%d ",ans[i]);
			printf("%d\n",ans[anss]);
			Q=read();
		}
		N=0;
    }
	return 0;
}
时间: 2024-10-09 22:11:08

【DFS】STAMPS的相关文章

poj 3009 Curling 2.0 【DFS】

题意:从2出发,要到达3, 0可以通过,碰到1要停止,并且1处要变成0, 并且从起点开始沿着一个方向要一直前进,直至碰到1(或者3)处才能停止,(就是反射来反射去知道反射经过3).如果反射10次还不能到达3,就输出-1. 策略:深搜. 易错点,方向不容易掌握,并且,出题人把n, m顺序反了. 代码: #include<stdio.h> #include<string.h> int map[25][25]; int ans, n, m; const int dir[4][2] = {

NYOJ 587 blockhouses 【DFS】

blockhouses 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle th

NYOJ 722 数独 【DFS】+【预处理】

数独 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 数独是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个3*3宫内的数字均含1-9,不重复. 每一道合格的数独谜题都有且仅有唯一答案,推理方法也以此为基础,任何无解或多解的题目都是不合格的. 有一天hrdv碰到了一道号称是世界上最难的数独的题目,作为一名合格的程序员,哪能随随便便向困难低头,于是他决定编个程序来解决它.. 输入 第一行有一个

poj 1011 Sticks 【DFS】+【剪枝】

题意:有未知根(长度一样)木棒(小于等于n),被猪脚任意的截成n段,猪脚(脑抽了)想知道被截之前的最短长度(我推测猪脚得了健忘症). 这道题光理解题意就花了好久,大意就是任意根被截后的木棒拼到一起,能不能组成s(<=n)根的相同的木棒, 例:数据 9  5 1 2 5 1 2 5 1 2 可以组成最短为6 的(5+1, 2+2+2)3根木棒. 策略:深搜. 不过要是传统的深搜的话,TLE妥妥的.所以要剪枝(所谓剪枝,就是多加几个限制条件). 话不多说直接上代码. 代码1: #include <

HDU1010 Tempter of the Bone 【DFS】+【剪枝】

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 70895    Accepted Submission(s): 19535 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

nyoj 325 zb的生日 【DP】||【DFS】

两种方法: 第一种:将总数一半当做背包,用总数-2*最多能装的数目就是所求: 第二种:深搜: zb的生日 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么庆祝生日,经过调查,zb发现C小加和never都很喜欢吃西瓜,而且一吃就是一堆的那种,zb立刻下定决心买了一堆西瓜.当他准备把西瓜送给C小加和never的时候,遇到了一个难题,never和C小加不在一块住,只能

【dfs】hdu 1016 Prime Ring Problem

[dfs]hdu 1016 Prime Ring Problem 题目链接 刚开始接触搜索,先来一道基本题目练练手. 注意对树的深度进行dfs dfs过程中注意回退!!! 素数提前打表判断快一些 参考代码 /*Author:Hacker_vision*/ #include<bits/stdc++.h> #define clr(k,v) memset(k,v,sizeof(k)) using namespace std; const int _max=1e3+10;//素数打表 int n,pr

hdoj 1045 Fire Net 【DFS】

题意:如果两个点要放在同一行或者同一列,那么两个点中间要有一个墙,否则的话只能放一个点,最后问你最多能放几个点. 看了一个星期.. 这道题的解法我还是第一次见,就是逐个逐个的来放置每个点,然后每经过一个点都判断一次,详情看代码 代码: #include <stdio.h> #include <string.h> int ans, n; char map[10][10]; int judge(int lin, int row) { int i; for(i = lin-1; i &g

【DFS】【拓扑排序】【动态规划】Gym - 100642A - Babs&#39; Box Boutique

给你10个箱子,有长宽高,每个箱子你可以决定哪个面朝上摆.把它们摞在一起,边必须平行,上面的不能突出来,问你最多摆几个箱子. 3^10枚举箱子用哪个面.然后按长为第一关键字,宽为第二关键字,从大到小排序. 如果前面的宽大于等于后面的宽,就连接一条边. 形成一张DAG,拓扑排序后跑最长路即可. #include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespa