Machine Works
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 245 Accepted Submission(s): 67
Problem Description
You are the director of Arbitrarily Complex Machines (ACM for short), a company producing advanced machinery using even more advanced machinery. The old production machinery has broken down, so you need to buy new production machines for the company. Your goal is to make as much money as possible during the restructuring period. During this period you will be able to buy and sell machines and operate them for profit while ACM owns them. Due to space restrictions, ACM can own at most one machine at a time.
During the restructuring period, there will be several machines for sale. Being an expert in the advanced machines market, you already know the price P i and the availability day Di for each machines M i. Note that if you do not buy machine M i on day Di, then somebody else will buy it and it will not be available later. Needless to say, you cannot buy a machine if ACM has less money than the price of the machine.
If you buy a machine M i on day D i, then ACM can operate it starting on day D i + 1. Each day that the machine operates, it produces a profit of Gi dollars for the company.
You may decide to sell a machine to reclaim a part of its purchase price any day after you’ve bought it. Each machine has a resale price R i for which it may be resold to the market. You cannot operate a machine on the day that you sell it, but you may sell a machine and use the proceeds to buy a new machine on the same day.
Once the restructuring period ends, ACM will sell any machine that it still owns. Your task is to maximize the amount of money that ACM makes during the restructuring.
During the restructuring period, there will be several machines for sale. Being an expert in the advanced machines market, you already know the price P i and the availability day Di for each machines M i. Note that if you do not buy machine M i on day Di, then somebody else will buy it and it will not be available later. Needless to say, you cannot buy a machine if ACM has less money than the price of the machine.
If you buy a machine M i on day D i, then ACM can operate it starting on day D i + 1. Each day that the machine operates, it produces a profit of Gi dollars for the company.
You may decide to sell a machine to reclaim a part of its purchase price any day after you’ve bought it. Each machine has a resale price R i for which it may be resold to the market. You cannot operate a machine on the day that you sell it, but you may sell a machine and use the proceeds to buy a new machine on the same day.
Once the restructuring period ends, ACM will sell any machine that it still owns. Your task is to maximize the amount of money that ACM makes during the restructuring.
Input
The input consists of several test cases. Each test case starts with a line containing three positive integers N, C, and D. N is the number of machines for sale (N <= 10^5), C is the number of dollars with which the company begins the restructuring (C <= 10^9), and D is the number of days that the restructuring lasts (D <= 10^9).
Each of the next N lines describes a single machine for sale. Each line contains four integers D i, P i, R i and G i,
denoting (respectively) the day on which the machine is for sale, the dollar price for which it may be bought, the
dollar price for which it may be resold and the daily profit generated by operating the machine. These numbers satisfy
1 <= D i <= D, 1 <= R i < P i <=10^9 and 1 <= G i <= 10^9.
The last test case is followed by a line containing three zeros.
Each of the next N lines describes a single machine for sale. Each line contains four integers D i, P i, R i and G i,
denoting (respectively) the day on which the machine is for sale, the dollar price for which it may be bought, the
dollar price for which it may be resold and the daily profit generated by operating the machine. These numbers satisfy
1 <= D i <= D, 1 <= R i < P i <=10^9 and 1 <= G i <= 10^9.
The last test case is followed by a line containing three zeros.
Output
For each test case, display its case number followed by the largest number of dollars that ACM can have at the end of day D + 1.
Follow the format of the sample output.
Follow the format of the sample output.
Sample Input
6 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 0
Sample Output
Case 1: 44
参见:http://blog.csdn.net/sdj222555/article/details/40919903
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node
{
ll d,p,r,g;
}p[100010];
struct node2
{
ll x,y;
}A[100010],C[100010];
bool cmp(node a,node b)
{
return a.d<b.d;
}
bool cmp2(node2 a,node2 b)
{
return a.x<b.x ||(a.x==b.x && a.y<b.y);
}
int T,t,n,m,d;
ll f[100010];
ll h(int j)
{
return f[j]-p[j].p+p[j].r-p[j].g*(p[j].d+1);
}
bool slopecomp(node2 a,node2 b,node2 c)
{
ll xa,xb,ya,yb;
xa=b.x-a.x;ya=b.y-a.y;
xb=c.x-a.x;yb=c.y-a.y;
double tmp=(double)xa*yb-(double)xb*ya;
return tmp<0;
}
void cdq(int l,int r)
{
if(l+1<=r)
{
int i,j,k,na=0,nb=0,nc=0,mi=(l+r)/2;
cdq(l,mi);
for(j=l;j<=mi;j++)
if(f[j]>=p[j].p)
{
A[++na].x=p[j].g;
A[na].y=h(j);
}
sort(A+1,A+1+na,cmp2);
for(i=1;i<=na;i++)
{
while(nc>1 && !slopecomp(C[nc-1],C[nc],A[i]))
nc--;
C[++nc]=A[i];
}
j=0;
for(i=mi+1;i<=r;i++)
{
while(j<nc)
{
if(p[i].d*C[j].x+C[j].y>=p[i].d*C[j+1].x+C[j+1].y)
break;
j++;
}
f[i]=max(f[i],p[i].d*C[j].x+C[j].y);
}
cdq(mi+1,r);
}
}
int main()
{
int i,j,k;
while(~scanf("%d%I64d%d",&n,&f[0],&d) && n+f[0]+d!=0)
{
for(i=1;i<=n;i++)
scanf("%I64d%I64d%I64d%I64d",&p[i].d,&p[i].p,&p[i].r,&p[i].g);
sort(p+1,p+1+n,cmp);
n++;
p[n].d=d+1;
p[n].p=p[n].r=p[n].g=0;
for(i=1;i<=n;i++)
f[i]=f[0];
cdq(0,n);
printf("Case %d: %I64d\n",++t,f[n]);
}
}