Question:http://poj.org/problem?id=1010
问题点:DFS、剪枝。
 Memory: 220K        Time: 32MS
Language: C++ Result: Accepted #include <iostream>
using namespace std; #define MAX_STAMP_TYPE 100
int stamp[MAX_STAMP_TYPE];
bool tie,none;
int now[],ans[];//记录stamp的index
int max_stamp;//记录最大邮票
int getInfo(int sta[]){
int tmp_a,tmp_b,tmp_c;
tmp_a=;
tmp_b=;
tmp_c=sta[];
for(int i=;i< && sta[i]>;i++)
{
if(sta[i-]!=sta[i]) tmp_a++;//取种类数 千位
tmp_b++;//取总张数 百位
tmp_c=sta[i-]>sta[i]?sta[i-]:sta[i];//取最大值 十位和个位
}
return tmp_a*+(-tmp_b)*+tmp_c;
}
void compare(){
none=false;//只要有结果,none就置为false
int nowInfo=getInfo(now);
int ansInfo=getInfo(ans);
char r=nowInfo>ansInfo?'g':(nowInfo<ansInfo?'l':'e');//g:出现更优解 l:不如现有解 e:tie
if(r=='g'){//如果出现更优解,将now中的数据复制到ans中保存
memcpy(ans,now,sizeof(now));
tie=false;
}else if(r=='e'){
tie=true;
}
return;
}
void dfs(int num,int cnt){
if(num==){//剩余数为0,即为解的出现条件
compare();
return;
}else if(cnt>=){//邮票数不能大于4张
return;
}else if(num<){//剩余需分配数不能小于0
return;
}else if(num>max_stamp*(-cnt)){
return;
}
for(int i=(cnt>?now[cnt-]:);i<=stamp[];i++)
{
now[cnt]=i;
dfs(num-stamp[i],cnt+);
now[cnt]=;
}
}
int main()
{
do{
int tmp,i=;
max_stamp=;
memset(stamp,,sizeof(stamp));
while(cin>>tmp && tmp!=){
stamp[i++]=tmp;
max_stamp=max_stamp>tmp?max_stamp:tmp;
}
stamp[]=i-;//记录邮票种类数
while(cin>>tmp && tmp!=){
tie=false;
none=true;
memset(now,,sizeof(now));
memset(ans,,sizeof(ans));
dfs(tmp,);
if(tie){
cout<<tmp<<" ("<<getInfo(ans)/<<"): "<<"tie"<<endl;
}else if(none){
cout<<tmp<<" ---- none"<<endl;
}else{
cout<<tmp<<" ("<<getInfo(ans)/<<"):";
for(int i=;i< && ans[i]>;i++){
cout<<" "<<stamp[ans[i]];
}
cout<<endl;
}
}
}while(getchar()!=EOF);
return ;
}

北大ACM(POJ1010-STAMPS)的更多相关文章

  1. 北大ACM - POJ试题分类(转自EXP)

    北大ACM - POJ试题分类 -- By EXP 2017-12-03 转载请注明出处: by EXP http://exp-blog.com/2018/06/28/pid-38/ 相关推荐文: 旧 ...

  2. 北大 ACM 分类 汇总

    1.搜索 //回溯 2.DP(动态规划) 3.贪心 北大ACM题分类2009-01-27 1 4.图论 //Dijkstra.最小生成树.网络流 5.数论 //解模线性方程 6.计算几何 //凸壳.同 ...

  3. POJ1010 Stamps

    题目来源:http://poj.org/problem?id=1010 题目大意: 某邮局要设计新的邮资管理软件,依据顾客的需要和现有的面值给顾客分派邮票. 该邮局有很多顾客是集邮爱好者.这些人希望得 ...

  4. 北大ACM题库习题分类与简介(转载)

    在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------- ...

  5. POJ-1010 Stamps

    [题目描述] 题目大意是:邮票发行商会发行不同面值.不同种类的邮票给集邮爱好者,集邮爱好者有总目标面额,通过不同的邮票组合(总数在4张以内)达到该面值,卖给集邮爱好者.另外,发行商发行的邮票面值最多2 ...

  6. 北大ACM试题分类+部分解题报告链接

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...

  7. 北大ACM - POJ试题分类

    1.入门水题 可用于练手与增强自信 POJ-1003POJ-1004 POJ-1005 POJ-1207 POJ-3299 POJ-2159 POJ-1083POJ-3094 2.初级 2.1. 基本 ...

  8. 北大ACM(POJ1014-Dividing)

    Question:http://poj.org/problem?id=1014 问题点:抽屉原理.dfs.多重背包. Memory: 248K Time: 16MS Language: C++ Res ...

  9. 北大ACM(POJ1013-Counterfeit Dollar)

    Question:http://poj.org/problem?id=1013 问题点:排除+验证. Memory: 244K Time: 16MS Language: C++ Result: Acc ...

  10. 北大ACM(POJ1012-Joseph)

    Question:http://poj.org/problem?id=1012 问题点:约瑟夫环. Memory: 220K Time: 329MS Language: C++ Result: Acc ...

随机推荐

  1. Codeforces Gym 100015G Guessing Game 差分约束

    Guessing Game 题目连接: http://codeforces.com/gym/100015/attachments Description Jaehyun has two lists o ...

  2. java建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历

    import java.util.LinkedList; import java.util.Scanner; import java.util.Stack; //structure of binary ...

  3. Android学习笔记(四十):Preference的使用

    Preference直译为偏好,博友建议翻译为首选项.一些配置数据,一些我们上次点击选择的内容,我们希望在下次应用调起的时候依旧有效,无须用户再一次进行配置或选择.Android提供preferenc ...

  4. Html Agility Pack基础类介绍及运用

    第一篇只对Html Agility Pack做了一个大概的介绍,在接下来的章节会比较深入的介绍Html Agility Pack. Html Agility Pack 源码中的类大概有28个左右,其实 ...

  5. jQuery代码性能小细节

    选择器Selector的使用 $("#id")使用id来定位DOM元素无疑是最佳提高性能方式,因为jQuery底层将直接调用本地方法document.getElementbyId( ...

  6. JavaWeb学习总结

    http://www.cnblogs.com/xdp-gacl/tag/JavaWeb%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93/ http://www.cnblogs. ...

  7. 无需图片,使用CSS3实现圆角按钮[转]

    首先来看看效果:   事例HTML代码: <a href="#" class="button green">button</a> < ...

  8. ubuntu13.04装配oracle11gR2

    http://jingyan.baidu.com/album/bea41d435bc695b4c41be648.html?picindex=2 http://www.360doc.com/conten ...

  9. Excel处理数据用到的一些公式和VBA脚本

    最近工作中用到EXCEL统计处理一些数据,正好之前有自学了一段时间的EXCEL,这次正好用上.为了加深印象,以后方便翻阅,就记录下来.这篇会不断补充. IF 多条件判断返回值 IF(logical_t ...

  10. LeetCode15 3Sum

    题意: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...