Codeforces C. Almost Equal (数学规律)
题目链接:http://codeforces.com/contest/1206/problem/C
题解 : 观察可以发现当n为偶数时,1 - 2n是不满足题意的,可以举例n = 2,n = 4试一试。那么当n为奇数时,则满足题意,通过n = 3和n = 5的情况可以发现一个规律,当a[ 1 ] = 1时候,a[ n + 1] = 2 ,a[ 2 ] = 4,此时 a[ n + 1] = 3,a[ 3 ] = 5,则a[ n+ 3] = 6,发现a[ i ] 和a[ i + n] 是连续的两个数,而且其奇偶性是交错的,所以猜一下结论:当 i 为奇数时,a[ i ] = 2 * i -1, a[ i + n] = 2 * i;i为偶数时, a[ i ] = 2 * i , a[ i + n ] = 2 * i - 1 , 模拟一下这个过程即可。
AC代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#define maxn 100005
#define inf 0x3f3f3f3f
using namespace std;
int main(){
int n;
cin>>n;
if(n%2 == 0){
cout<<"NO";
return 0;
}
cout<<"YES"<<endl;
long long int a[n+2];
int cnt = 1;
for(int i = 1;i<=n;i++){
if(i%2 == 0){
cout<<2*i<<" ";
a[cnt] = 2*i-1;
cnt++;
}
else{
cout<<2*i-1<<" ";
a[cnt] = 2*i;
cnt++;
}
}
for(int i = 1;i<=n;i++){
if(i == n){
cout<<a[i];
}
else{
cout<<a[i]<<" ";
}
}
return 0;
}