This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
int R, C;
int n, m;
vector<vector<int>> u;
vector<int> dsu, sz;
vector<int> lst;
vector<int> ans;
vector<int> lim;
vector<vector<int>> vst;
int tme = 0, ts = 0;
int mnans = INT_MAX;
vector<char> cs = {'N', 'S', 'W', 'E'};
vector<pii> dir = {mp(-1, 0), mp(1, 0), mp(0, -1), mp(0, 1)};
vector<int> cid;
void init(){
n = R * C;
u.resize(R, vector<int>(C));
dsu.resize(n);
sz.resize(n, 1);
iota(iter(dsu), 0);
lst.resize(n);
ans.resize(n, -1);
lim.resize(16);
cid.resize(26);
vst.resize(R, vector<int>(C));
for(int i = 0; i < 4; i++) cid[cs[i] - 'A'] = i;
}
int findDSU(int a){
if(dsu[a] != a) dsu[a] = findDSU(dsu[a]);
return dsu[a];
}
void unionDSU(int a, int b){
a = findDSU(a);
b = findDSU(b);
if(a == b) return;
//cerr << "union " << a / C << ',' << a % C << " " << b / C << ',' << b % C << " " << tme << "\n";
dsu[b] = dsu[a];
sz[a] += sz[b];
lst[a] = tme;
}
int getid(int x, int y){
if(x < 0 || R <= x || y < 0 || C <= y) return -1;
return x * C + y;
}
bool check(int x, int y){
if(u[x][y] == 0) return false;
int tmp = 0;
for(int i = 0; i < 4; i++){
int nx = x + dir[i].F, ny = y + dir[i].S;
int id = getid(nx, ny);
if(id == -1) continue;
if(vst[nx][ny] == ts) tmp |= 1 << i;
}
return lim[tmp] >= u[x][y];
}
bool bfs(int sx, int sy){
ts++;
bool ok = false;
int cnt = 0;
int sid = getid(sx, sy);
queue<pii> q;
vst[sx][sy] = ts;
q.push(mp(sx, sy));
while(!q.empty()){
int x = q.front().F, y = q.front().S;
q.pop();
int id = getid(x, y);
cnt++;
if(findDSU(sid) != findDSU(id)){
unionDSU(id, sid);
ok = true;
break;
}
for(auto [dx, dy] : dir){
int nx = x + dx, ny = y + dy;
int nid = getid(nx, ny);
if(nid == -1) continue;
if(vst[nx][ny] == ts) continue;
if(!check(nx, ny)) continue;
vst[nx][ny] = ts;
q.push(mp(nx, ny));
}
}
if(!ok){
ans[findDSU(sid)] = cnt;
//cerr << "ans " << sx << " " << sy << " : " << cnt << "\n";
if(cnt < mnans){
mnans = cnt;
}
}
return ok;
}
int main(){
StarBurstStream
cin >> m >> R >> C;
string s;
cin >> s;
for(int i = 0; i < m; i++) s += s[i];
init();
for(int i = 0; i < R; i++){
for(int j = 0; j < C; j++) cin >> u[i][j];
}
for(int i = 0; i < 16; i++){
int now = 0;
int mx = 0;
for(int j = 0; j < 2 * m; j++){
int id = cid[s[j] - 'A'];
if(!(1 << id & i)){
now = 0;
continue;
}
now++;
mx = max(mx, now);
}
if(mx == 2 * m) mx = MAX;
lim[i] = mx;
//cerr << bitset<4>(i) << ": " << lim[i] << "\n";
}
while(true){
tme++;
//cerr << "round " << tme << "\n";
bool ok = false;
for(int i = 0; i < R; i++){
for(int j = 0; j < C; j++){
if(u[i][j] == 0) continue;
int id = getid(i, j);
if(id != findDSU(id)) continue;
if(ans[id] != -1) continue;
if(lst[id] == tme) continue;
//cerr << "check " << i << " " << j << " " << lst[id] << " " << tme << "\n";
if(bfs(i, j)) ok = true;
}
}
//cerr << "map:\n";
for(int i = 0; i < R; i++){
for(int j = 0; j < C; j++){
int t = findDSU(getid(i, j));
//cerr << t / C << "," << t % C << " ";
}
//cerr << "\n";
}
if(!ok) break;
}
int anscnt = 0;
for(int i = 0; i < R; i++){
for(int j = 0; j < C; j++){
int id = getid(i, j);
if(findDSU(id) != id) continue;
if(ans[id] != mnans) continue;
anscnt += min(mnans, sz[id]);
}
}
cout << mnans << "\n" << anscnt << "\n";
return 0;
}
Compilation message (stderr)
virus.cpp: In function 'bool bfs(int, int)':
virus.cpp:142:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
142 | for(auto [dx, dy] : dir){
| ^
virus.cpp: In function 'int main()':
virus.cpp:209:21: warning: unused variable 't' [-Wunused-variable]
209 | int t = findDSU(getid(i, j));
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |