UVA812-Trade on Verweggistan (violent), ontrade

Source: Internet
Author: User

UVA812-Trade on Verweggistan (violent), ontrade

Question Link


A merchant wants to buy pruls. Then its value is a sequence, which should be strictly obtained from start to end during purchase. For example, if you want to buy 5th, the first four should also be bought together, ask the merchant for the maximum profit.

Idea: the maximum profit must be the sum of the maximum values of each sequence. For the output, we record the position where each row can obtain the maximum value, calculate all possible values back, and then output the first 10 smallest values.

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int INF = 0x3f3f3f3f;const int MAXN = 55;int arr[MAXN][MAXN], sum[MAXN][MAXN];int num[MAXN], x[MAXN * MAXN + 5];int w, b, ans, n;vector<int> v[MAXN];void dfs(int cnt, int s) {    if (cnt == w) {        x[s] = 1;           return;    }    int l = v[cnt].size();    for (int i = 0; i < l; i++)        dfs(cnt + 1, s + v[cnt][i]);}void outPut() {    int c = 10;    for (int i = 0; i < MAXN * MAXN; i++) {        if (c == 0)             break;        if (x[i]) {             printf(" %d", i);             c--;        }    }     printf("\n");}int main() {    int t = 0;    while (scanf("%d", &w) && w) {        memset(sum, 0, sizeof(sum));        memset(num, 0, sizeof(num));        for (int i = 0; i < w; i++) {            scanf("%d", &b);            num[i] = b;            for (int j = 0; j < b; j++) {                 scanf("%d", &arr[i][j]);                arr[i][j] = 10 - arr[i][j];                sum[i][j] = sum[i][j - 1] + arr[i][j];            }         }                 ans = 0;        for (int i = 0; i < w; i++) {            int Max = 0;            v[i].clear();            v[i].push_back(0);            for (int j = 0; j < num[i]; j++) {                 if (sum[i][j] > Max) {                    v[i].clear();                    v[i].push_back(j + 1);                      Max = sum[i][j];                }                else if (sum[i][j] == Max)                    v[i].push_back(j + 1);            }              ans += Max;         }         memset(x, 0, sizeof(x));        dfs(0, 0);        if (t)             printf("\n");        printf("Workyards %d\n", ++t);        printf("Maximum profit is %d.\n", ans);        printf("Number of pruls to buy:");        outPut();    }    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.