Codeforces - 612D. The Union of k-Segments 排序+思维
2017-02-23 22:05
513 查看
1.题目描述:
D. The Union of k-Segments
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given n segments on the coordinate axis Ox and
the number k. The point is satisfied if it belongs to at least k segments.
Find the smallest (by the number of segments) set of segments on the coordinate axis Ox which contains all satisfied points
and no others.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 106)
— the number of segments and the value of k.
The next n lines contain two integers li, ri ( - 109 ≤ li ≤ ri ≤ 109)
each — the endpoints of the i-th segment. The segments can degenerate and intersect each other. The segments are given in arbitrary
order.
Output
First line contains integer m — the smallest number of segments.
Next m lines contain two integers aj, bj (aj ≤ bj)
— the ends of j-th segment in the answer. The segments should be listed in the order from left to right.
Sample test(s)
input
output
input
output
2.题目大意:
给定n个区间,问你被覆盖至少k次的区间(两端连续区间可以合并)最少有多少个,并输出。
3.解题思路:
某个区间被覆盖至少k次,意味着在它前面的区间起点至少有k个且这些区间的终点不能出现在它前面。这样sort下,直接统计,遇到起点加一,终点减一,每k个一记录就行了。
4.AC代码:
D. The Union of k-Segments
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given n segments on the coordinate axis Ox and
the number k. The point is satisfied if it belongs to at least k segments.
Find the smallest (by the number of segments) set of segments on the coordinate axis Ox which contains all satisfied points
and no others.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 106)
— the number of segments and the value of k.
The next n lines contain two integers li, ri ( - 109 ≤ li ≤ ri ≤ 109)
each — the endpoints of the i-th segment. The segments can degenerate and intersect each other. The segments are given in arbitrary
order.
Output
First line contains integer m — the smallest number of segments.
Next m lines contain two integers aj, bj (aj ≤ bj)
— the ends of j-th segment in the answer. The segments should be listed in the order from left to right.
Sample test(s)
input
3 2 0 5 -3 2 3 8
output
2 0 2 3 5
input
3 2 0 5 -3 3 3 8
output
1 0 5
2.题目大意:
给定n个区间,问你被覆盖至少k次的区间(两端连续区间可以合并)最少有多少个,并输出。
3.解题思路:
某个区间被覆盖至少k次,意味着在它前面的区间起点至少有k个且这些区间的终点不能出现在它前面。这样sort下,直接统计,遇到起点加一,终点减一,每k个一记录就行了。
4.AC代码:
#include <cstdio> #include <algorithm> #define INF 0x3f3f3f3f #define maxn 10100000 using namespace std; struct node { int p, val; }num[maxn]; int cmp(node a, node b) { if (a.val != b.val) return a.val < b.val; return a.p > b.p; } int ans[maxn]; int main() { int n, k, top, cnt, flag; while (scanf("%d%d", &n, &k) != EOF) { top = 0; cnt = 0; flag = 0; for (int i = 0; i < n; i++) { int sta, ed; scanf("%d%d", &sta, &ed); num[top].val = sta; num[top++].p = 1; num[top].val = ed; num[top++].p = 0; } sort(num, num + top, cmp); for (int i = 0; i < top; i++) { if (num[i].p) { flag++; if (flag == k) ans[cnt++] = num[i].val; } else { if (flag == k) ans[cnt++] = num[i].val; flag--; } } printf("%d\n", cnt / 2); for (int i = 0; i < cnt; i += 2) printf("%d %d\n", ans[i], ans[i + 1]); } return 0; }
相关文章推荐
- CodeForces 22D Segments 排序水题
- The Union of k-Segments CodeForces - 612D (思维)
- Codeforces 612D The Union of k-Segments 【思维】
- Educational Codeforces Round 4 D. The Union of k-Segments 排序,思维
- CodeForces - 844C Sorting by Subsequences (排序+思维)
- Codeforces 901C. Bipartite Segments(思维题)
- CodeForces 22D Segments 排序水问题
- CodeForces - 439D Devu and his Brother 思维+排序
- CodeForces 612D The Union of k-Segments(排序+区间计数、扫描线)
- Codeforces 846C Four Segments【思维+预处理+前缀和枚举】
- Codeforces 593B Anton and Lines 【思维】【枚举】【排序】
- codeforces_612D. The Union of k-Segments(排序)
- ★codeforces 612D The Union of k-Segments (思维or类扫描线)
- Codeforces 160C Find Pair【思维+排序】
- Dragons(CodeForces 230A)(贪心和排序)
- Queue CodeForces - 141C (思维脑洞题)
- CodeForces 714A 思维题 C
- 【CodeForces 572B】Order Book(排序)
- Codeforces 652B z-sort 【思维】
- codeforces 590A A. Median Smoothing(思维)