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. ssl证书文件

    证书(Certificate) - *.cer *.crt私钥(Private Key) - *.key证书签名请求(Certificate signing request) - *.csr 至于pe ...

  2. Livereload介绍

    Livereload可理解为即时刷新,在前端开发中,开发人员在编写或调试html/js/css代码后须要从编辑器切换到浏览器.再刷新浏览器才干看到页面变化,这样的十分频繁的操作在一定程度上影响了工作效 ...

  3. winform最小化及关闭提示

    public PrintService() { InitializeComponent(); this.WindowState = FormWindowState.Minimized; } priva ...

  4. idea lib下有jar包但是仍然报错 找不到类

    现象: idea lib下有jar包但是仍然报错 找不到类 但是有个奇怪现象 同样的配置下项目在eclipse中可以正常编译 启动. package com.puhui.car.aspect; imp ...

  5. EHCache 在WEB项目的使用方法

    EHCache 是一个纯java的在进程中的缓存,它具有下面特性:高速,简单,为Hibernate2.1充当可插入的缓存,最小的依赖性,全面的文档和測试.官方站点http://ehcache.sour ...

  6. atitit.atiOrm.js v2 q61 版本新特性.docx

    atitit.atiOrm.js v2 q61 版本新特性.docx 1. V1新特性如下1 1.1. V2规划,直接生成sql在js端1 2. Orm设计框架图1 2.1. atiOrm.js的原理 ...

  7. 数据库设计(五)第一范式(1NF)?

    In our last tutorial we learned and understood how data redundancy or repetition can lead to several ...

  8. 【shell】使用 /dev/null crontab

    1.linux组成kernel.shell.工具程序有sh.bash 一个例子 !#/bin/bash echo '' 执行之前chmod +x 执行./ 2.一个小窍门 cp /dev/null / ...

  9. Zookeeper实战

    这里的实战主要是用Java API对Zookeeper进行相应的操作,来实现我们的业务需求.首先简单地测试一下Zookeeper节点的增删改查操作,然后测试两个例子,分别是分布式应用HA以及分布式锁. ...

  10. MapReduce机制

    1. MapReduce是一种分布式计算模型,由Google提出,主要用于搜索领域,解决海量数据的计算问题.2. MR由两个阶段组成:Map和Reduce,用户只需要实现map()和reduce()两 ...