The main topic: There are n piles of things, only from the top down to buy, if you want to buy the following things, can only buy the above all things can, like a stack
Every thing has a price, every thing sold can only sell 10, ask to sell the biggest profit, and how many to sell to achieve maximum profit
Problem-solving ideas: greedy, each bought to sell, in order to maximize profits, record each heap to sell the maximum profit in all cases, and then use the DFS one by one enumeration, to be sentenced to heavy and sort output
Attention to the special case, it's all 0.
1
5 10 10 10 10 10
The output should have 0 1 2 3 4 5
#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;#define MAXNintW, price[maxn][ -], SUM, NUM[MAXN], CNT, vis[2500],ans[2500];structMax {intCntintm[ -];intNum;} MAX[MAXN];voidInit () {intMax =0, sum2; sum =0; CNT =0;memset(Vis,0,sizeof(VIS)); for(inti =0; i < W; i++) {scanf("%d", &num[i]); sum + = Max; sum2 = Max =0; for(intj =0; J < Num[i]; J + +) {scanf("%d", &price[i][j]); Sum2 + =Ten-PRICE[I][J]; max = max (max, sum2); } max[i].num = MAX; max[i].cnt =0; } sum + = Max; for(inti =0; i < W; i++) {sum2 =0;if(Max[i].num = =0) Max[i]. m[max[i].cnt++] =0; for(intj =0; J < Num[i]; J + +) {sum2 + =Ten-PRICE[I][J];if(sum2 = = Max[i].num) Max[i]. m[max[i].cnt++] = j +1; } }}voidDfsintSum,intcur) {if(cur = = w &&!vis[sum]) {ans[cnt++] = Sum; Vis[sum] =1;return; } for(inti =0; i < max[cur].cnt; i++) {Sum + = Max[cur]. M[i]; DFS (sum,cur+1); Sum-= Max[cur]. M[i]; }}intMain () {intCAS =0; while(scanf("%d", &w) = =1&& W) {if(CAS)printf("\ n"); Init (); Dfs0,0); Sort (ans,ans+cnt);printf("Workyards%d\n", ++cas);printf("Maximum profit is%d.\n", sum);printf("Number of pruls to buy:"); for(inti =0; I < cnt && I <Ten; i++)printf("%d", Ans[i]);printf("\ n"); }return 0;}
UVA-812 trade on Verweggistan DFS + greedy