我用的是搜索,后来百度了一下,大概有3种做法,一直接搜索,二,用递归深搜,三,动态规划(http://blog.youkuaiyun.com/cugbliang/article/details/2742242)
一开始wa,是因为漏了这种情况 7 4 0 <cr> 1 0,我的输出为1(0):2,1(0):2,应该为1 --- none ,太粗心了,稍稍改动之后AC
描述
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.
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.
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.
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
7 (3): 1 1 2 3 4 (2): 1 3 6 ---- none 2 (2): 1 1 3 (2): tie
# include <stdio.h> # include <string> # include <string.h> # include <stdlib.h> int swap (int &a, int &b) { if (a == b) return 0; a ^= b ^= a ^= b; return 0; } int partition (int A[], int p, int r) { int q = p; for (int i = p + 1; i <= r; ++ i) { if (A[i] < A[p]) { ++ q; swap (A[q], A[i]); } } swap (A[p], A[q]); return q; } int qsort (int A[], int p, int r) { while (p < r) { int q = partition(A, p, r); qsort (A, p, q - 1); p = q + 1; } return 0; } int check (int s[], int size, int c[], int best[], int r[], int flag[]) { int cnt = size; int max = 0; int tot = size; int b[size]; //for (int i = 0; i < size; ++ i) printf ("%d ", c[i] ); memset(b, 0, sizeof(b)); for (int i = 0; i < size; ++ i) { max = s[c[i]] > max ? s[c[i]] : max; if (b[i] == 0) for (int j = i + 1; j < size; ++ j) { if (c[i] == c[j]) { -- cnt; b[j] = -1; } } } if (cnt > best[1]) { memcpy (r, c, sizeof(int) * 4); best[1] = cnt; best[2] = max; best[3] = tot; flag[0] = 0; } if (cnt == best[1] && tot < best[3]) { memcpy (r, c, sizeof(int) * 4); best[1] = cnt; best[2] = max; best[3] = tot; flag[0] = 0; } if (cnt == best[1] && tot == best[3] && max > best[2]) { memcpy (r, c, sizeof(int) * 4); best[1] = cnt; best[2] = max; best[3] = tot; flag[0] = 0; } //for (int i = 0; i < size; ++ i)printf ("%d ", c[i]);printf (" - "); if (cnt == best[1] && tot == best[3] && max == best[2]) { //qsort (r, 0, size - 1); //qsort (c, 0, size - 1); //for (int i = 0; i < size; ++ i) printf ("%d-%d ", c[i], r[i]); //printf ("\n"); for (int i = 0; i < size; ++ i) { if (r[i] != c[i]) { ++ flag[0]; break; } } } return 0; } int go(int s[], int nd) { if (s[s[0]] * 4 < nd || s[1] > nd) {printf("%d ---- none\n", nd); return 0;} int best[4], flag[4], size, r[4], c[4]; memset(best, 0, sizeof(best)); memset(r, 0, sizeof(r)); memset(flag, 0, sizeof(flag)); memset(c, 0, sizeof(c)); int sum; for (int i = 1; i <= s[0]; ++ i) { sum = s[i]; if (sum > nd) break; if (sum == nd) { c[0] = i; size = 1; check(s, size, c, best, r, flag); } for (int j = i; j <= s[0]; ++ j) { sum = s[i] + s[j]; if (sum > nd) break; if (sum == nd) { c[0] = i; c[1] = j; size = 2; check(s, size, c, best, r, flag); } for (int m = j; m <= s[0]; ++ m) { sum = s[i] + s[j] + s[m]; //if (sum == nd) printf ("%d %d %d -- ", i, j, m); if (sum > nd) break; if (sum == nd) { c[0] = i; c[1] = j; c[2] = m; size = 3; check(s, size, c, best, r, flag); } for (int n = m; n <= s[0]; ++ n) { sum = s[i] + s[j] + s[m] + s[n]; if (sum > nd) break; if (sum == nd) { c[0] = i; c[1] = j; c[2] = m; c[3] = n; size = 4; check(s, size, c, best, r, flag); } } } } } if (flag[0])printf ("%d (%d): tie\n", nd, best[1]); else if (best[3] != 0){ printf ("%d (%d): ", nd, best[1]); for (int i = 0; i < best[3] - 1; ++ i) { printf ("%d ", s[r[i]]); } printf ("%d\n", s[r[best[3] - 1]]); }else printf("%d ---- none\n", nd); return 0; } int main () { int s[100]; s[0] = 0; int tmp; while (1) { if(scanf ("%d", &tmp) == EOF) return 0; if (tmp) { ++ s[0]; s[s[0]] = tmp; } else { //for (int i = 1; i <= s[0]; ++ i) printf ("%d ", s[i]); //printf ("\n************\n"); qsort (s, 1, s[0]); while (1) { if(scanf ("%d", &tmp) == EOF) return 0; if (tmp) { go (s, tmp); } else { s[0] = 0; break; } } } } return 0; }