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. JSP中Session的使用

    JSP session使用方法 <%@page contentType="text/html;charset=GB2312"%><html>  <he ...

  2. ORA-01722: invalid number

    ---问题 select owner,index_name,DEGREE from DBA_INDEXES where DEGREE>1                             ...

  3. [Angular2 Router] Build Angular 2 Navigation with routerLink

    Angular 2 navigation is configured using the routerLink directive. The routerLink directive behaves ...

  4. Flex+Struts2+JSON实现Flex和后台的HTTP Service请求

    http://www.fengfly.com/plus/view-191093-1.html Flex+Struts2+JSON的后台代码我在这就不多说了.不懂得请看我写的上一篇文章<Strut ...

  5. pg 资料大全1

    https://github.com/ty4z2008/Qix/blob/master/pg.md?from=timeline&isappinstalled=0 PostgreSQL(数据库) ...

  6. IIS6_IIS7日志文件位置

    准备统计下页面访问量 查找IIS日志,发现在以前IIS6日志的位置,竟然木有找到日志... 查看下IIS设置,发现IIS7和6的默认日志位置不一样额... IIS 6 Log files locati ...

  7. nexus-2.13.0-01.war

    https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.13.0-01.war

  8. hadoop2 作业执行过程之reduce过程

    reduce阶段就是处理map的输出数据,大部分过程和map差不多 //ReduceTask.run方法开始和MapTask类似,包括initialize()初始化,根据情况看是否调用runJobCl ...

  9. iOS开发,让数据更安全的几个加密方式

    任何应用的开发中安全都是重中之重,在信息交互异常活跃的现在,信息加密技术显得尤为重要.在app应用开发中,我们需要对应用中的多项数据进行加密处理,从而来保证应用上线后的安全性,给用户一个安全保障.这篇 ...

  10. seajs 源码解读

    之前面试时老问一个问题seajs 是怎么加载js 文件的 在网上找一些资料,觉得这个写的不错就转载了,记录一下,也学习一下 seajs 源码解读 seajs 简单介绍 seajs是前端应用模块化开发的 ...