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>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
//#define endl "\n"
const ll maxn=1e6+10;
const ll mod =998244353 ;
const ll base=3e18;
ll a[812][812];
ll ax[5]= {-1,0,1,0};
ll ay[5]= {0,1,0,-1};
ll msk[17];
ll cntnw=0;
ll chk[812][812];
ll p,n, m;
ll ans[812][812];
bool dd[maxn];
ll hsh(ll x,ll y)
{
return (x-1)*m+y;
}
ll par[maxn];
ll find_par(ll u)
{
if (par[u]<0)
return u;
return par[u]=find_par(par[u]);
}
void dsu(ll x,ll y)
{
x=find_par(x);
y=find_par(y);
if (dd[y])
{
assert(1);
dd[x]=1;
return ;
}
assert(x!=y);
par[x]=y;
}
bool dosth(ll x,ll y)
{
vector<pll> vt;
vt.pb(make_pair(x,y));
chk[x][y]=cntnw;
for (int i=0; i<vt.size(); i++)
{
auto p=vt[i];
int x=p.ff;
int y=p.ss;
for (int j=0; j<4; j++)
{
int x1=x+ax[j];
int y1=y+ay[j];
if (x1>=1&&x1<=n&&y1>=1&&y1<=m&&chk[x1][y1]!=cntnw&&a[x1][y1])
{
ll nw=0;
for (int k=0; k<4; k++)
{
ll x2=x1+ax[k];
ll y2=y1+ay[k];
if (a[x2][y2]&&x2>=1&&x2<=n&&y2>=1&&y2<=m&&chk[x2][y2]==cntnw)
nw+=(1ll<<k);
}
if (a[x1][y1]<=msk[nw])
{
if (find_par(hsh(x1,y1))!=find_par(hsh(x,y)))
{
dsu(find_par(hsh(x,y)),find_par(hsh(x1,y1)));
return true;
}
chk[x1][y1]=cntnw;
vt.pb(make_pair(x1,y1));
}
}
}
}
dd[hsh(x,y)]=1;
for (auto to:vt)
{
ans[to.ff][to.ss]=vt.size();
}
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen("t.inp", "r"))
{
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
memset(par,-1,sizeof(par));
memset(ans,0x3f,sizeof(ans));
cin>>p>> n>> m;
string s;
cin>> s;
for (int i=0; i<p; i++)
{
if (s[i]=='N')
s[i]='0';
else if (s[i]=='E')
s[i]='1';
else if (s[i]=='S')
s[i]='2';
else
s[i]='3';
}
for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++)
{
cin>>a[i][j];
}
}
p+=p;
s+=s;
for (int i=1; i<16; i++)
{
ll pre=-1;
for (int j=0; j<p; j++)
{
ll t=s[j]-'0';
// cout <<t<<endl;
if (i&(1ll<<t))
msk[i]=max(msk[i],j-pre);
else
pre=j;
}
if (msk[i]==p)
msk[i]=base;
// cout<<msk[i]<<" ";
}
//1 2 3 2 2 4 5 1 2 2 4 3 4 5 3000000000000000000
while (1)
{
bool chk=0;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++)
{
if (a[i][j]&&!dd[hsh(i,j)]&&find_par(hsh(i,j))==hsh(i,j))
{
cntnw++;
chk|=dosth(i,j);
}
}
}
if (!chk)
break;
}
pll res=make_pair(base,0);
for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++)
{
if (!a[i][j])
continue;
if (res.ff>ans[i][j])
{
res.ff=ans[i][j];
res.ss=1;
}
else if (res.ff==ans[i][j])
res.ss++;
}
}
cout <<res.ff<<endl;
cout <<res.ss<<endl;
}
Compilation message (stderr)
virus.cpp: In function 'bool dosth(long long int, long long int)':
virus.cpp:52:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i=0; i<vt.size(); i++)
| ~^~~~~~~~~~
virus.cpp: In function 'int main()':
virus.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
virus.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |