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

}
时间: 2024-10-08 00:55:48

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

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

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

ZOJ 3829 模拟贪心

2014牡丹江现场赛水题 给出波兰式,判断其是否合法,如果不合法有两种操作: 1:任意位置加一个数字或者操作符 2:任意两个位置的元素对调 贪心模拟即可 先判断数字数是否大于操作符数,若不大于 ans+=sum2-sum1+1:新加入的数字全部放到左端. 然后从左到右遍历一遍,存储到当前位置为止,数字数和sum1,和操作数和sum2 若sum2>=1sum1,优先与队尾的数字对调,若没有则sum1++,表示在最左端加一个数字 #include "stdio.h" #include

ZOJ 3905 Cake(贪心+dp)

动态规划题:dp[i][j]表示有i个Cake,给了Alice j个,先按照b排序,这样的话,能保证每次都能成功给Alice Cake,因为b从大到小排序,所以Alice选了j个之后,Bob最少选了j个,所以i>=2*j, 并且每次Alice选的时候Bob已经选过了.所以当i>=2 * j的时候Alice一定能选. 所以dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + ary[i].a); dp[i - 1][j]表示Alice不选第i个,dp[i

有人男妓

http://quote.hexun.com/stock/icb.aspx?code=1&name=%A8%7B%C6%BD%C1%B9%C4%C4%C0%EF%D3%D0%C7%E8%BB%AF%CE%EF%C2%F2%A3%D1%3A%A3%B1%A3%B1%A3%B2%A3%B7%A3%B4%A3%B0%A3%B1%A3%B1%A3%B7%A3%B5 http://quote.hexun.com/stock/icb.aspx?code=1&name=%A8%7C%D5%C5%D2%B

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

Leetcode 贪心 Best Time to Buy and Sell Stock

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Best Time to Buy and Sell Stock Total Accepted: 13234 Total Submissions: 43145 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

ZOJ 2109 FatMouse&#39; Trade (背包 dp + 贪心)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109 FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J

zoj 3627(贪心)

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