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][],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的更多相关文章

  1. Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)

    Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...

  2. Codeforces 744C. Hongcow Buys a Deck of Cards(状压DP)

    这题的难点在于状态的设计 首先显然是个状压,需要一维表示卡的状态,另一维如果设计成天数,难以知道当前的钱数,没法确定是否能够购买新的卡,如果设计成钱数,会发现状态数过多,空间与时间都无法承受.但是可以 ...

  3. Codeforces 745E Hongcow Buys a Deck of Cards 状压DP / 模拟退火

    题意:现在有n张卡片(n <= 16), 每一轮你可以执行两种操作中的一种.1:获得一张红色令牌和一张蓝色令牌.2:购买一张卡片(如果可以买的话),购买的时候蓝色卡片可以充当蓝色令牌,红色同理, ...

  4. 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 ...

  5. Hongcow Buys a Deck of Cards CodeForces - 744C (状压)

    大意: n个红黑卡, 每天可以选择领取一块红币一块黑币, 或者买一张卡, 第$i$张卡的花费红币数$max(r_i-A,0)$, 花费黑币数$max(b_i-B,0)$, A为当前红卡数, B为当前黑 ...

  6. 「CF744C」Hongcow Buys a Deck of Cards「状压 DP」

    题意 你有\(n\)个物品,物品和硬币有\(A\),\(B\)两种类型,假设你有\(M\)个\(A\)物品和\(N\)个\(B\)物品 每一轮你可以选择获得\(A, B\)硬币各\(1\)个,或者(硬 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. 改造过的JS颜色选择器

    项目中用到颜色选择功能,在网上找了个颜色选择器,自己改了改代码.做成了一个可动态加载的颜色选择器. 把代码贴上,当是记录. /*Copyright(c)2009,www.supersite.me*/ ...

  2. Leetcode: Sentence Screen Fitting

    Given a rows x cols screen and a sentence represented by a list of words, find how many times the gi ...

  3. FTP服务器的安装和配置

    FTP服务器的安装和配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本来是想出一个源码安装ftp的教程,但是想想还是算了,如果你自学python有个10天的话你自己就能写一个f ...

  4. 《量化投资:以MATLAB为工具》连载(2)基础篇-N分钟学会MATLAB(中)

    http://www.matlabsky.com/thread-43937-1-1.html   <量化投资:以MATLAB为工具>连载(3)基础篇-N分钟学会MATLAB(下)     ...

  5. sublime text3 前端编译神器,浏览器实时显示

    前端编译器有很多,Dreamweaver,sublime text ,webstorm,但在使用之后感觉sublime text3就是前端的编译神器 首先sublime text3最好使用英文原版,虽 ...

  6. 图像开发的p2s模式:halcon+opencv的联动

    [<zw版·Halcon与delphi系列原创教程> 图像开发的p2s模式:halcon+opencv的联动 尽管halcon功能强大,基本上cv只是halcon的一个子集,不过cv毕竟是 ...

  7. matlab GUI界面编程总结

    去年做了一些关于Matlab GUI的程序,现在又要做相关的东西,回想起来,当时很多经验没有记录下来,现在回顾起来始终觉得不爽,所以从现在开始,一定要勤写记录. 从简单的例子说起吧. 创建Matlab ...

  8. SSIS变量属性中EvaluateAsExpression设置的作用

    我们在做SqlServer SSIS包开发的时候,经常会用到SSIS的变量,我们可以使用和修改SSIS变量的值使得SSIS包的逻辑更灵活,如下图所示: 在定义SSIS变量的时候可以使用固定值(如上图中 ...

  9. HttpClient——Get,Post

    package com.ch.day5_httpclient; import java.util.List; import com.ch.myutils.NetWorkUtil; import com ...

  10. C#抽象方法和虚拟方法理解

    http://www.cnblogs.com/denylau/archive/2010/04/11/1709573.html http://www.cnblogs.com/naariah/archiv ...