地址:http://codeforces.com/problemset/problem/744/C

题目:

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, the i-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.

思路:dp好题!(恩,也就意味着我不会)
  看到16很容想到枚举买卡片的状态,然后就是状态间的相互转移了,
  dp[i][j]的意义很重要,dp[i][j]表示在卡片状态i下,省下j张红色代币时最多省下蓝色代币的数量。
  状态转移方程具体见代码吧,最后答案枚举dp[(1<<n)-1][j]即可。
  还有dp数组必须初始化为负无穷
 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int n,sz,sumr,sumb,r[],b[],dp[<<][],srr[],sbb[];
char col[];
int main(void)
{
cin>>n;
sz=<<n;
for(int i=;i<n;i++)
scanf("%s%d%d",col+i,r+i,b+i),sumr+=r[i],sumb+=b[i];
memset(dp,0xef,sizeof(dp));
dp[][]=;
for(int i=;i<sz;i++)
{
int sr,sb;
sr=sb=;
for(int j=;j<n;j++)
if(i&(<<j))
col[j]=='R'?sr++:sb++;
for(int j=;j<n;j++)
if(!(i&(<<j)))
srr[j]=min(sr,r[j]),sbb[j]=min(sb,b[j]);
for(int j=;j<n;j++)
if(!(i&(<<j)))
for(int k=;k<=;k++)
dp[i|(<<j)][k+srr[j]]=max(dp[i|(<<j)][k+srr[j]],dp[i][k]+sbb[j]);
}
int ans=2e9;
for(int i=;i<=;i++)
ans=min(max(sumr-i,sumb-dp[sz-][i]),ans);
printf("%d\n",ans+n);
return ;
}

Codeforces Round #385 (Div. 1) C. Hongcow Buys a Deck of Cards的更多相关文章

  1. Codeforces Round #385 (Div. 2) B - Hongcow Solves A Puzzle 暴力

    B - Hongcow Solves A Puzzle 题目连接: http://codeforces.com/contest/745/problem/B Description Hongcow li ...

  2. Codeforces Round #385 (Div. 2) A. Hongcow Learns the Cyclic Shift 水题

    A. Hongcow Learns the Cyclic Shift 题目连接: http://codeforces.com/contest/745/problem/A Description Hon ...

  3. Codeforces Round #385 (Div. 2) C - Hongcow Builds A Nation

    题目链接:http://codeforces.com/contest/745/problem/C 题意:给出n个点m条边,还有k个不能连通的点,问最多能添加几条边. 要知道如果有n个点最多的边是n*( ...

  4. codeforces 744C Hongcow Buys a Deck of Cards

    C. Hongcow Buys a Deck of Cards time limit per test 2 seconds memory limit per test 256 megabytes in ...

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

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

  6. Codeforces Round #385 (Div. 2) Hongcow Builds A Nation —— 图论计数

    题目链接:http://codeforces.com/contest/745/problem/C C. Hongcow Builds A Nation time limit per test 2 se ...

  7. Codeforces Round #385(div 2)

    A =w= B QwQ C 题意:n个点m条边的无向图,其中有k个特殊点,你在这张图上尽可能多的连边,要求k个特殊点两两不连通,问最多能连多少边 分析:并查集 对原图做一次并查集,找出特殊点所在集合中 ...

  8. Codeforces Round #385 (Div. 2) A,B,C 暴力,模拟,并查集

    A. Hongcow Learns the Cyclic Shift time limit per test 2 seconds memory limit per test 256 megabytes ...

  9. Codeforces Round #385 (Div. 2)A B C 模拟 水 并查集

    A. Hongcow Learns the Cyclic Shift time limit per test 2 seconds memory limit per test 256 megabytes ...

随机推荐

  1. js移除某一类的div

    (转载)Javascript removeChild()不能删除全部子节点的解决办法   在Javascript中,只提供了一种删除节点的方法:removeChild().removeChild() ...

  2. 第一百三十八节,JavaScript,封装库--插件

    JavaScript,封装库--插件 库主要是用来封装一般JavaScript的常规操作代码,而拖拽这种特效代码属于功能性代码,并不是必须的,所以这种类型的代码,我们建议另外封装,在需要的时候作为插件 ...

  3. java 包冲突解决方法

    1.诊断包冲突 java.lang.NoSuchMethodError: org.apache.commons.io.output.DeferredFileOutputStream.<init& ...

  4. JZOJ.5231【NOIP2017模拟8.5】序列问题

    Description   Input 输入文件名为seq.in.首先输入n.接下来输入n个数,描述序列 A. Output 输出文件名为seq.out.输出一行一个整数代表答案.   Sample ...

  5. Entity Frameword 查询 sql func linq 对比

    Entity Framework是个好东西,虽然没有Hibernate功能强大,但使用更简便.今天整理一下常见SQL如何用EF来表达,Func形式和Linq形式都会列出来(本人更多在用Func形式,l ...

  6. 【BZOJ4624】农场种植 FFT

    [BZOJ4624]农场种植 Description 农夫约翰想要在一片巨大的土地上建造一个新的农场. 这块土地被抽象为个 R*C 的矩阵.土地中的每个方格都可以用来生产一种食物:谷物(G)或者是牲畜 ...

  7. Oracle给大数值添加逗号的分位符形如:9,999,999,999

    SELECT TO_CHAR(1231231123, '9,999,999,999') FROM dual; 1,231,231,123 SELECT TO_CHAR(1231231123, '9,9 ...

  8. AVCaptureInput和AVCaptureOutput子类

    1.AVCaptureInput AVCaptureDeviceInput:用于从AVCaptureDevice对象捕获数据. AVCaptureScreenInput:从macOS屏幕上录制的一种捕 ...

  9. Spring4学习笔记-AOP(基于配置文件的方式)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://shamrock.blog.51cto.com/2079212/1557743 引 ...

  10. mongoose连接数据库的两种形式

    不废话,直接 ---------------------- .如果你的应用程序只使用一个数据库, 应该使用 mongoose.connect. 如果您需要创建额外的连接,使用 mongoose.cre ...