[丘班 2026 除夕夜跨年赛] 奇偶行 / even-odd 题解
奇数
奇数
偶数
所以如果连续三项
构造方式:
- 对于
n=2 :输出1 2. - 对于
n>2 的偶数:可以依次输出p-3 p-2 p p-1(p = 4,8,12,...,4\times\lceil\frac{n}{4}\rceil )。若n\nmid4 ,最后只输出p-3 p-2。 例如n=8 :1 2 4 3 5 6 8 7。n=6 :1 2 4 3 5 6。 - 对于
n>1 的奇数:可以先输出一个1,然后按输出偶数个的方法反过来进行输出。
例如n=7 :1 3 2 4 5 7 6。n=5 :1 3 2 4 5。
其实本题没有输出
代码如下:
#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");
}