D. The Union of k-Segments(扫描线)
来源:互联网 发布:js 强制转换字符串 编辑:程序博客网 时间:2024/06/04 19:02
题目链接: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;}
阅读全文
0 0
- D. The Union of k-Segments(扫描线)
- D. The Union of k-Segments----扫描线初步
- ★codeforces 612D The Union of k-Segments (思维or类扫描线)
- CodeForces 612D The Union of k-Segments(排序+区间计数、扫描线)
- Educational Codeforces Round 4 D The Union of k-Segments (扫描线)
- CodeForces 612 D. The Union of k-Segments(水~)
- cf612D-The Union of k-Segments(思路题(类似扫描线))
- codeforces D. 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 (sorting)
- Educational Codeforces Round 4 D. The Union of k-Segments
- CodeForces 612D The Union of k-Segments
- CodeForces 612D - The Union of k-Segments(模拟)
- Educational Codeforces Round 4-D. The Union of k-Segments
- Codeforces 612D The Union of k-Segments
- Codeforce 题目612 D The Union of k-Segments(暴力,排序,技巧)
- codeforces 612D The Union of k-Segments (前缀和处理区间)
- js选中checkbox
- Java中判断字符串是否为数字的五种方法
- 常用Android studio调用快捷键
- https://maven.google.com连接不上的解决办法
- shell脚本实现关闭指定程序名的进程
- D. The Union of k-Segments(扫描线)
- TSUNG学习教程 运行环境安装(一)
- http://zthdd.bokee.com/6189963.html
- 机器学习书籍课程推荐
- HDU-1873 看病要排队(队列模拟)
- ios webview webViewDidFinishLoad 执行多次
- 华为面试题
- guided flter 资料汇总
- 一分钟教会你理解思维导图如何做的问题