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 ...
随机推荐
- Java 关于finally、static
论坛上看到的两道题目,如下: //为啥运行结果是1 0 不是 0 0呢 谁能解释下啊 public class FinallyDemo { static int value = 0; static i ...
- hdu2147 kiki's game(博弈)
这个是纳什博弈?不知道怎么看的 依据PN图,从左下角開始推 左下角P 最后一行都是PNPNPN 第一列都是 P N P N P 完了填完即可了 #include<cstdio> int m ...
- find_circ 识别circRNA 的原理
find_circ 通过识别junction reads 来预测circRNA 和参考基因组比对完之后,首先剔除和基因组完全比对的reads,保留没比对上的reads, 这部分reads 直接比是比对 ...
- js 补零方法,如果不足位数
var pad = function() { var tbl = []; return function(num, n) { var len = n-num.toString().length; if ...
- 【Java面试题】52 java中会存在内存泄漏吗,请简单描述。
所谓内存泄露就是指一个不再被程序使用的对象或变量一直被占据在内存中.Java中有垃圾回收机制,它可以保证一对象不再被引用的时候,即对象编程了孤儿的时候,对象将自动被垃圾回收器从内存中清除掉.由于Jav ...
- 查看 SharePoint 2013 部署到GAC的自定义dll
在SharePoint 2007和2010中,自定义dll存放在“C:\Windows\assembly\”文件夹中,在Windows资源管理器中可以看到. 但在Sharepoint 2013中,却无 ...
- js焦点轮播图
汇集网上焦点轮播图的实现方式,自己试了下,不过鼠标悬浮停止动画和鼠标离开动画播放好像没生效,不太明白,最后两行代码中,为什么可以直接写stop和play.不用加括号调用函数么?求懂的大神指点! 所用知 ...
- day17<集合框架+>
集合框架(HashSet存储字符串并遍历) 集合框架(HashSet存储自定义对象保证元素唯一性) 集合框架(HashSet存储自定义对象保证元素唯一性图解及代码优化) 集合框架(HashSet如何保 ...
- Emulator Error: Could not load OpenGLES emulation library: Could not load DLL!
Copy the file below from SDK\tools\lib to SDK\tools. libEGL_translator.dlllibGLES_CM_translator.dlll ...
- 《转载》POI导出excel日期格式
参考帖子: [1]http://www.ithao123.cn/content-2028409.html [2]http://javacrazyer.iteye.com/blog/894850 再读本 ...