UVA-812 trade on Verweggistan DFS + greedy

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.