Submission #925402

#TimeUsernameProblemLanguageResultExecution timeMemory
925402esomerGarden (JOI23_garden)C++17
75 / 100
3035 ms11800 KiB
#include <bits/stdc++.h> using namespace std; struct DSU{ vector<int> v, next, prev; vector<bool> zero; int mx; void init(int n){ v.assign(n, -1); mx = 0; zero.assign(n, false); next.resize(n); prev.resize(n); for(int i = 0; i < n; i++){ next[i] = (i+1)%n; prev[i] = (i+n-1)%n; } } int get(int x){return v[x] < 0 ? x : v[x] = get(v[x]);} void unite(int x, int y){ x = get(x); y = get(y); if(x == y) return; if(v[x] > v[y]) swap(x, y); v[x] += v[y]; v[y] = x; mx = max(mx, -v[x]); } void setZero(int i){ zero[i] = true; mx = max(mx, 1); if(zero[prev[i]]) unite(i, prev[i]); if(zero[next[i]]) unite(i, next[i]); } }; signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); int N, M, D; cin >> N >> M >> D; vector<pair<int, int>> A(N); for(int i = 0; i < N; i++) cin >> A[i].first >> A[i].second; vector<pair<int, int>> B(M); for(int i = 0; i < M; i++) cin >> B[i].first >> B[i].second; vector<int> cols(D, 0); vector<vector<int>> rows(2*D); for(int i = 0; i < N; i++){ cols[A[i].first]++; rows[A[i].second].push_back(i); rows[A[i].second+D].push_back(i); } for(int i = 0; i < M; i++){ cols[B[i].first]++; rows[B[i].second].push_back(N+i); rows[B[i].second+D].push_back(N+i); } int ans = D * D; DSU initial; initial.init(D); for(int i = 0; i < D; i++){ if(cols[i] == 0) initial.setZero(i); } for(int startRow = 0; startRow < D; startRow++){ int lft = N; vector<int> col = cols; DSU UF = initial; for(int r = startRow; r < startRow + D; r++){ for(int i : rows[r]){ if(i < N){ lft--; }else{ i -= N; col[B[i].first]--; if(col[B[i].first] == 0) UF.setZero(B[i].first); } } if(lft > 0) continue; ans = min(ans, (r - startRow + 1) * (D - UF.mx)); } } cout << ans << "\n"; }
#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...