codeforces 744C Hongcow Buys a Deck of Cards
2 seconds
256 megabytes
standard input
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.
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
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:
- Collect tokens
- Collect tokens
- Buy card 2
- Collect tokens
- Buy card 3
- 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很小,显然可以考虑状压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][],totr,totb; void init()
{
cin>>n;
for (llg i=;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=;x<=(<<n);x++)
{
for (llg i=;i<n;i++)
if (x&(<<i))
{
if (a[i+].ch=='R') c[x].r++; else c[x].b++;
}
}
for (llg x=;x<=(<<n);x++)
{
for (llg i=;i<=n;i++) if (x&(<<i)) cnt[x]++;
}
} void DP()
{
for (llg i=;i<=(<<n);i++) for (llg j=;j<=n*n;j++) f[i][j]=-;
f[][]=;
for (llg x=;x<=(<<n);x++)
{
for (llg y=;y<=n*n;y++)
if (f[x][y]!=-)
{
for (llg k=;k<n;k++)
if (!(x&(<<k)))
{
f[x+(<<k)][y+min(c[x].r,a[k+].r)]=max(f[x+(<<k)][y+min(c[x].r,a[k+].r)],f[x][y]+min(a[k+].b,c[x].b));
}
}
} } int main()
{ init();
DP();
llg ans=0x7fffffff;
llg x=(<<n)-;
for (llg i=;i<=n*n;i++) if (f[x][i]!=-) ans=min(ans,max(totr-i,totb-f[x][i]));
cout<<ans+n;
return ;
}
codeforces 744C Hongcow Buys a Deck of Cards的更多相关文章
- Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)
Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...
- Codeforces 744C. Hongcow Buys a Deck of Cards(状压DP)
这题的难点在于状态的设计 首先显然是个状压,需要一维表示卡的状态,另一维如果设计成天数,难以知道当前的钱数,没法确定是否能够购买新的卡,如果设计成钱数,会发现状态数过多,空间与时间都无法承受.但是可以 ...
- Codeforces 745E Hongcow Buys a Deck of Cards 状压DP / 模拟退火
题意:现在有n张卡片(n <= 16), 每一轮你可以执行两种操作中的一种.1:获得一张红色令牌和一张蓝色令牌.2:购买一张卡片(如果可以买的话),购买的时候蓝色卡片可以充当蓝色令牌,红色同理, ...
- Codeforces Round #385 (Div. 1) C. Hongcow Buys a Deck of Cards
地址:http://codeforces.com/problemset/problem/744/C 题目: C. Hongcow Buys a Deck of Cards time limit per ...
- Hongcow Buys a Deck of Cards CodeForces - 744C (状压)
大意: n个红黑卡, 每天可以选择领取一块红币一块黑币, 或者买一张卡, 第$i$张卡的花费红币数$max(r_i-A,0)$, 花费黑币数$max(b_i-B,0)$, A为当前红卡数, B为当前黑 ...
- 「CF744C」Hongcow Buys a Deck of Cards「状压 DP」
题意 你有\(n\)个物品,物品和硬币有\(A\),\(B\)两种类型,假设你有\(M\)个\(A\)物品和\(N\)个\(B\)物品 每一轮你可以选择获得\(A, B\)硬币各\(1\)个,或者(硬 ...
- Codeforces Round #376 (Div. 2) F. Video Cards —— 前缀和 & 后缀和
题目链接:http://codeforces.com/contest/731/problem/F F. Video Cards time limit per test 1 second memory ...
- Codeforces Round #376 (Div. 2) F. Video Cards 数学,前缀和
F. Video Cards time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- [Swift]LeetCode914.一副牌中的X | X of a Kind in a Deck of Cards
In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ...
随机推荐
- MVVM与Backbone demo
MVVM https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel
- lua拾遗之lua中冒号(:)与点号(.)的区别和来由
参考资料 1.https://my.oschina.net/lonewolf/blog/173065 其结论为: 1.定义的时候:Class:test()与 Class.test(self)是等价的, ...
- Hive:常用的一些命令
1.一般可以通过beeline,代理方式登录hive; 2.使用数据库abc_hive_db:use abc_hive_db; 3.查看数据库中有哪些表:show tables :有哪些特定表 sho ...
- Eclipse开发快捷键技巧
1.alt+?或alt+/:自动补全代码或者提示代码 这个是我最得意的快捷键组合了,尤其是当输入syso几个字符之后,2个手指轻松按下这2个键的时候,自动就补全System.out.println() ...
- poj 2393 Yogurt factory
http://poj.org/problem?id=2393 Yogurt factory Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- 【C# 小窍门】WeakEventManager 无法识别!ErrorCS0246The type or namespace name 'WeakEventManager' could not be found
WeakEventManager 想用一下这个,在学习C# 事件的时候,结果添加了Using System.Windows, WeakEventManager却一直是红色的,无法识别,报这个错哦~ 好 ...
- [转]DAO层,Service层,Controller层、View层
来自:http://jonsion.javaeye.com/blog/592335 DAO层 DAO 层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DA ...
- vue-cli安装失败问题
1.尝试 管理员权限 安装,看是否能解决问题 2.仍未解决问题, 系统变量增加: C:\Program Files\nodejs\node cache\node_modules\vue-cli\bi ...
- Ado.net 数据库读取文件
string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; using ...
- CEGUI环境配置
由于最近项目需要CEGUI做界面,于是我去下载了CEGUI源码,但是却不知道怎么搭建.网上的教程都不是很详细,不能让我们这种新手理解,经过几天折腾,终于弄好了,特此记录下来. 1.下载官网CEGUI源 ...