题意:给定n个区间,问你被覆盖至少k次的区间(两端连续区间可以合并)最少有多少个,并输出。
思路:某个区间被覆盖至少k次,意味着在它前面的区间起点至少有k个且这些区间的终点不能出现在它前面。这样sort下,直接统计,遇到起点加一,终点减一,每k个一记录就行了。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (2000000+10)
#define MAXM (200000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
using namespace std;
struct Node{
int op, val;
};
Node num[MAXN];
bool cmp(Node a, Node b){
if(a.val != b.val)
return a.val < b.val;
else
return a.op > b.op;
}
int ans[MAXN];
int main()
{
int n, k;
Ri(n); Ri(k); int top = 0;
for(int i = 0; i < n; i++)
{
int s, t;
Ri(s); Ri(t);
num[top].val = s;
num[top++].op = 1;
num[top].val = t;
num[top++].op = 0;
}
sort(num, num+top, cmp);
int cnt = 0; int have = 0;
for(int i = 0; i < top; i++)
{
if(num[i].op)
{
have++;
if(have == k) ans[cnt++] = num[i].val;
}
else
{
if(have == k) ans[cnt++] = num[i].val;
have--;
}
}
Pi(cnt / 2);
for(int i = 0; i < cnt; i+=2)
printf("%d %d\n", ans[i], ans[i+1]);
return 0;
}