QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#546118#9132. Painting Fencespengpeng_fudanWA 0ms3864kbC++203.0kb2024-09-03 19:59:562024-09-03 19:59:57

Judging History

你现在查看的是最新测评结果

  • [2024-09-03 19:59:57]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3864kb
  • [2024-09-03 19:59:56]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ull=unsigned long long;
using ll=long long;
void solve() {
    int n,m;
    cin>>n>>m;
    vector<string> mp(n);
    vector<vector<int>> seg(n,vector<int>(m,0));
    for(int i=0;i<n;i++)    cin>>mp[i];
    for(int i=0;i<n;i++){
        for(int j=m-1;j>=0;j--){
            if(mp[i][j]=='0') seg[i][j]=0;
            else seg[i][j]=(j==m-1?0:seg[i][j+1])+1;
        }
    }
    int mn=-1;
    auto get=[&](int l,int r,int n)->int {
        {
            int lo=l,ro=r;
            int t=lo/(ro-lo+1)+(lo%(ro-lo+1)?1:0);
            bool flag=false;
            int res=0;
            while(t){
                if(t&1){
                    lo=min(0,lo-(ro-lo+1));
                }
                else{
                    ro+=ro-lo+1;
                    if(ro>n-1)  {flag=true;break;}
                }
                res++;
                t>>=1;
            }
            if(!flag){
                while(ro!=n-1){
                    res++;
                    ro=min(n-1,ro+ro-lo+1);
                }
                return res;
            }    
        }
        {
            int lo=l,ro=r;
            int t=(n-1-ro)/(ro-lo+1)+((n-1-ro)%(ro-lo+1)?1:0);
            bool flag=false;
            int res=0;
            while(t){
                if(t&1){
                    ro=min(n-1,ro+(ro-lo+1));
                }
                else{
                    lo-=(ro-lo+1);
                    if(lo<0)  {flag=true;break;}
                }
                res++;
                t>>=1;
            }
            if(!flag){
                while(lo!=0){
                    res++;
                    lo=max(0,lo-(ro-lo+1));
                }
                return res;
            }    
        }
        int ans=0;
        while(l!=0||r!=n-1){
            if(l==0||(r!=n-1&&l<(r-l+1))){
                r=min(n-1,r+r-l+1);
            } 
            else{
                l=max(0,l-(r-l+1));
            }
            ans++;
        }
        return ans;
    };
    //cerr<<get(4,4,m)<<'\n';
    for(int i=0;i<m;i++){
        vector<int> pstk;
        vector<int> lo(n),ro(n);
        for(int j=0;j<n;j++){
            while(!pstk.empty()&&seg[pstk.back()][i]>=seg[j][i])    pstk.pop_back();
            lo[j]=(pstk.empty()?0:pstk.back()+1); 
            pstk.push_back(j);  
        }
        pstk.clear();
        for(int j=n-1;j>=0;j--){
            while(!pstk.empty()&&seg[pstk.back()][i]>=seg[j][i])    pstk.pop_back();
            ro[j]=(pstk.empty()?n-1:pstk.back()-1); 
            pstk.push_back(j);  
        }
        for(int j=0;j<n;j++){
            if(mp[j][i]=='1'){
                int sum=get(lo[j],ro[j],n)+get(i,i+seg[j][i]-1,m);
                if(mn==-1||mn>sum)  mn=sum;
            }
        }
    }
    cout<<mn<<'\n';
}
int main() {
    ios::sync_with_stdio(0),cin.tie(0);
    int _ = 1;
    //cin >> _;
    while(_--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3596kb

input:

4 4
1001
0100
0110
0110

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3864kb

input:

3 3
000
111
111

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

4 3
011
011
001
110

output:

2

result:

ok 1 number(s): "2"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3656kb

input:

4 4
0011
1111
1111
1111

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3864kb

input:

4 4
0000
0010
0100
1000

output:

4

result:

ok 1 number(s): "4"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3628kb

input:

2 5
00010
00111

output:

2

result:

ok 1 number(s): "2"

Test #7:

score: 0
Accepted
time: 0ms
memory: 3820kb

input:

5 5
11111
11111
11111
01111
11111

output:

1

result:

ok 1 number(s): "1"

Test #8:

score: 0
Accepted
time: 0ms
memory: 3568kb

input:

5 5
00101
00000
00001
00000
00100

output:

6

result:

ok 1 number(s): "6"

Test #9:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

5 5
00000
00000
00001
10000
00000

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 0ms
memory: 3616kb

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: 0ms
memory: 3504kb

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: 0ms
memory: 3556kb

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: 0ms
memory: 3852kb

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: 0ms
memory: 3620kb

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: 0ms
memory: 3572kb

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'