Logo Wy Online Judge

WyOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#372#42. 「NOIP2010」引水入城Pigsyy1001155ms7820kbC++231.8kb2025-04-22 13:38:552025-04-22 13:42:16

answer

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 505;

int n, m;
int a[N][N] = {{0}};
bool vld[N] = {false};
bool vis[N][N] = {{false}};

int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};

void srh(int x, int y) {
    vis[x][y] = true;

    for (int k = 0; k < 4; k++) {
        int vx = x + dx[k], vy = y + dy[k];

        if (1 <= vx && vx <= n && 1 <= vy && vy <= m && a[x][y] > a[vx][vy] && !vis[vx][vy])
            srh(vx, vy);
    }
}

struct Seg {
    int l, r;
    Seg(int _l = 0, int _r = 0) :
        l(_l), r(_r) {}
} s[N];

int main() {
    scanf("%d%d", &n, &m);

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            scanf("%d", &a[i][j]);

    bool flg = true;
    int tot = 0;

    for (int i = 1; i <= m; i++) {
        memset(vis, false, sizeof vis);
        srh(1, i);
        int l = m + 1, r = 0;

        for (int j = 1; j <= m; j++)
            if (vis[n][j])
                vld[j] = true, l = min(l, j), r = max(r, j);

        if (l == m + 1)
            continue;

        for (int j = l; j <= r; j++)
            if (!vis[n][j])
                flg = false;

        s[++tot] = Seg(l, r);
    }

    for (int i = 1; i <= m; i++)
        flg = flg && vld[i];

    if (!flg) {
        int ans = 0;

        for (int i = 1; i <= m; i++)
            ans += !vld[i];

        printf("0\n%d\n", ans);
        return 0;
    }

    sort(s + 1, s + tot + 1, [](Seg x, Seg y) {
        return x.l < y.l;
    });
    int p = 1, mxr = 0, ans = 0;

    for (int i = 1; i <= tot; i++) {
        if (s[i].l > p)
            ans++, p = mxr + 1;

        mxr = max(mxr, s[i].r);
    }

    if (p <= m)
        ans++;

    printf("1\n%d\n", ans);
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 3ms
memory: 3992kb

input:

10 10
517 626 565 502 512 551 622 557 518 621
515 498 463 456 507 504 452 598 450 454
432 537 522 41...

output:

0
7

result:

ok 2 tokens

Test #2:

score: 10
Accepted
time: 18ms
memory: 4256kb

input:

100 100
8000 7999 7998 7997 7996 7995 7994 7993 7992 7991 7990 7989 7988 7987 7986 7985 7984 7983 79...

output:

0
53

result:

ok 2 tokens

Test #3:

score: 10
Accepted
time: 1040ms
memory: 7820kb

input:

500 500
200000 199999 199998 199997 199996 199995 199994 199993 199992 199991 199990 199989 199988 1...

output:

0
269

result:

ok 2 tokens

Test #4:

score: 10
Accepted
time: 1ms
memory: 3796kb

input:

1 10
109 108 101 100 102 104 104 102 102 105

output:

1
4

result:

ok 2 tokens

Test #5:

score: 10
Accepted
time: 0ms
memory: 3748kb

input:

10 10
1004 1004 1004 1004 1009 1009 1009 1009 1009 1009
904 904 904 904 904 904 904 904 904 904
800 ...

output:

1
6

result:

ok 2 tokens

Test #6:

score: 10
Accepted
time: 2ms
memory: 4012kb

input:

100 20
300 1 1 1 1 1 10009 10009 10009 10009 10009 10009 10009 10009 10009 10009 10009 10009 10009 1...

output:

1
9

result:

ok 2 tokens

Test #7:

score: 10
Accepted
time: 3ms
memory: 4004kb

input:

100 50
300 1 1 1 1 1 300 1 1 1 1 1 10006 10006 10006 10006 10006 10006 10006 10006 10006 10006 10006...

output:

1
21

result:

ok 2 tokens

Test #8:

score: 10
Accepted
time: 8ms
memory: 4192kb

input:

100 100
300 1 1 1 1 1 300 1 1 1 1 1 300 1 1 1 1 1 10004 10004 10004 10004 10004 10004 10004 10004 10...

output:

1
28

result:

ok 2 tokens

Test #9:

score: 10
Accepted
time: 16ms
memory: 4416kb

input:

200 200
600 1 1 1 1 1 600 1 1 1 1 1 20005 20005 20005 20005 20005 20005 20005 20005 20005 20005 2000...

output:

1
41

result:

ok 2 tokens

Test #10:

score: 10
Accepted
time: 64ms
memory: 4808kb

input:

500 500
1500 1 1 1 1 1 1500 1 1 1 1 1 50005 50005 50005 50005 50005 50005 50005 50005 50005 50005 50...

output:

1
86

result:

ok 2 tokens