Machine Works - HDU 3842 cdq分治

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.
 

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.
 

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.
 

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]);
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值