[丘班 2026 除夕夜跨年赛] 奇偶行 / even-odd 题解

· · 个人记录

奇数 + 奇数 = 偶数
奇数 + 偶数 = 奇数
偶数 + 偶数 = 偶数

所以如果连续三项 p_i,p_{i+1},p_{i+2} 中,p_i+p_{i+1} 与 p_{i+1}+p_{i+2} 的奇偶性不同,则这三项的奇偶性分别为奇奇偶,奇偶偶,偶偶奇,偶奇奇中的一个。

构造方式:

其实本题没有输出 -1 的情况。

代码如下:

#include<bits/stdc++.h>
using namespace std;
#pragma G++ optimize(2)//本地运行太慢了,手动开了个 O2
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        if(n%2 == 0){
            bool pd = 0;
            int cnt = 1;
            while(1){
                if(pd == 0){
                    cout<<cnt<<" "<<cnt+1<<" ";
                    pd = 1;
                    cnt += 2;
                }
                if(cnt == n+1){
                    break;
                }
                if(pd == 1){
                    cout<<cnt+1<<" "<<cnt<<" ";
                    pd = 0;
                    cnt += 2;
                }
                if(cnt == n+1){
                    break;
                } 
            }
            cout<<'\n';
        }
        if(n%2 == 1){
            bool pd = 0;
            cout<<1<<" ";
            int cnt = 2;
            while(1){
                if(pd == 0){
                    cout<<cnt+1<<" "<<cnt<<" ";
                    pd = 1;
                    cnt += 2;
                }
                if(cnt == n+1){
                    break;
                }
                if(pd == 1){
                    cout<<cnt<<" "<<cnt+1<<" ";
                    pd = 0;
                    cnt += 2;
                }
                if(cnt == n+1){
                    break;
                }
            }
            cout<<'\n';
        }
    } 
    return 0;
}

SPJ 代码:

#include "testlib.h"
#include <bits/stdc++.h>

bool checkAlternating(const std::vector<int>& p) {
    int n = (int)p.size();
    if (n <= 1) return true;
    for (int i = 0; i < n - 2; ++i) {
        int sum1 = p[i] + p[i + 1];
        int sum2 = p[i + 1] + p[i + 2];
        if (sum1 % 2 == sum2 % 2)
            return false;
    }
    return true;
}

int main(int argc, char* argv[]) {
    setName("even-odd checker");
    registerTestlibCmd(argc, argv);

    int n = inf.readInt();

    // Read participant output
    if (ans.seekEof()) {
        quitf(_pe, "The contestant's output is empty");
    }

    int first = ans.readInt();
    if (first == -1) {
        // Check if rest is empty
        if (!ans.seekEof()) {
            quitf(_pe, "Output starts with -1 but has more tokens");
        }
        // According to problem analysis, solution always exists for n >= 2
        quitf(_wa, "User claimed no solution, but one exists.");
    }

    std::vector<int> perm;
    perm.push_back(first);

    while (!ans.seekEof()) {
        perm.push_back(ans.readInt());
    }

    if ((int)perm.size() != n) {
        quitf(_wa, "Expected %d numbers, found %d", n, (int)perm.size());
    }

    // Check if it's a permutation of [1, n]
    std::vector<bool> used(n + 1, false);
    for (int x : perm) {
        if (x < 1 || x > n) {
            quitf(_wa, "Number %d is out of range [1, %d]", x, n);
        }
        if (used[x]) {
            quitf(_wa, "Duplicate number %d", x);
        }
        used[x] = true;
    }

    // Check alternating condition
    if (!checkAlternating(perm)) {
        quitf(_wa, "The contestant's answer does not meet the requirements.");
    }

    quitf(_ok, "Correct answer");
}