题意:有n个货架上摆放了b个货物,然后从左到右的给出每个货架上货物买入的价格,要拿去卖掉以统一价10,问如果买入形式是栈,要买一个货物必须把他左边的东西都买了才可以,问最大利润是多少,要买多少个货物达到最大利润,按字典序输出所有可能情况(最多10种)。
题解:水题,暴力出所有情况。
第二组样例举例:
3 10 9 10 10
购买数量可能情况:2 4 5
9 17 24 30 30 24 24 30 24
购买数量可能情况: 4 5 8
然后就是6个数字的排列组合相加得到所有解。注意数量可能为0。
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int M = 1005; const int N = 55; int box[N], n, num[N][N], mp[M], num2[N]; void dfs(int cur, int sum) { if (cur >= n) { mp[sum] = 1; return; } for (int i = 0; i < num2[cur]; i++) dfs(cur + 1, sum + num[cur][i]); } int main() { int cas = 0; while (scanf("%d", &n) == 1 && n) { memset(mp, 0, sizeof(mp)); int temp, res = 0; for (int i = 0; i < n; i++) { scanf("%d", &temp); int sum = 0, maxx = 0; for (int j = 0; j < temp; j++) { scanf("%d", &box[j]); sum += 10 - box[j]; maxx = max(maxx, sum); } res += maxx; sum = 0; int cnt = 0; if (maxx == 0) num[i][cnt++] = 0; for (int j = 0; j < temp; j++) { sum += 10 - box[j]; if (sum == maxx) num[i][cnt++] = j + 1; } num2[i] = cnt; } if (cas) printf("\n"); printf("Workyards %d\n", ++cas); printf("Maximum profit is %d.\n", res); printf("Number of pruls to buy:"); dfs(0, 0); temp = 0; for (int i = 0; i < M; i++) { if (temp == 10) break; if (mp[i]) { temp++; printf(" %d", i); } } printf("\n"); } return 0; }
原文地址:http://blog.csdn.net/hyczms/article/details/45228131