题目来源: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. AAC头部格式

    一共有2种AAC头格式,一种是StreamMuxConfig,另一种是AudioSpecificConfig 1.AudioSpecificConfig 读写header的代码参考    ffmpeg ...

  2. Gym 101142 I.Integral Polygons(计算几何)

    题意:给定一个凸包,现在让你连接凸包上两点,把凸包变为两个多边形,满足两个多边形的面积都是整数. 思路:我们知道整点的三角形面积S=叉积/2,则S要么是整数,要么是整数+0.5.那么多边形有多个三角形 ...

  3. BZOJ5372: [Pkusc2018]神仙的游戏

    BZOJ5372: [Pkusc2018]神仙的游戏 https://lydsy.com/JudgeOnline/problem.php?id=5372 分析: 如果\(len\)为\(border\ ...

  4. ACM学习历程—ZOJ3785 What day is that day?(数论)

    Description It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days? Input There are ...

  5. Winform程序实现多显示屏、多屏幕显示的2种方法

    这篇文章主要介绍了Winform窗口实现多显示屏显示的2种方法,本文直接给出了实现代码,并对其中的一些重要参数做了解释,需要的朋友可以参考下. 一台主机连接了2台显示器(2个显卡),要求一个程序的两个 ...

  6. VijosP1274:神秘的咒语

    描述 身为拜月教的高级间谍,你的任务总是逼迫你出生入死.比如这一次,拜月教主就派你跟踪赵灵儿一行,潜入试炼窟底. 据说试炼窟底藏着五行法术的最高法术:风神,雷神,雪妖,火神,山神的咒语.为了习得这些法 ...

  7. java代码throws异常

    总结:抛出异常 package com.ds; //异常捕获 public class fdsg { private static void throwException() { try { Stri ...

  8. AD9 如何画4层pcb板

    新建的PCB文件默认的是2层板,教你怎么设置4层甚至更多层板. 在工具栏点击Design-->Layer Stack Manager.进入之后显示的是两层板,添加为4层板,一般是先点top la ...

  9. asp.net mvc Partial OutputCache 在SpaceBuilder中的应用实践

    最近给SpaceBuilder增加OutputCache 时发现了一些问题,贴在这做个备忘,也方便遇到类似问题的朋友查阅. 目前SpaceBuilder表现层使用是asp.net mvc v1.0,使 ...

  10. TS学习之解构与展开

    一.解构 1.解构数组 let input = [1, 2]; let [first, second] = input; console.log(first); // outputs 1 consol ...