编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间
#28390 #2065. 数字华容道 Accepted 100 654 ms 12304 K C++ 11 (Clang) / 1.9 K 192022211773 2025-03-15 15:51:55
显示原始代码
#include <bits/stdc++.h>
using namespace std;

int mergeAndCount(vector<int>& arr, vector<int>& temp, int left, int mid, int right) {
    int i = left, j = mid + 1, k = left, inv_count = 0;
    while (i <= mid && j <= right) {
        if (arr[i] <= arr[j]) {
            temp[k++] = arr[i++];
        } else {
            temp[k++] = arr[j++];
            inv_count += (mid - i + 1);
        }
    }
    while (i <= mid) temp[k++] = arr[i++];
    while (j <= right) temp[k++] = arr[j++];
    for (i = left; i <= right; i++) arr[i] = temp[i];
    return inv_count;
}

int countInversions(vector<int>& arr, vector<int>& temp, int left, int right) {
    if (left >= right)
        return 0;
    int mid = (left + right) / 2;
    int inv_count = 0;
    inv_count += countInversions(arr, temp, left, mid);
    inv_count += countInversions(arr, temp, mid + 1, right);
    inv_count += mergeAndCount(arr, temp, left, mid, right);
    return inv_count;
}

bool isSolvable(vector<vector<int>>& board, int n, int m) {
    vector<int> flattened;
    int zeroRow = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (board[i][j] == 0) {
                zeroRow = i;
            } else {
                flattened.push_back(board[i][j]);
            }
        }
    }

    vector<int> temp(flattened.size());
    int inv_count = countInversions(flattened, temp, 0, flattened.size() - 1);

    if (m % 2 == 1) {
        return inv_count % 2 == 0;
    } else {
        return (inv_count + (n - zeroRow)) % 2 == 1;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    vector<vector<int>> board(n, vector<int>(m));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) cin >> board[i][j];

    cout << (isSolvable(board, n, m) ? "YES" : "NO") << '\n';
    return 0;
}
子任务 #1
Accepted
得分:100
测试点 #1
Accepted
得分:100
用时:3 ms
内存:412 KiB

输入文件(test1.in

3 3
1 2 6 
7 3 8 
0 4 5 

答案文件(test1.out

YES

用户输出

YES

系统信息

Exited with return code 0
测试点 #2
Accepted
得分:100
用时:3 ms
内存:412 KiB

输入文件(test2.in

3 3
7 1 5 
6 0 2 
3 4 8 

答案文件(test2.out

YES

用户输出

YES

系统信息

Exited with return code 0
测试点 #3
Accepted
得分:100
用时:3 ms
内存:328 KiB

输入文件(test3.in

4 4
13 11 1 9 
15 2 14 7 
0 10 3 8 
4 5 6 12 

答案文件(test3.out

YES

用户输出

YES

系统信息

Exited with return code 0
测试点 #4
Accepted
得分:100
用时:3 ms
内存:412 KiB

输入文件(test4.in

4 4
14 3 9 8 
4 15 5 0 
1 2 6 7 
10 11 12 13 

答案文件(test4.out

NO

用户输出

NO

系统信息

Exited with return code 0
测试点 #5
Accepted
得分:100
用时:3 ms
内存:336 KiB

输入文件(test5.in

4 4
9 13 2 5 
1 14 12 0 
3 4 10 7 
8 6 11 15 

答案文件(test5.out

NO

用户输出

NO

系统信息

Exited with return code 0
测试点 #6
Accepted
得分:100
用时:5 ms
内存:472 KiB

输入文件(test6.in

100 100
7690 5682 2677 2004 5559 6119 4307 2035 8893 7862 9155 2640 4139 6211 1754 6042 8035 8694 1
<48999 bytes omitted>

答案文件(test6.out

YES

用户输出

YES

系统信息

Exited with return code 0
测试点 #7
Accepted
得分:100
用时:5 ms
内存:540 KiB

输入文件(test7.in

100 100
7131 5702 6393 2569 6232 2219 5826 1730 3120 8643 4870 8412 4828 9339 2366 738 5908 5688 52
<48999 bytes omitted>

答案文件(test7.out

NO

用户输出

NO

系统信息

Exited with return code 0
测试点 #8
Accepted
得分:100
用时:245 ms
内存:12304 KiB

输入文件(test8.in

1000 1000
852093 582459 126709 111945 392720 997041 498864 532066 280215 433547 734819 780448 18612
<6890801 bytes omitted>

答案文件(test8.out

YES

用户输出

YES

系统信息

Exited with return code 0
测试点 #9
Accepted
得分:100
用时:251 ms
内存:12304 KiB

输入文件(test9.in

1000 1000
425610 18558 726158 634073 689595 11109 521599 44245 742153 819901 371590 568407 886681 3
<6890801 bytes omitted>

答案文件(test9.out

YES

用户输出

YES

系统信息

Exited with return code 0
测试点 #10
Accepted
得分:100
用时:133 ms
内存:6920 KiB

输入文件(test10.in

587 931
402769 239103 45894 99675 444353 214471 33158 331145 254043 429952 47540 544790 544051 3134
<3715452 bytes omitted>

答案文件(test10.out

YES

用户输出

YES

系统信息

Exited with return code 0