CodeForces - 287C Lucky Permutation(构造)

Posted Frozen_Guardian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 287C Lucky Permutation(构造)相关的知识,希望对你有一定的参考价值。

题目链接:点击查看

题目大意:构造一个合法的排列,满足 p p i = n − i + 1 p_p_i=n-i+1 ppi=ni+1

题目分析:因为第四个样例的存在降低了本题的难度,不然感觉还是有点难度的一道题。

5
2 5 3 1 4

观察上面的这个构造方法,不难看出可以令四个数组成一个环,这样当 n n n 只有是 4 4 4 的倍数或者余数为 1 1 1 时才有解,多出来的那个数放到中间就好啦

一组合法的环: i → i + 1 → n − i + 1 → n − i → i i\\rightarrow i+1\\rightarrow n-i+1 \\rightarrow n-i \\rightarrow i ii+1ni+1nii

代码:

// Problem: Lucky Permutation
// Contest: Virtual Judge - CodeForces
// URL: https://vjudge.net/problem/CodeForces-287C
// Memory Limit: 262 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)

	T f=1;x=0;
	char ch=getchar();
	while(0==isdigit(ch))if(ch=='-')f=-1;ch=getchar();
	while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	x*=f;

template<typename T>
inline void write(T x)

	if(x<0)x=~(x-1);putchar('-');
    if(x>9)write(x/10);
    putchar(x%10+'0');

const int inf=0x3f3f3f3f;
const int N=1e6+100;
int ans[N];
int main()

#ifndef ONLINE_JUDGE
//	freopen("data.in.txt","r",stdin);
//	freopen("data.out.txt","w",stdout);
#endif
//	ios::sync_with_stdio(false);
	int n;
	read(n);
	if(n%4!=0&&n%4!=1) 
		puts("-1");
		return 0;
	
	if(n%4==1) 
		ans[n/2+1]=n/2+1;
	
	for(int i=1;i<=n/2;i+=2) 
		ans[i]=i+1;
		ans[i+1]=n-i+1;
		ans[n-i+1]=n-i;
		ans[n-i]=i;
	
	for(int i=1;i<=n;i++) 
		printf("%d ",ans[i]);
	
	return 0;

以上是关于CodeForces - 287C Lucky Permutation(构造)的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces - 808A Lucky Year

CodeForces - 808A Lucky Year

『题解』Codeforces121A Lucky Sum

Lucky Numbers (easy) CodeForces - 96B

CodeForces E. Lucky Array 幸运数列

@codeforces - 1096G@ Lucky Tickets