CF2031C

cf2031c

简单构造题,但是我一开始没想出来。

首先可以想到

\(n\mod 2=0\) 则 可以 1 1 2 2 3 3 4 4... 这样是满足题意的方案

\(n\mod 2=1\) 则一定有一个数字出现了 3 次,我们考虑第一次出现这种情况:

假设 \(a_x,a_y,a_z(1\le x<y<z\leq n)\) 是同种颜色,由题意得

\[y-x=A^2\\ z-y=B^2 \\z-x=C^2\\z-x=(y-x)+(z-y)\\C^2=A^2+B^2 \]

手玩一下发现 最小的 A B C 是 3 4 5.

\(C^2=5^2=25\) ,最小: \(1+25=26\) 。由于 n 是奇数,所以此时最小的 n 为 27。$n< 27 $ 则无解. \(n>27\) 时 多的部分可以按照 n 为偶数时的方案造。

手动构造一个 \(n=27\) 时的方案 得到

\(\color{red}{1}\ \color{blue}{2}\)\(\ 3\ 3\ 4\ 4\ \color{green}{5}\)\(\ 6\ 6\ \color{red}{1}\ \color{green}{5}\) \(7\ 7\ 8\ 8\ 9\ 9\ 10\ 10\ 11\ 11\ 12\ 12\ 13\ 13\) \(\color{red}{1}\ \color{blue}{2}\)

彩色是特殊要注意的部分

然后这道题就做完了

代码

//#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
using namespace std;  

#define spa putchar(' ')
#define ero putchar('\n')

inline int read(){
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
inline void write(int x){
	if(!x)putchar('0');
	if(x<0)x=-x,putchar('-');
	int cnt=0,a[30];
	while(x)a[++cnt]=x%10,x/=10;
	while(cnt--)putchar(a[cnt+1]+'0');
}

void solve(){
    int n=read();
	if(n&1){
		if(n<27){
			write(-1),ero;
			return ;
		}
		else{
			cout<<"1 2 3 3 4 4 5 6 6 1 5 7 7 8 8 9 9 10 10 11 11 12 12 13 13 1 2 ";
			n-=27;
			int i=14;
			while(n){n-=2;write(i),spa,write(i),spa;i++;}
			ero;
			return ;
		}
	}
	for(int i=1;i<=n>>1;i++){
		write(i),spa,write(i),spa;
	}
	ero;
}

signed main(){
    int T=read();
    while(T--){
        solve();
    }
	return 0;
}
posted @ 2024-11-19 13:23  Slayer-WT!!!!!!!!  阅读(9)  评论(0)    收藏  举报