3963: [Wf2011]machineworks time limit:30 Sec Memory limit:256 MB
submit:270 solved:80
[Submit] [Status] [Discuss] Description you are the manager of the Random complex Machine Company (arbitrarily Complex machines, ACM), the company uses more advanced machinery and equipment to produce advanced machines. The original production machine is broken, so you have to buy a new production machine for the company. Your job is to get as much revenue as possible during the transition period. During this time, you have to buy and sell machines, and when machines are owned by ACM, manipulate the machines to make profits. Because of the limitations of space, ACM can only have at most one machine at any time. During the transition period, there are several machines that may be sold. As an expert on advanced machines, for each machine mi, you already know its price pi and can buy the date di. Note that if you do not buy the machine mi on the first day, then other people will buy the machine, that is, you will not have the opportunity to buy this machine. If ACM's money is lower than the price of a single machine, then it is obviously impossible for you to buy this machine. If you bought the machine MI on the first day, the ACM company can start using this machine from (Di) + 1 days. Every day that you use this machine, you can create a GI dollar benefit for your company. You can decide to sell the machine at a discounted price one day after the purchase. Acquisition market for each machine, there is a discounted price ri. You can't use the machine on the day you sell it, but you can buy a new one on the day you sell it. At the end of the transition period, ACM will sell the machines it currently owns. Your task is to maximize the revenue that ACM can get during the transition.
Input inputs contain several sets of test cases. The first line of each set of test cases has 3 positive integers n,c and D. N is the number of machines (N<=10^5) that will be sold, and C is the number of dollars (C<=10^9) that the company has at the start of the transition period, and D is the number of days that the transition lasts (d<=10^9). Each row of the following n lines describes the situation of a machine. Each line has 4 positive integers di,pi,ri and GI, respectively, indicating the time the machine was sold, the amount of dollars it needed to buy the machine, the discount price for selling the machine and the profit it could get from using the machine. These numbers meet 1<=di<=d,1<=ri<pi<=10^9 and 1<=gi<=10^9. The last set of test cases is followed by a line consisting of 3 0 representing the input data.
Output for each set of test cases, the number of the test case is printed, followed by the maximum amount of USD that ACM can obtain at the end of the d+1 day. Please follow the sample output given below.
Sample Input6 10 20
6 12 1 3
1 9 1 2
3 2 1 2
8 20 5 4
4 11 7 4
2 10 9 1
0 0 0Sample OutputCase 1:44
Transfer does not satisfy monotonous ah, so need to use CDQ division to maintain monotonous transfer
You can also write splay
This blog is well written http://www.cnblogs.com/zj75211/p/8148800.html
1#include <bits/stdc++.h>2 #defineN 1000503 #definell Long Long4 #defineINF 21474836475 using namespacestd;6 intN,m,d,s[n];ll F[n];7 structdate{intD,p,r,g,id;} Q[n],a[n];8 //struct point{int x;ll y;} P[n],b[n];9 BOOLCMP1 (date A,date b) {returna.d<B.D;}Tenll X (inti) { One returnq[i].r+f[q[i].id]-q[i].p-(LL) q[i].g* (q[i].d+1); A } - intYinti) { - returnq[i].g; the } - - DoubleGintIintj) { - //if (Y (i) ==y (j)) return (double) (X (i) >x (j)? inf:-inf); + return(Double) (X (i)-X (j))/(Double) (Y (i)-Y (j)); - } + voidSolveintLintR) { A if(L>r)return; at if(l==R) { -F[l]=max (f[l],f[l-1]); - return; - } - intMid= (l+r) >>1; - intP1=l,p2=mid+1; in //for (int i=l;i<=r;i++) q[i]=a[i]; -Solve (L,mid);inttp=0, h=1; to for(inti=l;i<=mid;i++){ + if(Q[i].p>f[q[i].id])Continue; - if(Y (i) ==y (S[TP]) &&TP) { the if(X (i) >x (S[TP]) tp--; * Else Continue; $ }Panax Notoginseng while(tp>1&&g (s[tp],s[tp-1]) <=g (I,S[TP]) tp--; -s[++tp]=i; the } + for(intI=mid+1; i<=r;i++){ A while(H<tp&&g (s[h],s[h+1]) >=-Q[I].D) h++; theF[i]=max (f[i],f[i-1]); + if(H<=TP) F[i]=max (f[i],f[q[s[h]].id]-q[s[h]].p+q[s[h]].r+ (LL) q[s[h]].g* (q[i].d-q[s[h]].d-1)); - } $Solve (mid+1, R); $P1=l;p2=mid+1; - for(inti=l;i<=r;i++){ - if(P1<=MID&&Q[P1].G<=Q[P2].G) a[i]=q[p1++]; the Else if(p2<=r) a[i]=q[p2++]; - Elsea[i]=q[p1++];Wuyi } the for(inti=l;i<=r;i++) q[i]=A[i]; - } Wu intMain () { - intcas=0; About while(SCANF ("%d%d%d", &n,&m,&d)! =EOF) { $cas++; - if(!n&&!m&&! D Break; -memset (f,-1,sizeof(f)); -f[1]=f[0]=m;intd,p,r,g; A for(intI=1; i<=n;i++) +scanf"%d%d%d%d",&d,&p,&r,&g), theq[i]=(date) {d,p,r,g,i}; -q[++n]= (date) {d+1,0,0,0, n}; $Sort (q+1, q+1+n,cmp1); the for(intI=1; i<=n;i++) q[i].id=i; theSolve1, n); theprintf"Case %d:%lld\n", Cas,f[n]); the } - return 0; in}
Bzoj3963[wf2011]machineworks CDQ Division + slope optimization DP