이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/* in the name of allah */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define mk make_pair
#define int long long
const int N=1e6+7;
int mn,t,M,ty,x[N],y[N];
vector<int> z[N],a[N],b[N],yek[N],vis[N];
int dx[4]={-2,2,0,0};
int dy[4]={0,0,-2,2};
int sumz(int x,int y){
int ans=0;
for(int i=-1;i<=1;i++)
for(int j=-1;j<=1;j++)
ans+=z[x+i][y+j];
return ans;
}
void dfs(int x,int y){
vis[x][y]=1;M++;ty+=yek[x][y];
mn=min(mn,min(a[x][y+1],min(a[x][y-1],min(a[x+1][y],a[x-1][y]))));
b[x][y]=1,b[x-1][y]=1,b[x+1][y]=1,b[x][y-1]=1,b[x][y+1]=1;
for(int i=0;i<4;i++){
int p=x+dx[i],q=y+dy[i];
if(p>=0 && q>=0 && z[p][q]){
t++;
if(!vis[p][q]) dfs(p,q);
}
}
}
int32_t main(){
ios:: sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=0;i<=n+5;i++){
a[i].resize(m+5);
b[i].resize(m+5);
z[i].resize(m+5);
vis[i].resize(m+5);
yek[i].resize(m+5);
for(int j=0;j<m+5;j++){
a[i][j]=1007;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
int k;
cin>>k;
for(int i=0;i<k;i++){
cin>>x[i]>>y[i];x[i]++;y[i]++;
z[x[i]][y[i]]=1;
}
int f=0;
for(int i=0;i<k;i++){
if((x[i]==1 && y[i]==1) || (x[i]==n && y[i]==1) || (x[i]==1 && y[i]==m) || (x[i]==n && y[i]==m)){
if(sumz(x[i],y[i])>0) f=1;
}
else if(x[i]==1 || x[i]==n || y[i]==1 || y[i]==m){
if(sumz(x[i],y[i])>1) f=1;
}
else{
if(sumz(x[i],y[i])>2) f=1;
}
}
int ans=0;
for(int i=0;i<k;i++){
int p=x[i],q=y[i];
if(p==1 || p==n || q==1 || q==m){
b[p][q]=b[p-1][q]=b[p+1][q]=b[p][q-1]=b[p][q+1]=1;
yek[p][q]=1;
}
else if(sumz(p,q)==2){
b[p][q]=b[p-1][q]=b[p+1][q]=b[p][q-1]=b[p][q+1]=1;
yek[p][q]=1;
}
}
for(int i=0;i<k;i++){
if(!vis[x[i]][y[i]]){
mn=1e9;t=0;M=0;ty=0;
dfs(x[i],y[i]);
t/=2;
//cout<<M<<" "<<t<<endl;
if(ty>1) f=1;
if(t>M) f=1;
if(t==M && ty==1) f=1;
if(t==M-1 && ty==0) ans-=mn;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
ans+=b[i][j]*a[i][j];
}
}
if(n==1 || m==1) f=1;
if(n>N || m>N) return 1;
if(f){
cout<<"No";
return 0;
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |