題目連接:uva 812 - Trade on Verweggistan
題目大意:給出n個序列,每個序列給出k,表示有k個元素,每行只能從頭取,取i(i≤k),現在要在每個序列前取走c[i]個,保證說sum = ∑| 10 - num| (num為所選數的集合)最大,並且輸出可以選擇的個數,超過10個的話只要輸出最小的10個。
解題思路:對於每個序列遍歷一次,找出最大值和可以組成最大值的個數,注意可以取0個,然後遞歸求解出所有的組成情況。
#include#include #include #include using namespace std; const int tmp = 10; const int N = 60; const int M = 1005; const int INF = 0x3f3f3f3f; int n, ans, cnt[M]; vector v[N]; void init() { ans = 0; memset(cnt, 0, sizeof(cnt)); int m, num; for (int i = 0; i < n; i++) { scanf("%d", &m); v[i].clear(); int sum = 0, Max = 0; v[i].push_back(0); for (int j = 0; j < m; j++) { scanf("%d", &num); sum += (tmp - num); if (sum > Max) { v[i].clear(); v[i].push_back(j+1); Max = sum; } else if (sum == Max) { v[i].push_back(j+1); } } ans += Max; } } void dfs(int d, int s) { if (d >= n) { cnt[s] = 1; return ; } int top = v[d].size(); for (int i = 0; i < top; i++) { dfs(d + 1, s + v[d][i]); } } void solve() { dfs(0, 0); int c = 0; for (int i = 0; i < M; i++) { if (c >= tmp) break; if (cnt[i]) { printf(" %d", i); c++; } } printf("\n"); } int main() { int cas = 0; while (scanf("%d", &n) == 1 && n) { init(); if (cas) printf("\n"); printf("Workyards %d\n", ++cas); printf("Maximum profit is %d.\n", ans); printf("Number of pruls to buy:"); solve(); } return 0; }