codeForces 612D(思维)
来源:互联网 发布:nba数据查询器 编辑:程序博客网 时间:2024/05/29 03:08
转载自:http://blog.csdn.net/qq_34374664/article/details/72377345
题意:
给出n条线段和k,然后输出被这些线段经过了至少k次的点形成的线段。
思路:
一个计数变量cnt,遇到一个起点就cnt++,终点就cnt--,如果遇到起点cnt变成了k,说明他是所求线段的一个起点,把他扔进答案,如果遇到了一个终点,原来是k,说明他要结束了,把他扔进答案。
Code:
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e6 + 10;typedef pair<int,int> P;int n,k;vector<P>v,ans;int main(){ while( ~ scanf("%d%d",&n,&k)) { v.clear(); ans.clear(); for(int i = 1; i <= n; i ++) { int l,r; scanf("%d%d",&l,&r); v.push_back(make_pair(l,-1)); v.push_back(make_pair(r,1)); } sort(v.begin(),v.end()); int cnt = 0; int x,y; for(int i = 0; i < v.size(); i ++) { P t = v[i]; if(t.second == -1) { cnt ++; if(cnt == k) x = t.first; } else { if(cnt == k) ans.push_back(make_pair(x,t.first)); cnt --; } } cout << ans.size() << endl; for(int i = 0; i < ans.size(); i ++) cout << ans[i].first << " " << ans[i].second << endl; } return 0;}
阅读全文
0 0
- codeForces 612D(思维)
- codeforces 765D 思维
- codeforces 673D(思维)
- codeforces 724D 思维
- CodeForces 672D (思维+二分)
- Codeforces 612D The Union of k-Segments 【思维】
- codeforces 451 D.Count Good Substrings (思维)
- CodeForces 66D--思维(yy)
- Codeforces 660D Number of Parallelograms 【思维】
- [思维] codeforces 665D. Simple Subset
- Codeforces 617D Polyline【思维+分类讨论】
- Codeforces--486D-----Valid Sets思维
- Codeforces 711D Directed Roads【Dfs+思维】
- CodeForces 405D Toy Sum【思维】
- codeforces 738D Sea Battle 思维+构造
- 【Codeforces 729D Sea Battle】+ 简单思维
- Codeforces 353D Queue【思维】好题~
- Codeforces 675D Tree Construction【思维+set】
- Python 开发实用的库
- spring boot干货
- js中小数和百分数互化
- Springmvc的常用注解
- js判断手机型号
- codeForces 612D(思维)
- RCNN学习笔记(1):Rich feature hierarchies for accurate object detection and semantic segmentation
- MFC导出对话框类(带资源)
- 4412 ADC驱动示例
- HDU 2196 Computer 【树的直径】
- Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) C DNA Evolution
- 图像的数学表示
- android Java 反射多出来几个字段
- Struts2框架03