812-Trade on Verweggistan (brute force enumeration)

Source: Internet
Author: User

Trade on Verweggistan

Since the days of Peter Stuyvesant and Abel Tasman, Dutch merchants have been traveling all over the world to buy and parse goods. once there was some trade on Verweggistan, but it ended after a short time. after reading this story you will understand why.

At that time Verweggistan was quite popular, because it was the only place in the world where people knew how to make a 'prul '. the end of the trade on Verweggistan meant the end of the trade in pruls (or 'prullen', as the Dutch plural said), and very few people nowadays know what a prul actually is.

Pruls were manufactured in workyards. whenever a prul was finished it was packed in a box, which was then placed on top of the pile of previusly produced pruls. on the side of each box the price was written. the price depended on the time it took to manufacture the prul. if all went well, a prul wowould cost one or two florins, but on a bad day the price cocould easily rise to 15 florins or more. this had nothing to do with quality; all pruls had the same value.

In those days pruls sold for 10 florins each in Holland. transportation costs were negligible since the pruls were taken as extra on ships that wowould sail anyway. when a Dutch merchant went to Verweggistan, he had a clear purpose: buy pruls, incluthem in Holland, and maximize his profits. unfortunately, the Verweggistan way of trading pruls made this more complicated than one wowould think.

One wowould perform CT that merchants wowould simply buy the cheapest pruls, and the pruls that cost more than 10 florins wowould remain unsold. unfortunately, all workyards on Verweggistan sold their pruls in a particle order. the box on top of the pile was sold first, then the second one from the top, and so on. so even if the maximum th box from the top was the cheapest one, a merchant wowould have to buy the other four boxes above to obtain it.


As you can imagine, this made it quite difficult for the merchants to maximize their profits by buying the right set of pruls. not having computers to help with optimization, they quickly lost interest in trading pruls at all.

In this problem, you are given the description of several workyard piles. you have to calculate the maximum profit a merchant can obtain by buying pruls from the piles according to the restrictions given above. in addition, you have to determine the number of pruls he has to buy to achieve this profit.

Input The input describes several test cases. The first line of input for each test case contains a single integer W, The number of workyards in the test case (1 W50 ).

This is followedWLines, each describing a pile of pruls. The first number in each line is the numberBOf boxes in the pile (0B20). Following it areBPositive integers, indicating the prices (in florins) of the pruls in the stack, given from top to bottom.

The input is terminated by a description startingW= 0. This description shocould not be processed.

Output For each test case, print the case number (1, 2 ,...). then print two lines, the first containing the maximum profit the merchant can achieve. the second line shoshould specify the number of pruls the merchant has to buy to obtain this profit. if this number is not uniquely determined, print the possible values in increasing order. if there are more than ten possible values, print only the 10 smallest.

Display a blank line between test cases.

Sample Input
16 12 3 10 7 16 525 7 3 11 9 109 1 2 3 4 10 16 10 4 160

Sample Output
Workyards 1Maximum profit is 8.Number of pruls to buy: 4Workyards 2Maximum profit is 40.Number of pruls to buy: 6 7 8 9 10 12 13

N wordyard entries are given. Each given B pruls. This is a stack from the top to the end. Now, each wordyard can be retrieved ., Each profit is 10-The value of each pruls. Calculate the maximum profit and the number of components. Note that if the number of components is different, the first 10 minimum values are output.

Idea: For each wordyard enumeration of the maximum profit, save the number, and then add all the types to a set.

Code:

#include 
 
  #include 
  
   #include 
   
    #include #include 
    
     using namespace std;#define max(a,b) (a)>(b)?(a):(b)#define min(a,b) (a)<(b)?(a):(b)const int N = 55;const int M = 25;int n, num[N][M], res[N][M], resn[N];set
     
       ans;void init() {ans.clear();memset(resn, 0, sizeof(resn));for (int i = 0; i < n; i++) {scanf("%d", &num[i][0]);for (int j = 1; j <= num[i][0]; j++)scanf("%d", &num[i][j]);}}void cal(int i, int sum) {if (i == n) {ans.insert(sum);return;}for (int j = 0; j < resn[i]; j++)cal(i + 1, sum + res[i][j]);}void solve() {int Max = 0;for (int i = 0; i < n; i++) {int sum = 0, Maxx = 0; resn[i] = 1; res[i][0] = 0;for (int j = 1; j <= num[i][0]; j++) {sum += 10 - num[i][j];if (sum > Maxx) {Maxx = sum;resn[i] = 1;res[i][0] = j;}else if (sum == Maxx)res[i][resn[i]++] = j;}Max += Maxx;}printf("Maximum profit is %d.\n", Max);printf("Number of pruls to buy:");cal(0, 0);int count = 0;for (set
      
       ::iterator it = ans.begin(); it != ans.end() && count != 10; it++, count++)cout <<" "<< *it;printf("\n");}int main() {int cas = 0;while (~scanf("%d", &n) && n) {if (cas) printf("\n");init();printf("Workyards %d\n", ++cas);solve();}return 0;}
      
     
    
   
  
 


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.