地址: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. sed awk文本处理教程

    sed全名叫stream editor,流编辑器,用程序的方式来编辑文本,相当的hacker啊.sed基本上就是玩正则模式匹配,所以,玩sed的人,正则表达式一般都比较强. 把my字符串替换成Hao ...

  2. 第二百一十九节,jQuery EasyUI,DateTimeBox(日期时间输入框)组件

    jQuery EasyUI,DateTimeBox(日期时间输入框)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解 EasyUI 中 DateTimeBox(日期时间输入框 ...

  3. null!= xxx 和 xxx!=null有什么区别?

    从意义上将没有区别,从编程规范上讲,第一种写法是为了防止写成:null = xxx

  4. @Override must override a superclass method 有关问题解决

    1.Java开发环境时 如果在使用Eclipse开发Java项目时,在使用 @Override 出现以下错误: The method *** of type *** must override a s ...

  5. 【Charles】使用教程+破解+Windows版本https乱码+https证书安装注意

    一.使用教程参考: 这一篇就够了,其他都是大同小异.Windows版和MAC版使用没太多区别. Charles 从入门到精通 | 唐巧的博客 https://blog.devtang.com/2015 ...

  6. HDU 1796 How many integers can you find(容斥原理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  7. 巨蟒python全栈开发数据库前端6:事件onclick的两种绑定方式&&onblur和onfocus事件&&window.onload解释&&小米商城讲解

    1.回顾上节内容(JavaScript) 一.JavaScript概述 1.ECMAScript和JavaScript的关系 2.ECMAScript的历史 3.JavaScript是一门前后端都可以 ...

  8. WebBrowser 控件-说明

    WebBrowser.Document 为活动的文档返回自动化对象,引用 Microsoft HTML Object Library 可查看详细属性和方法 下面的解说假设窗体中有一个名称为 Web1 ...

  9. AsciiDoc Text based document generation

    AsciiDoc Text based document generation    AsciiDoc Home Page http://asciidoc.org/   AsciiDoc is a t ...

  10. 数据库字符集(AL32UTF8)和客户端字符集(2%)是不同的

    登录oracle数据库时我们会遇到这样的提示信息:“数据库字符集(AL32UTF8)和客户端字符集(2%)是不同的”. 这是由于数据库服务端和客户端的字符集不一致所造成的,服务端字符集和客户端字符集相 ...