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个一记录就行了
题意:给定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; }
相关文章推荐
- Educational Codeforces Round 4 D. The Union of k-Segments 排序
- Educational Codeforces Round 4 D The Union of k-Segments (扫描线)
- 【扫描线】Educational Codeforces Round 4 D. The Union of k-Segments
- Educational Codeforces Round 4-D. The Union of k-Segments
- Educational Codeforces Round 4 D. The Union of k-Segments(★)
- codeforces D. The Union of k-Segments 排序
- Educational Codeforces Round 4 D. The Union of k-Segments 排序
- Educational Codeforces Round 9 C. The Smallest String Concatenation(字符串排序)
- Educational Codeforces Round 22 C. The Tag Game(思维)
- (Educational Codeforces Round 9 )The Smallest String Concatenation(排序)
- Educational Codeforces Round 9-C. The Smallest String Concatenation(字符串排序)
- Educational Codeforces Round 26 G. Functions On The Segments 主席树
- Educational Codeforces Round 9 C. The Smallest String Concatenation 排序
- Codeforce 题目612 D The Union of k-Segments(暴力,排序,技巧)
- 【codefores 612】 The Union of k-Segments 【思维+ 线段处理】
- Educational Codeforces Round 5 E. Sum of Remainders 思维 数学转换
- [Educational Codeforces Round 7]F. The Sum of the k-th Powers
- Educational Codeforces Round 22 C. The Tag Game(思维 搜索)
- Codeforces Round #428 (Div. 2) B. Game of the Rows (思维)
- Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法