codeforces 744C Hongcow Buys a Deck of Cards
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.
The first line of input will contain a single integer n (1 ≤ n ≤ 16).
The next n lines of input will contain three tokens ci, ri and bi. ci 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 a single integer, denoting the minimum number of turns needed to acquire all the cards.
3
R 0 1
B 1 0
R 1 1
4
3
R 3 0
R 2 0
R 1 0
6
For the first sample, Hongcow's four moves are as follows:
- Collect tokens
- Buy card 1
- Buy card 2
- Buy card 3
For the second sample, one optimal strategy is as follows:
- Collect tokens
- Collect tokens
- Buy card 2
- Collect tokens
- Buy card 3
- Buy card 1
考虑到N很小,显然可以考虑状压DP,将已经获得的物品记为状态,同时考虑到答案只和节省下来的步数有关,则记F[S][j],S为状态集合,j为节省了多少R类货币时,B类货币最多节省了多少。
#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; }