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 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("%lldn",res);
}
return 0;
}
- 长文 | 手把手教你如何使用python进行数据分析(最好将文章代码自己码一遍)
- 回归与梯度下降法及实现原理
- 【宅男宅女们的福音】电影天堂最新电影爬取及搜索脚本
- 把模块有关联的放在一个文件夹中 在python2中调用文件夹名会直接失败 在python3中调用会成功,但是调用不能成功的解决方案
- numpy用法小结
- 凯撒密码加解密及破解实现原理
- linux bash Shell脚本经典 Fork炸弹演示及命令详解
- python易错盲点排查之+=与+的区别分析以及一些赋值运算踩过的坑
- Selenium2+python自动化57-捕获异常(NoSuchElementException)
- 你真的会用ABAP, Java和JavaScript里的constructor么?
- 【Python学习笔记之三】lambda表达式用法小结
- Selenium2+python自动化58-读取Excel数据(xlrd)
- 软件测试金字塔
- Selenium2+python自动化59-数据驱动(ddt)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 一致性哈希的golang实现
- Array - 75. Sort Colors
- Array - 309. Best Time to Buy and Sell Stock with Cooldown
- Array - 55. Jump Game
- 【技术创作101训练营】超简单的公司Git+个人Git共存方式
- css高度坍塌与清除浮动
- 无缝切换在线升级的终极探索
- promise详解
- Vuex是怎样进行状态管理的
- Matplotlib玩转动态可视化
- JavaScript类型判断的四种方法
- Markdown基础语法
- 仿bilibili弹幕样式的404页面
- JS中的原型和原型链
- 一文看懂 Pandas 中的透视表