zoj—3941

暂存。。。。。。。。。。。。。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int n;
long long k,m;
long long cnt,res;
struct node
{
    long long l,r;
}a[20],b[20];
int cmp(node a,node b)
{
    if(a.l==b.l)
        return a.r<b.r;
    return a.l<b.l;
}

void dfs(long long pos,int i,long long ans,long long n)
{
    if(n>m)
        return;//表示i这段区间选的次数超了
    if(i>=cnt+1||n==m)//说明超了段数,并且n==m所以这时候直接返回结果就好了
    {
        res=max(res,ans);
        return ;
    }
    long long len,p,p2,posLL,num;
    if(pos<b[i].r)
    {
        len=min(b[i].r-b[i].l+1,b[i].r-pos);//
        p=len%k;
        if(p==0)
        {
            p2=len/k;
        }
        else
        {
            p2=len/k+1;
        }
        if(p2+n>m)//次数超了,这时候进行ans的变化
        {
            int lala=(m-n);
            res=max(res,ans+lala*k);
        }
        posLL=max(b[i].l+p2*k-1,pos+p2*k);
        num=p2*k;

        dfs(posLL,i+1,ans+num,n+p2);//不选最后一个点
        dfs(b[i].r+k-1,i+1,ans+num+b[i].r+k-1-posLL,n+p2+1);//选择最后一个点
    }
    else//这时候只需要判断i区域的最后一个点取不取。
    {
        dfs(pos,i+1,ans,n);//不选最后一个点
        dfs(b[i].r+k-1,i+1,ans+b[i].r+k-1-pos,n+1);//选择最后一个点
        //注意这个地方会重合,写的时候容易出错!!!!
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %lld %lld",&n,&k,&m);
        for(int i=1;i<=n;i++)
        {
            cin>>a[i].l>>a[i].r;
        }
        sort(a+1,a+1+n,cmp);
        cnt=0;
        //进行区间合并
        int i,j;
        for(i=1;i<=n;)
        {
            for(j=i+1;j<=n;j++)
            {
                if(a[i].r+1>=a[j].l)
                {
                    a[i].r=max(a[i].r,a[j].r);
                    //a[i].l=a[j].l;
                }
                else
                    break;//这样之后的就没法与i合并了
            }
            b[++cnt]=a[i];
            i=j;//之后就直接从合并之后的来找。
        }
        res=0;
        dfs(0,1,0,0);
        cout<<res<<endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值