Codeforces 233A-Perfect Permutation(規律),codeforces

來源:互聯網
上載者:User

Codeforces 233A-Perfect Permutation(規律),codeforces
A. Perfect Permutationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

A permutation is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. Let's denote the i-th element of permutation p as pi. We'll call number n the size of permutation p1, p2, ..., pn.

Nickolas adores permutations. He likes some permutations more than the others. He calls such permutations perfect. A perfectpermutation is such permutation p that for any i (1 ≤ i ≤ n) (n is the permutation size) the following equations hold ppi = i and pi ≠ i. Nickolas asks you to print any perfect permutation of size n for the given n.

Input

A single line contains a single integer n (1 ≤ n ≤ 100) — the permutation size.

Output

If a perfect permutation of size n doesn't exist, print a single integer -1. Otherwise print n distinct integers from 1 to n, p1, p2, ..., pn — permutation p, that is perfect. Separate printed numbers by whitespaces.

Sample test(s)input
1
output
-1
input
2
output
2 1 
input
4
output
2 1 4 3 
題意: 給一個長度為n的序列p,序列中的元素大小範圍為[1,n]且各不相同,現在要求一個完美序列,使得 p[p[i]]=i&&p[i]!=i;
初看很難。仔細一想,假設初始化序列 f[i]=i;  現在只要交換奇偶位置上的元素值就可以了 即 swap(s[i],s[i+1]) (i%2==1) 前提條件是序列的大小為偶數
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <string>#include <vector>using namespace std;int main(){int n;while(cin>>n){if(n%2)puts("-1");elsefor(int i=1;i<=n;i++)if(i%2)cout<<i+1<<" ";    elsecout<<i-1<<" ";cout<<endl;}return 0;}



C語言Perfect Permutation的演算法

只有偶數個有答案,兩兩翻轉
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.