QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#575543#9132. Painting Fencesucup-team3519WA 0ms3908kbC++203.6kb2024-09-19 15:15:342024-09-19 15:15:34

Judging History

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

  • [2024-09-19 15:15:34]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3908kb
  • [2024-09-19 15:15:34]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define V vector
#define all0(x) (x).begin(),(x).end()
#define all1(x) (x).begin()+1,(x).end()
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define cin std::cin
#define cout std::cout
typedef long long LL;
typedef pair<int, int> pi;
typedef pair<LL, LL> pl;

//const int MN = 2e5 + 20;
const int INF = 2e9 + 1000;//INF
const LL INFLL = 8e18 + 1000;//INF long long 
mt19937 mrand(chrono::steady_clock().now().time_since_epoch().count());
const int mod = 998244353;
//模板区域~~~~~~~

//模板结束~~~~~~~

void solve() {
    int n, m; cin >> n >> m;
    V<V<int>> a(n + 1, V<int>(m + 1));
    for(int i = 1; i <= n; i++) {
        string s; cin >> s;
        for(int j = 1; j <= m; j++) a[i][j] = s[j - 1] - '0';
    }
    if(n > m) {
        V<V<int>> b(m + 1, V<int>(n + 1));
        for(int i = 1; i <= m; i++) {
            for(int j = 1; j <= n; j++) {
                b[i][j] = a[j][i];
            }
        }
        swap(n, m);
        swap(a, b);
    }
    
    V<V<int>> dp(n + 1, V<int>(n + 1, INF / 2));
    dp[1][n] = 0;

    auto cal_l = [&](int l, int r) -> int {
        int ll = l - (r - l + 1);
        ll = max(1, ll);
        return ll;
    };
    auto cal_r = [&](int l, int r, int n) -> int {
        int rr = r + (r - l + 1);
        rr = min(rr, n);
        return rr;
    };

    for(int len = n - 1; len >= 1; len--) {
        // int mn = INF;
        for(int l = 1; l <= n - len + 1; l++) {
            int r = l + len - 1;

            int ll = cal_l(l, r);
            int rr = cal_r(l, r, n);

            dp[l][r] = min(dp[l][r], dp[ll][r] + 1);
            dp[l][r] = min(dp[l][r], dp[l][rr] + 1);

            // mn = min(dp[l][r], mn);
        }
        // for(int l = 1; l <= n - len + 1; l++) {
        //     int r = l + len - 1;
        //     dp[l][r] = max(dp[l][r], mn + 1);
        // }
    }

    V<array<int, 3>> useful;

    for(int i = 1; i <= n; i++) {
        int now = 30;
        for(int j = i; j <= n; j++) {
            if(dp[i][j] < now) now = dp[i][j], useful.pb({i, j, dp[i][j]});
            // cout << i << " " << j << endl;
        }
    }

    // cout << useful.size() << endl;

    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) a[i][j] += a[i - 1][j];
    }

    V<int> v(m + 1);
    V<int> f(m + 1), n_f(m + 1);
    int totans = INF;
    for(auto [l, r, w] : useful) {
        for(int i = 1; i <= m; i++) v[i] = a[r][i] - a[l - 1][i] == r - l + 1;//, cout << a[r][i] - a[l - 1][i] << " ";

        bool ok = 0;

        for(int l = 1; l <= m; l++) {
            if(!v[l]) {
                f[l] = 0;
                continue;
            }
            int r = l;
            while(r <= m && v[r] == 1) r++;
            f[l] = r - 1;
            ok = 1;
            l = r - 1;
        }

        if(!ok) continue;
        int ans = w;

        while(1) {
            if(f[1] == m) break;
            for(auto &x : n_f) x = 0;
            for(int i = 1; i <= m; i++) {
                if(f[i] == 0) continue;
                int ll = cal_l(i, f[i]);
                int rr = cal_r(i, f[i], m);
                n_f[ll] = max(n_f[ll], f[i]);
                n_f[i] = max(n_f[i], rr);
            }
            ans++;
            swap(n_f, f);
        }
        // cout << ans << endl;
        totans = min(ans, totans);
    }
    
    cout << totans << endl;
}

int32_t main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t=1;
    // cin >> t;
    while (t--) 
    solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 4
1001
0100
0110
0110

output:

3

result:

ok 1 number(s): "3"

Test #2:

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

input:

3 3
000
111
111

output:

1

result:

ok 1 number(s): "1"

Test #3:

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

input:

4 3
011
011
001
110

output:

2

result:

ok 1 number(s): "2"

Test #4:

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

input:

4 4
0011
1111
1111
1111

output:

1

result:

ok 1 number(s): "1"

Test #5:

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

input:

4 4
0000
0010
0100
1000

output:

4

result:

ok 1 number(s): "4"

Test #6:

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

input:

2 5
00010
00111

output:

2

result:

ok 1 number(s): "2"

Test #7:

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

input:

5 5
11111
11111
11111
01111
11111

output:

1

result:

ok 1 number(s): "1"

Test #8:

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

input:

5 5
00101
00000
00001
00000
00100

output:

6

result:

ok 1 number(s): "6"

Test #9:

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

input:

5 5
00000
00000
00001
10000
00000

output:

6

result:

ok 1 number(s): "6"

Test #10:

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

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: 3684kb

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: 3708kb

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: 3592kb

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: 3656kb

input:

30 31
0000000000000000000000000000000
0000000000000000000000000000000
1111111111111110000000000000011
1111111111111110000000000000011
1111111111111110000000000000011
1111111111111111111111111111111
1111111111111111111111111111111
1111111111111111111111111111100
1111111111111111111111111111100
111111...

output:

3

result:

ok 1 number(s): "3"

Test #15:

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

input:

30 31
0000000000000000000000000000000
0000000000000000000000000000000
0000000001000000000000000000000
0000000000000000000000100000000
0000000000000000000100000000000
0000000000000000001000000000000
0000000000000010000000000000000
0000000000000000000000000000000
0000000000000000000000000100110
000000...

output:

10

result:

ok 1 number(s): "10"

Test #16:

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

input:

30 31
0000000000000000000000000000000
0000000011111111111111000000000
0000000011111111111111000000000
1111111111111111111111000000000
1111111111111111111111000000000
1111111111111111111111000000000
1111111111111111111111000111100
1111111111111111111111000111100
1111111111111111111111000111100
111111...

output:

3

result:

ok 1 number(s): "3"

Test #17:

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

input:

30 31
0000001010000000000000000000000
0000000000000000000000000000000
0000000000000000001000000000000
0000010000000000000000000000000
0000000000000000000000000000000
0000000000000000000000000000000
0000001000010000000000000000000
0000100000010010000000000000000
0000000001000001000000010000000
000000...

output:

9

result:

ok 1 number(s): "9"

Test #18:

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

input:

50 50
01111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111...

output:

1

result:

ok 1 number(s): "1"

Test #19:

score: -100
Wrong Answer
time: 0ms
memory: 3908kb

input:

50 50
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000...

output:

5

result:

wrong answer 1st numbers differ - expected: '6', found: '5'