UVA812-Trade on Verweggistan(暴力),ontrade

來源:互聯網
上載者:User

UVA812-Trade on Verweggistan(暴力),ontrade

題目連結


題意:商人要去買pruls這種東西。然後它的價值是一個序列,買的時候要嚴格從頭到尾取,比如你要買第5個,那麼前4個也要一起買下來,求商人能獲得的最大的利潤。

思路:最大利潤肯定就是每個序列的最大值的和。對於輸出的話,我們記錄下每行能取得最大值的位置,然後回溯去計算所有可能值,然後輸出前10個最小的值。

#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;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.