POJ 1010 STAMPS
STAMPS
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13528 | Accepted: 3749 |
Description
Have you done any Philately lately?
You have been hired by the Ruritanian Postal Service (RPS) to design their new postage software. The software allocates stamps to customers based on customer needs and the denominations that are currently in stock.
Ruritania is filled with people who correspond with stamp collectors. As a service to these people, the RPS asks that all stamp allocations have the maximum number of different types of stamps in it. In fact, the RPS has been known to issue several stamps of the same denomination in order to please customers (these count as different types, even though they are the same denomination). The maximum number of different types of stamps issued at any time is twenty-five.
To save money, the RPS would like to issue as few duplicate stamps as possible (given the constraint that they want to issue as many different types). Further, the RPS won't sell more than four stamps at a time.
You have been hired by the Ruritanian Postal Service (RPS) to design their new postage software. The software allocates stamps to customers based on customer needs and the denominations that are currently in stock.
Ruritania is filled with people who correspond with stamp collectors. As a service to these people, the RPS asks that all stamp allocations have the maximum number of different types of stamps in it. In fact, the RPS has been known to issue several stamps of the same denomination in order to please customers (these count as different types, even though they are the same denomination). The maximum number of different types of stamps issued at any time is twenty-five.
To save money, the RPS would like to issue as few duplicate stamps as possible (given the constraint that they want to issue as many different types). Further, the RPS won't sell more than four stamps at a time.
Input
The input for your program will be pairs of positive integer sequences, consisting of two lines, alternating until end-of-file. The first sequence are the available values of stamps, while the second sequence is a series of customer requests. For example:
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
Note: the comments in this example are *not* part of the data file; data files contain only integers.
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
Note: the comments in this example are *not* part of the data file; data files contain only integers.
Output
For each customer, you should print the "best" combination that is exactly equal to the customer's needs, with a maximum of four stamps. If no such combination exists, print "none".
The "best" combination is defined as the maximum number of different stamp types. In case of a tie, the combination with the fewest total stamps is best. If still tied, the set with the highest single-value stamp is best. If there is still a tie, print "tie".
For the sample input file, the output should be:
7 (3): 1 1 2 3
4 (2): 1 3
6 ---- none
2 (2): 1 1
3 (2): tie
That is, you should print the customer request, the number of types sold and the actual stamps. In case of no legal allocation, the line should look like it does in the example, with four hyphens after a space. In the case of a tie, still print the number of types but do not print the allocation (again, as in the example).Don't print extra blank at the end of each line.
The "best" combination is defined as the maximum number of different stamp types. In case of a tie, the combination with the fewest total stamps is best. If still tied, the set with the highest single-value stamp is best. If there is still a tie, print "tie".
For the sample input file, the output should be:
7 (3): 1 1 2 3
4 (2): 1 3
6 ---- none
2 (2): 1 1
3 (2): tie
That is, you should print the customer request, the number of types sold and the actual stamps. In case of no legal allocation, the line should look like it does in the example, with four hyphens after a space. In the case of a tie, still print the number of types but do not print the allocation (again, as in the example).Don't print extra blank at the end of each line.
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
=================================================================================================
这个题目就是DFS搜索了...减枝条件是超用户需求,数量超4
还有网上提到对于n(n>4)张邮票有相同值,可以去掉超过4的那些邮票.
自己写的代码烂的不行..有时间再重新刷.
#include <iostream> #include <vector> #include <stdio.h> using namespace std; int need = 0;//客户需求 int sum = 0;//已经达到的 vector<int> table;//使用了哪些 int ntype = 0;//使用的种类 int nstamp = 0;//使用的数量 int number = 0;//使用的数量 int max_types = -1000;//使用最多的种类 int min_stamp = 1000;//使用最少的邮票 int max_value = -100;//包含最大的邮票 vector<int> best_collection; int count = 0; //相同方案数量 void search(vector<int> &types,int dep=0) { //加起来超过需要或者使用的种类超过4 if(sum > need || ntype > 4 || nstamp > 4) return; if(sum == need)//满足需求 { int mmax = -100; for(unsigned int i=0;i<table.size();++i) { if(table[i] != 0) { mmax = max(mmax,types[i]); } } if((ntype > max_types) || (ntype == max_types && nstamp < min_stamp) || (ntype == max_types && nstamp == min_stamp && mmax > max_value)) { best_collection = table; max_types = ntype; max_value = mmax; min_stamp = nstamp; count = 1; } else if(nstamp == min_stamp && mmax == max_value && ntype == max_types) { count += 1; } } if(sum < need && dep < (int)types.size()) { int v = types[dep];//当前票值 //1.不用当前票 search(types,dep+1); //2.用当前票 if(ntype + 1 > 4) return; int old_sum = sum; int old_stamp = nstamp; ntype += 1; for(unsigned i=1;;++i) { //用i张 int sv = i*v + old_sum;//值 if(sv > need) break; nstamp = old_stamp + i;//用的数量 table[dep] = i;//用i张 sum = sv; search(types,dep+1); } ntype -= 1; sum = old_sum; nstamp = old_stamp; table[dep] = 0; } } void solve(vector<int> &types,vector<int> &customers) { for(unsigned int i=0;i<customers.size();++i) { table = vector<int>(); table.resize(30); max_value = -1000; max_types = -1000; min_stamp = 1000; ntype = 0; nstamp = 0; number = 0; sum = 0; count = 0; need = customers[i]; search(types); if(count == 0) { printf("%d ---- none\n",need); } if(count == 1) { printf("%d (%d):",need,max_types); for(unsigned int i=0;i<best_collection.size();++i) { int n = best_collection[i]; for(int j=0;j<n;++j) { printf(" %d",types[i]); } } printf("\n"); } if(count > 1) { printf("%d (%d): tie\n",need,max_types); } } } void next_line() { while(getchar()!='\n'); } int main() { freopen("1010.data","r",stdin); while(true) { vector<int> types; int n; while(cin>>n) { if(n == 0) break; types.push_back(n); } if(!cin) break; next_line(); vector<int> customers; while(cin>>n) { if(n == 0) break; customers.push_back(n); } next_line(); solve(types,customers); } return 0; }