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 }
  • 相关阅读:
    R语言实战实现基于用户的简单的推荐系统(数量较少)
    MapReduce计数器
    MapReduce的Shuffle过程介绍
    R语言两种方式求指定日期所在月的天数
    ggplot2作图详解:入门函数qplot
    R语言中的数据处理包dplyr、tidyr笔记
    Metronic_下拉列表Select2插件的使用
    Linux_使用Linux之安装jdk 7
    HttpClient_HttpClient 4.3.6 HTTP状态管理
    HttpClient_HttpClient 对 cookie的处理
  • 原文地址:https://www.cnblogs.com/wsaaaaa/p/4329197.html
Copyright © 2011-2022 走看看