Gym - 101864L School Reunion

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值