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 ...
随机推荐
- docker 服务升级
使用docker大家一般都是微服务部署了.个人的经验是乖乖的用docker自己的注册发现机制. 创建一个overlay类型的network.把所有的微服务加入进去.就可以把service name当作 ...
- (转)HTTP 长连接和短连接
1. HTTP协议与TCP/IP协议的关系 HTTP的长连接和短连接本质上是TCP长连接和短连接.HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议.IP协议主要解决网络路由和寻址问 ...
- 理工科应该的知道的C/C++数学计算库(转)
理工科应该的知道的C/C++数学计算库(转) 作为理工科学生,想必有限元分析.数值计算.三维建模.信号处理.性能分析.仿真分析...这些或多或少与我们常用的软件息息相关,假如有一天你只需要这些大型软件 ...
- js隐式转换
JavaScript的数据类型分为六种,分别为null,undefined,boolean,string,number,object.object是引用类型,其它的五种是基本类型或者是原始类型.我们可 ...
- 论文阅读(Xiang Bai——【TIP2014】A Unified Framework for Multi-Oriented Text Detection and Recognition)
Xiang Bai--[TIP2014]A Unified Framework for Multi-Oriented Text Detection and Recognition 目录 作者和相关链接 ...
- [原] MSSQL MDF文件太大问题
USE 你的库名; GO -- 数据库空间使用情况 EXEC sp_spaceused; -- 查下文件空间使用情况 SELECT file_id, name, [文件大小(MB)] = size / ...
- DELPHI XE5 与SQLITE
最近一次使用DELPHI做项目是使用DELPHI2009,为了访问本地数据库方便,使用ACCESS数据库,不需要安装驱动,(WINDOWS自带),但是ACCESS数据库的性能确实很糟糕,通过ADO连接 ...
- mysql索引的使用和优化
参考: http://blog.csdn.net/xluren/article/details/32746183 http://www.cnblogs.com/hustcat/archive/2009 ...
- 用 正则表达式 限定XML simpleType 定义
<xsd:simpleType name="ResTrictions"> <xsd:restriction base="xsd:string" ...
- openlayer 3 在layer上添加feature
首先获取layer的source,所以你的source里就先要有内容. vectorLayer.getSource().addFeature(iconFeature); 如果source里面没有定义过 ...