跳到正文

个人记录

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

hydroxy约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年2月10日
收录
2026年2月10日
更新
2026年11月8日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1

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

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

构造方式:

  • 对于 n=2n=2:输出 1 2.
  • 对于 n>2n>2 的偶数:可以依次输出 p-3 p-2 p p-1(p=4,8,12,...,4×⌈n4⌉p = 4,8,12,...,4\times\lceil\frac{n}{4}\rceil)。若 n∤4n\nmid4,最后只输出 p-3 p-2。 例如 n=8n=8:1 2 4 3 5 6 8 7。n=6n=6:1 2 4 3 5 6。
  • 对于 n>1n>1 的奇数:可以先输出一个 1,然后按输出偶数个的方法反过来进行输出。 例如 n=7n=7:1 3 2 4 5 7 6。n=5n=5:1 3 2 4 5。

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

代码如下:

C++
#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 代码:

C++
#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");
}
原文