UVA 812 Trade on Verweggistan

Source: Internet
Author: User

Test instructions

To the W shelves, each shelf has bi goods, each time can only take the top of the goods, each goods have a value, all the goods are priced at 10.

Q: What is the maximum profit that can be obtained and how many goods will be required to obtain this profit. (only the first 10 outputs are available in multiple combinations)

Ideas:

At first I was pre-preprocessed the maximum value, and then the DFS find the number of scenarios, the results timed out, and later found that the complexity is O (w*bi), complete violence, can first the maximum profit per shelf to deal with, while processing out to obtain the maximum profit of the number of items required.

Then WA a few rounds, the first time is to find that they did not handle if the profit is negative, the result should be output 0 of the situation.

The second discovery did not deal with a shelf maximum profit of 0 can not take a situation.

Code:

  

1#include <cmath>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <ctime>6#include <Set>7#include <map>8#include <list>9#include <stack>Ten#include <queue> One#include <string> A#include <vector> -#include <fstream> -#include <iterator> the#include <iostream> -#include <algorithm> - using namespacestd; - #defineLL Long Long + #defineINF 0x3f3f3f3f - #defineMOD 1000000007 + #defineEPS 1e-6 A #defineMAXN 100 at #defineMAXM 30 - #defineDD {cout<< "Debug" &LT;&LT;ENDL;} - #definePA {System ("pause");} - #defineP (x) {printf ("%d\n", x);} - #definePD (x) {printf ("%.7lf\n", x);} - #defineK (x) {printf ("Case%d:", ++x);} in #defineS (x) {scanf ("%d", &x);} - #defineSD (x) {scanf ("%lf", &x);} to #defineMes (x, D) {memset (x, D, sizeof (x));} + #defineDo (i, X) for (i = 0; i < x; i + +) - #defineDoD (i, X, L) for (i = x; I >= l; I-) the #defineDoe (i, X) for (i = 1; i <= x; i + +) * intW; $ intF[MAXN][MAXM];Panax Notoginseng intMax_ans, kcase =0; - Set<int>ans; theVector <int>G[MAXN]; + voidRead () A { theMax_ans =0; +      for(inti =0; i < W; i + +) -     { $scanf"%d", &f[i][0]); $  -         intsum =0; -         intMax_tmp =0; the g[i].clear (); -          for(intj =1; J <= f[i][0]; J + +)Wuyi         { thescanf"%d", &f[i][j]); -F[I][J] =Ten-F[i][j]; WuSum + =F[i][j]; -             if(Sum >max_tmp) About             { $Max_tmp =sum; - g[i].clear (); -             } -             if(Sum = =max_tmp) A G[i].push_back (j); +         } the         if(G[i].empty () | | max_tmp = =0) G[i].push_back (0);//!!! -Max_ans + =max_tmp; $     } the } the voidDfsintPosintnum) the { the     if(pos = =W) -     { in Ans.insert (num); the         return ; the     } About  the      for(inti =0; I < g[pos].size (); i + +) theDFS (POS +1, Num +g[pos][i]); the } + voidShow () - { the     intCNT =0;Bayiprintf"workyards%d\n",  ++kcase); theprintf"Maximum profit is%d.\n", Max_ans); theprintf"Number of pruls to buy:"); -      for(Set<int>::iterator it = Ans.begin (); It! = Ans.end () && CNT <Ten; It + +, CNT + +) -printf"%d", *it); theprintf"\ n"); the } the voidSolve () the { - ans.clear (); theDfs0,0); the Show (); the }94  the intMain () the { the      while(SCANF ("%d", &w) &&W)98     { About         if(kcase) printf ("\ n"); - read ();101 solve ();102     }103     return 0;104}
View Code

UVA 812 Trade on Verweggistan

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.