★codeforces 612D The Union of k-Segments (思维or类扫描线)
来源:互联网 发布:苹果anywhere软件 编辑:程序博客网 时间:2024/05/16 19:06
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.
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.
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.
3 20 5-3 23 8
20 23 5
3 20 5-3 33 8
10 5
题意:
给定n条线段,给出一个k,定义一个点符合条件的是被至少k条线段覆盖到,要求找出一个最小的线段集合,这个集合能够包含所有满足那个条件的点。
(这里要注意这个线段集合中的线段不需要是之前的线段了,只要由一开始给的n条线段的端点集中的元素构成的就可以了。)
思路:
哇,这题差一点啊。。比赛的时候我也想的是左端点排序,然后是想暴力搞。。其实这题思维很好的,一个计数变量cnt,遇到一个起点就cnt++,终点就cnt--,如果遇到起点cnt变成了k,说明他是所求线段的一个起点,把他扔进答案,如果遇到了一个终点,原来是k,说明他要结束了,把他扔进答案。。哇,这种变量控制++--的我暑假的时候做到过,为什么现在想不出来呢,老了老了。。。
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <vector>using namespace std;const int maxn = 1e6 + 5;vector <pair<int,int> > v;int n, k;int main(){ while(~scanf("%d%d", &n, &k)) { int l, r; v.clear(); for(int i = 1; i <= n; i++) { scanf("%d%d", &l, &r); v.push_back(make_pair(l,-1)); v.push_back(make_pair(r,1)); } sort(v.begin(), v.end()); vector<int> ans; int cnt = 0; for(int i = 0; i < v.size(); i++) { if(v[i].second == -1) { cnt++; if(cnt == k) ans.push_back(v[i].first); } else { if(cnt == k) ans.push_back(v[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;}
- ★codeforces 612D The Union of k-Segments (思维or类扫描线)
- Codeforces 612D The Union of k-Segments 【思维】
- CodeForces 612D The Union of k-Segments(排序+区间计数、扫描线)
- D. The Union of k-Segments(扫描线)
- CodeForces 612 D. The Union of k-Segments(水~)
- D. The Union of k-Segments----扫描线初步
- Educational Codeforces Round 4 D The Union of k-Segments (扫描线)
- Codeforces 612D The Union of k-Segments
- codeforces 612D The Union of k-Segments (sorting)
- CodeForces 612D The Union of k-Segments
- CodeForces 612D - The Union of k-Segments(模拟)
- Codeforces 612D The Union of k-Segments
- codeforces 612D The Union of k-Segments (前缀和处理区间)
- Codeforces 612D Educational Codeforces Round 4 D. The Union of k-Segments 离散化+map
- codeforces 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
- 【codefores 612】 The Union of k-Segments 【思维+ 线段处理】
- 欢迎使用CSDN-markdown编辑器
- js传值到后台,后台发生乱码问题
- Swift
- pandas之索引、选取和过滤
- 【精华】十二大最著名的Android播放器开源项目
- ★codeforces 612D The Union of k-Segments (思维or类扫描线)
- 在Linux系统下,重启Tomcat使用命令操作的
- mycat后台连接调用关系
- python中关于类的理解
- 易飞去除批处理删除历史单据权限
- [UE4]后期处理(Post Processing)相关的官方文档
- 【深度学习】VGGNet的tensorflow实现code
- 培养创造力的10个注意点
- 微信开发后台处理消息时使用反射,去掉繁琐的if判断