Code Forces 18D Seller Bob(简单DP)
2 seconds
128 megabytes
standard input
standard output
Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place:
- A customer came to Bob and asked to sell him a 2x MB
memory stick. If Bob had such a stick, he sold it and got 2x berllars. - Bob won some programming competition and got a 2x MB
memory stick as a prize. Bob could choose whether to present this memory stick to one of his friends, or keep it.
Bob never kept more than one memory stick, as he feared to mix up their capacities, and deceive a customer unintentionally. It is also known that for each memory stick capacity there was at most one customer, who wanted to buy that memory stick. Now, knowing
all the customers' demands and all the prizes won at programming competitions during the last n days, Bob wants to know, how much
money he could have earned, if he had acted optimally.
The first input line contains number n (1 ≤ n ≤ 5000)
— amount of Bob's working days. The following n lines contain the description of the days. Line sell
x stands for a day when a customer came to Bob to buy a 2x MB
memory stick (0 ≤ x ≤ 2000). It's guaranteed that for each x there
is not more than one line sell x. Line win x stands for
a day when Bob won a 2x MB
memory stick (0 ≤ x ≤ 2000).
Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time.
7
win 10
win 5
win 3
sell 5
sell 3
win 10
sell 10
1056
3
win 5
sell 6
sell 4
0
思路:动态规划,用一个数组表示二进制数,来表示可以赚的钱,这里可以用stl里面的bitset,最后一次性输出,当然也可以用高精度
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <bitset> using namespace std;
int n;
int pre[2005];
int a[5005];
int ans[5005];
vector<int> s;
bitset <2005> dp[5005],y;
string b;
int main()
{
scanf("%d",&n);
memset(pre,0,sizeof(pre));
s.clear(); dp[0]=0;
for(int i=1;i<=n;i++)
{
cin>>b>>a[i];
if(b[0]=='w')
{pre[a[i]]=i;dp[i]=dp[i-1];}
else
{
if(!pre[a[i]]) {dp[i]=dp[i-1];continue;}
y=dp[pre[a[i]]];
y[a[i]]=1;
for(int j=2000;j>=0;j--)
{
if(dp[i-1][j]>y[j]){dp[i]=dp[i-1];break;}
if(dp[i-1][j]<y[j]){dp[i]=y;break;}
if(j==0){dp[i]=y;}
}
}
}
s.push_back(0);
for(int i=dp[n].size()-1;i>=0;i--)
{
int k=0;
for(int j=0;j<s.size();j++)
{
int now=s[j];
s[j]=(now*2+k)%10;
k=(now*2+k)/10;
}
if(k)
s.push_back(k);
if(dp[n][i])
{
int k=0;s[0]++;
for(int j=0;j<s.size();j++)
{
int now=s[j];
s[j]=(now+k)%10;
k=(now+k)/10;
}
if(k)
s.push_back(k);
}
}
for(int i=s.size()-1;i>=0;i--)
printf("%d",s[i]);
cout<<endl;
return 0; }
Code Forces 18D Seller Bob(简单DP)的更多相关文章
- Codeforces Round #302 (Div. 2) C. Writing Code 简单dp
C. Writing Code Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544/prob ...
- HDU 5375 Gray code (简单dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375 题面: Gray code Time Limit: 2000/1000 MS (Java/Oth ...
- 『简单dp测试题解』
这一次组织了一场\(dp\)的专项考试,出了好几道经典的简单\(dp\)套路题,特开一篇博客写一下题解. Tower(双向dp) Description 信大家都写过数字三角形问题,题目很简单求最大化 ...
- Chapter3数学与简单DP
Chapter 3 数学与简单DP 上取整: a / b //下取整 (a + b - 1) / b //上取整 +++ 数学 1.买不到的数目 1205 //如果不知道公式,可以暴搜打表找规律(★) ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- codeforces Gym 100500H A. Potion of Immortality 简单DP
Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...
- 简单dp --- HDU1248寒冰王座
题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...
- poj2385 简单DP
J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
随机推荐
- (转)x264源码分析(1):main、parse、encode、x264_encoder_open函数代码分析
转自:http://nkwavelet.blog.163.com/blog/static/2277560382013103010312144/ x264版本: x264-snapshot-2014 ...
- mysql 中查看指定表的字段名 (可根据字段变量生成c#后台代码)
select DISTINCT data_type from COLUMNS where table_name='表名' 用ConCat();构造生成代码.....
- ChemDraw常用到的几种技巧
ChemDraw对于化学学习的重要性相当于CAD和建筑学.PS和设计,所以如果你是一名生化相关的工作人员,拥有ChemDraw并掌握ChemDraw的使用方法十分必要!这是一款对于我们研究化学的人来说 ...
- SVN目录权限设置
---恢复内容开始--- 如图,这里我建的项目库为myRepositories,其下边又有许多文件,现在要分别对每个文件进行svn权限配置. 配置 进入上面生成的文件夹conf下,进行配置.有以下几个 ...
- css文字超出自动显示省略号
只针对单行文本有效: 01.针对块状元素 ul li{ width: 180px; text-overflow: ellipsis; white-space: nowrap;/*禁止自动换行*/ ov ...
- 帝国cms目录结构
/ 系统根目录├d/ 附件和数据存放目录 (data) │├file/ 附件存放目录 │├js/ JS调用生成目录 │└txt/ 内容存文本存放目录 ├ ...
- 把握linux内核设计思想(七):内核定时器和定时运行
[版权声明:尊重原创,转载请保留出处:blog.csdn.net/shallnet,文章仅供学习交流,请勿用于商业用途] 前面章节说到了把工作推后到除如今以外的时间运行的机制是下半部机 ...
- C语言中do...while(0)的妙用-避免goto
使用goto的优雅并避免结构的混乱 将要跳转到的语句用do{-}while(0) 包起来就可以. reference #defien N 10 bool Execute() { // 分配资源 int ...
- 工作流JBPM_day01:3-使用JBPM的API添加与执行流程
工作流JBPM_day01:3-使用JBPM的API添加与执行流程 流程定义画完得到压缩文件--->部署流程定义-->启动流程实例-->查询我的个人任务列表-->办理任务--& ...
- EF--CodeFirst
1,增加EntityFramework的引用 2,创建实体类 public class Invoice { public Invoice() { LineItems = new List<Lin ...