Code Forces 18D Seller Bob(简单DP)
2016-04-04 16:52
267 查看
D. Seller Bob
time limit per test
2 seconds
memory limit per test
128 megabytes
input
standard input
output
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.
Input
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
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.
Examples
input
output
input
output
0
思路:动态规划,用一个数组表示二进制数,来表示可以赚的钱,这里可以用stl里面的bitset,最后一次性输出,当然也可以用高精度
time limit per test
2 seconds
memory limit per test
128 megabytes
input
standard input
output
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.
Input
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
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.
Examples
input
7 win 10 win 5 win 3 sell 5 sell 3 win 10 sell 10
output
1056
input
3 win 5 sell 6 sell 4
output
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 .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 [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; }
相关文章推荐
- hdoj 1437 天气情况
- 泛型的下限
- 泛型通配符
- Code Forces 18D Seller Bob(简单DP)
- 用php模拟做服务端侦听端口
- SpringMVC学习记录(二)--controller和view的联系
- Memcached Session Manager(MSM)
- POJ题目分类
- 阅读笔记06
- 代码大全_第二部分: 创建高质量的代码
- zynq的uboot编译问题汇总
- jar11
- 八皇后问题-回溯算法
- javabean, ejb, entitybean的区别,以及POJO的概念
- linux下查看主板内存槽与内存信息
- 泛型的上限的指定
- 泛型接口的实现方式一:在子类的定义上声明泛型类型
- 笔试题15. LeetCode OJ (2)
- 关于PHP
- CopyOnWriteArrayList与Collections.synchronizedList的性能对比