2018 BACS Contest Replay L - School Reunion (贪心)

原题地址:http://codeforces.com/gym/101864/attachments

题意:给出n个线段的覆盖范围,让你求最短的线段长度使得满足在这个范围内被覆盖了m次及以上.

思路:见代码

#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <cctype>
#define eps 1e-8
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define CLR(x,y) memset((x),y,sizeof(x))
#define fuck(x) cerr << #x << "=" << x << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int seed=131;
const int maxn=1e5+5;
const int mod = 998244353;
int t,n,m;
int x[maxn],y[maxn];
int main() {
    scanf("%d",&t);
    for(int cas=1; cas<=t; cas++) {
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++) {
            scanf("%d%d",&x[i],&y[i]);
        }
        sort(x+1,x+1+n);
        sort(y+1,y+1+n);
        int MIN=INF;
        /*
        考虑贪心的解法,要想某一个区间覆盖m次,那么在分别按照左端点和右端点排序之后,
        最优解一定来自于第i个区间的左端点值减去第i-m+1个区间的右端点值
        */
        for(int i=m; i<=n; i++) {
            MIN=min(MIN,x[i]-y[i-m+1]);
        }
        MIN=max(0,MIN);
        printf("Case %d: %d\n",cas,MIN);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yiqzq/article/details/81636380