Submission #1157752

#TimeUsernameProblemLanguageResultExecution timeMemory
1157752Kaztaev_AlisherRoad Construction (JOI21_road_construction)C++20
100 / 100
2172 ms22228 KiB
#include <bits/stdc++.h>

#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(a) a.begin() , a.end()
#define F first
#define S second
#define int ll

using namespace std;
using ll = long long;

const ll N = 250005 , inf = 4e9 + 7;
const ll INF = 1e18 ,   mod = 1e9+7;

int x[N] , y[N] , nxt[N];
int n , k;
int get(int i , int j){
	return abs(x[i]-x[j]) + abs(y[i]-y[j]);
}
void slow(){
	vector<pair<int,int>> v;
	for(int i = 1; i <= n; i++){
		for(int j = i+1; j <= n; j++){
			v.push_back({i,j});
		}
	}
	sort(all(v),[&](pair<int,int> i , pair<int,int> j){
		return (get(i.F , i.S) < get(j.F , j.S));
		
	});
	for(int i = 0; i < k; i++){
		cout << get(v[i].F,v[i].S) << "\n";
	}
}
pair<int,int> p[N];
void k_one(){
	multiset<int> ans;
	for(int i = 1; i <= k; i++) ans.insert(inf);
	for(int i = 1; i <= n; i++){
		p[i] = {x[i] , y[i]};
	} 
	sort(p+1,p+1+n);
	map<pair<int,int> , int> mp;
	set<pair<int,int>> st;
	for(int i = 1; i <= n; i++){
		x[i] = p[i].F; y[i] = p[i].S;
	}
	for(int i = 1 , j = 1; i <= n; i++){
		while(x[i]-x[j] > *ans.rbegin()){
			st.erase({y[j],j});
			j++;
		}
		auto it = st.upper_bound({y[i]-*ans.rbegin(),0});
		while(it != st.end() && it->F < y[i]+*ans.rbegin()){
			ans.insert(get(i,it->S));
			ans.erase(ans.find(*ans.rbegin()));
			it++;
		}
		st.insert({y[i],i});
	}
	for(int x : ans){
		cout << x <<"\n";
	}
}
void only_x(){
	sort(x+1,x+1+n);
	set<pair<int,int>> st;
	for(int i = 1; i <= n; i++){
		nxt[i] = i+1;
		if(nxt[i] <= n){
			st.insert({x[nxt[i]]-x[i] , i});
		}
	}
	while(k--){
		int i = st.begin()->S;
		st.erase(st.begin());
		cout << x[nxt[i]] - x[i] <<"\n";
		nxt[i]++;
		if(nxt[i] <= n) st.insert({x[nxt[i]]-x[i] , i});
	}
}
void solve(){
	cin >> n >> k;
	int cnt = 0;
	for(int i = 1; i <= n; i++){
		cin >> x[i] >> y[i];
		if(y[i] == 0){
			cnt++;
		}
	}
	k_one();
	// if(k <= 10){
	// } else if(cnt == n){
		// only_x();
	// } else {
		// slow();
	// }
}
/*

*/
signed main(){
	ios;
	solve();
}
	
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...