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天卖出。
所以我们从后到前依次贪心。将后面的可选结果存入优先队列。依次取出即可。
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; }
相关文章推荐
- Stock (zoj 2921 贪心经典)
- zoj-2921 Stock ***
- ZOJ-#3464 Rugby Football(贪心)
- ZOJ 3953 Intervals(区间贪心)
- ZOJ 1084 贪心 给顶点染色,相邻顶点不能染同样的颜色 算法有问题不能保证是最优解但能A
- ZOJ 3829 贪心 思维题
- ZOJ 3829 模拟贪心
- ZOJ Problem Set - 1082 Stockbroker Grapevine
- ZOJ贪心算法_题目
- ZOJ-2362 Beloved Sons 贪心 | KM
- zoj 3410 Layton's Escape(贪心+优先队列)
- hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)
- ZOJ 3715 Kindergarten Election(枚举+贪心)
- ZOJ-3953-Intervals【贪心】【17th浙大校赛】
- ZOJ 3607 Lazier Salesgirl 贪心
- ZOJ 2315 New Year Bonus Grant(贪心)
- 浙江14届省赛 F.Heap Partition(ZOJ 3963)[贪心][二分]
- zoj1360贪心
- 贪心(ZOJ 3778,Talented Chef)
- ZOJ 3987 Numbers 2017CCPC秦皇岛站G题 大整数 二进制 贪心