Submission #795713

#TimeUsernameProblemLanguageResultExecution timeMemory
795713someoneVirus Experiment (JOI19_virus)C++14
100 / 100
408 ms132536 KiB
#include <bits/stdc++.h> #define x first #define y second using namespace std; const int N = 2e5 + 42, M = 805, INF = 1e9 + 42; int dl[] = {-1, 0, 1, 0}, dc[] = {0, -1, 0, 1}; char ch[N], w[] = {'N', 'W', 'S', 'E'}; pair<int, int> acces[M][M], same[M][M]; vector<pair<int, int>> cfc[M][M], toAdd[M][M]; int m, row, col, mini = INF, nb = 0, maxi[16], u[M][M]; pair<int, int> getCFC(pair<int, int> coo) { if(same[coo.x][coo.y] == coo) return coo; return same[coo.x][coo.y] = getCFC(same[coo.x][coo.y]); } pair<int, int> getAcces(pair<int, int> coo) { if(acces[coo.x][coo.y] == coo) return coo; return acces[coo.x][coo.y] = getAcces(acces[coo.x][coo.y]); } inline bool valid(int x, int y) { return -1 < x && x < row && -1 < y && y < col; } inline bool hasAcces(int l, int c, int x, int y) { int mask = 0; for(int i = 0; i < 4; i++) { int adjX = l + dl[i], adjY = c + dc[i]; if(valid(adjX, adjY) && getCFC({x, y}) == getCFC({adjX, adjY})) mask += (1 << i); } return u[l][c] <= maxi[mask]; } void solve() { vector<pair<int, int>> candidat; for(int i = 0; i < row; i++) for(int j = 0; j < col; j++) if(u[i][j] != INF) candidat.push_back({i, j}); while(!candidat.empty()) { pair<int, int> cand = candidat.back(); candidat.pop_back(); bool nouv = false; while(!toAdd[cand.x][cand.y].empty()) { pair<int, int> coo = toAdd[cand.x][cand.y].back(); toAdd[cand.x][cand.y].pop_back(); if(valid(coo.x, coo.y)) { for(int i = 0; i < 4 && !nouv; i++) { int x = coo.x + dl[i], y = coo.y + dc[i]; if(valid(x, y) && getCFC(coo) != getCFC({x, y}) && hasAcces(x, y, coo.x, coo.y)) { nouv = true; if(getAcces({x, y}) != cand) { acces[cand.x][cand.y] = getCFC({x, y}); } else { auto act = cand, pre = getCFC({x, y}); if(cfc[act.x][act.y].size() < cfc[pre.x][pre.y].size()) { swap(cfc[pre.x][pre.y], cfc[pre.x][pre.y]); swap(toAdd[pre.x][pre.y], toAdd[pre.x][pre.y]); } for(pair<int, int> cas : cfc[pre.x][pre.y]) { cfc[act.x][act.y].push_back(cas); toAdd[act.x][act.y].push_back(cas); } toAdd[act.x][act.y].push_back(coo); same[pre.x][pre.y] = act; acces[pre.x][pre.y] = act; candidat.push_back(act); } } } } if(nouv) break; } if(!nouv) { int sz = (int)cfc[cand.x][cand.y].size(); if(mini == sz) nb += sz; else if(mini > sz) mini = nb = sz; } } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> m >> row >> col; for(int i = 0; i < m; i++) cin >> ch[i]; for(int i = 0; i < m; i++) ch[i + m] = ch[i]; for(int i = 0; i < 16; i++) { int len = 0; for(int j = 0; j < 2*m; j++) { int wind = 0; for(int k = 0; k < 4; k++) if(w[k] == ch[j] && (i & (1 << k))) wind = 1; if(wind == 0) len = 0; else len++; maxi[i] = max(maxi[i], len); } if(maxi[i] == 2*m) maxi[i] = N; } for(int i = 0; i < row; i++) for(int j = 0; j < col; j++) { cin >> u[i][j]; if(u[i][j] == 0) u[i][j] = INF; cfc[i][j].push_back({i, j}); toAdd[i][j].push_back({i, j}); same[i][j] = acces[i][j] = {i, j}; } solve(); cout << mini << '\n' << nb << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...