您的位置:首页 > 其它

ZOJ 2921 Stock(贪心)

2015-03-10 10:36 197 查看
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

贪心题目:
第一天的股票可以在第1,2..n天卖出,第二天的股票可以在第2,3..n天卖出。
所以我们从后到前依次贪心。将后面的可选结果存入优先队列。依次取出即可。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF=0x3f3f3f3f;
const int maxn=1e5+100;
int x[maxn],p[maxn],m[maxn];
int t,n;
struct node{
    int price;
    int num1,num2;
    bool operator<(const node &a)const
    {
        return price<a.price;
    }
}e[maxn];
priority_queue<node>q;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);node temp;
        REPF(i,1,n)  scanf("%d%d%d",&e[i].num1,&e[i].price,&e[i].num2);
        while(!q.empty()) q.pop();
        int ans=0;
        for(int i=n;i>=1;i--)
        {
            q.push(e[i]);
            while(!q.empty()&&e[i].num1>0)
            {
                temp=q.top();
                q.pop();
                if(temp.num2<=e[i].num1)
                {
                    ans+=temp.price*temp.num2;
                    e[i].num1-=temp.num2;
                }
                else
                {
                    ans+=e[i].num1*temp.price;
                    temp.num2-=e[i].num1;e[i].num1=0;
                    q.push(temp);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: