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:
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.
Sample Input
7 win 10 win 5 win 3 sell 5 sell 3 win 10 sell 10
1056
3 win 5 sell 6 sell 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"; } }