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

因为一次最多四张邮票,于是用了最傻的方法,找出所有可行的组合再按规则找最优解。另外测试数据中邮票面值实际是按升序输入的,若不按升序输入,则预先在程序中进行排序即可。拙劣代码奉上。

  1 //////////////////////////////////////////////////////////////////////////
  2 //        POJ1010 Stamps
  3 //        Memory: 5356K        Time: 32MS
  4 //        Language: C++        Result: Accepted
  5 //////////////////////////////////////////////////////////////////////////
  6 
  7 #include <iostream>
  8 #include <vector>
  9 
 10 using namespace std;
 11 
 12 class Solution {
 13 public:
 14     int stamps[4];
 15     int types;
 16     int count;
 17     int maxValue;
 18 };
 19 
 20 int types[100];
 21 int typeCount;
 22 int customer;
 23 int customerCount;
 24 vector<Solution> sVector;
 25 bool isTie;
 26 
 27 int getTypes(int i0, int i1, int i2 = -1, int i3 = -1) {
 28     if (i3 == -1 && i2 == -1) {
 29         if (i0 == i1) {
 30             return 1;
 31         } else {
 32             return 2;
 33         }
 34     } else if (i3 == -1) {
 35         if (i0 == i1 && i1 == i2) {
 36             return 1;
 37         } else if (i1 == i2 || i0 == i1 || i0 == i2) {
 38             return 2;
 39         } else {
 40             return 3;
 41         }
 42     } else if(i0 == i1 && i1 == i2 && i2 == i3) {
 43         return 1;
 44     } else if (i0 != i1 && i0 != i2 && i0 != i3 && i1 != i2 && i1 != i3 && i2 != i3) {
 45         return 4;    
 46     } else if (i0 == i1 && i1 != i2 && i2 != i3 && i3 != i1 
 47         || i0 == i2 && i0 != i1 && i0 != i3 && i1 != i3
 48         || i0 == i3 && i0 != i1 && i0 != i2 && i1 != i2
 49         || i1 == i2 && i0 != i1 && i2 != i3 && i0 != i3
 50         || i1 == i3 && i0 != i1 && i1 != i2 && i0 != i2
 51         || i2 == i3 && i2 != i0 && i2 != i1 && i0 != i1 ) {
 52             return 3;
 53     } else {
 54         return 2;
 55     }
 56 }
 57 
 58 int findBestSolution() {
 59     int bestSolution = 0;
 60     if (sVector.size() == 1) return 0;
 61     for (int i = 1; i < sVector.size(); i++) {
 62         if (sVector[i].types > sVector[bestSolution].types) {
 63             isTie = false;
 64             bestSolution = i;
 65         } else if (sVector[i].types == sVector[bestSolution].types) {
 66             if (sVector[i].count < sVector[bestSolution].count) {
 67                 isTie = false;
 68                 bestSolution = i;
 69             } else if (sVector[i].count == sVector[bestSolution].count) {
 70                 if (sVector[i].maxValue > sVector[bestSolution].maxValue) {
 71                     isTie = false;
 72                     bestSolution = i;
 73                 } else if (sVector[i].maxValue == sVector[bestSolution].maxValue) {
 74                     isTie = true;
 75                 }
 76             }
 77         }
 78     }
 79     return bestSolution;
 80 }
 81 
 82 void Output(int sindex) {
 83     Solution sulotion = sVector[sindex];
 84     cout << " (" << sulotion.types << "):";
 85     for(int i = 0; i < sulotion.count; i++) {
 86         cout << " " << sulotion.stamps[i]; 
 87     }
 88     cout << endl;
 89 }
 90 int main(void) {
 91     while(cin >> types[0]) {
 92         while (types[typeCount] != 0) {
 93             cin >> types[++typeCount];
 94         }
 95         while (cin >> customer) {
 96             if (customer == 0) {
 97                 break;
 98             }
 99             if (types[0] * 1 > customer) {
100                 cout << customer << " ---- none" << endl;
101                 continue;
102             }
103             if (types[typeCount - 1] * 4 < customer) {
104                 cout << customer << " ---- none" << endl;
105                 continue;
106             }
107             for (int i0 = 0; i0 < typeCount; i0++) {
108                 if(types[i0] == customer) {
109                     Solution solution;
110                     solution.stamps[0] = types[i0];
111                     solution.stamps[1] = 0;
112                     solution.stamps[2] = 0;
113                     solution.stamps[3] = 0;
114                     solution.maxValue = types[i0];
115                     solution.count = 1;
116                     solution.types = 1;
117                     sVector.push_back(solution);
118                     continue;
119                 } else if (types[i0] < customer){
120                     for (int i1 = i0; i1 < typeCount; i1++) {
121                         if(types[i0] + types[i1] == customer) {
122                             Solution solution;
123                             solution.stamps[0] = types[i0];
124                             solution.stamps[1] = types[i1];
125                             solution.stamps[2] = 0;
126                             solution.stamps[3] = 0;
127                             solution.maxValue = types[i1];
128                             solution.count = 2;
129                             solution.types = getTypes(i0, i1);
130                             sVector.push_back(solution);
131                             continue;
132                         } else if (types[i0] + types[i1] < customer) {
133                             for(int i2 = i1; i2 < typeCount; i2++) {
134                                 int sum = types[i0] + types[i1] + types[i2];
135                                 if(sum == customer) {
136                                     Solution solution;
137                                     solution.stamps[0] = types[i0];
138                                     solution.stamps[1] = types[i1];
139                                     solution.stamps[2] = types[i2];
140                                     solution.stamps[3] = 0;
141                                     solution.maxValue = types[i2];
142                                     solution.count = 3;
143                                     solution.types = getTypes(i0, i1, i2);
144                                     sVector.push_back(solution);
145                                 } else if (sum < customer){
146                                     for(int i3 = i2; i3 < typeCount; i3++) {
147                                         int sum = types[i0] + types[i1] + types[i2] + types[i3];
148                                         if(sum == customer) {
149                                             Solution solution;
150                                             solution.stamps[0] = types[i0];
151                                             solution.stamps[1] = types[i1];
152                                             solution.stamps[2] = types[i2];
153                                             solution.stamps[3] = types[i3];
154                                             solution.maxValue = types[i3];
155                                             solution.count = 4;
156                                             solution.types = getTypes(i0, i1, i2, i3);
157                                             sVector.push_back(solution);
158                                         } else if (sum > customer) {
159                                             break;
160                                         }
161                                     }
162                                 } else {
163                                     break;
164                                 }
165                             }
166                         } else {
167                             break;
168                         }
169                     }
170                 }  else {
171                     break;
172                 }
173             }
174             if (sVector.size() == 0) {
175                 cout << customer << " ---- none" << endl;
176                 continue;
177             }
178             int bestSolution = findBestSolution();
179             if (isTie) {
180                 cout << customer << " (" << sVector[bestSolution].types << "): tie" << endl;
181             } else {
182                 cout << customer;
183                 Output(bestSolution);
184             }
185             sVector.clear();
186             isTie = false;
187         }
188         customerCount = 0;
189         typeCount = 0;
190         sVector.clear();    
191     }
192     system("pause");
193     return 0;
194 }
View Code
posted @ 2013-07-30 23:12  小菜刷题史  阅读(519)  评论(0编辑  收藏  举报