Stock 贪心经典 Zoj2921
来源:互联网 发布:unity3d 录屏插件 编辑:程序博客网 时间:2024/06/05 05:05
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天及以后售出,那么开一个优先队列,价格最大的优先售出,保证最大利益;
1 #include<cstring> 2 #include<iostream> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<sstream> 8 #define AA struct ss 9 #define INF 0x3f3f3f3f10 #include<queue>11 using namespace std;12 AA{13 int x,p,m;14 friend bool operator < (AA a,AA b)15 {16 return a.p<b.p;17 }18 }s[100005];19 20 int main()21 {22 int T;23 cin>>T;24 while(T--)25 {26 int n;27 cin>>n;28 priority_queue<AA>que;29 long long sum=0,xum=0;30 31 for(int i=0;i<n;i++)32 {33 cin>>s[i].x>>s[i].p>>s[i].m;34 xum+=s[i].x*s[i].p;35 }36 37 for(int i=n-1;i>=0;i--)38 {39 que.push(s[i]);40 while(que.size()&&s[i].x)41 {42 AA k=que.top(); //取出队顶元素43 que.pop();//这里一定要弹出队顶元素 44 if(k.m>s[i].x)45 {46 sum+=s[i].x*k.p;47 k.m-=s[i].x;48 s[i].x=0;49 }50 else if(k.m<=s[i].x)51 {52 sum+=k.m*k.p;53 s[i].x-=k.m;54 k.m=0;55 }56 if(k.m!=0) que.push(k);//如果该天仍可继续抛出股票 再入队57 }58 }59 cout<<sum<<endl;60 }61 }
- Stock 贪心经典 Zoj2921
- 【贪心】zoj2921 stock
- Stock (zoj 2921 贪心经典)
- hdu-2385-Stock-贪心
- ZOJ 2921 Stock(贪心)
- zoj 2921 Stock(贪心)
- 经典贪心,哈夫曼编码。
- HD1009 经典贪心
- 经典贪心乘船问题
- poj1700 poj2573 经典贪心
- 贪心算法经典例子
- hdoj 1052经典贪心
- poj 1328 贪心经典
- poj 3040 <经典贪心>
- 贪心算法经典例子
- 2204 母舰[经典贪心]
- HDU2037-经典贪心
- 贪心算法经典例子
- 数据连接池——JNDI
- 烦人的dp
- html中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
- Android 性能优化 (二)数据库优化 秒变大神
- D. Block Tower
- Stock 贪心经典 Zoj2921
- 二分
- Loadrunner编辑脚本一些技巧
- Data Handler 大模拟 + 双端链表 hdu 4268
- Maze Stretching Poj3897 二分+BFS
- 【转】别人家的八数码 A* IDA*解法
- CodeForces 25D Roads not only in Berland
- Cards CodeForces 399C
- CodeForces 518C Anya and Smartphone