[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;
}

【DFS】STAMPS的更多相关文章

  1. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  2. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

  3. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. 【dfs】P1331 海战

    题目描述 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞机.此外,巡洋船只和舰队将被派去保护海岸线.不幸的是因为种种原因,国防海军部仅有很少的几 ...

  5. 【dfs】p1731 生日蛋糕

    1441:[例题2]生日蛋搞 [题目描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体.设从下往上数第i(1≤i≤M)层蛋糕是半径为Ri, 高 ...

  6. 【dfs】LETTERS

    1212:LETTERS [题目描述] 给出一个roe×colroe×col的大写字母矩阵,一开始的位置为左上角,你可以向上下左右四个方向移动,并且不能移向曾经经过的字母.问最多可以经过几个字母. [ ...

  7. 洛谷P1605 迷宫【dfs】

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  8. 【dfs】BZOJ1703-[Usaco2007 Mar]Ranking the Cows 奶牛排名

    [题目大意] 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序,约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他 ...

  9. 【DFS】BZOJ3522-[Poi2014]Hotel

    [题目大意] 给出一棵树,求三个节点使得它们两两之间的距离相等,问共有多少种可能性? [思路] 显然,这三个节点是关于一个中心点对称地辐射出去的. 枚举中心点,往它的各个子树跑Dfs.tmp[i]表示 ...

随机推荐

  1. python学习笔记(一)之为什么学习python

    python的特点: 跨平台 实现同一个功能是Java代码的1/5 python应用范围: 操作系统 web 3D动画 企业应用 云计算 如何学习python? 学习语法 验证例子 学会总结 课外实践

  2. YII 框架查询

    基础查询 Customer::find()->one();    此方法返回一条数据: Customer::find()->all();    此方法返回所有数据: Customer::f ...

  3. vim 实现括号以及引号的自动补全

    编辑文件/etc/vim/vimrc sudo vim /etc/vim/vimrc 在最后添加 inoremap ( ()<ESC>i inoremap [ []<ESC>i ...

  4. 土司论坛nc反弹神器使用方法

    说明: PS:我本机是linux,因为没有服务器所以使用win7来演示.倘若你是windows可以在本机生成dll以后再放到服务器上面去执行dll即可反弹shell物理机ip:192.168.1.12 ...

  5. Win10默认浏览器怎么设置

    1.首先在Win10桌面左下角的开始菜单图标上右键单击鼠标,在弹出的菜单选项中,点击进入“控制面板”,如下图所示. 接下来就可以找到“默认程序”设置了,找到后点击进入设置,如下图所示. 打开Win10 ...

  6. java基础 流程控制和条件语句,循环语句

    顺序结构 程序的顺序结构: 如果代码里没有流程控制,程序是按照书写的格式从上而下一行一行执行的, 一条语句执行完之后继续执行下一条语句,中间没有判断和跳转,直到程序的结束. if语句 if语句使用bo ...

  7. 【VI Script】你不知道的脚本编程

    前言 近期,小黑在写程序的时候,经常会遇到一些重复性的工作.尤其是在写到QMH(Queued Message Handler)程序时,经常需要创建UI界面上的一些控件引用,并且在程序中捆绑成簇使用. ...

  8. zookeeper安装和搭建集群方式(window)

    1.   概述 ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务.分布式同步.组服务等.ZooKeeper的目标就是封装好复杂 ...

  9. 【msgpack-python】安装

    1.安装pip:http://blog.iyestin.com/2014/03/15/python-pip-install/ http://www.linuxde.net/2014/05/15576. ...

  10. Freemarker的页面和JS遍历后台传入的Map

    后端传到前端的Map Freemarker页面遍历Map: JS遍历Map: