ZOJ 3941 Kpop Music Party

Kpop Music Party

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Marjar University often hosts Kpop music festival. A Kpop music festival will last several days. During a Kpop festival, there will be a Kpop party every day. Kpop music is very popular all over the world, so there may be more than one Kpop music festival being hosted simultaneously.

Edward, the headmaster of Marjar University, is always obsessed with Kpop music. He will be very excited continuously for K days after attending a Kpop music party. More specifically, If he attends a Kpop party in the i-th day, he would be very excited from the i-th day to the (i + K - 1)-th day (inclusive). But the excitatory state does not stack. For example, if K is 5 and Edward attended a party in Day 1 and a party in Day 3, he will be very excited only from Day 1 to Day 7.

Edward has got the schedule of N Kpop music festivals in Marjar University. Each Kpop music festival lasts one or more days. The i-th Kpop festival starts at the Si-th dayand ends at the Ei-th day (inclusive). Due to restrictions on the discipline for the headmaster, he can attend at most M Kpop parties. Now he wants to maximize the number of days being excited. Can you help him?

Input

There are multiple test cases.The first line of input is an integer T (≤ 1000)indicating the number of test cases. For each test case:

The first line contains three integers, N, K and M.

The next N lines, each line contains two integers Si and Ei(1 ≤ N ≤ 10, 1 ≤ K, M,Si, Ei ≤ 109).

Output

For each case, print the number of the most days that Edward can be excited for.

Sample Input

2
1 5 2
1 3
3 7 3
1 5
2 5
13 13

Sample Output

7
18

Hint

For the first case, the only Kpop festival lasts from Day 1 to Day 3.Edward can attend a party in Day 1 and a party in Day 3,he would be very excited from Day 1 to Day 7.Edward can be excited for 7 days at most.

For the second case, there are 3 Kpop festivals.Edward can take part in the Kpop parties in Day 1, Day 5 and Day 13.He would be very excited from Day 1 to Day 11 and from Day 13 to Day 19.Edward can be excited for 18 days at most.

 仅此纪念我做了两个小时,结果却审错题的贪心题。
 先将区间合并。在每一个举办聚会的时间区间内找参加的天数,使兴奋天数最多。即兴奋的天数尽量不交叠。

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

ll n,m,k,cnt,res;

struct Node
{
    ll x,y;
    bool operator < (const Node &other) const
    {
        if(x==other.x)
            return y<other.y;
        return x<other.x;
    }
}a[15],b[15];//起点从小到大,若起点相同,终点从小到大排序

void dfs(ll pos,ll i,ll ans,ll num)//当前检索到第几天,已经查看到第几个区间,最大的开心天数,已经选了多少天
{
    if(num>k)
        return;
    if(num==k||i>=cnt+1)//已经没有区间可供选择,或者已经选够k天
    {
        res=max(res,ans);
        return;
    }
    ll len,p1,p2,p3,t;
    if(pos<b[i].y)
    {

        len=min(b[i].y-pos,b[i].y-b[i].x+1);//pos从0开始,可供选择的区间长度
        p1=len%m;//参加一天聚会兴奋m天,尽量使兴奋时间不交叠
        if(p1==0)
            p2=len/m;
        else
            p2=len/m+1;
        if(p2+num>k)
            res=max(res,ans+(k-num)*m);
        p3=max(b[i].x+p2*m-1,pos+p2*m);
        t=p2*m;
        dfs(p3,i+1,ans+t,num+p2);//不要区间最后一天
        dfs(b[i].y+m-1,i+1,ans+t+b[i].y+m-1-p3,num+p2+1);//要区间最后一天
    }
    else if(pos>=b[i].y)
    {
        dfs(pos,i+1,ans,num);
        dfs(b[i].y+m-1,i+1,ans+b[i].y+m-1-pos,num+1);
    }

}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int i,j;
        cnt=0;
        cin>>n>>m>>k;
        for(i=1;i<=n;i++)
        {
            cin>>a[i].x>>a[i].y;
        }
        sort(a+1,a+1+n);
        for(i=1;i<=n;i!=0)
        {
            for(j=i+1;j<=n;j++)
            {
                if(a[j].x<=a[i].y+1)
                {
                    a[i].y=max(a[i].y,a[j].y);
                }
                else
                    break;
            }
            b[++cnt]=a[i];
            i=j;
        }//合并区间
        res=0;
        dfs(0,1,0,0);//当前在第几个元素,已经查看到第几个区间,最大的开心值,已经选了多少天
        //pos从0开始
        cout<<res<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jinghui_7/article/details/79860937
ZOJ