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. XMind--用他来理清自己的思路

    背景 一图胜千言,多年以前阅读了<图谋职场>后,深刻体会了这一点.工作学习,有效利用各种图,事半功倍. 简介 XMIND不仅可以绘制思维导图,还能绘制鱼骨图.二维图.树形图.逻辑图.组织结 ...

  2. [J2EE基础]初识JSP和Servlet

    近期须要用到J2EE,就開始学习与J2EE相关的知识了. JSP是一种Javaserver端技术,它用于在网页上显示动态内容. Tomcat相关知识 JSP的运行过程 JSP的页面构成元素 JSP的凝 ...

  3. SET QUOTED_IDENTIFIER OFF语句的作用 转载

    SET QUOTED_IDENTIFIER ON SELECT * FROM "USER" WHERE a='netasp' SET QUOTED_IDENTIFIER ON SE ...

  4. Centos下源码安装git

    1.centos下git版本太久了,才1.8几,而官方更新的还是很活跃的,于是我就想源码安装一个新版本. 2.首先到: https://github.com/git/git/releases 下载最新 ...

  5. 关于Qt半自动内存管理的思考及实验

    一时兴起,对Qt感了兴趣,决心想要研究一下. 按网上资料配好环境,Windows 7 64bit + Qt 5.3.1 + VS2010. 根据<C++ GUI Qt4 编程>这本书,写出 ...

  6. 3.selenium模块

    本节内容: 介绍 安装 基本使用 选择器 等待元素被加载 元素交互操作 其他 项目练习 一.介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行Ja ...

  7. migrate的使用

    安装完yii2后,需要创建一张user表,这里用migrate创建 1.在dos窗口下,先cd到项目的目录,比如E:\>cd E:\Visual-AMP-x64\www\blog\blog 2. ...

  8. 【ZT】NUMA架构的CPU -- 你真的用好了么?

    本文从NUMA的介绍引出常见的NUMA使用中的陷阱,继而讨论对于NUMA系统的优化方法和一些值得关注的方向. 文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部 作者:卢钧轶(cenalulu) ...

  9. python之读取Excel 文件

    # -*- coding: utf-8 -*- """ Created on Thu May 24 13:53:10 2018 @author: Frank " ...

  10. cocoa pods 升级遇到的问题

    1. cocoa pods 升级遇到的问题 运行 sudo gem update --system 报错 Updating rubygems-update ERROR: While executing ...