zoukankan      html  css  js  c++  java
  • codeforces 744C Hongcow Buys a Deck of Cards

    C. Hongcow Buys a Deck of Cards
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    One day, Hongcow goes to the store and sees a brand new deck of n special cards. Each individual card is either red or blue. He decides he wants to buy them immediately. To do this, he needs to play a game with the owner of the store.

    This game takes some number of turns to complete. On a turn, Hongcow may do one of two things:

    • Collect tokens. Hongcow collects 1 red token and 1 blue token by choosing this option (thus, 2 tokens in total per one operation).
    • Buy a card. Hongcow chooses some card and spends tokens to purchase it as specified below.

    The i-th card requires ri red resources and bi blue resources. Suppose Hongcow currently has A red cards and B blue cards. Then, thei-th card will require Hongcow to spend max(ri - A, 0) red tokens, and max(bi - B, 0) blue tokens. Note, only tokens disappear, but the cards stay with Hongcow forever. Each card can be bought only once.

    Given a description of the cards and their costs determine the minimum number of turns Hongcow needs to purchase all cards.

    Input

    The first line of input will contain a single integer n (1 ≤ n ≤ 16).

    The next n lines of input will contain three tokens ciri and bici will be 'R' or 'B', denoting the color of the card as red or blue. ri will be an integer denoting the amount of red resources required to obtain the card, and bi will be an integer denoting the amount of blue resources required to obtain the card (0 ≤ ri, bi ≤ 107).

    Output

    Output a single integer, denoting the minimum number of turns needed to acquire all the cards.

    Examples
    input
    3
    R 0 1
    B 1 0
    R 1 1
    output
    4
    input
    3
    R 3 0
    R 2 0
    R 1 0
    output
    6
    Note

    For the first sample, Hongcow's four moves are as follows:

    1. Collect tokens
    2. Buy card 1
    3. Buy card 2
    4. Buy card 3
    Note, at the fourth step, Hongcow is able to buy card 3 because Hongcow already has one red and one blue card, so we don't need to collect tokens.

    For the second sample, one optimal strategy is as follows:

    1. Collect tokens
    2. Collect tokens
    3. Buy card 2
    4. Collect tokens
    5. Buy card 3
    6. Buy card 1
    At the fifth step, even though Hongcow has a red token, Hongcow doesn't actually need to spend it, since Hongcow has a red card already.

      N个物品需要购买,分为R,B两类,每种物品需要(ri,bi)的价格,但是每持有一个R,购买物品中ri的价格就减少1(价格最小为0),B也同理。每一个单位时间可以用来买东西,或者获得一个单位货币r和一个单位货币b。求最少需要多少时间可以买下所有的物品。

      考虑到N很小,显然可以考虑状压DP,将已经获得的物品记为状态,同时考虑到答案只和节省下来的步数有关,则记F[S][j],S为状态集合,j为节省了多少R类货币时,B类货币最多节省了多少。
    ANS=min(max(totr-i,totb-f[x][i]))+n------------------------------x=(1<<n)-1,0<=i<=n
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<cstring>
    #include<vector>
    using namespace std;
    #define llg int
    #define maxn 20
    #define maxnn (1<<16)+10
    #define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
    
    struct node
    {
        char ch;
        llg r,b;
    }a[maxn];
    
    struct data
    {
        llg r,b;
    }c[maxnn];
    
    llg n,cnt[maxnn],f[maxnn][401],totr,totb;
    
    void init()
    {
        cin>>n;
        for (llg i=1;i<=n;i++)
        {
            a[i].ch=getchar(); while (a[i].ch!='R' && a[i].ch!='B') a[i].ch=getchar();
            cin>>a[i].r>>a[i].b;
            totr+=a[i].r; totb+=a[i].b;
        }
        for (llg x=0;x<=(1<<n);x++)
        {
            for (llg i=0;i<n;i++)
                if (x&(1<<i)) 
                {
                    if (a[i+1].ch=='R') c[x].r++; else c[x].b++;
                }
        }
        for (llg x=0;x<=(1<<n);x++)
        {
            for (llg i=0;i<=n;i++) if (x&(1<<i)) cnt[x]++;
        }
    }
    
    void DP()
    {
        for (llg i=0;i<=(1<<n);i++) for (llg j=0;j<=n*n;j++) f[i][j]=-1;
        f[0][0]=0;
        for (llg x=0;x<=(1<<n);x++)
        {
            for (llg y=0;y<=n*n;y++)
                if (f[x][y]!=-1)
                {
                    for (llg k=0;k<n;k++)
                        if (!(x&(1<<k)))
                        {
                            f[x+(1<<k)][y+min(c[x].r,a[k+1].r)]=max(f[x+(1<<k)][y+min(c[x].r,a[k+1].r)],f[x][y]+min(a[k+1].b,c[x].b));
                        }
                }
        }
        
    }
    
    int main()
    {
        
        init();
        DP();
        llg ans=0x7fffffff;
        llg x=(1<<n)-1;
        for (llg i=0;i<=n*n;i++) if (f[x][i]!=-1) ans=min(ans,max(totr-i,totb-f[x][i]));
        cout<<ans+n;
        return 0;
    }

      
    本文作者:xrdog 作者博客:http://www.cnblogs.com/Dragon-Light/ 转载请注明出处,侵权必究,保留最终解释权!
  • 相关阅读:
    Word Embedding理解
    几种简单的主题模型(生成模型)
    BTM学习小记
    LDA学习小记
    word2vec训练好的词向量
    java带字符编码编译
    web安全领域常见的攻击方式
    宝塔webhook布置gitee自动同步服务端
    unity3d学习路线
    缩略图含裁剪文件
  • 原文地址:https://www.cnblogs.com/Dragon-Light/p/6217977.html
Copyright © 2011-2022 走看看