Submission #553880

#TimeUsernameProblemLanguageResultExecution timeMemory
553880topovikFountain Parks (IOI21_parks)C++17
100 / 100
1041 ms77484 KiB
#include <bits/stdc++.h> #include "parks.h" #define pb push_back #define f first #define s second #define pi acos(-1) using namespace std; typedef long long ll; typedef long double ld; const ll oo = 1e9; const ld eps = (ld)1 / oo; const ll N = 2e5 + 100; const ll M = 1e6; const int step[4][2] = {{0, -2}, {0, 2}, {-2, 0}, {2, 0}}; vector <pair<int, int> > ans; map <int, int> mp[N]; map <int, pair<int, int> > mp1[N]; int pred[N]; //void build(vector <int> u, vector <int> v, vector <int> a, vector <int> b) //{ // for (int i = 0; i < u.size(); i++) cout << u[i] << " " << v[i] << " " << a[i] << " " << b[i] << endl; //} int get(int x) { return ((pred[x] == x) ? x : (pred[x] = get(pred[x]))); } void unite(int x, int y) { pred[x] = y; } int construct_roads (vector<int> x, vector<int> y) { std::vector<int> u, v, a, b; int n = x.size(); vector <pair<int, int> > st; for (int i = 0; i < n; i++) mp[x[i]][y[i]] = i + 1; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { int x1 = x[i] + step[j][0]; int y1 = y[i] + step[j][1]; if (mp[x1][y1] - 1 > i) { int nx = mp[x1][y1]; int x2 = (x[i] + x1) / 2; int y2 = (y[i] + y1) / 2; mp1[x2][y2] = {i + 1, nx}; for (int c = 0; c < 4; c++) { int x3 = x2 + step[c][0] / 2; int y3 = y2 + step[c][1] / 2; if ((x3 & 1) && (y3 & 1)) st.pb({x3, y3}); } } } } sort(st.begin(), st.end()); for (int i = 0; i < n; i++) pred[i] = i; int kol = n - 1; for (int p = 0; p < st.size() && kol > 0; p++) { pair<int, int> c = st[p]; if (p > 0 && c == st[p - 1]) continue; int x = c.f, y = c.s; for (int i = (((x + y) / 2) & 1) * 2; i < (((x + y) / 2) & 1) * 2 + 2; i++) { int x1 = x + step[i][0] / 2; int y1 = y + step[i][1] / 2; if (mp1[x1].find(y1) != mp1[x1].end()) { c = mp1[x1][y1]; c.f--, c.s--; if (get(c.f) != get(c.s)) { unite(get(c.f), get(c.s)); u.pb(c.f); v.pb(c.s); a.pb(x); b.pb(y); kol--; break; } } } } if (kol == 0) { build(u, v, a, b); return 1; } return 0; } //int main() //{ // int n; // cin >> n; // vector <int> x(n), y(n); // for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; // cout << construct_roads(x, y) << endl; //} /* 1 4 -100000 -100000 100000 -100000 -100000 100000 -100000 -100000 100000 -100000 -100000 100000 -100000 -100000 100000 -100000 -100000 100000 -100000 -100000 100000 -100000 -100000 100000 */

Compilation message (stderr)

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:69:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |     for (int p = 0; p < st.size() && kol > 0; p++)
      |                     ~~^~~~~~~~~~~
#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...