题目链接:http://codeforces.com/contest/612/problem/D
解析:每条线段加个编号,开始为0,结束为1,把所有端点存起来,然后从小到大排序,ans存覆盖k次的,最后输出
代码:
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
vector<P> mp;
vector<int> ans;
int main()
{
int n, k;
int l, r;
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &l, &r);
mp.push_back(make_pair(l, 0));
mp.push_back(make_pair(r, 1));
}
sort(mp.begin(), mp.end());
int cnt = 0;
for(int i = 0; i < mp.size(); i++)
{
if(mp[i].second == 0)
{
cnt++;
if(cnt == k) ans.push_back(mp[i].first);
}
else
{
if(cnt == k) ans.push_back(mp[i].first);
cnt--;
}
}
printf("%d\n", ans.size()/2);
for(int i = 0; i < ans.size(); i += 2) printf("%d %d\n", ans[i], ans[i+1]);
return 0;
}