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, NK and M.

The next N lines, each line contains two integers Si and Ei (1 ≤ N ≤ 10, 1 ≤ KMSiEi ≤
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;
}

ZOJ 3941 Kpop Music Party(省赛, 贪心)的更多相关文章

  1. ZOJ 3941 Kpop Music Party 贪心

    题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3941 题解: 先吧所给的区间合并,得到若干个独立的区间. 然后从左 ...

  2. ZOJ 3941 Kpop Music Party

    先把能合并的区间都合并起来. 考虑最裸的贪心策略,从左到右一段一段的取. 但是,这样会有错,错在没有考虑每段区间选取最后一个点. 因为N只有10,所以枚举一下哪些区间最后一个点会被选择,然后按照最裸的 ...

  3. ZOJ 1025 Wooden Sticks(快排+贪心)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=25 题目大意:机器运送n个木条,每个木条有一个长度和重量.运送第一根木 ...

  4. ZOJ 3872 浙江2015年省赛试题

    D - Beauty of Array Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu S ...

  5. ZOJ 2819 Average Score 牡丹江现场赛A题 水题/签到题

    ZOJ 2819 Average Score Time Limit: 2 Sec  Memory Limit: 60 MB 题目连接 http://acm.zju.edu.cn/onlinejudge ...

  6. [NOIP模拟赛][贪心]奶牛晒衣服.

    奶牛晒衣服(dry) [问题描述] 在熊大妈英明的带领下,时针和它的同伴生下了许多牛宝宝.熊大妈决定给每个宝宝都穿上可爱的婴儿装.于是,为牛宝宝洗晒衣服就成了很不爽的事情. 圣人王担负起了这个重任.洗 ...

  7. ZOJ - 4019 Schrödinger's Knapsack (背包,贪心,动态规划)

    [传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5747 [题目大意]:薛定谔的背包.薛定谔的猫是只有观测了才知道猫的死 ...

  8. HDU 6438 Buy and Resell ( 2018 CCPC 网络赛 && 贪心 )

    题目链接 题意 : 给出一些数.你可以从左到右对这些数进行三种操作花费 Ai 买入东西.以 Ai 价格卖出你当前有的东西.或者什么都不做.现在问你可以获取的最大利益是多少? 分析 : 和 CF 867 ...

  9. hdu6438 Buy and Resell 买卖物品 ccpc网络赛 贪心

    题目传送门 题目描述: 有n座城市,每座城市都可以对一个物品进行一次的买进或者卖出,可以同时拥有多个物品,计算利润最大值,并且交易次数要最少.(买入卖出算两次操作) 思路: 建立两个小根堆 优先队列, ...

随机推荐

  1. MongoDB - 内嵌角色

    数据库用户角色(每个数据库都有的角色)   角色名 说明 read 对non-system集合的读取权限,以及以下system集合的读取权限:system.indexes,system.js,syst ...

  2. Python处理验证码第一篇(pytesser初探及报错处理)

    (原文链接:http://blog.csdn.net/Bone_ACE/article/details/50436587) 前言: 春节期间,无法全身心投入地去写爬虫,那就玩玩验证码吧,应该比较有趣! ...

  3. .NET牛人应该知道些什么?

    任何一个使用.NET的人 描述线程与进程的区别? 什么是Windows服务,它的生命周期与标准的EXE程序有什么不同 Windows上的单个进程所能访问的最大内存量是多少?它与系统的最大虚拟内存一样吗 ...

  4. django 查询集 API

    filter 表示=, 返回一个新的QuerySet,包含与给定的查询参数匹配的对象.exclude 表示!=. 返回一个新的QuerySet,它包含不满足给定的查找参数的对象. annotate 使 ...

  5. fs 小计

    如果是export 就可以放到b-leg上 如果是set就可以对于a-leg

  6. vb.net winform exe 接参数

    找到如下文件 -\WindowsApplication1\My Project\Application.Designer.vb <Global.System.Diagnostics.Debugg ...

  7. atitit.md5算法的原理 与 总结

    atitit.md5算法的原理 与 总结 1. MD5的位数 128位1 2. 字节数组转换为32位字符串 base161 2.1. 十六进制字符用4个二进制位来表示1 2.2. byte[]和十六进 ...

  8. Unity3D中uGUI事件系统简述及使用方法总结

    Unity3D的uGUI系统的将UI可能触发的事件分为12个类型,即EventTriggerType枚举的12个值.如下图所示: 先以PointerClick为例.这个是用于某点点击事件.其他事件都可 ...

  9. 【转帖】关于sql server 2008 的mdf收缩问题

    原帖地址:http://social.msdn.microsoft.com/forums/windowsazure/pt-br/388f92e1-9a1e-497d-bde1-6664561fd44e ...

  10. 6:7 题一起MySQL数据库分库备份

    企业Shell面试题6:MySQL数据库分表备份 请实现对MySQL数据库进行分表备份,用脚本实现. 解答: [root@db01 scripts]# cat fenbiao.sh #!/bin/ba ...