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<queue>
#include<stack>
#include<vector>
#include<set>
#include<map> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1) #define eps 1e-8
//typedef __int64 ll; #define fre(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 bug pf("Hi\n") using namespace std; #define INF 0x3f3f3f3f
#define N 100005 int r[N],p[N],s[N];
int n; struct stud{
int p,num;
friend bool operator < (stud a,stud b)
{
return a.p<b.p;
}
}; priority_queue<stud>q; void solve()
{
int i,j;
while(!q.empty()) q.pop(); int ans=0; struct stud cur; for(i=n-1;i>=0;i--)
{
cur.num=s[i];
cur.p=p[i];
q.push(cur); while(!q.empty()&&r[i])
{
cur=q.top(); q.pop(); if(cur.num>r[i])
{
ans+=cur.p*r[i];
//pf("%d %d\n",cur.p,r[i]);
cur.num-=r[i];
r[i]=0;
q.push(cur);
}
else
{
ans+=cur.p*cur.num;
r[i]-=cur.num;
// pf("%d %d\n",cur.p,cur.num); }
} } pf("%d\n",ans);
}
int main()
{
int i,j,t;
sf(t);
while(t--)
{
sf(n);
fre(i,0,n)
sfff(r[i],p[i],s[i]);
solve();
}
return 0; }

zoj 2921 Stock(贪心)的更多相关文章

  1. 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 ...

  2. zoj 1025Wooden Sticks(贪心)

    递增子序列的最小组数.可以直接贪心,扫一遍 #include<iostream> #include<cstring> #include<cstdio> #inclu ...

  3. ZOJ 3829 模拟贪心

    2014牡丹江现场赛水题 给出波兰式,推断其是否合法.假设不合法有两种操作: 1:任何位置加一个数字或者操作符 2:随意两个位置的元素对调 贪心模拟就可以 先推断数字数是否大于操作符数,若不大于 an ...

  4. ZOJ FatMouse' Trade 贪心

    得之我幸,不得,我命.仅此而已. 学姐说呀,希望下次看到你的时候依然潇洒如故.(笑~) 我就是这么潇洒~哈哈. 感觉大家比我还紧张~ 我很好的.真的 ------------------------- ...

  5. ZOJ 3905 Cake(贪心+dp)

    动态规划题:dp[i][j]表示有i个Cake,给了Alice j个,先按照b排序,这样的话,能保证每次都能成功给Alice Cake,因为b从大到小排序,所以Alice选了j个之后,Bob最少选了j ...

  6. ZOJ 4067 - Books - [贪心][2018 ACM-ICPC Asia Qingdao Regional Problem J]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4067 题意: 给出 $n$ 本书(编号 $1 \sim n$), ...

  7. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  8. ZOJ Problem Set - 3829Known Notation(贪心)

    ZOJ Problem Set - 3829Known Notation(贪心) 题目链接 题目大意:给你一个后缀表达式(仅仅有数字和符号),可是这个后缀表达式的空格不幸丢失,如今给你一个这种后缀表达 ...

  9. Heap Partition ZOJ - 3963(贪心)

    ZOJ - 3963 贪心做一下就好了 反正别用memset #include <iostream> #include <cstdio> #include <sstrea ...

随机推荐

  1. Tasker, Android系统增强神器, 变量汇总

    http://tasker.dinglisch.net/userguide_summary.html#variables.html http://tasker.dinglisch.net/usergu ...

  2. MySQL删除数据Delete与Truncate语句使用比较

    在MySQL数据库中,DELETE语句和TRUNCATE TABLE语句都可以用来删除数据,但是这两种语句还是有着其区别的,下文就为您介绍这二者的差别所在 空mysqll表内容常见的有两种方法:一种d ...

  3. iptables学习与研究(使用LOG记录失败日志)

    原文地址: http://blog.csdn.net/fafa211/article/details/2307581 通常情况下,iptables的默认政策为DROP,不匹配的数据包将被直接丢弃.但在 ...

  4. Facebook产品的开发流程

    [编者注]王淮是Facebook第二位中国籍工程师,也是第一位中国籍研发经理,他一手开创了Facebook的支付安全和客服工具领域.2011年他离开Facebook,回国成为天使投资人,希望用自己在F ...

  5. RxJava 和 RxAndroid (生命周期控制和内存优化)

    RxJava使我们很方便的使用链式编程,代码看起来既简洁又优雅.但是RxJava使用起来也是有副作用的,使用越来越多的订阅,内存开销也会变得很大,稍不留神就会出现内存溢出的情况,这篇文章就是介绍Rxj ...

  6. OpenMP 中的线程任务调度

    OpenMP中任务调度主要针对并行的for循环,当循环中每次迭代的计算量不相等时,如果简单地给各个线程分配相同次数的迭代,则可能会造成各个线程计算负载的不平衡,影响程序的整体性能. 如下面的代码中,如 ...

  7. ScaleIO 1.2 基础

    The ScaleIO virtual SAN consists of 3 software components =================== Meta Data Manager (MDM ...

  8. FatSecret Platform API

    在现阶段饮食类的APP发展的非常迅猛,尤其在校园中,学生只需要凭借一个手机就能买到自己想要的食物,真正做到了足不出户.可是如果我们想独立完成一个app就需要有相应的数据支持,这里给大家介绍一个国外的开 ...

  9. 使用Intellij加载Spark源代码

    如何使用Intellij加载Spark源代码 转载注明原文http://www.cnblogs.com/shenh062326/p/6189643.html 查看Spark源代码或修改Spark源代码 ...

  10. (转)Akka学习笔记(二):Actor Systems

    Akka学习笔记(二):Actor Systems 图中表示的是一个Actor System,它显示了在这个Actor System中最重要实体之间的关系. 什么是actor,是一个封装了状态和行为的 ...