★codeforces 612D The Union of k-Segments (思维or类扫描线)
2017-05-17 13:44
344 查看
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.
Examples
input
output
input
output
题意:
给定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;
}
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.
Examples
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
题意:
给定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 731D】【差分+线段扫描 思维题】80-th Level Archeology【给你n个word,现在要使得word按字典序排列,随便操作多少次,每次可以使每个word+1】
- Codeforces 612D The Union of k-Segments 【思维】
- CodeForces 612D The Union of k-Segments(排序+区间计数、扫描线)
- The Union of k-Segments CodeForces - 612D (思维)
- Codeforces 901C. Bipartite Segments(思维题)
- Codeforces - 612D. The Union of k-Segments 排序+思维
- Codeforces 610D Vika and Segments 【线段树扫描线 求面积并】
- Codeforces 846C Four Segments【思维+预处理+前缀和枚举】
- CodeForces - 427A (警察和罪犯 思维题)
- CodeForces - 734D Anton and Chess (思维题)
- Codeforces 371C - Sonya and Queries(思维)
- Codeforces Round #210 (Div. 2) C. Levko and Array Recovery && Codeforces 360 A 详解(思维+维护理论值)
- codeforces 776c Molly's Chemicals 【思维】
- CodeForces - 750C(思维)
- CodeForces - 814B(思维)
- CodeForces - 844C Sorting by Subsequences (排序+思维)
- E - Toda 2 CodeForces - 730A(简单思维和处理技巧)
- codeforces-486【C-贪心-思维】【D-树状DP】
- C - Matrix CodeForces - 365C 思维
- Codeforces 722C(并查集 + 思维)