Stock 贪心经典 Zoj2921
2015-03-11 10:39
253 查看
Optiver sponsored problem.
After years of hard work Optiver has developed a mathematical model that allows them to predict wether or not a company will be succesful. This obviously gives them a great advantage on the stock market.
In the past, Optiver made a deal with a big company, which forces them to buy shares of the company according to a fixed schedule. Unfortunately, Optiver's model has determined that the company will go bankrupt after exactly n days, after which their shares will become worthless.
Still, Optiver holds a large number of sell options that allows them to sell some of the shares before the company goes bankrupt. However, there is a limit on the number of shares Optiver can sell every day, and price Optiver receives for a share may vary from day to day. Therefore, it is not immediately clear when Optiver should sell their shares to maximize their profit, so they asked you to write a program to calculcate this.
Input
On the first line an integer t (1 <= t <= 100): the number of test cases. Then for each test case:
One line with an integer n (1 <= n <= 100 000): the number of days before the company goes bankrupt.
n lines with three integers xi (0 <= xi <= 100), pi (0 <= pi <= 100) and mi (0 <= mi <= 10 000 000): the number of shares Optiver receives on day i, the (selling) price per share on day i, and the maximum number of shares Optiver can sell on day i, respectively.
Output
For each test case:
One line with the maximum profit Optiver can achieve.
Sample Input
1
6
4 4 2
2 9 3
2 6 3
2 5 9
2 2 2
2 3 3
Sample Output
76
题意 : 给出第 i 天的股票数x 每张股票价格p 每天最大股票抛出量 要求在n天内获得最大利益。
分析:从最后一天向前贪心 最后一天的股票只能在最后一天售出 第i天的股票可以在第i天及以后售出,那么开一个优先队列,价格最大的优先售出,保证最大利益;
After years of hard work Optiver has developed a mathematical model that allows them to predict wether or not a company will be succesful. This obviously gives them a great advantage on the stock market.
In the past, Optiver made a deal with a big company, which forces them to buy shares of the company according to a fixed schedule. Unfortunately, Optiver's model has determined that the company will go bankrupt after exactly n days, after which their shares will become worthless.
Still, Optiver holds a large number of sell options that allows them to sell some of the shares before the company goes bankrupt. However, there is a limit on the number of shares Optiver can sell every day, and price Optiver receives for a share may vary from day to day. Therefore, it is not immediately clear when Optiver should sell their shares to maximize their profit, so they asked you to write a program to calculcate this.
Input
On the first line an integer t (1 <= t <= 100): the number of test cases. Then for each test case:
One line with an integer n (1 <= n <= 100 000): the number of days before the company goes bankrupt.
n lines with three integers xi (0 <= xi <= 100), pi (0 <= pi <= 100) and mi (0 <= mi <= 10 000 000): the number of shares Optiver receives on day i, the (selling) price per share on day i, and the maximum number of shares Optiver can sell on day i, respectively.
Output
For each test case:
One line with the maximum profit Optiver can achieve.
Sample Input
1
6
4 4 2
2 9 3
2 6 3
2 5 9
2 2 2
2 3 3
Sample Output
76
题意 : 给出第 i 天的股票数x 每张股票价格p 每天最大股票抛出量 要求在n天内获得最大利益。
分析:从最后一天向前贪心 最后一天的股票只能在最后一天售出 第i天的股票可以在第i天及以后售出,那么开一个优先队列,价格最大的优先售出,保证最大利益;
#include<cstring> #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<sstream> #define AA struct ss #define INF 0x3f3f3f3f #include<queue> using namespace std; AA{ int x,p,m; friend bool operator < (AA a,AA b) { return a.p<b.p; } }s[100005]; int main() { int T; cin>>T; while(T--) { int n; cin>>n; priority_queue<AA>que; long long sum=0,xum=0; for(int i=0;i<n;i++) { cin>>s[i].x>>s[i].p>>s[i].m; xum+=s[i].x*s[i].p; } for(int i=n-1;i>=0;i--) { que.push(s[i]); while(que.size()&&s[i].x) { AA k=que.top(); //取出队顶元素 que.pop();//这里一定要弹出队顶元素 if(k.m>s[i].x) { sum+=s[i].x*k.p; k.m-=s[i].x; s[i].x=0; } else if(k.m<=s[i].x) { sum+=k.m*k.p; s[i].x-=k.m; k.m=0; } if(k.m!=0) que.push(k);//如果该天仍可继续抛出股票 再入队 } } cout<<sum<<endl; } }
相关文章推荐
- Stock 贪心经典 Zoj2921
- Stock (zoj 2921 贪心经典)
- 【贪心】zoj2921 stock
- LeetCode 122 Best Time to Buy and Sell Stock II (贪心)
- UVA 1615 Highway 区间选点 经典贪心 (SEERC 2005)
- POJ 1328 Radar Installation(经典贪心)
- HDOJ2037 今年暑假不AC (经典的贪心问题)
- Leetcode 贪心 Best Time to Buy and Sell StockII
- poj3903--Stock Exchange--nlogn贪心求最长上升子序列
- bzoj4596&&JZOJ5271&&5043保持平衡 贪心+堆(经典思想)
- LeetCode--Best Time to Buy and Sell Stock (贪心策略 or 动态规划)
- HD1009 经典贪心
- HDU:1789 Doing Homework again(经典贪心)
- 经典的贪心算法 Journey with Pigs (POJ 3544)
- NYOJ 14 会场安排问题(经典题目,贪心,区间问题)
- UVa10382 - Watering Grass(贪心经典模型:区间覆盖)
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- BZOJ 3790 浅谈MANACHER算法+求区间最小覆盖集经典贪心
- 经典贪心乘船问题
- POJ1700 【经典过河问题,贪心】