您的位置:首页 > 其它

Educational Codeforces Round 4 D. The Union of k-Segments 排序,思维

2017-03-30 14:28 369 查看
题目链接:http://codeforces.com/contest/612/problem/D

题意:给定n个区间,问你被覆盖至少k次的区间(两端连续区间可以合并)最少有多少个,并输出

解法:某个区间被覆盖至少k次,意味着在它前面的区间起点至少有k个且这些区间的终点不能出现在它前面。这样sort下,直接统计,遇到起点加一,终点减一,每k个一记录就行了

//CF 612D

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000010;

struct node{
int x, op;
node(){}
node(int x, int op) : x(x), op(op){}
}s[maxn];

bool cmp(node a, node b){
if(a.x == b.x) return a.op > b.op;
return a.x < b.x;
}

int ans[maxn];

int main()
{
int n, k;
scanf("%d%d", &n, &k);
int cnt = 0;
for(int i = 0; i < n; i++){
int u, v;
scanf("%d%d", &u, &v);
s[cnt].x = u;
s[cnt++].op = 1;
s[cnt].x = v;
s[cnt++].op = 0;
}
sort(s, s+cnt, cmp);
int tot = 0;
int temp = 0;
for(int i = 0; i < cnt; i++){
if(s[i].op){
temp++;
if(temp == k) ans[tot++] = s[i].x;
}
else{
if(temp == k) ans[tot++] = s[i].x;
temp--;
}
}
printf("%d\n", tot/2);
for(int i = 0; i < tot; i += 2){
printf("%d %d\n", ans[i], ans[i+1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: