Stock (zoj 2921 贪心经典)

Stock


Time Limit: 2 Seconds     
Memory Limit: 65536 KB



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

题意:有n张股票,给出每天股票的买进数量,当天的股票价格和当天最大抛出量,第i天得到的股票当天可以不抛,可以留到以后抛。问这n天最多能卖多少钱?

思路:贪心,从后往前贪心,最后一天的股票当然只能在最后一天卖出,第i天的可以在第i天及以后卖出,那么就可以维护一个优先队列来存放第i天及以后的天数中抛出量不为零的日期(价格高的优先),那抛出第i天时先从优先队列中取出价格最高的。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

struct Node
{
    int p,num;
    friend bool operator <(Node x,Node y)
    {
        return x.p<y.p;
    }
};

int a[maxn],b[maxn],c[maxn];
priority_queue<Node>Q;

int main()
{
    int i,j,t,n,x;
    sf(t);
    while (t--)
    {
        sf(n);
        FRL(i,0,n)
            sfff(a[i],b[i],c[i]);
        while (!Q.empty()) Q.pop();
        Node st;
        int ans=0;
        for (i=n-1;i>=0;i--)
        {
            st.num=c[i];
            st.p=b[i];
            Q.push(st);
            int temp=a[i];
            while (!Q.empty()&&temp)
            {
                st=Q.top();
                Q.pop();
                if (st.num>temp)
                {
                    st.num-=temp;
                    ans+=temp*st.p;
                    temp=0;
                    Q.push(st);
                }
                else
                {
                    temp-=st.num;
                    ans+=st.p*st.num;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-27 05:11:37

Stock (zoj 2921 贪心经典)的相关文章

zoj 3627(贪心)

思路:半夜了思路有点混乱wa了好几发.一开始坑定两个人距离为m才能获得最大的收益,所以我们就可以枚举单个端点,当距离达到m时在一同一个方向走这是我们只需要算一下剩下几秒,左右两边贪心去最大的即可. 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2

zoj 1543 贪心

Stripies Time Limit: 2 Seconds      Memory Limit: 65536 KB Our chemical biologists have invented a new very useful form of life called stripies (in fact, they were first called in Russian - polosatiki, but the scientists had to invent an English name

zoj 2921 Stock(贪心)

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, Optive

ZOJ - 3715贪心

ZOJ - 3715KindergartenElection 题目大意:幼儿园里正在举办班长选举,除1号小朋友外每个人都会投他最好的朋友,但1号小朋友可以贿赂别人(小伙子有丶想法),被贿赂的小朋友就会把票投给1号小朋友而不是他最好的朋友,对于不同的小朋友贿赂的花费也不同,1号小朋友想要自己是唯一的班长(票数最高),问他最少需要花费多少糖果? 由题目来想,很容易想到贪心,但是不知道怎么贪心.如果是简单让1号是票数最高的小朋友,他每次贿赂都有两种选择,一种是贿赂花费小的人来投他,另一种是贿赂票数比他

C - Ordering Pizza CodeForces - 867C 贪心 经典

C - Ordering Pizza CodeForces - 867C C - Ordering Pizza 这个是最难的,一个贪心,很经典,但是我不会,早训结束看了题解才知道怎么贪心的. 这个是先假设每个人都可以吃到他喜欢的,就是先求出答案,然后按照b-a 排序,分别放入两个优先队列里面, 如果b>a 那就吃第二块,否则就吃第一块,求出num,注意优先队列按照从小到达排序. num1记录第一块吃的数量,num2记录第二块吃的数量. num1%=s,num2%=s  如果num1+num2的数

ZOJ 3829 贪心 思维题

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题,自己智商不够,不敢搞,想着队友智商好,他们搞吧,但是没出来这题...... 以后任何时候,都自信点....该想的还是好好自己想,这类题感觉就是先去找性质,然后一点点找规律,如果必要的话,自己提出一点猜想,然后如果自己举不出来反例,就暂时认为是正确的 下午搞了一下午,发现还是悲剧,晚上参考了两个题解 http://blog.csdn.

ZOJ 38727(贪心)

这道题真心坑,越想越远  想的飞起来了, 最后纠结起后缀表达式的定义来了. 题意: 就是给你一个串 ,  让你用最少修改次数来实它变成一个合法的后缀表达式,  修改方式有两种, 一种是直接添加数字或者*,或者是交换两个字符的位置. 题解: 首先保证星号所需要的数字比当前串的数字大,如果不足,则添加需要数字到字符串首 , 然后从头朝尾扫,  如果当前状态合法则不需要管,当前状态不合法的时候将* 号移到最末尾.然后没了 代码: #include<stdio.h> #include<strin

LeetCode--Best Time to Buy and Sell Stock (贪心策略 or 动态规划)

Best Time to Buy and Sell Stock Total Accepted: 14044 Total Submissions: 45572My Submissions Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, b

Leetcode文章整理

LeetCode的题目种类比较多,感觉应该将自己联系过的题目进行分类,这个就是根据自己做过的题目进行划分,并做一定的总结,会持续更新 Sort: Two Pointer: 单链表: Reorder List将l1->l2...->ln转化为l1->ln->l2->ln-1.. 这里用的很直接的方法就是找到链表的中点,然后将链表分为两部分,后半截翻转后两个链表进行融合.我在想,如果能之间把后面半截放入vector当中,就简单很多,但是就是牺牲了空间,不知道有没有更好的办法. I