【ICPC 2022 澳门站】A题 So I'll Max Out My Constructive Algorithm 题解


题目大意

给定一个 $n \times n$ 的数字矩阵,代表每个点的高度,每个数各不相同,求一条遍历所有点的路径,要求只能上下左右移动,且高度下降的次数不小于高度上升次数

题目链接

思路

事实上,随便走一条路,如果高度下降的次数大于高度上升的次数,那么我们就反过来走就行了:)

代码

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
35
36
37
38
39
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int s[67][67], T, n;

int main()
{
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j)
scanf("%d", &s[i][j]);
if(!(i & 1))
reverse(s[i] + 1, s[i] + 1 + n);
}
vector<int> ans;
int cnt1 = 0, cnt2 = 0, lst = 0x3f3f3f3f;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
ans.push_back(s[i][j]);
for(int i = 1; i < ans.size(); ++i) {
if(ans[i] > ans[i - 1])
cnt1++;
else
cnt2++;
}
if(cnt1 > cnt2)
reverse(ans.begin(), ans.end());
printf("%d", ans[0]);
for(int i = 1; i < ans.size(); ++i)
printf(" %d", ans[i]);
printf("\n");
}
return 0;
}

Author: BY 水蓝
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source BY 水蓝 !
  TOC