ZOJ 3941 Kpop Music Party(省赛, 贪心)
2016-04-29 08:18
281 查看
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?
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).
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; }
相关文章推荐
- java linux book
- linux中的Curl工具
- 快速诊断Linux性能
- 看 nova-scheduler 如何选择计算节点 - 每天5分钟玩转 OpenStack(27)
- 看 nova-scheduler 如何选择计算节点 - 每天5分钟玩转 OpenStack(27)
- 看 nova-scheduler 如何选择计算节点 - 每天5分钟玩转 OpenStack(27)
- 使用wpa_supplicant中犯的小错误
- OBJ-C @property和@synthesize关键字学习笔记
- mongo 分片Replica Sets+Sharding架构
- jiffies的回绕问题及解决方案
- [ngix]CentOS6 下nginx的安装
- linux 解决大量SYN_RECV CLOSE_WAIT
- [linux]ln命令
- ubuntu安装nginx及负载均衡配置
- 在CentOS 6.X 上面安装 Python 2.7.X
- centos7编译安装mysql之2
- Centos 7 常用操作集合
- Quartz+TopShelf实现Windows服务作业调度
- CentOS ping百度无法识别主机原因和解决办法
- 搜索服务solr 一二事(1) - solr-5.5 使用自带Jetty或者tomcat 搭建单机版搜索服务器