Submission #893535

#TimeUsernameProblemLanguageResultExecution timeMemory
893535vjudge1Road Construction (JOI21_road_construction)C++17
0 / 100
971 ms2097152 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define ff first #define ss second #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() //~ #define int long long ///* __ __ __ */ ///* ====== _ /| /| __ _ / | | /| | @ | | | | / /| |\ | / | | @ | / */ ///* \- || |_| |_ / |/ | | | |_ |- | |--| /-| | | \ \ |==| |- /=| | \ | | |--| | |- */ ///* || | | |_ / | |__| _| |_ \__ | | / | |__ | __| | | | \ / | | \| \__ | | | | \ */ ///* typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pii> vii; multiset<int> ans; class Point { public: int x, y; }; int compareX(const void* a, const void* b) { Point *p1 = (Point *)a, *p2 = (Point *)b; return (p1->x - p2->x); } int compareY(const void* a, const void* b) { Point *p1 = (Point *)a, *p2 = (Point *)b; return (p1->y - p2->y); } float dist(Point p1, Point p2) { ans.insert(abs(p1.x - p2.x) + abs(p1.y - p2.y)); if(ans.size() > 300000)ans.erase(ans.find(*--ans.end())); return abs(p1.x - p2.x) + abs(p1.y - p2.y); } float bruteForce(Point P[], int n) { float mn = FLT_MAX; for (int i = 0; i < n; ++i) for (int j = i+1; j < n; ++j) mn = min(mn, dist(P[i], P[j])); return mn; } float min(float x, float y) { return (x < y)? x : y; } float stripClosest(Point strip[], int size, float d) { float mn = d; qsort(strip, size, sizeof(Point), compareY); for (int i = 0; i < size; ++i) for (int j = i+1; j < size && (strip[j].y - strip[i].y) < mn; ++j) mn = min(mn, dist(strip[i], strip[j])); return mn; } float closestUtil(Point P[], int n) { if (n <= 3) return bruteForce(P, n); int mid = n/2; Point midPoint = P[mid]; float dl = closestUtil(P, mid); float dr = closestUtil(P + mid, n - mid); float d = min(dl, dr); Point strip[n]; int j = 0; for (int i = 0; i < n; i++) if (abs(P[i].x - midPoint.x) < d) strip[j] = P[i], j++; return min(d, stripClosest(strip, j, d) ); } float closest(Point P[], int n) { qsort(P, n, sizeof(Point), compareX); return closestUtil(P, n); } void solve(){ int n, k; cin >> n >> k; if(n*n < 2e9){ vii v(n); vi ans2; for(auto &[i,j]:v)cin >> i >> j; for(int i = 0; i < n; i++){ for(int j = i+1; j < n; j++){ ans2.pb(abs(v[i].ff-v[j].ff)+abs(v[i].ss-v[j].ss)); } }sort(all(ans2)); for(int i = 0; i < k; i++)cout << ans2[i] << '\n'; }else{ Point p[n]; for (int i = 0; i < n; i++) { cin >> p[i].x >> p[i].y; } int x = closest(p, n); int cnt = 0; for(auto i:ans){ cout << i << '\n'; cnt++; if(cnt == k)break; } } } main(){ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); int test = 1; //~ cin >> test; while(test--){ solve(); } }

Compilation message (stderr)

road_construction.cpp: In function 'void solve()':
road_construction.cpp:105:7: warning: unused variable 'x' [-Wunused-variable]
  105 |   int x = closest(p, n);
      |       ^
road_construction.cpp: At global scope:
road_construction.cpp:114:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  114 | main(){
      | ^~~~
#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...