题目来源: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的更多相关文章

  1. POJ-1010 Stamps

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

  2. 【DFS】STAMPS

    [Poj1010]STAMPS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18867   Accepted: 5469 ...

  3. 【poj1010】 STAMPS

    http://poj.org/problem?id=1010 (题目链接) 感到了英语深深的恶意... 题意(真的很难懂....) 第一行数字是邮票的面值,每一个数字就是一个不同的种类,哪怕面值相同. ...

  4. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  5. 洛谷P2725 邮票 Stamps

    P2725 邮票 Stamps 37通过 224提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 为什么RE?在codevs上AC的. 题目背景 给一组 ...

  6. USACO Section 3.1: Stamps

    这题一开始用了dfs(注释部分),结果TLE,后来想了DP方法,f[i] = f[j] + f[i-j], j = 1, 2... i/2, 还是TLE,网上搜了别人的代码,发现自己的状态方程有问题, ...

  7. 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 ...

  8. USACO 邮票 Stamps

    f[x]表示组成 x 最少需要的邮票数量 一一举例 最多贴5张邮票,有三种邮票可用,分别是1分,3分,8分 组成0分需要0张邮票 ——f[0]=0 组成1分需要在0分的基础上加上一张1分邮票 ——f[ ...

  9. 洛谷 P2725 邮票 Stamps 解题报告

    P2725 邮票 Stamps 题目背景 给一组 N 枚邮票的面值集合(如,{1 分,3 分})和一个上限 K -- 表示信封上能够贴 K 张邮票.计算从 1 到 M 的最大连续可贴出的邮资. 题目描 ...

随机推荐

  1. perl: warning: Falling back to the standard locale ("C").

    /********************************************************************************** * perl: warning: ...

  2. 自定义type为file型input控件+该控件具有本地图片预览功能

    最近的一个项目需求是写一个type为filex型的input控件,这个控件: 第一,要自定义样式: 第二,要能直接在本地预览上传的图片: 第三,要能检测图片的尺寸是否符合要求. 故综合网上的资源写了下 ...

  3. 【转】 Pro Android学习笔记(四一):Fragment(6):数据保留

    目录(?)[-] 通过fragment参数实现数据保留 对TitleFragment进行修改 对DetailActivity进行修改 通过savedInstanceState进行数据保留 保留frag ...

  4. js产生不同的随机数

    前言:前几天写到一个程序,用到要使用不同随机数的方法,结果愣是整了半天没整出来,说来也是惭愧啊(亏我还是软件工程的学生,其实这个问题以前遇到过,只是我逃避了,哎,自己刨的坑终究会把自己陷进去,╮(╯▽ ...

  5. linux获取文件大小的函数

    C语言fstat()函数:由文件描述词取得文件状态 头文件:#include <sys/stat.h>   #include <unistd.h> 定义函数:int fstat ...

  6. 快速搭建SpringBoot项目

    Spring Boot简介: Spring Boot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目.它使用习惯优于配置的理念让你的项目快速运行起来,使用Spring Bo ...

  7. jquery插件开发常用总结一

    由于使用jquery插件后当form表单提交的时候,若发生错误,同时有验证错误文本时,即使用rules和message后,会自动生成一个label标签里面装有错误文件值. 我们可以替换它: 方式为:v ...

  8. 详解MYSQL各种优化原理

    说起MySQL的查询优化,相信大家收藏了一堆奇技淫巧:不能使用SELECT *.不使用NULL字段.合理创建索引.为字段选择合适的数据类型..... 你是否真的理解这些优化技巧?是否理解其背后的工作原 ...

  9. docker启动

    启动容器 启动容器有两种方式,一种是基于镜像新建一个容器并启动,另外一个是将在终止状态(stopped)的容器重新启动. 因为 Docker 的容器实在太轻量级了,很多时候用户都是随时删除和新创建容器 ...

  10. 不出现用户帐户控制-让Win7的用户账户控制(UAC)放过信任的程序

    微软有个官方工具 Microsoft Application Compatibility Toolkit: http://www.microsoft.com/downloads/details.asp ...