码迷,mamicode.com
首页 > 其他好文 > 详细

Stock 贪心经典 Zoj2921

时间:2015-03-11 12:34:44      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

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天及以后售出,那么开一个优先队列,价格最大的优先售出,保证最大利益;

 

 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 0x3f3f3f3f
10 #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

标签:

原文地址:http://www.cnblogs.com/wsaaaaa/p/4329197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!