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. Spring JdbcTemplate查询实例

    这里有几个例子向您展示如何使用JdbcTemplate的query()方法来查询或从数据库提取数据.整个项目的目录结构如下: 1.查询单行数据 这里有两种方法来查询或从数据库中提取单行记录,并将其转换 ...

  2. JavaScript学习10:动态载入脚本和样式

    我们在写Web页面的时候,须要引入非常多的JavaScript脚本文件和CSS样式文件,尤其是在站点需求量非常大的时候,脚本的需求量也随之变大,这样一来,站点的性能就会大打折扣.因此就出现了动态载入的 ...

  3. 辛星跟您解析在CSS面包屑中三角形的定位问题

    刚才看到有位网友非常纳闷第二个棕色三角形是怎么定位的,我当感觉在以下说不清楚,就特别开了一片博客.来说清楚它.首先,前面的代码我们先抄下来,至于前面这部分代码是怎么来的,读我的用CSS制作面包屑导航的 ...

  4. minor gc和full gc

    Minor GC ,Full GC 触发条件 Minor GC触发条件:当Eden区满时,触发Minor GC. Full GC触发条件: (1)调用System.gc时,系统建议执行Full GC, ...

  5. 资源合并fis-postpackager-simple插件的使用

    FIS默认只会进行文件打包,不会对页面中的静态资源引用进行替换,这时可以利用fis-postpackager-simple插件进行资源替换. 安装: npm install -g fis-postpa ...

  6. 将List集合中的map对象转为List<对象>形式--封装类

    import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Li ...

  7. SMTP用户枚举原理简介及相关工具

    前言 SMTP是安全测试中比较常见的服务类型,其不安全的配置(未禁用某些命令)会导致用户枚举的问题,这主要是通过SMTP命令进行的.本文将介绍SMTP用户枚举原理以及相关工具. SMTP SMTP命令 ...

  8. 几款很厉害的jQuery数字化签名插件(转)

    在浏览器中,我们有很多方式来绘制生成签名效果,并且有很多很棒很智能的jQuery插件.数字化签名是未来的发展方向,正是这个原因我们这里收集并且推荐了四款超棒的jQuery数字化签名插件,希望大家喜欢! ...

  9. 搜索引擎爬虫蜘蛛的useragent

    百度爬虫    * Baiduspider+(+http://www.baidu.com/search/spider.htm”) google爬虫    * Mozilla/5.0 (compatib ...

  10. 使用js的indexOf,lastIndexOf,slice三函数轻易得到url的服务器,路径和页名

    js的indexOf,lastIndexOf,slice能帮我们在js字符串处理时少走一些弯路. 程序如下: var url="http://www.cnblogs.com/xiandeda ...