QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#604800#3836. So I'll Max Out My Constructive Algorithm SkillsSunlight9#AC ✓25ms3960kbC++201.5kb2024-10-02 13:59:062024-10-02 13:59:06

Judging History

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

  • [2024-10-02 13:59:06]
  • 评测
  • 测评结果:AC
  • 用时:25ms
  • 内存:3960kb
  • [2024-10-02 13:59:06]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

void solve() {
    int n;
    cin >> n;
    vector a(n + 1, vector(n + 1, 0));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1;  j <= n; ++j) {
            cin >> a[i][j];
        }
    }

    int dr = 0;
    vector<int> b;
    vector vis(n + 1, vector(n + 1, 0));
    auto dfs = [&](auto&& self, int x, int y) -> void {
        b.push_back(a[x][y]);
        vis[x][y] = 1;
//        cerr << x << " " << y << "\n";
        if (ssize(b) == n * n) return;
        int nxtx = x, nxty = y;
        while (true) {
            nxtx = x + dx[dr];
            nxty = y + dy[dr];
            if (1 <= nxtx && nxtx <= n && 1 <= nxty && nxty <= n && !vis[nxtx][nxty]) break;
            dr = (dr + 1) % 4;
        }
        self(self, nxtx, nxty);
    };

    dfs(dfs, 1, 1);

    int cnt0 = 0, cnt1 = 0;
    for (int i = 1; i < ssize(b); ++i) {
        if (b[i - 1] < b[i]) cnt0++;
        else cnt1++;
    }
    if (cnt0 <= cnt1) {
        for (int i = 0; i < ssize(b); ++i) {
            cout << b[i] << " \n"[i == ssize(b) - 1];
        }
    } else {
        reverse(b.begin(), b.end());
        for (int i = 0; i < ssize(b); ++i) {
            cout << b[i] << " \n"[i == ssize(b) - 1];
        }
    }
}
int main() {
    cin.tie(nullptr) -> sync_with_stdio(false);

    int _;
    cin >> _;
    while (_--) solve();

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3564kb

input:

1
2
4 3
2 1

output:

4 3 1 2

result:

ok correct

Test #2:

score: 0
Accepted
time: 1ms
memory: 3608kb

input:

100
9
30 75 35 51 25 19 76 65 62
11 56 63 60 77 48 28 26 74
16 44 46 41 17 8 66 61 42
29 7 43 38 40 31 27 10 39
52 23 58 80 50 20 33 69 47
79 1 5 49 22 37 71 18 70
54 72 4 64 55 34 12 6 15
14 53 45 13 32 59 73 57 81
36 3 78 24 2 68 9 67 21
7
11 28 2 19 9 41 24
17 34 5 10 42 18 47
33 35 22 8 49 1 29
...

output:

30 75 35 51 25 19 76 65 62 74 42 39 47 70 15 81 21 67 9 68 2 24 78 3 36 14 54 79 52 29 16 11 56 63 60 77 48 28 26 61 10 69 18 6 57 73 59 32 13 45 53 72 1 23 7 44 46 41 17 8 66 27 33 71 12 34 55 64 4 5 58 43 38 40 31 20 37 22 49 80 50
11 28 2 19 9 41 24 47 29 26 30 13 4 15 38 40 23 31 21 46 39 14 33 ...

result:

ok correct

Test #3:

score: 0
Accepted
time: 3ms
memory: 3652kb

input:

100
11
77 68 83 43 116 28 75 74 34 94 5
22 64 13 4 107 1 61 59 85 101 97
26 56 3 66 25 80 79 53 41 78 115
99 16 100 98 72 121 31 102 93 38 44
7 114 57 71 36 76 95 105 117 92 29
54 81 18 58 19 73 118 90 24 112 21
113 91 50 60 86 103 110 52 40 9 55
6 42 67 119 39 48 63 62 37 2 82
8 27 89 96 109 15 111...

output:

77 68 83 43 116 28 75 74 34 94 5 97 115 44 29 21 55 82 65 87 49 45 84 32 104 23 88 35 30 70 17 12 8 6 113 54 7 99 26 22 64 13 4 107 1 61 59 85 101 78 38 92 112 9 2 106 108 33 120 14 11 20 47 46 10 27 42 91 81 114 16 56 3 66 25 80 79 53 41 93 117 24 40 37 69 51 111 15 109 96 89 67 50 18 57 100 98 72 ...

result:

ok correct

Test #4:

score: 0
Accepted
time: 25ms
memory: 3960kb

input:

100
59
788 1212 2725 713 3247 1378 94 940 1756 2647 888 149 3089 1871 1719 3225 285 1140 798 3375 1355 1207 1551 1022 2633 1647 3222 2979 1185 2489 2816 1709 1327 1623 523 3335 2542 892 3267 3009 2950 82 498 418 731 846 1847 2970 1929 1658 428 1273 3188 3122 2186 1631 1197 2420 326
1885 3328 414 126...

output:

788 1212 2725 713 3247 1378 94 940 1756 2647 888 149 3089 1871 1719 3225 285 1140 798 3375 1355 1207 1551 1022 2633 1647 3222 2979 1185 2489 2816 1709 1327 1623 523 3335 2542 892 3267 3009 2950 82 498 418 731 846 1847 2970 1929 1658 428 1273 3188 3122 2186 1631 1197 2420 326 1860 2284 2596 1599 3465...

result:

ok correct

Test #5:

score: 0
Accepted
time: 14ms
memory: 3920kb

input:

100
15
214 45 120 107 133 221 50 154 1 208 99 58 100 163 181
219 18 63 134 62 155 189 39 55 35 33 91 61 66 152
27 19 148 36 185 6 224 158 42 11 86 52 220 7 151
15 38 165 162 44 157 194 200 153 21 56 43 171 169 178
92 202 78 8 206 192 180 90 179 29 167 89 96 204 28
54 187 209 102 122 168 94 24 172 18...

output:

47 87 104 196 3 70 41 77 49 110 199 182 225 130 95 83 193 161 146 57 186 172 24 94 168 122 112 213 16 118 5 211 84 51 81 12 74 46 67 223 17 124 167 29 179 90 180 192 206 8 102 31 210 114 72 79 20 160 201 68 136 144 143 105 218 59 205 93 22 34 13 89 43 56 21 153 200 194 157 44 162 165 78 209 108 149 ...

result:

ok correct

Test #6:

score: 0
Accepted
time: 11ms
memory: 3756kb

input:

100
62
1751 1243 1461 493 3235 3723 2559 2110 1712 657 2195 1885 1652 3580 3744 2409 1011 1880 1976 1597 3621 3014 3498 356 1815 1583 66 45 104 3656 2961 2754 391 2381 3687 1894 2426 2877 3092 2027 2843 3101 1962 1977 1238 3161 245 2129 2946 516 1398 2893 1197 3090 2831 906 139 3318 2905 631 294 142...

output:

3753 3518 2789 1524 1862 2897 972 249 540 514 3720 3408 4 1933 1956 938 57 3120 2578 816 1082 2015 1560 3089 1333 2846 669 3181 1122 2793 474 866 3652 3545 3305 568 1500 271 3619 150 3844 10 2505 1779 937 2168 536 3799 234 3634 2834 3646 2234 3464 2342 2088 3659 2974 1127 2361 769 16 1138 1443 1289 ...

result:

ok correct

Test #7:

score: 0
Accepted
time: 14ms
memory: 3652kb

input:

100
49
7 1634 824 2118 1979 70 660 1913 913 1072 2027 1544 1793 1970 2 1495 1240 218 459 136 1872 1378 2373 308 2145 185 406 596 1374 2196 1573 1021 416 1298 46 1885 1708 1152 2285 1662 2377 2291 2187 1833 761 306 1352 745 2014
603 784 374 2136 1227 1771 2071 1443 360 91 955 1134 1009 1359 433 1013 ...

output:

1648 1802 2159 749 477 950 530 1621 1078 515 511 1051 1169 980 298 1225 362 2032 1425 937 893 1704 712 2060 1482 280 1732 2232 229 1523 1624 635 2216 327 1620 1502 1699 2270 1287 1464 2393 2010 1644 455 2075 1925 539 292 1972 1569 899 2082 647 1140 2206 191 325 111 1105 896 1199 1466 491 1166 1741 1...

result:

ok correct