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
Hint
For the first case, the only Kpop festival lasts from Day 1 to Day 3. Edward can attend a party in Day 1 and a party in Day 3, he would be very excited from Day 1 to Day 7. Edward can be excited for 7 days at most.
For the second case, there are 3 Kpop festivals. Edward can take part in the Kpop parties in Day 1, Day 5 and Day 13. He would be very excited from Day 1 to Day 11 and from Day 13 to Day 19. Edward can be excited for 18 days at most.
省赛的时候的F题 最后过了9个
比较可惜的是因为失误错失了一血机会 还浪费了好多时间
2.5小时的第一次提交合并区间忘了取max 直到最后20min才发现
这题首先需要把区间合并 变成不相交的区间
对于一个区间假设长度为len 那么肯定至少有len/k次可以完整的选取长度为k的区间
同时如果有len/k+2次选取 肯定能将所有的该区间能覆盖的点都搞定
所以先把所有的len/k求和 如果m<sum 则答案是m*k
如果m>sum+2*n 则肯定可以覆盖所有的点 相当于m=sum+2*n
然后枚举每个区间究竟给len/k + (0或1或2)
复杂度3^n
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
struct node
{
long long l,r,v;
int use;
} p[15];
bool cmp(node a,node b)
{
if(a.use==b.use) return a.l<b.l;
return a.use<b.use;
}
long long n,m,k,ans,a[15];
void solve()
{
long long now=0,pr,ret=0;
for(int i=1;i<=n;i++)
{
pr=max(now,p[i].l-1);
now=pr;
now+=(p[i].v+a[i])*k;
now=min(now,p[i].r+k-1);
ret+=now-pr;
}
ans=max(ret,ans);
}
void dfs(int st,int hav)
{
if(st>n)
{
if(hav!=m) return ;
solve();
return ;
}
if(hav>m) return;
if(hav+(n-st+1)*2<m) return ;
for(int i=0;i<=2;i++)
{
a[st]=i;
dfs(st+1,hav+i);
}
}
int main()
{
int i,j,T;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld%lld",&n,&k,&m);
long long sum=0,flag=1;
for(i=1; i<=n; i++)
{
scanf("%lld%lld",&p[i].l,&p[i].r);
p[i].use=0;
}
while(flag)
{
flag=0;
sort(p+1,p+1+n,cmp);
for(i=2; i<=n; i++)
{
if(p[i].use) break;
if(p[i].l<=p[i-1].r+1)
{
p[i-1].r=max(p[i-1].r,p[i].r),p[i].use=1,flag=1;
break;
}
}
}
for(i=1;i<=n;i++)
{
if(p[i].use) break;
p[i].v=(p[i].r-p[i].l+1)/k,sum+=p[i].v;
}
n=i-1;
if(sum>=m)
{
printf("%lld\n",m*k);
continue;
}
m-=sum;
ans=0;
if(m>2*n) m=2*n;
dfs(1,0);
cout<<ans<<endl;
}
return 0;
}