Submission #1157736

#TimeUsernameProblemLanguageResultExecution timeMemory
1157736Kaztaev_AlisherRoad Construction (JOI21_road_construction)C++20
38 / 100
2490 ms2103432 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(){ 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; while(k--){ int ans = inf; int X = 0 , Y = 0; 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){ st.erase({y[j],j}); j++; } auto it = st.upper_bound({y[i]-ans,0}); while(it != st.end() && it->F < y[i]+ans){ if(get(i,it->S) < ans && mp.count({i,it->S}) == 0){ ans = get(i,it->S); X = i , Y = it->S; } it++; } st.insert({y[i],i}); } mp[{X,Y}] = 1; // cout << X <<" " << Y << "\n"; cout << ans << "\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++; } } if(k <= 10){ k_one(); } 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...