NKOJ1236 排队

题目大意

元元曾经是班长。在校运动会上,元元的班要进行队列表演。元元要选出 \(2\times n\) 名同学编队,每人都被编上一个号,每一个从 \(1\)\(n\) 的自然数都被某 \(2\) 名同学佩戴,现在要求将他们排成一列,使两个编号为 \(1\) 的同学中间恰好夹 \(1\) 名同学,两个编号为 \(2\) 的同学中间恰好夹 \(2\) 名同学,\(\cdots\),两个编号为 \(n\) 的同学中间恰好夹 \(n\) 名同学,元元希望知道这样的排法能否实现。

如果不能实现,输出 No Solution.;可以实现则输出字典序最大的方案。

题目分析

dfs 求解是显然的,这里主要来证明无解情况。

设每个数字的位置分别为:\(a_1,a_2,\cdots,a_n\),其中 \(a_i\) 表示 \(i\) 的位置。

\(2\times n\) 个整数分别为 \(a_1,a_2,\cdots,a_n,a_1+1+1,a_2+2+1,a_3+3+1,\cdots,a_n+n+1\)

所以有:

\[\sum\limits_{i=1}^na_i+\sum\limits_{i=1}^n(a_i+i+1)=\sum\limits_{i=1}^{2\cdot n}i \]

\[2\cdot(\sum\limits_{i=1}^na_i)+\dfrac{n\cdot(n+1)}{2}+n=\dfrac{2\cdot n\cdot(2\cdot n+1)}{2} \]

\[2\cdot(\sum\limits_{i=1}^na_i)=\dfrac{4\cdot n^2+2\cdot n-n\cdot(n+1)-2\cdot n}{2} \]

\[4\cdot(\sum\limits_{i=1}^na_i)=n\cdot(3\cdot n - 1) \]

所以 \(n\cdot(3\cdot n-1)\)\(4\) 的倍数。\(n\bmod4\) 有且仅有 \(0,1,2,3\) 这四种情况,故 \(n\cdot(3\cdot n+1)\bmod 4=0,2,2,0\)。显然余数为 \(0\) 是方能满足条件,所以得出结论:

\(n\bmod 4=1\)\(2\) 时无解。

代码

因为要保证字典序最大,所以从大到小枚举。

#include <iostream>
#include <cstdio>
#include <climits>//need "INT_MAX","INT_MIN"
#include <cstring>//need "memset"
#include <numeric>
#include <algorithm>
#include <cmath>
#define enter putchar(10)
#define debug(c,que) std::cerr << #c << " = " << c << que
#define cek(c) puts(c)
#define blow(arr,st,ed,w) for(register int i = (st);i <= (ed); ++ i) std::cout << arr[i] << w;
#define speed_up() std::ios::sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0)
#define mst(a,k) memset(a,k,sizeof(a))
#define stop return(0)
const int mod = 1e9 + 7;
inline int MOD(int x) {
	if(x < 0) x += mod;
	return x % mod;
}
namespace Newstd {
	char buf[1 << 21],*p1 = buf,*p2 = buf;
	inline int getc() {
		return p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1 << 21,stdin),p1 == p2) ? EOF : *p1 ++;
	}
	inline int read() {
		int ret = 0,f = 0;char ch = getc();
		while (!isdigit(ch)) {
			if(ch == '-') f = 1;
			ch = getc();
		}
		while (isdigit(ch)) {
			ret = (ret << 3) + (ret << 1) + ch - 48;
			ch = getc();
		}
		return f ? -ret : ret;
	}
	inline double double_read() {
		long long ret = 0,w = 1,aft = 0,dot = 0,num = 0;
		char ch = getchar();
		while (!isdigit(ch)) {
			if (ch == '-') w = -1;
			ch = getchar();
		}
		while (isdigit(ch) || ch == '.') {
			if (ch == '.') {
				dot = 1;
			} else if (dot == 0) {
				ret = (ret << 3) + (ret << 1) + ch - 48;
			} else {
				aft = (aft << 3) + (aft << 1) + ch - '0';
				num ++;
			}
			ch = getchar();
		}
		return (pow(0.1,num) * aft + ret) * w;
	}
	inline void write(int x) {
		if(x < 0) {
			putchar('-');
			x = -x;
		}
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
	}
}
using namespace Newstd;

const int N = 1005;
int a[N];
bool vis[N];
int n;
bool mark;
inline void dfs(int step) {
	if (step == 2 * n + 1) {
		mark = true;
		return;
	}
	if (a[step]) {
		dfs(step + 1);
	} else {
		for (register int i = std::min(n,2 * n - step + 1);i >= 1; -- i) {
			if (!vis[i] && !a[step + i + 1]) {
				a[step] = a[step + i + 1] = i;
				vis[i] = true;
				dfs(step + 1);
				if (mark) return;
				vis[i] = false;
				a[step] = a[step + i + 1] = 0;
			}
		}
	}
}
int main(void) {
	n = read();
	if (n % 4 == 1 || n % 4 == 2) {
		puts("No Solution.");
	} else {
		dfs(1);
		for (register int i = 1;i <= 2 * n; ++ i) printf("%d%c",a[i]," \n"[i == 2 * n]);
	}
	
	return 0;
}
posted @ 2022-06-18 11:28  Coros_Trusds  阅读(53)  评论(0编辑  收藏  举报