ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#212772 | #3828. B | tommy | 80 | 1887ms | 31316kb | C++ | 2.5kb | 2024-10-20 10:53:50 | 2024-10-20 14:38:00 |
answer
#include <bits/stdc++.h>
using namespace std;
int T;
int n, m;
const int N = 2 * 1e5 + 10;
vector<int> a[N];
struct Node {
int x, y;
} wei[N];
vector<int> f[N];
vector<Node> from[N];
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, 1, -1};
int ans[N], cnt;
void work() {
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
int u;
scanf("%d", &u);
a[i].push_back(u);
f[i].push_back(0);
from[i].push_back({0, 0});
}
}
// printf("1111\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
wei[a[i][j]] = {i, j};
}
}
// for(int i = 0; i < n * m; i++) printf("%d %d\n", wei[i].x, wei[i].y);
// printf("1111\n");
for(int i = n * m - 1; i >= 0; i--) {
int xx = wei[i].x, yy = wei[i].y;
for(int k = 0; k < 4; k++) {
int nx = xx + dx[k], ny = yy + dy[k];
if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if(a[nx][ny] < a[xx][yy]) continue;
// printf("%d %d %d\n", i, nx, ny);
if(f[xx][yy] < f[nx][ny] + 1) {
f[xx][yy] = f[nx][ny] + 1;
from[xx][yy] = {nx, ny};
} else if(f[xx][yy] == f[nx][ny] + 1 && a[from[xx][yy].x][from[xx][yy].y] > a[nx][ny]) {
from[xx][yy] = {nx, ny};
}
}
}
// printf("1111\n");
int maxn = 0, x = 0, y = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
// printf("%d %d %d %d\n", i, j, from[i][j].x, from[i][j].y);
// printf("%d ", f[i][j]);
if(maxn < f[i][j]) {
maxn = f[i][j];
x = i, y = j;
} else if(maxn == f[i][j] && a[x][y] > a[i][j]) x = i, y = j;
}
// printf("\n");
}
printf("%d\n", maxn + 1);
cnt = 0;
while(x != 0 || y != 0 || cnt == 0) {
printf("%d ", a[x][y]);
cnt++;
int xx = x, yy = y;
x = from[xx][yy].x;
y = from[xx][yy].y;
}
printf("\n");
// for(int i = cnt; i >= 1; i--) printf("%d ", ans[i]);
// printf("\n");
for(int i = 0; i < n; i++) {
a[i].erase(a[i].begin(), a[i].end());
f[i].erase(f[i].begin(), f[i].end());
from[i].erase(from[i].begin(), from[i].end());
}
}
int main() {
scanf("%d", &T);
while(T--) {
work();
}
}
详细
小提示:点击横条可展开更详细的信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 20
Accepted
time: 4ms
memory: 15320kb
input:
10 3 3 0 1 2 3 4 5 6 7 8 3 3 0 1 2 3 4 5 6 8 7 3 3 0 1 2 3 4 5 7 6 8 3 3 0 1 2 3 4 5 7 8 6 3 3 0 1 2...
output:
5 0 1 2 5 8 6 0 1 2 5 7 8 5 0 1 2 5 8 6 0 1 2 5 6 8 5 0 1 2 5 7 7 0 1 2 5 6 7 8 5 0 1 2 6 8 6...
result:
ok 64 numbers
Test #2:
score: -20
Wrong Answer
time: 7ms
memory: 15324kb
input:
10 1 10 1 5 0 8 2 9 6 3 4 7 1 10 2 5 8 0 9 7 3 1 6 4 3 3 0 1 2 5 4 3 6 7 8 1 10 3 4 7 2 5 6 9 0 1 8 ...
output:
3 3 4 7 4 1 3 7 9 9 0 1 2 3 4 5 6 7 8 4 2 5 6 9 3 1 7 9 4 4 3 0 4 7 10 0 1 2 3 4 5 6 7 8 9 4...
result:
wrong answer 31st numbers differ - expected: '5', found: '3'
Subtask #2:
score: 20
Accepted
Test #3:
score: 20
Accepted
time: 4ms
memory: 15396kb
input:
10 22 45 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33...
output:
990 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 3...
result:
ok 5011 numbers
Test #4:
score: 0
Accepted
time: 3ms
memory: 15404kb
input:
10 17 58 655 668 708 805 640 926 52 16 9 662 765 129 949 348 678 235 18 464 819 450 211 968 724 754 ...
output:
10 11 42 106 223 265 421 903 923 971 982 10 227 258 271 479 499 509 574 578 580 918 8 1 230 308 44...
result:
ok 2052 numbers
Subtask #3:
score: 10
Accepted
Test #5:
score: 10
Accepted
time: 291ms
memory: 22676kb
input:
10 2 100000 143604 106821 145034 44402 118718 156663 77133 28800 81890 12336 191537 118894 103331 75...
output:
13 11306 16007 49522 70570 76999 90088 97453 105217 116458 118241 145649 165150 168193 9 9688 35806...
result:
ok 600082 numbers
Subtask #4:
score: 10
Accepted
Test #6:
score: 10
Accepted
time: 292ms
memory: 24444kb
input:
10 1 200000 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32...
output:
200000 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3...
result:
ok 400095 numbers
Subtask #5:
score: 40
Accepted
Test #7:
score: 40
Accepted
time: 322ms
memory: 30452kb
input:
10 145 1379 140324 86968 96426 123781 39754 103720 60835 118904 114639 53717 27146 110309 39232 5608...
output:
14 8850 11113 25989 34151 36456 84869 93686 115053 144522 160408 175609 177211 186680 198340 14 701...
result:
ok 799431 numbers
Test #8:
score: 0
Accepted
time: 318ms
memory: 28016kb
input:
10 137 1459 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32...
output:
199883 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3...
result:
ok 599839 numbers
Test #9:
score: 0
Accepted
time: 315ms
memory: 29236kb
input:
10 431 464 73283 114291 153291 121544 122062 142417 169824 87437 63383 123169 5578 161240 12296 2810...
output:
13 8693 46170 58031 84535 85037 98585 107097 109913 125647 135974 173048 173291 193164 16 5468 1850...
result:
ok 400038 numbers
Test #10:
score: 0
Accepted
time: 331ms
memory: 31316kb
input:
10 287 696 42594 17204 186134 183377 153412 122746 42424 4738 107615 187084 183594 161385 57847 9481...
output:
14 136 58739 82533 100919 115173 123428 128558 129415 134074 151802 155712 174974 184151 184213 14 ...
result:
ok 599743 numbers