CF-18D - Seller Bob(贪心+简单大数)
来源:互联网 发布:php用逗号拼接字符串 编辑:程序博客网 时间:2024/05/19 21:42
Crawling in process...Crawling failedTime Limit:2000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u
Description
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 got2x 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 lastn 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 followingn lines contain the description of the days. Linesell x stands for a day when a customer came to Bob to buy a2x MB memory stick (0 ≤ x ≤ 2000). It's guaranteed that for eachx there is not more than one line sell x. Line win x stands for a day when Bob won a2x 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.
Sample Input
7win 10win 5win 3sell 5sell 3win 10sell 10
1056
3win 5sell 6sell 4
0
思路:贪心,因为2^n>2^(n-1)+2^(n-2)...+2;所以这题可以从底向上比较,每次先选最大的数匹配。具体实现方法有多种,我是按照sell从大到小排序,然后从底向上每次都找到最近的匹配点,加上,同时去掉这个区间内的所有数,因为从有经过这个区间的操作都不在合法了。
最后的结果就是这些匹配值的和。
这里涉及大整数加和,不过这个和很简单不解释。
#include<iostream>#include<cstdio>#include<vector>#include<algorithm>#include<cstring>using namespace std;const int mm=5005;const int mod=10;int f[mm][mm];int a[mm],ans[mm];char s[mm];vector<pair<int,int> >b;int main(){ int tt; memset(f,0,sizeof(f)); ///预处理出2的2000次方内的数 f[0][1]=1;f[0][0]=1;tt=0; for(int i=1;i<2002;i++) {for(int j=1;j<=f[i-1][0];j++) { f[i][j]+=f[i-1][j]+f[i-1][j]+tt; tt=f[i][j]/mod; f[i][j]%=mod; ++f[i][0]; } if(tt)f[i][++f[i][0]]=tt; tt=0; } ///for(int i=1;i<110;i++,cout<<"\n") /// for(int j=f[i][0];j>0;j--)cout<<f[i][j]; int cas; while(cin>>cas) { b.clear(); for(int i=0;i<cas;i++) {cin>>s>>a[i]; if(s[0]=='s')b.push_back(make_pair(a[i],i)),a[i]=-1; } sort(b.rbegin(),b.rend());///从大到小排序 int x,y; memset(ans,0,sizeof(ans)); ans[0]=1; for(int i=0;i<b.size();i++) { x=b[i].first;y=b[i].second; for(int j=y-1;j>=0&&a[j]!=-2;j--) if(a[j]==x)///符合条件的最近匹配 { tt=0; int bit;///答案加和 if(ans[0]>f[x][0])bit=ans[0]; else bit=f[x][0]; for(int k=1;k<=bit;k++) {ans[k]+=f[x][k]+tt; tt=ans[k]/mod; ans[k]%=mod; } if(tt)ans[++bit]+=tt; ans[0]=bit; /// if(tt)ans[f[x][0]+1]+=tt; ///if(ans[0]<=f[x][0]){ans[0]=f[x][0];if(tt)++ans[0];} for(int k=j;k<=y;k++)a[k]=-2;///去掉区间 break; } } for(int i=ans[0];i>=1;i--) cout<<ans[i]; cout<<"\n"; }}
- CF-18D - Seller Bob(贪心+简单大数)
- CF 18D Seller Bob
- Codeforces 18D Seller Bob java大数+贪心
- Codeforces 18D Seller Bob (贪心)
- Codeforces 18D Seller Bob
- CodeForces 18D Seller Bob
- Code Forces 18D Seller Bob(简单DP)
- Codeforces 18D Seller Bob && 18E Flag 2 简单dp
- Codeforces Beta Round #18 (Div. 2 Only) D - Seller Bob
- CF 402-D 贪心
- CF 721D贪心
- CF 738D 贪心
- CF 748D 贪心
- CF 190div2 D(贪心)
- CF 515D 图,贪心
- CF 593D LCA,贪心
- CF-45D. Event Dates(贪心)
- CF D. Population Size(贪心+模拟)
- 数据类型相互转换
- 招聘中级测试工程师、开发测试工程师各一名
- lease 分布式
- SEO网络优化方法
- 50个必备的实用jQuery代码段
- CF-18D - Seller Bob(贪心+简单大数)
- Python 初体验之 最基本的if-else-while-for
- 一个字节造成的巨大性能差异——SQL Server存储结构
- 数据绑定之DataBinding细解
- decision_tree (ID3) with python
- 代码风格
- GEF 进阶,第四部分: Locator
- HTML的object对象小结
- Android禁止横屏竖屏切换