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满足 pi < pj 时,gpipj = 1,也就是小的在大的前面

解题思路

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,,a} 的形式,但是如果直接这样构造会让最后的答案是n-2,所以只需要当两侧的数字构造出能补足这个部分即可,稍微凑一下即可得到一个简单的答案,{1,1,2,,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";
    }
}
posted @   udiandianis  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
  1. 1 山海不可平 CMJ
山海不可平 - CMJ
00:00 / 00:00
An audio error has occurred.

纯音乐,请欣赏

点击右上角即可分享
微信分享提示