Codeforces Round 997 (Div. 2)
A. Shape Perimeter
题目大意
给你一个m*m的正方形,再给你n个坐标表示每次在xy移动的距离(第一个坐标是初始位置正方形左下角),问路径图形的周长
解题思路
记录好第一次的位置之后一直累加最后求总移动距离的差值即可
代码实现
#include <bits/stdc++.h>using i64 = long long;int main() {std::ios::sync_with_stdio(false);std::cin.tie(0);int t;std::cin >> t;while (t--) {int n, m;std::cin >> n >> m;std::vector<int> X = {0, 0}, Y = {0, 0};for (int i = 0; i < n; i++) {int x, y;std::cin >> x >> y;if (!X[0]) {X[0] += x;}if (!Y[0]) {Y[0] += y;}X[1] += x;Y[1] += y;}std::cout << (X[1] + m - X[0] + Y[1] + m - Y[0]) * 2 << "\n";}
}
B. Find the Permutation
题目大意
给你一个邻接矩阵g,求一个排列p,g满足 p i p_i pi < p j p_j pj 时, g p i p j g_{p_i p_j} gpipj = 1,也就是小的在大的前面
解题思路
g i j g_{ij} gij 是1说明i处的数字比j小且i在前面,因此只需要按题意自定义排序规则重新排序即可
代码实现
#include <bits/stdc++.h>using i64 = long long;int main() {std::ios::sync_with_stdio(false);std::cin.tie(0);int t;std::cin >> t;while (t--) {int n;std::cin >> n;std::vector<std::string> g(n);for (int i = 0; i < n; i++) {std::cin >> g[i];}std::vector<int> p(n);std::iota(p.begin(), p.end(), 0);std::sort(p.begin(), p.end(), [&](int x, int y) {if (g[x][y] == '1') {return x < y;} else {return x > y;}});for (int i = 0; i < n; i++) {std::cout << p[i] + 1 << " \n"[i == n - 1];}}
}
C. Palindromic Subsequences
题目大意
给你一个数字n,请你构造一个长度为n的数组,满足数组中最长回文子序列长度大于n
解题思路
首先考虑最长长度为1和2,发现显然都不能满足,当长度为3的时候,如果两个相同的数字在外侧,则中间放x个不一样的数字就能对数量贡献x,也就是 $ \{a,\dots,a\} $ 的形式,但是如果直接这样构造会让最后的答案是n-2,所以只需要当两侧的数字构造出能补足这个部分即可,稍微凑一下即可得到一个简单的答案,$ \{1,1,2,\dots,1,2\} $ 直接构造即可
代码实现
#include <bits/stdc++.h>using i64 = long long;int main() {std::ios::sync_with_stdio(false);std::cin.tie(0);int t;std::cin >> t;while (t--) {int n;std::cin >> n;std::cout << "1 1 2 ";for (int i = 3; i <= n - 3; i++) {std::cout << i << " ";}std::cout << "1 2\n";}
}