612D - The Union of k-Segments - CodeForces Solution


greedy sortings *1800

Please click on ads to support us..

Python Code:

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

n, k = map(int, input().split())
event = []
for i in range(n):
    l, r = map(int, input().split())
    event.append(2*l)
    event.append(2*r+1)

event.sort()
cur = 0
ans = []
for x in event:
    if x%2 == 0:
        if cur+1 == k and cur < k:
            start = x//2
        cur += 1
    else:
        if cur == k and cur-1 < k:
            ans.append((start, x//2))
        cur -= 1
print(len(ans))
for l, r in ans:
    print(l, r)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

using ll = long long;

template<typename T> void min_self( T &a, const T b ) { if(b < a) a = b; }
template<typename T> void max_self( T &a, const T b ) { if(b > a) a = b; }

const int inf = (int)1e9 + 7;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n, k;
	cin >> n >> k;
	
	vector<array<int, 2>> p(n);
	vector<pair<int, int>> events;
	
	for(auto &[x, y] : p){
		cin >> x >> y;
		events.emplace_back(x, 1);
		events.emplace_back(y, -1);
	}
	
	#define pii pair<int, int>
	sort(events.begin(), events.end(), [&](pii a, pii b){
		if(a.first == b.first)
			return a.second > b.second;
		return a.first < b.first;
	});

	vector<pair<int, int>> segs;
	
	int m = (int)events.size();
	int c = 0;
	int who = inf;
	
	for(int i = 0;i < m;i++){
		c += events[i].second;
		if(c == k - 1 && events[i].second == -1){
			assert(who != inf);
			segs.emplace_back(who, events[i].first);
			who = inf;
		}else if(c >= k){
			if(who == inf) who = events[i].first;
		}
	}
	
	cout << (int)segs.size() << "\n";
	for(auto [X, Y] : segs){
		cout << X << " " << Y << "\n";
	}

	return 0x0;
}


Comments

Submit
0 Comments
More Questions

1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility
20B - Equation
600B - Queries about less or equal elements
1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping