POJ1010 Stamps
题目来源:http://poj.org/problem?id=1010
题目大意:
某邮局要设计新的邮资管理软件,依据顾客的需要和现有的面值给顾客分派邮票。
该邮局有很多顾客是集邮爱好者。这些人希望得到最多种类不同的邮票。该邮局会发行同一面值的不同邮票。邮票的面值最大为25.
为节约成本,邮局希望尽可能少的重复邮票。(他们希望发行尽可能多的不同种类的邮票)。而且,邮局对一个客户一次最多卖4张邮票。
输入:程序的输入是多组两行的数据。以EOF结束。第一行是现有的邮票的面值,以0结束。第二行是一系列的客户需求。
输出:对于每一个客户,输出“最好”的邮票组合(邮票种类数最多)。面值和恰好为客户的需要,邮票张数最大为4。如果找不到这样的组合,输出“none”。如果有多个“最好”组合,选择邮票总数最少的一组,如果仍然相等,邮票面值中单张价格最高的最优,若仍然相等,输出“tie”。具体格式见Example Output.
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
因为一次最多四张邮票,于是用了最傻的方法,找出所有可行的组合再按规则找最优解。另外测试数据中邮票面值实际是按升序输入的,若不按升序输入,则预先在程序中进行排序即可。拙劣代码奉上。
//////////////////////////////////////////////////////////////////////////
// POJ1010 Stamps
// Memory: 5356K Time: 32MS
// Language: C++ Result: Accepted
////////////////////////////////////////////////////////////////////////// #include <iostream>
#include <vector> using namespace std; class Solution {
public:
int stamps[];
int types;
int count;
int maxValue;
}; int types[];
int typeCount;
int customer;
int customerCount;
vector<Solution> sVector;
bool isTie; int getTypes(int i0, int i1, int i2 = -, int i3 = -) {
if (i3 == - && i2 == -) {
if (i0 == i1) {
return ;
} else {
return ;
}
} else if (i3 == -) {
if (i0 == i1 && i1 == i2) {
return ;
} else if (i1 == i2 || i0 == i1 || i0 == i2) {
return ;
} else {
return ;
}
} else if(i0 == i1 && i1 == i2 && i2 == i3) {
return ;
} else if (i0 != i1 && i0 != i2 && i0 != i3 && i1 != i2 && i1 != i3 && i2 != i3) {
return ;
} else if (i0 == i1 && i1 != i2 && i2 != i3 && i3 != i1
|| i0 == i2 && i0 != i1 && i0 != i3 && i1 != i3
|| i0 == i3 && i0 != i1 && i0 != i2 && i1 != i2
|| i1 == i2 && i0 != i1 && i2 != i3 && i0 != i3
|| i1 == i3 && i0 != i1 && i1 != i2 && i0 != i2
|| i2 == i3 && i2 != i0 && i2 != i1 && i0 != i1 ) {
return ;
} else {
return ;
}
} int findBestSolution() {
int bestSolution = ;
if (sVector.size() == ) return ;
for (int i = ; i < sVector.size(); i++) {
if (sVector[i].types > sVector[bestSolution].types) {
isTie = false;
bestSolution = i;
} else if (sVector[i].types == sVector[bestSolution].types) {
if (sVector[i].count < sVector[bestSolution].count) {
isTie = false;
bestSolution = i;
} else if (sVector[i].count == sVector[bestSolution].count) {
if (sVector[i].maxValue > sVector[bestSolution].maxValue) {
isTie = false;
bestSolution = i;
} else if (sVector[i].maxValue == sVector[bestSolution].maxValue) {
isTie = true;
}
}
}
}
return bestSolution;
} void Output(int sindex) {
Solution sulotion = sVector[sindex];
cout << " (" << sulotion.types << "):";
for(int i = ; i < sulotion.count; i++) {
cout << " " << sulotion.stamps[i];
}
cout << endl;
}
int main(void) {
while(cin >> types[]) {
while (types[typeCount] != ) {
cin >> types[++typeCount];
}
while (cin >> customer) {
if (customer == ) {
break;
}
if (types[] * > customer) {
cout << customer << " ---- none" << endl;
continue;
}
if (types[typeCount - ] * < customer) {
cout << customer << " ---- none" << endl;
continue;
}
for (int i0 = ; i0 < typeCount; i0++) {
if(types[i0] == customer) {
Solution solution;
solution.stamps[] = types[i0];
solution.stamps[] = ;
solution.stamps[] = ;
solution.stamps[] = ;
solution.maxValue = types[i0];
solution.count = ;
solution.types = ;
sVector.push_back(solution);
continue;
} else if (types[i0] < customer){
for (int i1 = i0; i1 < typeCount; i1++) {
if(types[i0] + types[i1] == customer) {
Solution solution;
solution.stamps[] = types[i0];
solution.stamps[] = types[i1];
solution.stamps[] = ;
solution.stamps[] = ;
solution.maxValue = types[i1];
solution.count = ;
solution.types = getTypes(i0, i1);
sVector.push_back(solution);
continue;
} else if (types[i0] + types[i1] < customer) {
for(int i2 = i1; i2 < typeCount; i2++) {
int sum = types[i0] + types[i1] + types[i2];
if(sum == customer) {
Solution solution;
solution.stamps[] = types[i0];
solution.stamps[] = types[i1];
solution.stamps[] = types[i2];
solution.stamps[] = ;
solution.maxValue = types[i2];
solution.count = ;
solution.types = getTypes(i0, i1, i2);
sVector.push_back(solution);
} else if (sum < customer){
for(int i3 = i2; i3 < typeCount; i3++) {
int sum = types[i0] + types[i1] + types[i2] + types[i3];
if(sum == customer) {
Solution solution;
solution.stamps[] = types[i0];
solution.stamps[] = types[i1];
solution.stamps[] = types[i2];
solution.stamps[] = types[i3];
solution.maxValue = types[i3];
solution.count = ;
solution.types = getTypes(i0, i1, i2, i3);
sVector.push_back(solution);
} else if (sum > customer) {
break;
}
}
} else {
break;
}
}
} else {
break;
}
}
} else {
break;
}
}
if (sVector.size() == ) {
cout << customer << " ---- none" << endl;
continue;
}
int bestSolution = findBestSolution();
if (isTie) {
cout << customer << " (" << sVector[bestSolution].types << "): tie" << endl;
} else {
cout << customer;
Output(bestSolution);
}
sVector.clear();
isTie = false;
}
customerCount = ;
typeCount = ;
sVector.clear();
}
system("pause");
return ;
}
POJ1010 Stamps的更多相关文章
- POJ-1010 Stamps
[题目描述] 题目大意是:邮票发行商会发行不同面值.不同种类的邮票给集邮爱好者,集邮爱好者有总目标面额,通过不同的邮票组合(总数在4张以内)达到该面值,卖给集邮爱好者.另外,发行商发行的邮票面值最多2 ...
- 【DFS】STAMPS
[Poj1010]STAMPS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18867 Accepted: 5469 ...
- 【poj1010】 STAMPS
http://poj.org/problem?id=1010 (题目链接) 感到了英语深深的恶意... 题意(真的很难懂....) 第一行数字是邮票的面值,每一个数字就是一个不同的种类,哪怕面值相同. ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- 洛谷P2725 邮票 Stamps
P2725 邮票 Stamps 37通过 224提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 为什么RE?在codevs上AC的. 题目背景 给一组 ...
- USACO Section 3.1: Stamps
这题一开始用了dfs(注释部分),结果TLE,后来想了DP方法,f[i] = f[j] + f[i-j], j = 1, 2... i/2, 还是TLE,网上搜了别人的代码,发现自己的状态方程有问题, ...
- Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序
C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...
- USACO 邮票 Stamps
f[x]表示组成 x 最少需要的邮票数量 一一举例 最多贴5张邮票,有三种邮票可用,分别是1分,3分,8分 组成0分需要0张邮票 ——f[0]=0 组成1分需要在0分的基础上加上一张1分邮票 ——f[ ...
- 洛谷 P2725 邮票 Stamps 解题报告
P2725 邮票 Stamps 题目背景 给一组 N 枚邮票的面值集合(如,{1 分,3 分})和一个上限 K -- 表示信封上能够贴 K 张邮票.计算从 1 到 M 的最大连续可贴出的邮资. 题目描 ...
随机推荐
- OpenCV——颜色均匀渐变
参考来源: 利用OpenCV生成关于某点的颜色径向均匀渐变图像 // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_A ...
- C语言小程序(一)、判断三角型类型
最近回炉重造C语言,陆续写一些比较短的代码,选择其中的一些贴到这里,都是在Linux下的代码,Windows未测试. 第一个判断三角形的类型,两个浮点型数据不能直接判断相等,为了输入方便一些,自己设置 ...
- 动态调试smali代码学习记录
预备知识 DDMS Dalvik Debug Monitor Serivce,Dalvik调试监控服务,为Android SDK提供的一款拥有监控Dalvik虚拟机的调试软件,启动文件位于<An ...
- FFMPEG-AVFilter研究
FFMPEG中的libswscale是做像素转换的,但是对于一些复杂的操作,比如添加水印等,这个库就不行了,这时候就要说一下另外一个AVFilter.AVFilter完全可以替代libswscale的 ...
- bzoj 3221: Obserbing the tree树上询问 树链剖分+线段树
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=3221 题解 啊呀...这是昨天的考试题啊...直接就粘了.. 与4515: [Sdoi2 ...
- OI省选算法汇总( 转发黄学长博客 )
[原文链接] http://hzwer.com/1234.html 注 : 蓝色为已学习算法 , 绿色为不熟练算法 , 灰色为未学习算法 1.1 基本数据结构 1. 数组 2. 链表,双向链表 3. ...
- HUD1455:Sticks
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 【转】 Pro Android学习笔记(五十):ActionBar(3):搜索条
目录(?)[-] ActionBar中的搜索条 通过Menu item上定义search view 进行Searchable的配置 在activity中将search view关联searchable ...
- js---倒计时的自动跳转.html
============================================================================== 倒计时的自动跳转.html <!DO ...
- linux命令-xz压缩
xz gzip bzip2使用方法基本一样 压缩文件 [root@wangshaojun ~]# xz 111.txt[root@wangshaojun ~]# ls //////111.txt文件 ...