Codeforces 18D Seller Bob (贪心)
来源:互联网 发布:java研发工程师南昌 编辑:程序博客网 时间:2024/06/05 22:42
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.
7win 10win 5win 3sell 5sell 3win 10sell 10
1056
3win 5sell 6sell 4
0
在已经知道什么时候会有人买什么类型的硬盘的前提下,问你能获得的最大收益是多少
思路:从大到小枚举x, 然后遇到一个sell x, 就往前找第一个win这个x的, 然后这一段全都做上标记。。
import java.math.BigInteger;import java.util.Scanner;import java.util.*;public class Main {public static void main(String[] args) {boolean[] flag = new boolean[5005];boolean[] book = new boolean[5005];BigInteger ans = BigInteger.valueOf(0);BigInteger ER = BigInteger.valueOf(2);Scanner sc = new Scanner(System.in);String s = new String();int n = sc.nextInt();int[] x = new int[5005];for(int i = 0; i < 5005; i++)x[i] = -1;for(int i = 0; i < n; i++){s = sc.next();x[i] = sc.nextInt();if(s.equals("win")) flag[i] = true;}for(int i = 2000; i >= 0; i--){for(int j = 0; j < n; j++){if(flag[j] == false && x[j] == i && book[j] == false){int k; for(k = j-1; k >= 0; k--){if(book[k] == true || (flag[k] == true && x[k] == i)) break;}if(k == -1 || book[k] == true) continue;for(int l = k; l <= j; l++) book[l] = true;ans = ans.add(ER.pow(i));}}}System.out.println(ans); }}
- Codeforces 18D Seller Bob (贪心)
- Codeforces 18D Seller Bob
- CodeForces 18D Seller Bob
- Codeforces 18D Seller Bob java大数+贪心
- CF-18D - Seller Bob(贪心+简单大数)
- Codeforces Beta Round #18 (Div. 2 Only) D - Seller Bob
- CF 18D Seller Bob
- Codeforces 18D Seller Bob && 18E Flag 2 简单dp
- Code Forces 18D Seller Bob(简单DP)
- cf18D Seller Bob (线性DP_好题)
- codeforces 222D Olympiad(贪心)
- CodeForces 128D Numbers(贪心?)
- Codeforces 222D Olympiad(贪心)
- CodeForces 732 D.Exams(二分+贪心)
- CodeForces 35 D.Animals(贪心)
- CodeForces 893D Credit Card (贪心)
- CodeForces 545D贪心
- codeforces 276D 贪心
- 547. Friend Circles(并查集)
- 【51Nod1353】树
- redis编译报致命错误:jemalloc/jemalloc.h:没有那个文件或目录
- 挑战程序竞赛系列(39):4.1模运算的世界(2)
- 博弈论水题列表
- Codeforces 18D Seller Bob (贪心)
- Springboot中使用定时器
- 常用表单元素
- 基于RNN的中文古诗词生成神经网络实现
- Linux内核高性能优化【生产环境实例】
- 1730数字三角形问题(三角形内路径最大)
- OC : NSError (错误)
- Windows OpenVPN客户端配置
- glibc升级