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 ...
随机推荐
- EF 延迟加载和预先加载
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 本节探讨延迟加载和预先加载 Entity Frame ...
- 手机app开发:浅谈APP登录方式的优劣
手机app开发公司亿合科技要是给你一个机会设计一款APP,你会用什么方式做这个APP的登录模块?根据APP的业务模型的不同会有不同的设计方法.如果是偏内容型的APP,需要优先展示内容给用户,当用户需要 ...
- C#操作Mongodb的心得
Mongodb是一个强大的文档型数据库,采用BSON的数据格式.本文主要采用其官方的C#驱动来操作其表中的集合.驱动版本为1.1.0,下载地址为: http://mongodb.github.io/m ...
- LL谱面分析和难度标定
LL谱面分析和难度标定 先介绍一下LL谱面的存储方式:TimeLine序列(简称TL序列),TL序列中的每一个元素(即音符)可以由一个C语言中的结构体来表示: struct note{ int lin ...
- z-index
1.定位对象能够遮盖非定位对象,除非定位对象的z-index值为负数. 2.同层级定位对象z-index大的能够覆盖z-index值小的. 2.1 同层级的子集相互比较要先看父集(即同层级)的大小; ...
- jQuery ajax的traditional参数的作用///////////////////////////////////zzzzzzzzzzz
jQuery ajax的traditional参数的作用 i33 发布时间: 2013/04/03 10:05 阅读: 9859 收藏: 9 点赞: 1 评论: 0 一般的,可能有些人在一个参数有多个 ...
- zepto和jquery的区别,zepto的不同使用8条小结
说到诡异事件发生的原因,自然是想到两者之间的差异性. 首先是效果: jquery中有fadeIn和fadeOut两个效果,用来实现渐隐渐显的效果,这个在PC端自然是常用的效果.然后我们前端组的组员Mr ...
- Java被忽略的基本知识(四)
Java IO(不是一般的重要) 54.IO中的流:字节流(InputStream.OutputStream).字符流(Reader.Writer).转换流(InputStreamReader.Out ...
- js 获取根目录 获取参数
function getRootPath() { //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp var curWwwPath ...
- 书籍推荐《以C语言解析电脑》
这本书要想买到,在大陆看起来比较难,理出个目录,看个大概: 另外在这个地方可以预览前20页:http://openebook.hyread.com.tw/ebookservice/hyviewer/o ...