QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#524314 | #9132. Painting Fences | daduoli | WA | 17ms | 98528kb | C++14 | 2.0kb | 2024-08-19 15:31:34 | 2024-08-19 15:31:34 |
Judging History
answer
#include<bits/stdc++.h>
#define Yzl unsigned long long
#define pi pair<int,int>
#define mp make_pair
#define fi first
#define se second
typedef long long LL;
using namespace std;
const Yzl Lty=20120712;
const int MAXN=1e6+10;
const int inf=1e9;
int n,m;
char ch[MAXN];
vector<int> a[MAXN],l[MAXN],r[MAXN],up[MAXN];
map<pi,int> t;
int dfs(int x,int y) {
if(x==1) return 0;
if(t[mp(x,y)]) return t[mp(x,y)];
int l=y-1,r=y,res=inf;
if(y>1) {
int ll=l-1,rr=x-r;
if(ll>=1) ll=(ll-1)/2+1;
else ll=0;
if(rr>=1) rr=(rr-1)/2+1;
res=min(res,dfs(ll+rr+1,ll+1));
}
l=y,r=y+1;
if(y<x) {
int ll=l-1,rr=x-r;
if(ll>=1) ll=(ll-1)/2+1;
else ll=0;
if(rr>=1) rr=(rr-1)/2+1;
res=min(res,dfs(ll+rr+1,ll+1));
}
return t[mp(x,y)]=res+1;
}
signed main () {
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i) {
scanf("%s",ch+1);
a[i].resize(m+3);
l[i].resize(m+3);
r[i].resize(m+3);
up[i].resize(m+3);
for(int j=1;j<=m;++j) {
if(ch[j]=='0') a[i][j]=0;
else {
a[i][j]=1;
l[i][j]=r[i][j]=j;
}
}
}
int ans=inf;
// cout<<dfs(1,2)<<' ';
for(int i=1;i<=n;++i) {
for(int j=2;j<=m;++j) {
if(a[i][j-1]&&a[i][j]) l[i][j]=l[i][j-1];
}
for(int j=m-1;j>=1;--j) {
if(a[i][j+1]&&a[i][j]) r[i][j]=r[i][j+1];
}
if(i!=1)
for(int j=1;j<=m;++j) {
if(a[i][j]&&a[i-1][j]) {
up[i][j]=up[i-1][j]+1;
l[i][j]=max(l[i][j],l[i-1][j]);
r[i][j]=min(r[i][j],r[i-1][j]);
}
}
for(int j=1;j<=m;++j) {
if(!a[i][j]) continue;
int x=i-up[i][j],y=i,X=l[i][j],Y=r[i][j];
int l1=(x-2)/(y-x+1)+(x>1),l2=(n-y-1)/(y-x+1)+(y<n);
int l3=(X-2)/(Y-X+1)+(X>1),l4=(m-Y-1)/(Y-X+1)+(Y<m);
// if(i==4&&j==2) {
// cout<<l1+l2+1<<" "<<l3+l4+1<<endl;
// }
l1=max(l1,0); l2=max(l2,0);
l3=max(l3,0); l4=max(l4,0);
int ls=dfs(l1+l2+1,l1+1)+dfs(l3+l4+1,l3+1);
if(ls<ans) {
ans=ls;
// cout<<i<<" "<<j<<" "<<l1+l2+1<<' '<<l3+l4+1<<endl;
// cout<<l2<<' '<<l4<<endl;
}
}
}
// cout<<dfs(4,2)<<" "<<dfs(2,2)<<endl;
printf("%d\n",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 97564kb
input:
4 4 1001 0100 0110 0110
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 8ms
memory: 97588kb
input:
3 3 000 111 111
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 12ms
memory: 97900kb
input:
4 3 011 011 001 110
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 0
Accepted
time: 14ms
memory: 98440kb
input:
4 4 0011 1111 1111 1111
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 12ms
memory: 97668kb
input:
4 4 0000 0010 0100 1000
output:
4
result:
ok 1 number(s): "4"
Test #6:
score: 0
Accepted
time: 7ms
memory: 97960kb
input:
2 5 00010 00111
output:
2
result:
ok 1 number(s): "2"
Test #7:
score: 0
Accepted
time: 7ms
memory: 98296kb
input:
5 5 11111 11111 11111 01111 11111
output:
1
result:
ok 1 number(s): "1"
Test #8:
score: 0
Accepted
time: 8ms
memory: 98528kb
input:
5 5 00101 00000 00001 00000 00100
output:
6
result:
ok 1 number(s): "6"
Test #9:
score: 0
Accepted
time: 10ms
memory: 97552kb
input:
5 5 00000 00000 00001 10000 00000
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 17ms
memory: 98300kb
input:
10 10 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111
output:
0
result:
ok 1 number(s): "0"
Test #11:
score: 0
Accepted
time: 8ms
memory: 98160kb
input:
10 10 0001000000 0000000000 0000000000 0000000001 0000000001 0000000001 0000000000 0000000000 0000000000 0000000001
output:
6
result:
ok 1 number(s): "6"
Test #12:
score: 0
Accepted
time: 10ms
memory: 97692kb
input:
10 10 1111111110 1111111110 1111111110 1111111110 1111111110 1111100110 1111100010 1111101110 1111101100 1111100000
output:
1
result:
ok 1 number(s): "1"
Test #13:
score: 0
Accepted
time: 8ms
memory: 97636kb
input:
10 10 0000000000 0000001000 0000000000 0000000000 0000000000 0100000000 0000000000 0000000100 0000000000 0000000000
output:
8
result:
ok 1 number(s): "8"
Test #14:
score: 0
Accepted
time: 7ms
memory: 97652kb
input:
30 31 0000000000000000000000000000000 0000000000000000000000000000000 1111111111111110000000000000011 1111111111111110000000000000011 1111111111111110000000000000011 1111111111111111111111111111111 1111111111111111111111111111111 1111111111111111111111111111100 1111111111111111111111111111100 111111...
output:
3
result:
ok 1 number(s): "3"
Test #15:
score: -100
Wrong Answer
time: 4ms
memory: 97720kb
input:
30 31 0000000000000000000000000000000 0000000000000000000000000000000 0000000001000000000000000000000 0000000000000000000000100000000 0000000000000000000100000000000 0000000000000000001000000000000 0000000000000010000000000000000 0000000000000000000000000000000 0000000000000000000000000100110 000000...
output:
9
result:
wrong answer 1st numbers differ - expected: '10', found: '9'