School Reunion
题 意:在一个茶馆,小明相见p个人,总共有n个人,每个人会在茶馆呆[si,ti]的时间,问小明最少需要在茶馆呆的时间。
数据范围:
1<=T<=30
1<=p<=N<=1e5
1<=st<=en<=1e9
样例输入:
2
5 3
1 3
8 12
6 9
14 17
2 7
3 3
1 6
4 7
6 9
样例输出:
Case 1: 1
Case 2: 0
思 路:区间贪心问题。对开始区间排序,对结尾区间排序。然后..看代码。
收 获:又学习了一种区间贪心的姿势。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
typedef long long ll;
const ll INF = (ll)1e18;
ll st[maxn],en[maxn];
ll p,n;
int main(){
int t,Kase=1;
scanf("%d",&t);
while(t--){
scanf("%lld%lld",&n,&p);
for(int i=0;i<n;i++)scanf("%lld %lld",&st[i],&en[i]);
sort(st,st+n);
sort(en,en+n);
ll MIN = INF;
for(int i=p-1;i<n;i++){
ll temp = st[i]-en[i-(p-1)];
if(temp<=0ll){MIN = 0;break;}
else MIN = min(MIN,temp);
}
printf("Case %d: %lld\n",Kase++,MIN);
}
return 0;
}