「CFgym100506J」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 in- and output
Input
1
6
4 4 2
2 9 3
2 6 3
2 5 9
2 2 2
2 3 3
Output
76
数据范围大的可怕
100组数据,每组100000天,给出每天获得股票数量,股票价格,当天可卖出的股票数量
然后求最大获利。。。
倒序。。。然后由于股票价格<=100。。。可以用set在几乎常数时间内得到这天之后的可卖的最高价格,然后和当前价格一起选高的卖,如果当天卖出股票数量有剩余的加入set
复杂度100*100000*log(100)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<cstring> #include<set> #define inf 1000000000 #define ll long long #define mod 1000000007 using namespace std; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } ll ans; int T,n; int x[100005],p[100005],m[100005]; int sum[105]; set<int> q; int main() { T=read(); while(T--) { ans=0; n=read(); memset(sum,0,sizeof(sum)); q.clear(); for(int i=1;i<=n;i++) x[i]=read(),p[i]=read(),m[i]=read(); for(int i=n;i;i--) { while(q.size()&&x[i]) { int ed=*--q.end(); if(ed<p[i])break; int t=min(sum[ed],x[i]); x[i]-=t;sum[ed]-=t;ans+=t*ed; if(!sum[ed])q.erase(ed); } int t=min(m[i],x[i]); x[i]-=t;m[i]-=t;ans+=t*p[i]; while(q.size()&&x[i]) { int ed=*--q.end(); int t=min(sum[ed],x[i]); x[i]-=t;sum[ed]-=t;ans+=t*ed; if(!sum[ed])q.erase(ed); } if(m[i]) { if(!sum[p[i]])q.insert(p[i]); sum[p[i]]+=m[i]; } } printf("%I64d\n",ans); } return 0; } |