zoukankan      html  css  js  c++  java
  • Stock 贪心经典 Zoj2921

    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 }
  • 相关阅读:
    1039 Course List for Student (25 分)
    1054 The Dominant Color (20 分)
    1065 A+B and C (64bit) (20 分)
    1046 Shortest Distance (20 分)
    1124 Raffle for Weibo Followers (20 分)
    第四章——scrapy爬虫
    scrapy命令行与shell
    表的插入,更新,删除,索引
    JQuery前端开发实战笔记
    今日回顾数据库
  • 原文地址:https://www.cnblogs.com/wsaaaaa/p/4329197.html
Copyright © 2011-2022 走看看