当前位置: 代码迷 >> 综合 >> 排序 Codeforces612D The Union of k-Segments
  详细解决方案

排序 Codeforces612D The Union of k-Segments

热度:76   发布时间:2023-12-14 03:31:00.0

传送门:点击打开链接

题意:告诉你很多条线段,从小到大输出所有的区间使得区间里面所有的点都被覆盖了至少k次,输出的区间数量越小越好。

思路:先把左端点记为1,右端点记为-1,然后把端点合在一起排序,然后一顿乱搞。

#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck(x) cout<<"["<<x<<"]"
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int>PII;const int MX = 2e6 + 5;
const int INF = 0x3f3f3f3f;struct Data {int id, x;bool operator<(const Data &P) const {if(id == P.id) return x > P.x;return id < P.id;}
} A[MX];int n, k;
void solve() {vector<PII>ans;for(int i = 1; i <= n; i++) {int l, r;scanf("%d%d", &l, &r);A[i].id = l; A[i].x = 1;A[i + n].id = r; A[i + n].x = -1;}sort(A + 1, A + 1 + 2 * n);int l = INF, r, now = 0;for(int c = 1; c <= 2 * n; c++) {if(now >= k) r = A[c].id;now += A[c].x;while(c < 2 * n && A[c + 1].id == A[c].id && A[c + 1].x == A[c].x) {now += A[++c].x;}if(A[c].x > 0) {if(l == INF && now >= k) l = A[c].id;if(now >= k) r = A[c].id;while(c < 2 * n && A[c + 1].id == A[c].id) {now += A[++c].x;}}if(now < k) {if(A[c].x < 0 && l != INF && now < k) {ans.push_back(PII(l, r));l = INF;}}}printf("%d\n", (int)ans.size());for(int i = 0; i < (int)ans.size(); i++) {printf("%d %d\n", ans[i].first, ans[i].second);}
}int main() {//FIN;while(~scanf("%d%d", &n, &k)) {solve();}return 0;
}


  相关解决方案