首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ZOJ 3941 Kpop Music Party(省赛, 贪心)

ZOJ 3941 Kpop Music Party(省赛, 贪心)

作者头像
ShenduCC
发布2018-04-26 17:06:47
7160
发布2018-04-26 17:06:47
举报
文章被收录于专栏:算法修养算法修养

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 day and 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
这道题目最先想到的是贪心,从最左边开始,依次选择长度k的区间,不相交,这样是最大的。但是这样贪心完之后发现m还有剩余,发现每个区间的最后一点也可以选,同样可以有增益效果,但是回头再去选,会打乱一开始的贪心,所以在贪心的时候就枚举每个区间最后一个点要不要选,最后也就2的10次方种可能#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>


using namespace std;
typedef long long int LL;
struct Node{
    LL l,r;
}a[15],b[15];
int cmp(Node a,Node b){
   if(a.l==b.l)
        return a.r<b.r;
  return a.l<b.l;
}
int n1;
LL m,k,res;
int cnt;
void dfs(LL pos,int i,LL ans,LL n){
	if(n>m)
		return;
    if((i>=cnt+1)||n==m){
        res=max(res,ans);
		return;
    }
    LL len,pos1,num,p2,p;
    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) res=max(res,ans+(m-n)*k);
       pos1=max(b[i].l+p2*k-1,pos+p2*k);
       num=p2*k;
       dfs(pos1,i+1,ans+num,n+p2);//不选最后一个点
       dfs(b[i].r+k-1,i+1,ans+num+b[i].r+k-1-pos1,n+p2+1);//选择最后一个点
    }
    else if(pos>=b[i].r){
        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",&n1,&k,&m);
         for(int i=1;i<=n1;i++)
             scanf("%lld%lld",&a[i].l,&a[i].r);
         sort(a+1,a+n1+1,cmp);
         cnt=0;
         int i,j;
         for(i=1;i<=n1;i){
             for( j=i+1;j<=n1;j++){
                if(a[j].l<=a[i].r+1)//区间可以合并
                {a[i].r=max(a[i].r,a[j].r);}
                else break;
             }
			 b[++cnt]=a[i];
             i=j;
        }
        res=0;
        dfs(0,1,0,0);
        printf("%lld\n",res);
     }
     return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-04-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Input
  • Output
  • Sample Input
  • Sample Output
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档