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 ...
随机推荐
- C++ 程序可以定义为对象的集合
C++ 基本语法C++ 程序可以定义为对象的集合,这些对象通过调用彼此的方法进行交互.现在让我们简要地看一下什么是类.对象,方法.即时变量. 对象 - 对象具有状态和行为.例如:一只狗的状态 - 颜色 ...
- (转)I 帧和 IDR 帧的区别
I 帧和 IDR 帧的区别:http://blog.csdn.net/skygray/article/details/6223358 IDR 帧属于 I 帧.解码器收到 IDR frame 时,将所 ...
- mysql -- 循环插入数据到表中
备忘: 1.经搜索发现,MySql不支持直接写SQL语句实现循环插入功能. 想要实现该功能,可以用其他语言操控MySql来实现,或者用存储过程来实现(Store Procedure--SP). 2 ...
- apache -- xampp配置虚拟主机
<VirtualHost *:80> ServerName www.myblog.com DocumentRoot "F:/Code/myblog"<Direct ...
- 查看CentOS的网络带宽出口
检查维护系统的时候,经常会要查看服务器的网络端口是多大的,所以需要用到Linux的一个命令. 如何查看CentOS的网络带宽出口多大?可以用下面的命令来查看. # ethtool eth0 前面是命令 ...
- jQuery-处理class属性
1.addClass方法 为每个匹配的元素添加指定的样式类名 参数类型说明: 1)class名称(字符串) 每个匹配元素添加的一个或多个用空格隔开的样式名 2)function(index, curr ...
- Sprite和UI Image的区别
Unity3D最初是一个3D游戏引擎,而从4.3开始,系统加入了Sprite组件,Unity也终于有了2D游戏开发的官方解决方案.4.6更是增加了新的UI系统uGUI,使得使用Unity开发2D游戏效 ...
- MathType出现乱码公式怎么恢复
在我们平时使用word上的数学公式编辑器的时候,有时一些公式会出现乱码的问题.这个时候可以改为使用MathType时,那么MathType出现乱码公式怎么恢复呢?如果只是少量公式可以手动重新输入,如果 ...
- sftp,get命令使用*通配符的方式获取批量的文件
需求描述: 今天在使用sftp进行get文件的时候,有很多文件名类似的文件,以为还是需要一个一个get 后来发现get也可以使用通配符的方式进行匹配获取多个文件,在此记录下 操作过程: 1.通过sft ...
- tiny6410移植opencv
1.错误1, 解决办法:取消一下两个选项: 2.错误2, 解决办法: 原因是找不到pthread链接库,打开文件夹下的CmakeCache.txt进行修改 3.错误3, 解决办法: