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 ...
随机推荐
- 初识EseNt
转自:http://www.cnblogs.com/goosao/archive/2011/09/23/2186412.html 一.什么是EseNtEseNt(Extensible Storage ...
- swift学习笔记之—自定义函数的规则说明
原文出自:www.hangge.com 转载请保留原文链接:http://www.hangge.com/blog/cache/detail_517.html 1,无返回值的函数 func test( ...
- 【RF库测试】Variable Should not Exist & variable should exist
Variable Should not Exist variable should exist
- python远程登录服务器(paramiko模块安装和使用)
转自:http://www.jb51.net/article/46285.htm 一:简介 由paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器 ...
- 改变PS1的颜色
我们能够通过配置PS1变量使提示符成为彩色.在PS1中配置字符序列颜色的格式为: \[\e[F;Bm\] 基本上是夹在 "\e["(转义开方括号)和 &qu ...
- AssetBundle 在Android机子上进行读取 .
http://game.ceeger.com/Manual/StreamingAssets.html 我看到官方文档中说明:Note that bundles are not fully compat ...
- 百度地图sdk问题 error inflating class com.baidu.mapapi.map.mapview
最近在封装开发中,有机会遇到问题还是记录下吧 但是其实都是一个原因 就是 初始化 在MyAplication onCreate()中加入 SDKInitializer.initialize(get ...
- cocos2d-x游戏引擎核心之一——坐标系
cocos2d-x:OpenGL坐标系.绝对坐标系.相对坐标系.屏幕坐标系 cocos2d-x采用的是笛卡尔平面坐标系,也就是平面上两条垂直线构成的坐标系,平面上任意一点都可以用(x,y)来表示. ( ...
- java基础---->FilenameFilter之文件过滤
FilenameFilter用于对列表中文件名的过滤,今天我们就开始java中FilenameFilter的学习.好多年了,你一直在我的伤口中幽居,我放下过天地,却从未放下过你,我生命中的千山万水,任 ...
- MQTT的学习研究(五) MQTT moquette 的 Blocking API 发布消息服务端使用
参看官方文档: http://publib.boulder.ibm.com/infocenter/wmqv7/v7r0/index.jsp?topic=/com.ibm.mq.amqtat.doc/t ...