CF - 高精度 + 貪心
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
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
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
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.
Example Input7Output
win 10
win 5
win 3
sell 5
sell 3
win 10
sell 10
1056Input
3Output
win 5
sell 6
sell 4
0
題目分析 : 一個人賣內存條,sell 表示有人去買此內存條, win表示獲取到此內存條,這個人最多只能擁有一個內存條,並且同時也保證買不同型號內存條的只會有一個顧客,問此人能獲得的最大收益。
思路分析 : 輸出答案得是高精度,選購物品的時候貪心的去選, 因為 2^5 一定大於 2^1 + 2^2 + 2^3 + 2^4。
代碼示例 :
#define ll long long const int maxn = 1e6+5; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; int n; struct node { int state, x; // 1 是 win, 0 是 sell int pt; // 1 是存在 }pre[5005]; vector<int>f[2005]; int p[2005]; vector<int>ans, mid; void init() { f[0].push_back(1); for(int i = 1; i <= 2000; i++){ f[i].assign(f[i-1].size(), 0); for(int j = 0; j < f[i].size(); j++){ f[i][j] = f[i-1][j] + f[i-1][j]; } for(int j = f[i].size()-1; j >= 0; j--){ if (f[i][j] > 9){ f[i][j] -= 10; if (j != 0) f[i][j-1]++; else f[i].insert(f[i].begin(), 1); } } } } void bigsum(int x){ mid.clear(); mid.assign(ans.size(), 0); for(int i = ans.size()-1, j = f[x].size()-1; i >= 0; ){ if (j >= 0){ mid[i] = ans[i] + f[x][j]; i--, j--; } else {mid[i] = ans[i]; i--;} } for(int i = mid.size()-1; i >= 0; i--){ if (mid[i] > 9){ mid[i] -= 10; if (i != 0) mid[i-1]++; else mid.insert(mid.begin(), 1); } } ans = mid; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); cin >> n; char s[10]; int x; init(); for(int i = 1; i <= n; i++) { scanf("%s%d", s, &x); if (s[0] == ‘w‘) pre[i].state = 1; else { pre[i].state = 0; p[x] = i; } pre[i].x = x; pre[i].pt = 1; } int sign = 1; for(int i = 2000; i >= 0; i--){ if (!p[i]) continue; int pos = 999999; for(int j = p[i]-1; j >= 1; j--){ if (pre[j].pt == 0) break; if (pre[j].state == 1 && pre[j].x == i){ pos = j; if (sign) {ans = f[i]; sign = 0;} else bigsum(i); break; } } for(int j = p[i]; j >= pos; j--){ if (pre[j].state == 0) p[pre[j].x] = 0; else pre[j].pt = 0; } //printf("----- %d %d\n", i, pos); } if (sign) {printf("0\n"); return 0;} for(int i = 0; i < ans.size(); i++){ printf("%d", ans[i]); } printf("\n"); //puts(""); return 0; }
CF - 高精度 + 貪心