Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int v[100005] = {0};
int q[100005] = {0};
int main() {
int n;
scanf("%d", &n);
memset(v, -1, sizeof(v));
if ((n - (n % 2)) % 4) {
printf("-1\n");
return 0;
}
int cur = 0;
int sz = 1;
for (int i = 0; i < n / 2; i += 2) {
q[cur] = i;
v[i] = 0;
if (n > 1) v[i] = i + 1;
sz++;
while (cur < sz) {
int pos = q[cur];
if (v[v[pos]] == -1) {
v[v[pos]] = n - pos - 1;
q[sz++] = v[pos];
} else if (v[v[pos]] != n - pos - 1) {
printf("-1\n");
return 0;
}
cur++;
}
}
if (n % 2 != 0 && v[n / 2] == -1) v[n / 2] = n / 2, sz++;
if (sz < n) {
printf("-1\n");
return 0;
}
for (int i = 0; i < n; i++) printf("%d ", v[i] + 1);
printf("\n");
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.*;
import java.util.*;
public class A {
public static void main(String[] args) {
new A().run();
}
BufferedReader br;
StringTokenizer in;
PrintWriter out;
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public void solve() throws IOException {
int n = nextInt();
if (n % 4 == 2 || n % 4 == 3) {
out.println(-1);
return;
}
int[] p = new int[n];
if (n % 4 == 1)
p[n / 2] = n / 2;
for (int i = 0; i < n / 2; i += 2) {
p[i] = i + 1;
p[i + 1] = n - i - 1;
p[n - i - 1] = n - i - 2;
p[n - i - 2] = i;
}
for (int i = 0; i < p.length; i++) {
out.print((p[i] + 1) + " ");
}
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int arr[100005];
int main() {
memset(arr, -1, sizeof arr);
int n;
cin >> n;
if (n & 1) arr[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i < n / 2; i += 2) {
arr[i] = i + 1;
arr[i + 1] = n - i + 1;
arr[n - i] = i;
arr[n - i + 1] = n - i;
}
for (int i = 1; i <= n; i++) {
if (arr[i] == -1) {
puts("-1");
exit(0);
}
}
for (int i = 1; i <= n; i++) cout << arr[i] << " ";
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import sys
n = int(sys.stdin.read().strip())
if n == 1:
res = [1]
elif n % 4 in [2, 3]:
res = [-1]
elif n % 4 == 1:
res = [0] * n
for i in xrange(0, n, 2):
res[i] = i+2
for i in xrange(1, n, 2):
res[i] = n-i-1
for i in xrange(n/2, n, 2):
res[i] -= 2
for i in xrange(1, n/2, 2):
res[i] += 2
res[n/2] = n/2 + 1
else:
res = [0] * n
for i in xrange(0, n/2, 2):
res[i] = i+2
for i in xrange(n/2, n, 2):
res[i] = n-i-1
for i in xrange(1, n/2, 2):
res[i] = n-i+1
for i in xrange(n/2+1, n, 2):
res[i] = i
sys.stdout.write(' '.join([str(i) for i in res])) | PYTHON |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.util.*;
import java.math.*;
import java.io.*;
public class A{
static FastReader scan=new FastReader();
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
static LinkedList<Edge>edges[];
static boolean stdin = true;
static String filein = "input";
static String fileout = "output";
static int dx[] = { -1, 0, 1, 0 };
static int dy[] = { 0, 1, 0, -1 };
int dx_8[]={1,1,1,0,0,-1,-1,-1};
int dy_8[]={-1,0,1,-1,1,-1,0,1};
static char sts[]={'U','R','D','L'};
static boolean prime[];
static long LCM(long a,long b){
return (Math.abs(a*b))/gcd(a,b);
}
public static int upperBound(long[] array, int length, long value) {
int low = 0;
int high = length;
while (low < high) {
final int mid = low+(high-low) / 2;
if ( array[mid]>value) {
high = mid ;
} else {
low = mid+1;
}
}
return low;
}
static long gcd(long a, long b) {
if(a!=0&&b!=0)
while((a%=b)!=0&&(b%=a)!=0);
return a^b;
}
static int countSetBits(int n)
{
int count = 0;
while (n > 0) {
if((n&1)!=1)
count++;
//count += n & 1;
n >>= 1;
}
return count;
}
static void sieve(long n)
{
prime = new boolean[(int)n+1];
for(int i=0;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
}
static boolean isprime(long x)
{
for(long i=2;i*i<=x;i++)
if(x%i==0)
return false;
return true;
}
static int perm=0,FOR=0;
static boolean flag=false;
static int len=100000000;
static ArrayList<Pair>inters=new ArrayList<Pair>();
static StringBuilder sb;
static void swap(int i,int j,StringBuilder st)
{
char tmp=st.charAt(i);
st.setCharAt(i,st.charAt(j));
st.setCharAt(j,tmp);
}
private static int next(int[] arr, int target)
{
int start = 0, end = arr.length - 1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
// Move to right side if target is
// greater.
if(arr[mid]==target)
return mid;
if (arr[mid] <target) {
start = mid + 1;
}
// Move left side.
else {
ans = mid;
end = mid - 1;
}
}
return ans;
}
//static boolean vis[][];
static long solve(int h,long n,int cur)
{
if(h==0)
return 0;
long half=1L<<(h-1);
if(n<=half)
{
if((cur^1)==0)
return 1+solve(h-1,n,0);
else
return 2*half+solve(h-1,n,0);
}
else
{
if((cur^1)==0)
return 2*half+solve(h-1,n-half,1);
else
return 1+solve(h-1,n-half,1);
}
}
public static class comp1 implements Comparator<String>{
public int compare(String o1,String o2){
return o1.length()-o2.length();
}
}
public static class comp2 implements Comparator<String>{
public int compare(String o1,String o2){
return o1.compareTo(o2);
}
}
static StringBuilder a,b;
static boolean isPowerOfTwo(int n)
{
if(n==0)
return false;
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) ==
(int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
static ArrayList<Integer>v;
static ArrayList<Integer>pows;
static void block(long x)
{
v = new ArrayList<Integer>();
pows=new ArrayList<Integer>();
while (x > 0)
{
v.add((int)x % 2);
x = x / 2;
}
// Displaying the output when
// the bit is '1' in binary
// equivalent of number.
for (int i = 0; i < v.size(); i++)
{
if (v.get(i) == 1)
{
pows.add(i);
}
}
}
static int n;
static int h,m;
static StringBuilder tmp;
static boolean check(StringBuilder x)
{
tmp=new StringBuilder(x);
tmp.reverse();
if(tmp.charAt(0)=='3'||tmp.charAt(0)=='4'||tmp.charAt(0)=='6'||tmp.charAt(0)=='7'||tmp.charAt(0)=='9')
return false;
if(tmp.charAt(1)=='3'||tmp.charAt(1)=='4'||tmp.charAt(1)=='6'||tmp.charAt(1)=='7'||tmp.charAt(1)=='9')
return false;
if(tmp.charAt(3)=='3'||tmp.charAt(3)=='4'||tmp.charAt(3)=='6'||tmp.charAt(3)=='7'||tmp.charAt(3)=='9')
return false;
if(tmp.charAt(4)=='3'||tmp.charAt(4)=='4'||tmp.charAt(4)=='6'||tmp.charAt(4)=='7'||tmp.charAt(4)=='9')
return false;
if(tmp.charAt(0)=='2')
tmp.setCharAt(0,'5');
else if(tmp.charAt(0)=='5')
tmp.setCharAt(0,'2');
if(tmp.charAt(1)=='2')
tmp.setCharAt(1,'5');
else if(tmp.charAt(1)=='5')
tmp.setCharAt(1,'2');
if(tmp.charAt(3)=='2')
tmp.setCharAt(3,'5');
else if(tmp.charAt(3)=='5')
tmp.setCharAt(3,'2');
if(tmp.charAt(4)=='5')
tmp.setCharAt(4,'2');
else if(tmp.charAt(4)=='2')
tmp.setCharAt(4,'5');
// if(x.toString().equals("50:00"))
// out.println(tmp.charAt(4));
StringBuilder s=new StringBuilder("");
s.append(tmp.charAt(0));
s.append(tmp.charAt(1));
if(Integer.parseInt(s.toString())>=h)
return false;
s=new StringBuilder("");
s.append(tmp.charAt(3));
s.append(tmp.charAt(4));
if(Integer.parseInt(s.toString())>=m)
return false;
return true;
}
public static void main(String[] args) throws Exception
{
//SUCK IT UP AND DO IT ALRIGHT
//scan=new FastReader("div7.in");
//out = new PrintWriter("div7.out");
//System.out.println(countSetBits(2015));
//int elem[]={1,2,3,4,5};
//System.out.println("avjsmlfpb".compareTo("avjsmbpfl"));
int tt=1;
/*for(int i=0;i<=100;i++)
if(prime[i])
arr.add(i);
System.out.println(arr.size());*/
// check(new StringBuilder("05:11"));
//System.out.println(tmp);
// tt=scan.nextInt();
outer:while(tt-->0)
{
int n=scan.nextInt();
int idx=1;
int arr[]=new int[n+1];
//int el=2;
//0 0 0 0 0 0 0
//arr[1]=el;
//el=2 idx=1
int el=2;
int k=2;
//out.println(5/4);
int tmp=n;
idx=1;
for(int j=0;j<=n/4&&tmp>1;j++,tmp-=4){
for(int i=1;i<=4;i++)
{
// System.out.println(el);
arr[el]=n-idx+1;
idx=el;
el=arr[el];
//out.println(el+" "+idx);
}
//for(int i=1;i<=n;i++)
//out.print(arr[i]+" ");
//out.println();
idx+=2;
el+=2;
}
if(n%2==1)
{
arr[n/2+1]=n/2+1;
}
for(int i=1;i<=n;i++)
{
if(arr[arr[i]]!=n-i+1){
out.println(-1);
out.close();
return;
}
}
for(int i=1;i<=n;i++)
out.print(arr[i]+" ");
}
out.close();
//SEE UP
}
static class special{
char c;
int idx;
special(char c,int idx)
{
this.c=c;
this.idx=idx;
}
}
static long binexp(long a,long n)
{
if(n==0)
return 1;
long res=binexp(a,n/2);
if(n%2==1)
return res*res*a;
else
return res*res;
}
static long powMod(long base, long exp, long mod) {
if (base == 0 || base == 1) return base;
if (exp == 0) return 1;
if (exp == 1) return base % mod;
long R = powMod(base, exp/2, mod) % mod;
R *= R;
R %= mod;
if ((exp & 1) == 1) {
return base * R % mod;
}
else return R % mod;
}
static double dis(double x1,double y1,double x2,double y2)
{
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
static long mod(long x,long y)
{
if(x<0)
x=x+(-x/y+1)*y;
return x%y;
}
public static long pow(long b, long e) {
long r = 1;
while (e > 0) {
if (e % 2 == 1) r = r * b ;
b = b * b;
e >>= 1;
}
return r;
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long object : arr) list.add(object);
Collections.sort(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
public static class FastReader {
BufferedReader br;
StringTokenizer root;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
FastReader(String filename)throws Exception
{
br=new BufferedReader(new FileReader(filename));
}
boolean hasNext(){
String line;
while(root.hasMoreTokens())
return true;
return false;
}
String next() {
while (root == null || !root.hasMoreTokens()) {
try {
root = new StringTokenizer(br.readLine());
} catch (Exception addd) {
addd.printStackTrace();
}
}
return root.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception addd) {
addd.printStackTrace();
}
return str;
}
public int[] nextIntArray(int arraySize) {
int array[] = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = nextInt();
}
return array;
}
}
static class Pair implements Comparable<Pair>{
public int x, y;
public Pair(int x1, int y1) {
x=x1;
y=y1;
}
@Override
public int hashCode() {
return (int)(x + 31 * y);
}
public String toString() {
return x + " " + y;
}
@Override
public boolean equals(Object o){
if (o == this) return true;
if (o.getClass() != getClass()) return false;
Pair t = (Pair)o;
return t.x == x && t.y == y;
}
public int compareTo(Pair o)
{
return (x-o.x);
}
static class pair{
int i;
int j;
pair(int i,int j){
this.i=i;
this.j=j;
}}}
static class tuple{
int x,y,z;
tuple(int a,int b,int c){
x=a;
y=b;
z=c;
}
}
static class Edge{
int d,w;
Edge(int d,int w)
{
this.d=d;
this.w=w;
}
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | a = [0]*100010
n = int(raw_input())
if n % 4 > 1:
print '-1'
else:
stpos = 1
edpos = n-1
stval = 2
edval = 1
cnt = n/4
for i in range(cnt):
a[stpos] = stval
a[edpos] = edval
a[stpos+1] = n +2 - stval
a[edpos+1] = n - edval
stpos += 2
edpos -= 2
stval += 2
edval += 2
if n % 4 == 1:
a[stpos] = stval - 1
for i in range(1,n+1):
print a[i],
print '\n'
| PYTHON |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | n = input()
p = []
for i in range(0, n / 2, 2):
p += [i + 2, n - i]
if n % 4:
p += [n / 2 + 1]
for i in range(n / 2 - 1, 0, -2):
p += [i, n - i]
if n % 4 > 1:
p = [-1]
print ' '.join(map(str, p)) | PYTHON |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author zodiacLeo
*/
public class Main
{
public static void main(String[] args)
{
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC
{
public void solve(int testNumber, FastScanner in, FastPrinter out)
{
int n = in.nextInt();
if (n == 1)
{
out.println(1);
return;
}
if (n == 2)
{
out.println(-1);
return;
}
if (n % 4 > 1)
{
out.println(-1);
return;
}
int[] a = new int[n + 1];
for (int i = 1; i < n / 2; i += 2)
{
int p1 = i;
int q1 = n + 1 - i;
int p2 = p1 + 1;
int q2 = q1 - 1;
a[p1] = p2;
a[p2] = q1;
a[q1] = q2;
a[q2] = p1;
}
if (n % 2 != 0)
{
a[(n / 2) + 1] = (n / 2) + 1;
}
StringBuilder sb = new StringBuilder("");
for (int i = 1; i <= n; i++)
{
sb.append(a[i] + " ");
}
out.println(sb);
}
}
static class FastScanner
{
public BufferedReader br;
public StringTokenizer st;
public FastScanner(InputStream is)
{
br = new BufferedReader(new InputStreamReader(is));
}
public FastScanner(File f)
{
try
{
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
public String next()
{
while (st == null || !st.hasMoreElements())
{
String s = null;
try
{
s = br.readLine();
} catch (IOException e)
{
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
}
static class FastPrinter extends PrintWriter
{
public FastPrinter(OutputStream out)
{
super(out);
}
public FastPrinter(Writer out)
{
super(out);
}
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author vadimmm
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
if ((n & 3) > 1)
out.println(-1);
else {
int[] permutation = new int[n + 1];
int up = n >> 1;
if ((n & 1) == 1)
permutation[up + 1] = up + 1;
for (int i = 1; i <= up; i += 2) {
permutation[i] = i + 1;
permutation[n - i + 1] = n - i;
permutation[i + 1] = n - i + 1;
permutation[n - i] = i;
}
out.print(permutation[1]);
for (int i = 2; i <= n; ++i) {
out.print(" ");
out.print(permutation[i]);
}
}
}
}
class InputReader {
private static BufferedReader bufferedReader;
private static StringTokenizer stringTokenizer;
public InputReader(InputStream inputStream) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
stringTokenizer = null;
}
public String next() {
while(stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
try {
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return stringTokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
long a[110000];
int main() {
int n, i, j;
scanf("%d", &n);
if ((n % 4) > 1) {
printf("-1\n");
} else {
for (i = 1; i <= n / 2; i += 2) {
a[i] = i + 1;
a[i + 1] = n - i + 1;
a[n - i] = i;
a[n - i + 1] = n - i;
}
if ((n % 2) == 1) {
a[n / 2 + 1] = n / 2 + 1;
}
printf("%d", a[1]);
for (i = 2; i <= n; i++) {
printf(" %d", a[i]);
}
printf("\n");
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.util.Scanner;
public class CodeForces {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while (s.hasNext()) {
int n = s.nextInt();
int[] p = new int[n+1];
p = getLuckyPerm(p, n);
if (p[0] == 0) {
System.out.println("-1");
} else {
for (int i=1; i <= n; i++) {
System.out.print(p[i]);
if (i != n) {
System.out.print(' ');
}
}
}
}
}
public static int[] getLuckyPerm(int[] p, int n) {
int m = -1;
switch ( n%2 ) {
case 1:
m = (n+1)/2 - 1; p[m+1] = m+1; break;
case 0:
m = (n+1)/2; break;
default:
break;
}
if ( m%2 == 0) {
for (int i=1; i <= m-1; i+=2) {
p[i] = i+1; p[i+1] = n-i+1;
p[n-i] = i; p[n-i+1] = n-i;
}
p[0] = 1;
} else {
p[0] = 0;
}
return p;
}
} | JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
const int N = 100100;
using namespace std;
int n, p[N];
int l;
void dfs(int x) {
p[p[x]] = n - x + 1;
int y = p[x];
while (y != x) {
p[p[y]] = n - y + 1;
y = p[y];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
if (n % 2) p[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i <= n; i++) {
if (p[i]) continue;
l = i;
p[i] = i + 1;
dfs(i);
}
for (int i = 1; i <= n; i++) {
if (p[p[i]] != n - i + 1) {
cout << -1 << endl;
return 0;
}
}
for (int i = 1; i <= n; i++) cout << p[i] << " ";
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
int n, t, a[110000], tnum, cas;
int main() {
cas = 0;
scanf("%d", &n);
if (n == 1) {
printf("1\n");
} else {
if (n % 2 != 0) {
a[(n / 2) + 1] = (n / 2) + 1;
tnum = n - 1;
} else {
tnum = n;
}
if (tnum % 4 != 0) {
printf("-1\n");
} else {
for (int i = 1; i <= n / 2; i += 2) {
a[i] = n - i;
a[n - i] = n - i + 1;
a[n - i + 1] = i + 1;
a[i + 1] = i;
}
for (int i = 1; i <= n; i++) {
printf("%d", a[i]);
if (i != n) {
printf(" ");
}
}
printf("\n");
}
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class C {
static StreamTokenizer st;
static PrintWriter pw;
private static int nextInt() throws IOException {
st.nextToken();
return (int) st.nval;
}
public static void main(String[] args) throws IOException {
st = new StreamTokenizer(new BufferedReader(new InputStreamReader(
System.in)));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
System.out)));
// -----------begin-------------------------
int n = nextInt();
if ((n - 1) % 4 == 1 || (n - 1) % 4 == 2) {
pw.println("-1");
pw.close();
return;
}
if (n % 2 == 0) {
for (int i = 0; i < n/2; i += 2) {
pw.print(i + 2+ " ");
pw.print(n-i+" ");
}
int a[][] = new int [n/2][2];
int cnt = 0;
for (int i = 0; i < n/2; i += 2) {
a[cnt][0] = i+1;
a[cnt][1] = n-i-1;
cnt++;
}
for (int i = cnt-1; i >= 0; i--) {
pw.print(a[i][0]+" ");
pw.print(a[i][1]+" ");
}
} else {
for (int i = 0; i < n/2; i += 2) {
pw.print(i + 2+ " ");
pw.print(n-i+" ");
}
int a[][] = new int [n/2][2];
int cnt = 0;
for (int i = 0; i < n/2; i += 2) {
a[cnt][0] = i+1;
a[cnt][1] = n-i-1;
cnt++;
}
pw.print(n/2+1+" ");
for (int i = cnt-1; i >= 0; i--) {
pw.print(a[i][0]+" ");
pw.print(a[i][1]+" ");
}
}
// pw.print(n);
pw.close();
// ------------end--------------------------
}
} | JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
bool flag[20];
int ans[20];
bool dfs(int k, int n) {
if (k > n) {
bool temp = true;
for (int i = 1; i <= n; i++)
if (ans[ans[i]] != n - i + 1) temp = false;
if (temp) {
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
printf("\n");
return true;
}
return false;
}
for (int i = 1; i <= n; i++)
if (flag[i]) {
flag[i] = false;
ans[k] = i;
bool temp = dfs(k + 1, n);
if (temp) return true;
flag[i] = true;
}
}
int main() {
int n;
scanf("%d", &n);
if (n == 1) {
printf("1\n");
return 0;
}
if (((n) / 2) % 2 == 1) {
printf("-1\n");
return 0;
}
int out[100010];
out[0] = 2;
out[1] = n;
out[n - 1] = n - 1;
out[n - 2] = 1;
int head = 2;
int tail = n - 3;
while (head < tail) {
if (head % 2 == 0)
out[head] = out[head - 2] + 2;
else
out[head] = out[head - 2] - 2;
if ((n - 1 - tail) % 2 == 0)
out[tail] = out[tail + 2] - 2;
else
out[tail] = out[tail + 2] + 2;
head++;
tail--;
}
if (head == tail) out[head] = (n + 1) / 2;
printf("%d", out[0]);
for (int i = 1; i < n; i++) printf(" %d", out[i]);
printf("\n");
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
if (n % 4 == 2 || n % 4 == 3) {
cout << -1;
return 0;
}
int ans[n + 10];
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 + 1 - i;
ans[n + 1 - i] = n - i;
ans[n - i] = i;
}
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
cout << endl;
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
if (n % 4 == 2 || n % 4 == 3) {
printf("-1\n");
return 0;
}
int lo, hi;
int A[100006];
lo = 1;
hi = n;
while (lo < hi) {
A[lo] = lo + 1;
A[lo + 1] = hi;
A[hi] = hi - 1;
A[hi - 1] = lo;
lo += 2;
hi -= 2;
}
if (lo == hi) A[lo] = lo;
for (int i = 1; i < n + 1; i++) printf("%d ", A[i]);
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
int a[100001];
int main() {
int n, i;
scanf("%d", &n);
if (n % 4 == 2 || n % 4 == 3)
printf("-1\n");
else {
for (i = 1; i <= n / 2; i = i + 2) {
a[i] = i + 1;
a[i + 1] = n + 1 - i;
a[n - i] = i;
a[n + 1 - i] = n - i;
}
if (n % 4 == 1) a[i] = i;
for (i = 1; i < n; i++) printf("%d ", a[i]);
printf("%d\n", a[n]);
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
int n;
cin >> n;
if (n % 4 >= 2) return puts("-1"), 0;
for (int i = 1; i <= n / 2; i += 2) {
a[i] = i + 1;
a[i + 1] = n - i + 1;
a[n - i + 1] = n - i;
a[n - i] = i;
}
if (n % 2 == 1) a[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i == n];
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int a[100005], n;
bool tui() {
int nd2 = n / 2, w = 2;
if (n % 4 == 0) {
for (int i = 1; i <= nd2; i += 2) {
a[i] = w, a[i + 1] = n + 2 - w;
w += 2;
}
w = nd2 - 1;
for (int i = nd2 + 1; i <= n; i += 2) {
a[i] = w, a[i + 1] = n - w, w -= 2;
}
} else if (n % 4 == 1) {
a[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i <= nd2; i += 2) {
a[i] = w, a[i + 1] = n + 2 - w;
w += 2;
}
w = nd2 - 1;
for (int i = nd2 + 2; i <= n; i += 2) {
a[i] = w, a[i + 1] = n - w, w -= 2;
}
} else
return false;
return true;
}
int main() {
scanf("%d", &n);
if (n == 1) {
printf("1\n");
return 0;
}
a[1] = 2;
if (tui()) {
for (int i = 1; i < n; ++i) printf("%d ", a[i]);
printf("%d\n", a[n]);
} else {
printf("-1\n");
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
int p[100009], n, s, t, i;
bool none = false;
int main() {
scanf("%d", &n);
for (s = 1, t = n; s <= t;) {
if (t - s + 1 >= 4) {
p[s] = s + 1;
p[s + 1] = t;
p[t] = t - 1;
p[t - 1] = s;
s += 2;
t -= 2;
} else {
if (t - s + 1 >= 2) {
none = 1;
break;
} else {
p[s] = t;
s++;
t--;
}
}
}
if (none)
printf("-1\n");
else
for (i = 1; i <= n; i++) printf("%d ", p[i]);
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | n = input()
if 2 == n % 4 or 3 == n % 4:
print -1
exit()
cnt4 = n / 4
a = [0] * n
i = 0
while cnt4 > 0:
cnt4 -= 1
a[i] = i + 2
a[i + 1] = n - i
a[n - 1 - i] = n - i - 1
a[n - 2 - i] = i + 1
i += 2
if 1 == n % 4: a[i] = i + 1
for x in a: print x,
| PYTHON |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int a[100009];
bool FL;
void f(int st, int en, int i, int j) {
if (st > en) return;
if (st == en)
a[i] = st;
else {
if ((j - i) == 1 || (j - i) == 2) {
FL = false;
return;
}
a[i] = st + 1;
a[i + 1] = en;
a[j] = en - 1;
a[j - 1] = st;
f(st + 2, en - 2, i + 2, j - 2);
}
}
int main() {
int n;
cin >> n;
FL = true;
f(1, n, 1, n);
if (FL) {
for (typeof(1) i = (1); i < (n); i++) cout << a[i] << " ";
cout << a[n] << "\n";
for (typeof(1) i = (1); i < (n + 1); i++) assert(a[a[i]] == (n - i + 1));
} else
cout << "-1\n";
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | n = int(input())
if n%4 > 1:
print(-1)
exit()
a = [(n+1) // 2] * n
for i in range(n//4):
j = 2*i
a[j], a[j+1], a[-j-2], a[-j-1] = j+2, n-j, j+1, n-j-1
print(' '.join(map(str, a)))
| PYTHON3 |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long int inf = 1e14;
long long int mod = 1e9 + 7;
char en = '\n';
long long int arr[300005];
long long int res[300005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(arr, 0, sizeof(arr));
long long int n;
cin >> n;
if ((n % 4 == 2) or (n % 4 == 3)) {
cout << -1 << en;
return 0;
}
long long int leftPtr = 100000, rightPtr;
vector<long long int> left1, right1;
vector<long long int> curr;
if (n & 1) {
curr.push_back(1);
rightPtr = 100000;
} else {
rightPtr = 100003;
curr.push_back(2);
curr.push_back(4);
curr.push_back(1);
curr.push_back(3);
}
long long int currSize = curr.size() + 4;
while (currSize <= n) {
arr[leftPtr] += 2;
arr[rightPtr + 1] -= 2;
leftPtr -= 2;
rightPtr += 2;
left1.push_back(currSize);
left1.push_back(2);
right1.push_back(1);
right1.push_back(currSize - 1);
currSize += 4;
}
long long int cnt = 0;
reverse(left1.begin(), left1.end());
for (long long int x : left1) res[++cnt] = x;
for (long long int x : curr) res[++cnt] = x;
for (long long int x : right1) res[++cnt] = x;
for (long long int i = leftPtr; i <= rightPtr; i++) {
arr[i] += arr[i - 1];
cout << res[i - leftPtr + 1] + arr[i] << " ";
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.*;
import java.util.*;
public class Main {
private InputStream is;
private PrintWriter out;
int time = 0, val[][], dp[][], DP[], start[], end[], dist[], black[], MOD = (int)(1e9+7), arr[], weight[][], x[], y[], parent[];
int MAX = 800000, N, K;
long red[];
ArrayList<Integer>[] amp;
ArrayList<Pair>[][] pmp;
boolean b[], boo[][];
Pair prr[];
HashMap<Integer,Long> hm[] = new HashMap[305];
long Dp[][][][] = new long[110][110][12][12];
void soln() {
is = System.in;
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
//out.close();
out.flush();
//tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Thread(null, new Runnable() {
public void run() {
try {
//new CODEFORCES().soln();
} catch (Exception e) {
System.out.println(e);
}
}
}, "1", 1 << 26).start();
new Main().soln();
}
int ans = 0, cost = 0;
char ch[], ch1[];
void solve() {
int n = ni();
if(n==1){
System.out.println(1);
return;}
if(n%4==2 || n%4==3){
System.out.println(-1);
return;
}
int arr[] = new int[n+10];
if(n%4==0){
int c = 1;
for(int i = 2;i<=n/2;i+=2){
arr[c] = i;
arr[c+1] = n-i+2;
c+=2;
}
c = n-1;
for(int i = 1;i<n/2;i+=2){
arr[c] = i;
arr[c+1] = n-i;
c-=2;
}
}
if(n%4==1){
arr[(n+1)/2] = (n+1)/2;
int c = 1;
for(int i = 2;i<=n/2;i+=2){
arr[c] = i;
arr[c+1] = n-i+2;
c+=2;
}
c = n;
for(int i = n-1;i>n/2;i-=2){
arr[c] = i;
arr[c-1] = n-i;
c-=2;
}
}
/*int arr[] = new int[n+10];
arr[1] = 2;
arr[2] = n;
arr[n] = n-1;
arr[n-1] = 1;
int x = 3, y = n-2;
for(int i = n-2;i>(n+1)/2;i--){
arr[i] = x++;
arr[n-i+1] = y--;
}
if(n%2==1) arr[(n+1)/2] = (n+1)/2;
//System.out.println(ts);
//if(n%2==0) arr[3] = 3;*/
for(int i = 1;i<=n;i++) out.print(arr[i]+" ");
}
class Pair implements Comparable<Pair>{
int u, v, i;
Pair(int u, int v){
this.u = u;
this.v = v;
}
Pair(){
}
Pair(int u, int v, int i){
this.u = u;
this.v = v;
this.i = i;
}
public int hashCode() {
return Objects.hash();
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return ((u == other.u && v == other.v));
}
public int compareTo(Pair other) {
//return Integer.compare(val, other.val);
return Long.compare(u, other.u) != 0 ? (Long.compare(u, other.u)) : (Long.compare(v,other.v));
}
public String toString(){
return this.u +" " + this.v;
}
}
int max(int a, int b){
if(a>b) return a;
return b;
}
public static class FenwickTree {
int[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates
public FenwickTree(int size) {
array = new int[size + 1];
}
public int rsq(int ind) {
assert ind > 0;
int sum = 0;
while (ind > 0) {
sum += array[ind];
//Extracting the portion up to the first significant one of the binary representation of 'ind' and decrementing ind by that number
ind -= ind & (-ind);
}
return sum;
}
public int rsq(int a, int b) {
assert b >= a && a > 0 && b > 0;
return rsq(b) - rsq(a - 1);
}
public void update(int ind, int value) {
assert ind > 0;
while (ind < array.length) {
array[ind] += value;
//Extracting the portion up to the first significant one of the binary representation of 'ind' and incrementing ind by that number
ind += ind & (-ind);
}
}
public int size() {
return array.length - 1;
}
}
void buildGraph(int n){
for(int i = 0; i<n ;i++){
int x1 = ni() -1, y1 = ni()-1;
amp[x1].add(y1);
amp[y1].add(x1);
x[i] = x1; y[i] = y1;
}
}
long modInverse(long a, long mOD2){
return power(a, mOD2-2, mOD2);
}
long power(long x, long y, long m)
{
if (y == 0)
return 1;
long p = power(x, y/2, m) % m;
p = (p * p) % m;
return (y%2 == 0)? p : (x * p) % m;
}
public int gcd(int a, int b){
if(b==0) return a;
return gcd(b,a%b);
}
static class ST1{
int arr[], st[], size;
ST1(int a[]){
arr = a.clone();
size = 10*arr.length;
st = new int[size];
build(0,arr.length-1,1);
}
void build(int ss, int se, int si){
if(ss==se){
st[si] = arr[ss];
return;
}
int mid = (ss+se)/2;
int val = 2*si;
build(ss,mid,val); build(mid+1,se,val+1);
st[si] = Math.min(st[val], st[val+1]);
}
int get(int ss, int se, int l, int r, int si){
if(l>se || r<ss || l>r) return Integer.MAX_VALUE;
if(l<=ss && r>=se) return st[si];
int mid = (ss+se)/2;
int val = 2*si;
return Math.min(get(ss,mid,l,r,val), get(mid+1,se,l,r,val+1));
}
}
static class ST{
int arr[],lazy[],n;
ST(int a){
n = a;
arr = new int[10*n];
lazy = new int[10*n];
}
void up(int l,int r,int val){
update(0,n-1,0,l,r,val);
}
void update(int l,int r,int c,int x,int y,int val){
if(lazy[c]!=0){
lazy[2*c+1]+=lazy[c];
lazy[2*c+2]+=lazy[c];
if(l==r)
arr[c]+=lazy[c];
lazy[c] = 0;
}
if(l>r||x>y||l>y||x>r)
return;
if(x<=l&&y>=r){
lazy[c]+=val;
return ;
}
int mid = l+r>>1;
update(l,mid,2*c+1,x,y,val);
update(mid+1,r,2*c+2,x,y,val);
arr[c] = Math.max(arr[2*c+1], arr[2*c+2]);
}
int an(int ind){
return ans(0,n-1,0,ind);
}
int ans(int l,int r,int c,int ind){
if(lazy[c]!=0){
lazy[2*c+1]+=lazy[c];
lazy[2*c+2]+=lazy[c];
if(l==r)
arr[c]+=lazy[c];
lazy[c] = 0;
}
if(l==r)
return arr[c];
int mid = l+r>>1;
if(mid>=ind)
return ans(l,mid,2*c+1,ind);
return ans(mid+1,r,2*c+2,ind);
}
}
public static int[] shuffle(int[] a, Random gen){
for(int i = 0, n = a.length;i < n;i++)
{
int ind = gen.nextInt(n-i)+i;
int d = a[i];
a[i] = a[ind];
a[ind] = d;
}
return a;
}
long power(long x, long y, int mod){
long ans = 1;
while(y>0){
if(y%2==0){
x = (x*x)%mod;
y/=2;
}
else{
ans = (x*ans)%mod;
y--;
}
}
return ans;
}
// To Get Input
// Some Buffer Methods
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
// ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) {
if (!oj)
System.out.println(Arrays.deepToString(o));
}
} | JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, x, y, a[100010];
int main() {
scanf("%d", &n);
if (n % 4 != 0 && n % 4 != 1) {
printf("-1");
return 0;
}
for (int i = 1; i <= n / 2; i += 2) {
int x = i, y = i + 1;
for (int j = 1; j <= 4; j++) {
a[x] = y;
y = n - x + 1;
x = a[x];
}
}
if (n % 4 == 1) a[(n / 2) + 1] = (n / 2) + 1;
for (int i = 1; i <= n; i++) printf("%d ", a[i]);
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int a[1000000 + 10];
void dfs(int l, int r) {
if (r - l + 1 < 4) return;
a[l] = l + 1;
a[l + 1] = r;
a[r] = r - 1;
a[r - 1] = l;
dfs(l + 2, r - 2);
}
int main() {
int xiaohao;
int zheshixiaohao;
int n;
cin >> n;
if (n == 1) {
cout << "1";
}
if (n == 2) {
cout << "-1";
}
if (n == 3) {
cout << "-1";
}
if (n == 4) {
cout << "2 4 1 3";
}
if (n == 5) {
cout << "2 5 3 1 4";
}
if (n == 6) {
cout << "-1";
}
if (n > 6)
if (n % 4 == 2 || n % 4 == 3)
cout << -1;
else {
for (int i = 1; i <= n; i++) a[i] = i;
dfs(1, n);
for (int i = 1; i < n; i++) cout << a[i] << " ";
cout << a[n];
}
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | //package round176;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class A {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni();
if(n % 4 == 2 || n % 4 == 3){
out.println(-1);
}else if(n % 4 == 0){
int[] a = new int[n];
for(int i = 0;i < n/2;i+=2){
a[i] = i+1;
a[i+1] = n-i-1;
a[n-1-i] = n-(i+1)-1;
a[n-1-(i+1)] = i;
}
for(int i = 0;i < n;i++){
if(i > 0)out.print(" ");
out.print(a[i]+1);
}
out.println();
}else{
int[] a = new int[n];
for(int i = 0;i < n/2;i+=2){
a[i] = i+1;
a[i+1] = n-i-1;
a[n-1-i] = n-(i+1)-1;
a[n-1-(i+1)] = i;
}
a[n/2] = n/2;
for(int i = 0;i < n;i++){
if(i > 0)out.print(" ");
out.print(a[i]+1);
}
out.println();
}
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new A().run(); }
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
long time = System.nanoTime();
int n = next();
if (n % 4 == 2 || n % 4 == 3) {
out.println(-1);
out.close();
return;
}
int[] x = new int[n];
for (int i = 0; 2*i + 1 < n; i += 2) {
int a = i;
int b = i + 1;
int c = n - 1 - i;
int d = n - 1 - i - 1;
x[a] = d;
x[d] = c;
x[c] = b;
x[b] = a;
}
if (n % 2 == 1) x[n/2] = n/2;
for (int i = 0; i < n; i++) out.print((x[i] + 1) + " ");
out.println();
// System.out.println((System.nanoTime() - time)* 1e-9);
out.close();
}
static PrintWriter out = new PrintWriter(System.out);
static BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer in = new StringTokenizer("");
static String nextToken() throws Exception {
if (!in.hasMoreTokens()) in = new StringTokenizer(bufferedreader.readLine());
return in.nextToken();
}
static int next() throws Exception {return Integer.parseInt(nextToken());};
static int[] next(int n) throws Exception {
int[] x = new int[n];
for (int i = 0; i < n; i++) x[i] = next();
return x;
}
static int[][] next(int n, int m) throws Exception {
int[][] x = new int[n][];
for (int i = 0; i < n; i++) x[i] = next(m);
return x;
}
static long nextl() throws Exception {return Long.parseLong(nextToken());};
static long[] nextl(int n) throws Exception {
long[] x = new long[n];
for (int i = 0; i < n; i++) x[i] = nextl();
return x;
}
static long[][] nextl(int n, int m) throws Exception {
long[][] x = new long[n][];
for (int i = 0; i < n; i++) x[i] = nextl(m);
return x;
}
static double nextd() throws Exception {return Double.parseDouble(nextToken());};
static double[] nextd(int n) throws Exception {
double[] x = new double[n];
for (int i = 0; i < n; i++) x[i] = nextd();
return x;
}
static double[][] nextd(int n, int m) throws Exception {
double[][] x = new double[n][];
for (int i = 0; i < n; i++) x[i] = nextd(m);
return x;
}
static String nextline() throws Exception {
in = new StringTokenizer("");
return bufferedreader.readLine();
}
} | JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.util.Scanner;
public class c_176 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if ((n-1) % 4 == 1 || (n-1) % 4 == 2) {
System.out.println(-1);
return;
}
if (n % 2 == 0) {
for (int i = 2; i <= n / 2; i += 2) {
System.out.print(i + " " + ((n + 2) - i) + " ");
}
for (int i = n / 2 - 1; i >= 1; i -= 2) {
System.out.print(i + " " + (n - i) + " ");
}
} else {
for (int i = 2; i <= n / 2; i += 2) {
System.out.print(i + " " + ((n + 2) - i) + " ");
}
System.out.print(n/2+1+" ");
for (int i = n / 2 - 1; i >= 1; i -= 2) {
System.out.print(i + " " + (n - i) + " ");
}
}
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | n=int(input())
if n%4>1:print(-1)
else:
ans=[i for i in range(1,n+1)]
for i in range(0,n//2,2):
ans[i]=i+2
ans[i+1]=n-i
ans[n-i-1]=n-i-1
ans[n-i-2]=i+1
print(*ans)
| PYTHON3 |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int a[100001];
int main() {
int i, j, k, m, n;
cin >> n;
if (n % 4 > 1) {
cout << "-1" << endl;
return 0;
}
int _front = 0, end_ = n - 1;
list<int> L;
for (i = 1; i <= n; i++) L.push_back(i);
while (!L.empty()) {
if (_front == end_) {
a[end_] = L.front();
L.pop_back();
break;
}
int front1 = L.front();
L.pop_front();
int front2 = L.front();
L.pop_front();
int end1 = L.back();
L.pop_back();
int end2 = L.back();
L.pop_back();
a[_front] = front2;
a[_front + 1] = end1;
a[end_] = end2;
a[end_ - 1] = front1;
_front += 2;
end_ -= 2;
}
for (i = 0; i < n; i++) cout << a[i] << ' ';
cout << endl;
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int i, n;
bool was[1000001];
vector<int> ans, a;
int main() {
cin >> n;
for (i = 1; i <= n; i += 2) {
if (i + 1 > n - i + 1) break;
ans.push_back(i + 1);
ans.push_back(n - i + 1);
was[i + 1] = was[n - i + 1] = true;
}
for (i = 1; i <= n; i++) {
if (was[i] == false) a.push_back(i);
}
if (a.size() % 2 == 1) {
ans.push_back(a[a.size() / 2]);
a.erase(a.begin() + (a.size() / 2));
}
for (i = 0; i <= n; i++) {
if ((a.size() / 2 - 1) - i < 0 || (a.size() / 2 - 1) + i + 1 > a.size())
break;
ans.push_back(a[(a.size() / 2 - 1) - i]);
ans.push_back(a[(a.size() / 2 - 1) + i + 1]);
}
for (int j = 0; j < n; j++)
if (ans[ans[j] - 1] != n - j) cout << -1, exit(00);
for (i = 1; i <= n; i++) cout << ans[i - 1] << " ";
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e1 + 4;
vector<int> circle;
bool vis[N];
int parent[N];
int chk[N];
int color[N];
int tim[N];
int dis[N];
int position[N];
vector<int> adj[N];
vector<int> adj1[N];
vector<int> graph[N];
bool has_cycle;
int maxdis, maxnode, Totnode, depth = 1;
bool ok;
queue<int> q;
stack<int> stk;
vector<int> solution;
int indegree[N];
int go[N];
int to[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t;
int cas = 0;
int n, m, i, j, cnt = 0, cnt1 = 0, cnt2 = 0, even = 0, odd = 0, len, k, r, l,
z = 0, x = 0, y = 0, flag = 0, sum = 0;
int a = 0, b = 0, c = 0, d = 0, p, q, ans = 0LL, rem, quot, zero = 0, fst = 0,
null = 0, snd = 0, lst = 0, rone = 0, one = 0, pos = 0, neg = 0,
mn = INT_MAX, mx = INT_MIN;
char ch;
int h1, h2, m1, m2, h;
int velo1, velo2, ac1, ac2, tim;
int node, edge, u, v, cost;
int best, worst;
double nd, ad, bd, xd;
string str, str1 = "", str2 = "", str3 = "", strstore1 = "", strstore2 = "";
cin >> n;
int arr[n + 4];
if (n % 4 <= 1) {
x = 2;
for (i = 1; i <= n / 2; i += 2, x += 2) {
arr[i] = x;
arr[n - i + 1] = n - x + 1;
}
x = n;
for (i = 2; i <= n / 2; i += 2, x -= 2) {
arr[i] = x;
arr[n - i + 1] = n + 1 - x;
}
if (n % 4 == 1) {
arr[n / 2 + 1] = n / 2 + 1;
}
for (i = 1; i <= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
} else {
cout << -1 << endl;
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
int a[100005];
int main() {
int n, i;
while (~scanf("%d", &n)) {
if (n % 4 == 2 || n % 4 == 3) {
printf("-1\n");
continue;
} else {
for (i = 1; i <= n / 2; i += 2) {
a[i] = i + 1;
a[i + 1] = n - i + 1;
a[n - i + 1] = n - i;
a[n - i] = i;
}
if (n % 4 == 1) a[n / 2 + 1] = n / 2 + 1;
for (i = 1; i < n; i++) printf("%d ", a[i]);
printf("%d\n", a[n]);
}
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long a[100005];
int main(void) {
long n;
cin >> n;
if (n == 1)
cout << "1" << endl;
else if (n % 4 == 0) {
for (int i = 1; i <= n / 2; i += 2) a[n - i] = i;
for (int i = 2; i <= n / 2; i += 2) a[i - 1] = i;
for (int i = n; i > n / 2; i -= 2) a[n + 2 - i] = i;
for (int i = n - 1; i > n / 2; i -= 2) a[i + 1] = i;
for (int i = 1; i <= n - 1; i++) cout << a[i] << " ";
cout << a[n] << endl;
} else if ((n - 1) % 4 == 0) {
a[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i <= n / 2; i += 2) a[n - i] = i;
for (int i = 2; i <= n / 2; i += 2) a[i - 1] = i;
for (int i = n; i > n / 2 + 1; i -= 2) a[n + 2 - i] = i;
for (int i = n - 1; i > n / 2 + 1; i -= 2) a[i + 1] = i;
for (int i = 1; i <= n - 1; i++) cout << a[i] << " ";
cout << a[n] << endl;
} else
cout << "-1" << endl;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n;
void solve() {
if (n % 4 == 2 || n % 4 == 3) {
cout << -1 << endl;
return;
}
if (n == 1) {
cout << 1 << endl;
return;
}
cout << 2 << ' ' << n;
for (int i = 1; i < n / 4; ++i)
cout << ' ' << 2 * (i + 1) << ' ' << (n - 2 * i);
if (n & 1) cout << ' ' << n / 2 + 1;
for (int i = 0; i < (n / 4); ++i)
cout << ' ' << (n / 2 - 1 - 2 * i) << ' ' << ((n + 1) / 2 + 2 * i + 1);
cout << endl;
}
int main() {
cin >> n;
solve();
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class A {
int[] doit(int n){
int[] a=new int[n];
int count=0;
int cur=0;
int x=0;
int val=2;
if(n==1)val=1;
while(count!=n){
while(a[x]==0){
a[x]=val;
val=n-x;
x=a[x]-1;
count++;
}
if(n-count==1)
val=a[cur]+1;
else
val=a[cur]+2;
while(cur<n&&a[cur]!=0)cur++;
x=cur;
}
return a;
}
void solve() throws IOException {
int n=nextInt();
if(n%4>1){
out.print(-1);
}
else{
int[] a=doit(n);
for(int i=0;i<n;i++)
out.print(a[i]+" ");
}
}
public static void main(String[] args) throws IOException {
new A().run();
}
void run() throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
out = new PrintWriter(new OutputStreamWriter(System.out));
solve();
reader.close();
out.flush();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter out;
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | n = int(raw_input())
if n%4 in [0,1]:
p = range(n)
l = [(i,n-i-1) for i in xrange(n/2)]
while l:
(a,b),(c,d) = l.pop(),l.pop()
p[a] = c
p[c] = b
p[b] = d
p[d] = a
print ' '.join(str(x+1) for x in p)
else:
print -1
| PYTHON |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n;
int *a;
void foo(int k, int i) {
a[i] = k;
a[k] = n - i + 1;
a[n - k + 1] = i;
a[n - i + 1] = n - k + 1;
}
void Zero() {
for (int i = 1; i <= n; ++i) {
a[i] = 0;
}
}
void Print() {
for (int i = 1; i <= n; ++i) {
cout << a[i] << " ";
}
}
int main() {
cin >> n;
if (n % 4 == 3 || n % 4 == 2) {
cout << -1;
return 0;
}
a = new int[n + 1];
Zero();
if (n % 4 == 0) {
for (int j = 1; j <= n / 2;) {
foo(j + 1, j);
j += 2;
}
Print();
}
if (n % 4 == 1) {
for (int j = 1; j <= (n - 1) / 2;) {
foo(j + 1, j);
j += 2;
}
a[n / 2 + 1] = n / 2 + 1;
Print();
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 300;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
int n, p[110000];
int main() {
cin >> n;
if (n % 4 == 2 || n % 4 == 3) {
cout << -1 << endl;
return 0;
}
for (int i = n; i > n / 2 + 1; i -= 2) {
p[i] = i - 1;
p[i - 1] = n - i + 1;
p[n - i + 2] = i;
p[n - i + 1] = n - i + 2;
}
if (n % 4 == 1) p[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i < n; i++) cout << p[i] << " ";
cout << p[n] << endl;
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.util.Arrays;
import java.util.Scanner;
public class LuckyPermutation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
int[] numeros = new int[n + 1];
Arrays.fill(numeros, 0);
if (n%4 > 1){
System.out.println(-1);
return;
}
int actual = 1;
int cantidad = 0;
if (n % 2 == 1) {
numeros[n / 2 + 1] = n / 2 + 1;
cantidad++;
}else if(n == 1){
System.out.println(1);
return;
}
while (cantidad < n) {
while (numeros[actual] != 0) {
actual++;
}
int c = actual;
actual++;
while (numeros[actual] != 0) {
actual++;
}
int r = actual;
numeros[c] = r;
cantidad++;
while (numeros[r] == 0) {
numeros[r] = n - c + 1;
c = r;
r = numeros[r];
cantidad++;
}
}
for (int i = 1; i < n + 1; i++) {
System.out.print(numeros[i]);
if (i != n)
System.out.print(" ");
}
System.out.println();
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
if (n % 4 == 3 || n % 4 == 2) {
out.print(-1);
return;
}
int[] result = new int[n];
for (int i = 0; i < n / 2; i += 2) {
result[n - i - 1 - 1] = i + 1;
result[i] = i + 2;
result[i + 1] = n - i;
result[n - i - 1] = n - 1 - i;
}
if (n % 4 == 1) {
result[n / 2] = n / 2 + 1;
}
for (int i = 0; i < n; i++) {
out.print(result[i] + " ");
}
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
long long mod = 1e9 + 7;
double eps = 1e-9;
double pi = acos(-1);
long long fastpower(long long b, long long p) {
double ans = 1;
while (p) {
if (p % 2) {
ans = (ans * b);
}
b = b * b;
p /= 2;
}
return ans;
}
bool valid(int x, int y, int n, int m) {
return (x >= 0 && y < m && x < n && y >= 0);
}
using namespace std;
unordered_map<long long, int> cnt;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long n;
cin >> n;
vector<long long> ans(n + 1, 0);
long long l = 1;
long long r = n;
for (int i = 0; i < (n / 2); i++) {
if (i % 2 == 0) {
ans[l] = r - 1;
ans[r] = l + 1;
} else {
ans[l] = l - 1;
ans[r] = r + 1;
}
l++;
r--;
}
if (n % 2) {
ans[(n + 1) / 2] = (n + 1) / 2;
}
for (int i = 1; i <= n; i++) {
if (cnt.count(ans[i])) {
cout << -1;
return 0;
}
cnt[ans[i]] = 1;
if (ans[ans[i]] != n - i + 1) {
cout << -1;
return 0;
}
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
int a[111111];
int main() {
int n;
scanf("%d", &n);
if (n % 4 == 2 || n % 4 == 3)
printf("-1\n");
else {
int b = 1, e = n, mn = 1, mx = n;
while (e - b + 1 >= 4) {
a[b] = mn + 1;
a[b + 1] = mx;
a[e - 1] = mn;
a[e] = mx - 1;
b += 2;
e -= 2;
mn += 2;
mx -= 2;
}
if (n % 4 == 1) a[b] = mn;
for (int i = 1; i <= n; ++i) printf("%d ", a[i]);
printf("\n");
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int size = 100008;
int p[size], n;
void dfs_p(int i, int x) {
if (x > 4) return;
p[p[i]] = n - i + 1;
dfs_p(p[i], x + 1);
}
int main() {
int k, i, j, id;
while (cin >> n) {
memset(p, 0, sizeof(p));
if (n % 4 == 0 || n % 4 == 1) {
k = n / 4;
for (i = 1; i <= k; i++) {
id = i * 2 - 1;
p[id] = i * 2;
dfs_p(id, 0);
}
if (n % 4 == 1) p[(n + 1) / 2] = (n + 1) / 2;
printf("%d", p[1]);
for (i = 2; i <= n; i++) printf(" %d", p[i]);
puts("");
} else
puts("-1");
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int oo = 2000000009;
const int mx = 100005;
int mustbe[mx], p[mx], n;
void solve(int a, int b) {
if (b - a < 0) return;
if (b - a == 0) {
p[a] = a;
return;
}
p[a] = a + 1;
p[a + 1] = b;
p[b - 1] = a;
p[b] = b - 1;
solve(a + 2, b - 2);
}
int main() {
scanf("%d", &n);
if (n % 4 >= 2) {
printf("-1");
return 0;
}
solve(1, n);
for (int i = 1; i < (n + 1); ++i) printf("%d ", p[i]);
fflush(stdout);
for (int i = 1; i < (n + 1); ++i) assert(p[p[i]] == n - i + 1);
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long n, a[((long long)101 * 1000)], p[((long long)101 * 1000)];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
if (n == 2 || n % 4 == 3 || n % 4 == 2) return cout << -1, 0;
for (int i = 1, l = 1, r = n; i <= n - (n % 2); l += 2, r -= 2, i += 4)
p[l] = l + 1, p[l + 1] = r, p[r] = r - 1, p[r - 1] = l;
if (n % 2 == 1) p[(n + 1) / 2] = (n + 1) / 2;
for (int i = 1; i <= n; i++) cout << p[i] << " ";
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class A {
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter out;
public void solve() throws IOException {
int N = nextInt();
int[] ans = new int[N];
int remain = N;
int start_num = 1;
int end_num = N;
int start_space = 0;
while(remain >= 4){
ans[start_space] = start_num + 1;
ans[start_space+1] = end_num;
ans[N-1-start_space] = end_num - 1;
ans[N-1-start_space-1] = start_num;
start_num += 2;
end_num -= 2;
start_space += 2;
remain -= 4;
}
if( remain == 1){
ans[start_space] = start_num;
}
else if( remain == 2){
out.println(-1); return;
}
else if( remain == 3){
out.println(-1); return;
}
for(int i = 0; i < N; i++){
out.print( ans[i] + " ");
}
}
/**
* @param args
*/
public static void main(String[] args) {
new A().run();
}
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
out = new PrintWriter(System.out);
solve();
reader.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int a[n + 1];
if (n % 4 > 1)
cout << -1 << endl;
else {
for (int i = 1; i < n / 2; i += 2)
a[i] = i + 1, a[n - i + 1] = n - i, a[i + 1] = n - i + 1, a[n - i] = i;
if (n % 4 == 1) a[(n / 2) + 1] = (n / 2) + 1;
for (int i = 1; i <= n; i++) cout << a[i] << ' ';
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.util.*;
public class LuckPerm286A
{
public static void main(String[] args)
{
// Set up scanner
Scanner sc = new Scanner(System.in);
// System.out.println("Enter n");
int n = sc.nextInt();
if (n==1)
{
System.out.println(1);
return;
}
if (n%4 == 2 || n%4 == 3)
{
System.out.println(-1);
return;
}
int[] a = new int[n+1];
int numleft = n;
int offset = 0;
while (numleft >= 4)
{
a[1+offset] = 2+offset;
a[2+offset] = n-offset;
a[n-offset-1] = 1+offset;
a[n-offset] = n-offset-1;
numleft -= 4;
offset+= 2;
}
if (numleft == 1) // One more in the middle, to itself
{
a[offset+1] = offset+1;
}
StringBuilder sb = new StringBuilder();
for (int i=1; i<=n; i++)
{
sb.append(a[i] + " ");
}
System.out.println(sb);
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const double PI = 3.141592653589793238;
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vector) {
for (size_t i = 0; i < vector.size(); ++i) {
os << vector[i] << " ";
}
return os;
}
vector<int> FindHappyPermutaion(int n) {
if (n == 1) {
return vector<int>(2, 1);
} else if (n % 4 == 2 || n % 4 == 3) {
return vector<int>(2, -1);
} else {
vector<int> v(n + 1, 0);
if (n % 4 == 0) {
int k = n / 4;
int l = n / 2;
int r = l + 1;
for (int i = 1; i <= k; ++i) {
v[i] = l;
v[l] = n - i + 1;
v[n - i + 1] = r;
v[r] = i;
--l;
++r;
}
} else if (n % 4 == 1) {
int k = n / 4;
int l = n / 2;
int r = l + 2;
for (int i = 1; i <= k; ++i) {
v[i] = l;
v[l] = n - i + 1;
v[n - i + 1] = r;
v[r] = i;
--l;
++r;
}
v[n / 2 + 1] = n / 2 + 1;
}
return v;
}
}
void TestPermutation(const vector<int>& v) {
int n = v.size() - 1;
if (n != 1) {
for (int i = 1; i <= n; ++i) {
bool s = v[v[i]] == n - i + 1;
cout << "n = " << n << ", i = " << i << ": " << s << "\n";
if (!s) {
cerr << "\nFAIL"
<< "\nn = " << n << "\nn mod 4 = " << n % 4 << "\n";
throw std::runtime_error("FAIL");
}
}
}
}
void StressTest() {
for (int n = 1; n <= 100000; ++n) {
vector<int> v = FindHappyPermutaion(n);
TestPermutation(v);
}
}
int main() {
std::ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<int> v = FindHappyPermutaion(n);
for (int i = 1; i < v.size(); ++i) {
cout << v[i] << " ";
}
cout << "\n";
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010 * 2;
int p[MAXN];
int main() {
int N;
cin >> N;
if (N % 4 > 1)
puts("-1");
else {
for (int i = 1; i * 2 < N; i += 2) {
p[i] = i + 1;
p[i + 1] = N + 1 - i;
p[N + 1 - i] = N - i;
p[N - i] = i;
}
if (N % 2) p[N / 2 + 1] = N / 2 + 1;
for (int i = 1; i <= N; ++i) {
cout << p[i] << " ";
}
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <class T>
T _max(T a, T b) {
return a > b ? a : b;
}
template <class T>
T _min(T a, T b) {
return a < b ? a : b;
}
int sgn(const double &x) { return (x > 1e-8) - (x < -1e-8); }
int a[100010];
int main() {
int n;
cin >> n;
if (n % 4 > 1) {
puts("-1");
return 0;
}
if (n & 1) {
int x = 2;
int y = n;
a[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i <= n / 2; i++) {
if (i % 2 == 0) {
a[i] = y;
a[n + 1 - i] = n + 1 - y;
y -= 2;
} else {
a[i] = x;
a[n + 1 - i] = n + 1 - x;
x += 2;
}
}
} else {
int x = 2;
int y = n;
for (int i = 1; i <= n / 2; i++) {
if (i % 2 == 0) {
a[i] = y;
a[n + 1 - i] = n + 1 - y;
y -= 2;
} else {
a[i] = x;
a[n + 1 - i] = n + 1 - x;
x += 2;
}
}
}
for (int i = 1; i <= n; i++) cout << a[i] << " ";
cout << endl;
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int INF = 1 << 29;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int& n, int b) { n |= two(b); }
inline void unset_bit(int& n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) {
int res = 0;
while (n && ++res) n -= n & (-n);
return res;
}
template <class T>
void chmax(T& a, const T& b) {
a = max(a, b);
}
template <class T>
void chmin(T& a, const T& b) {
a = min(a, b);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n;
cin >> n;
if (n == 1)
cout << "1" << endl;
else if (n % 4 > 1)
cout << "-1" << endl;
else {
for (long long int i = 2; i <= n / 2; i += 2) {
cout << i << " " << n - i + 2 << " ";
}
if (n % 2 == 1) cout << (n + 1) / 2 << " ";
for (int i = n / 2 - 1; i >= 1; i -= 2) cout << i << " " << n - i << " ";
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | n = int(input())
if n % 4 > 1:
print(-1)
exit()
a = [i for i in range(0, n+1)]
for i in range(1, n//2+1, 2):
p, q, r, s = i, i+1, n-i,n-i+1
a[p], a[q], a[r], a[s] = a[q], a[s], a[p], a[r]
def check(arr):
for i in range(1, n+1):
k = arr[i]
if arr[arr[k]] != n-k+1:
return False
return True
# print(check(a))
print(*a[1:])
# Made By Mostafa_Khaled | PYTHON3 |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
//package luckyperm;
import java.util.Scanner;
/**
*
* @author admin
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner scan=new Scanner(System.in);
int n=scan.nextInt(),countN=0;
int[] count=new int[n+1];
int[] p=new int[n+1];
int i=1,temp=n;
if(n%4==2||n%4==3){
System.out.println("-1");
}
else{
while(temp>1){
p[i]=i+1;
p[i+1]=n-i+1;
p[n-i+1]=n-i;
p[n-i]=i;
i+=2;
temp-=4;
}
if(temp==1){
p[(n+1)/2]=(n+1)/2;
}
for(i=1;i<=n;i++){
System.out.print(p[i]);
if(i<n)
System.out.print(" ");
}
System.out.println("");
}
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
if ((n - (n & 1)) % 4 != 0)
cout << -1;
else {
set<int> disp;
int v[n + 1], k = 1;
memset(v, -1, sizeof v);
for (int i = 1; i <= n; i++) disp.insert(i);
if (n & 1) {
v[(n + 1) / 2] = (n + 1) / 2;
disp.erase((n + 1) / 2);
}
while (disp.size()) {
if (v[k] != -1) {
k++;
continue;
}
v[k] = *((*disp.begin() == k) ? next(disp.begin()) : disp.begin());
v[v[k]] = n - k + 1;
v[v[v[k]]] = n - v[k] + 1;
v[v[v[v[k]]]] = n - v[v[k]] + 1;
disp.erase(v[k]);
disp.erase(v[v[k]]);
disp.erase(v[v[v[k]]]);
disp.erase(v[v[v[v[k]]]]);
}
for (int i = 1; i <= n; i++) cout << v[i] << " \n"[i == n];
}
return (0);
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int a[1000001], n;
int main() {
cin >> n;
if ((n / 2) % 2) {
cout << -1 << endl;
return 0;
}
int l = 2, r = n;
for (int i = 1; i <= n / 2; i++) {
if (i % 2)
a[i] = l, a[n - i] = l - 1, l += 2;
else
a[i] = r, a[n - i + 2] = r - 1, r -= 2;
}
if (n % 2) a[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i <= n; i++) cout << a[i] << " ";
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 |
import java.util.Scanner;
public class C {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int n=in.nextInt();
int a[]=new int[n+1];
boolean b[]=new boolean[n+1];
a[n-1]=1;
b[n-1]=true;
if(n%4==0 || n%4==1 ){
for(int j=0;j<n/2;j+=2){
a[j+1]=j+2;
a[j+2]=n-j;
a[n-j]=n-j-1;
a[n-j-1]=j+1;
}
if(n%2==1)
a[(n+1)/2]=(n+1)/2;
for(int j=1;j<=n;j++)
System.out.print(a[j]+" ");
}
else
System.out.println(-1);
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | n = int(input())
p = [0] * n
if n == 1:
print(1)
exit()
for sh in range(n // 4):
p[n - sh * 2 - 2] = n - sh * 2
p[sh * 2] = n - 1 - sh * 2
p[n - sh * 2 - 1] = 2 + sh * 2
p[sh * 2 + 1] = 1 + sh * 2
if n % 4 == 1:
p[n // 2] = n // 2 + 1
if n % 4 == 2:
print(-1)
exit()
if n % 4 == 3:
print(-1)
exit()
print(" ".join([str(i) for i in p])) | PYTHON3 |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author ocelopilli
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
int[] p;
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
if ( n%4==0 || n%4==1 )
{
p = new int[ n+1 ];
int le = 1, ri = n;
while ( le < ri )
{
p[le] = ri - 1;
p[le+1] = le;
p[ri] = le+1;
p[ri-1] = ri;
le += 2;
ri -= 2;
}
if ( le == ri ) p[le] = le;
for (int i=1; i<=n; i++)
{
if ( i > 1 ) out.print(" ");
out.print( p[i] );
}
out.println();
}
else out.println( -1 );
}
}
class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream in)
{
br = new BufferedReader(new InputStreamReader(in));
st = null;
}
public String next()
{
while (st==null || !st.hasMoreTokens())
{
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() { return Integer.parseInt(next()); }
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.util.*;
public class SinIsh {
public static void main(String [] args){
Scanner in = new Scanner(System.in);
int n=in.nextInt();
int array[]=new int[n];
boolean flag=false;
if(n==1)
System.out.print("1");
else if(n%4==3||n%4==2)
System.out.print("-1");
else{
if(n%4==1)
flag=true;
for(int i=0;i<n/2;i+=2){
array[i]=i+2;
array[i+1]=n-i;
array[n-1-i]=n-1-i;
array[n-2-i]=i+1;
}
StringBuilder str=new StringBuilder();
if(flag)
array[(n-1)/2]=(n+1)/2;
for(int i=0;i<n;i++){
str.append(array[i]+" ");
}
System.out.print(str);
}
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class ProblemA {
public static void main(String[] args) throws IOException {
ProblemA solver = new ProblemA();
solver.init();
solver.solve();
}
private void init() {
}
private void solve() throws IOException {
Reader in = new Reader(System.in);
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int n = in.nextInt();
int[] p = new int[n + 1];
int from = 1;
int to = n;
while (from <= to) {
if (from == to) {
p[from] = from;
break;
} else if (to - from <= 2) {
p = null;
break;
} else {
p[from] = from + 1;
p[from + 1] = to;
p[to] = to - 1;
p[to - 1] = from;
}
from += 2;
to -= 2;
}
if (p == null) out.println("-1");
else {
for (int i = 1; i <= n; i++) {
out.print(p[i] + " ");
}
out.println();
}
out.flush();
out.close();
}
private static class Reader {
BufferedReader reader;
StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
Reader(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
public String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt( next() );
}
public double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | def main():
n = int(raw_input())
a = [0] * (n+1)
if n % 4 > 1:
print -1
return
for y in xrange(n/4):
x = y * 2
a[x+1] = x+2
a[x+2] = n-x
a[n-x] = n-x-1
a[n-x-1] = x+1
if n % 4 == 1:
a[(n+1)/2] = (n+1)/2
for x in a[1:]:
print x,
main()
| PYTHON |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
int p[100001];
bool v[100001];
int main() {
int n;
scanf("%d", &n);
int fill = 1;
int c = 0;
while (fill <= n) {
while (p[fill] != 0 && fill <= n) fill++;
if (fill > n) break;
if (c == n - 1) {
p[fill] = fill;
if (n - fill + 1 == fill) c++;
break;
}
p[fill] = fill + 1;
v[fill + 1] = true;
c++;
int cur = fill;
while (!(v[n - cur + 1] || p[p[cur]] > 0)) {
if (p[p[cur]] == fill) fill++;
p[p[cur]] = n - cur + 1;
v[n - cur + 1] = true;
cur = p[cur];
c++;
}
}
if (c < n)
printf("-1\n");
else {
for (int i = 1; i <= n; ++i) printf("%d ", p[i]);
printf("\n");
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
char f(vector<int> a) {
int n = a.size() - 1;
for (int i = 1; i <= n; i++)
if (a[a[i]] != n - i + 1) return false;
return true;
}
int main() {
int n;
scanf("%d", &n);
vector<int> a(n + 1, -1);
a[1] = 2;
a[n - 1] = 1;
a[n] = n - 1;
if (n % 2 == 0) {
for (int i = 3; i <= n / 2; i += 2) a[i] = a[i - 2] + 2;
for (int i = n - 3; i > n / 2; i -= 2) a[i] = a[i + 2] + 2;
} else {
for (int i = 3; i < n / 2 + n % 2; i += 2) a[i] = a[i - 2] + 2;
a[n / 2 + 1] = n / 2 + 1;
}
for (int i = 1; i <= n; i++) {
if (a[i] == -1) continue;
if (a[a[i]] == -1) a[a[i]] = n - i + 1;
if (a[a[i]] != n - i + 1) {
printf("-1");
return 0;
}
}
for (int i = 1; i <= n; i++) printf("%d ", a[i]);
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
int p[100002];
int vis[100002];
int main() {
int n;
while (scanf("%d", &n) != EOF) {
if (n % 4 != 0 && n % 4 != 1) {
printf("-1\n");
continue;
} else {
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n / 2; i++) {
if (vis[i] == 0) {
p[i] = n - i;
vis[i] = 1;
p[n - i] = n - i + 1;
vis[n - i] = 1;
p[n - i + 1] = i + 1;
vis[n - i + 1] = 1;
p[i + 1] = i;
vis[i + 1] = 1;
}
}
if (n % 4 == 1) p[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i < n; i++) {
printf("%d ", p[i]);
}
printf("%d\n", p[n]);
}
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int p[N];
int main() {
int n;
cin >> n;
int t = n % 4;
if (t < 2) {
if (t == 1) {
p[n / 2 + 1] = n / 2 + 1;
}
for (int i = 1; 2 * i <= n; i += 2) {
p[i] = i + 1;
p[i + 1] = n - i + 1;
p[n - i + 1] = n - i;
p[n - i] = i;
}
for (int i = 1; i <= n; ++i) cout << p[i] << " ";
cout << endl;
} else {
cout << "-1" << endl;
}
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import static java.lang.Math.*;
import static java.math.BigInteger.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
public class A {
final static boolean autoflush = false;
public A () {
int N = sc.nextInt();
int [] res = new int [N];
switch(N%4) {
case 1:
res[(N-1)/2] = (N-1)/2;
case 0:
for (int i : rep(N/4)) {
int a = 2*i, b = a+1;
res[a] = b;
res[b] = N-1-a;
res[N-1-a] = N-1-b;
res[N-1-b] = a;
}
for (int i : rep(N))
++res[i];
exit(res);
default:
exit(-1);
}
}
////////////////////////////////////////////////////////////////////////////////////
static int [] rep(int N) { return rep(0, N); }
static int [] rep(int S, int T) { int [] res = new int [max(T-S, 0)]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
static int [] req(int S, int T) { return rep(S, T+1); }
////////////////////////////////////////////////////////////////////////////////////
/* Dear hacker, don't bother reading below this line, unless you want to help me debug my I/O routines :-) */
final static MyScanner sc = new MyScanner();
static class MyScanner {
public String next() {
newLine();
return line[index++];
}
public char nextChar() {
return next().charAt(0);
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
line = null;
return readLine();
}
public String [] nextStrings() {
line = null;
return readLine().split(" ");
}
public char [] nextChars() {
return next ().toCharArray ();
}
public Integer [] nextInts() {
String [] L = nextStrings();
Integer [] res = new Integer [L.length];
for (int i : rep(L.length))
res[i] = Integer.parseInt(L[i]);
return res;
}
public Long [] nextLongs() {
String [] L = nextStrings();
Long [] res = new Long [L.length];
for (int i : rep(L.length))
res[i] = Long.parseLong(L[i]);
return res;
}
public Double [] nextDoubles() {
String [] L = nextStrings();
Double [] res = new Double [L.length];
for (int i : rep(L.length))
res[i] = Double.parseDouble(L[i]);
return res;
}
public String [] next (int N) {
String [] res = new String [N];
for (int i : rep(N))
res[i] = sc.next();
return res;
}
public Integer [] nextInt (int N) {
Integer [] res = new Integer [N];
for (int i : rep(N))
res[i] = sc.nextInt();
return res;
}
public Long [] nextLong (int N) {
Long [] res = new Long [N];
for (int i : rep(N))
res[i] = sc.nextLong();
return res;
}
public Double [] nextDouble (int N) {
Double [] res = new Double [N];
for (int i : rep(N))
res[i] = sc.nextDouble();
return res;
}
public String [][] nextStrings (int N) {
String [][] res = new String [N][];
for (int i : rep(N))
res[i] = sc.nextStrings();
return res;
}
public Integer [][] nextInts (int N) {
Integer [][] res = new Integer [N][];
for (int i : rep(N))
res[i] = sc.nextInts();
return res;
}
public Long [][] nextLongs (int N) {
Long [][] res = new Long [N][];
for (int i : rep(N))
res[i] = sc.nextLongs();
return res;
}
public Double [][] nextDoubles (int N) {
Double [][] res = new Double [N][];
for (int i : rep(N))
res[i] = sc.nextDoubles();
return res;
}
//////////////////////////////////////////////
private boolean eol() {
return index == line.length;
}
private String readLine() {
try {
return r.readLine();
} catch (Exception e) {
throw new Error (e);
}
}
private final java.io.BufferedReader r;
MyScanner () {
this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in)));
}
MyScanner (java.io.BufferedReader r) {
try {
this.r = r;
while (!r.ready())
Thread.sleep(1);
start();
} catch (Exception e) {
throw new Error(e);
}
}
private String [] line;
private int index;
private void newLine() {
if (line == null || eol()) {
line = readLine().split(" ");
index = 0;
}
}
}
static void print (Object o, Object... a) {
printDelim(" ", o, a);
}
static void cprint (Object o, Object... a) {
printDelim("", o, a);
}
static void printDelim (String delim, Object o, Object... a) {
pw.println(build(delim, o, a));
}
static void exit (Object o, Object... a) {
print(o, a);
exit();
}
static void exit() {
pw.close();
System.out.flush();
System.err.println("------------------");
System.err.println("Time: " + ((millis() - t) / 1000.0));
System.exit(0);
}
static void NO() {
throw new Error("NO!");
}
////////////////////////////////////////////////////////////////////////////////////
static String build (String delim, Object o, Object... a) {
StringBuilder b = new StringBuilder();
append(b, o, delim);
for (Object p : a)
append(b, p, delim);
return b.toString().trim();
}
static void append(StringBuilder b, Object o, String delim) {
if (o.getClass().isArray()) {
int L = java.lang.reflect.Array.getLength(o);
for (int i : rep(L))
append(b, java.lang.reflect.Array.get(o, i), delim);
} else if (o instanceof Iterable<?>)
for (Object p : (Iterable<?>)o)
append(b, p, delim);
else
b.append(delim).append(o);
}
////////////////////////////////////////////////////////////////////////////////////
static void statics() {
abs(0);
valueOf(0);
asList(new Object [0]);
reverseOrder();
}
public static void main (String[] args) {
new A();
exit();
}
static void start() {
t = millis();
}
static java.io.PrintWriter pw = new java.io.PrintWriter(System.out, autoflush);
static long t;
static long millis() {
return System.currentTimeMillis();
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.*;
import java.util.*;
import java.math.*;
public class A implements Runnable {
static BufferedReader in;
static PrintWriter out;
static StringTokenizer st;
static Random rnd;
void solve() throws IOException {
int n = nextInt();
if (n == 1) {
out.println(1);
} else if (n == 2) {
out.println(-1);
} else {
int startFrom = n - 2;
int[] p = new int[n];
solve(n, p, startFrom);
if (n % 2 != 0) {
p[n / 2] = (n / 2);
}
if (!check(n, p)) {
out.println(-1);
} else {
for (int i = 0; i < n; i++) {
out.print(p[i] + 1);
out.print((i + 1) == n ? "\n" : " ");
}
}
// rec(0, n, new int[n], new boolean[n]);
}
}
private void rec(int step, int n, int[] h, boolean[] u) {
if (step == n) {
if (check(n, h)) {
out.println(Arrays.toString(h));
out.flush();
}
} else {
for (int i = 0; i < n; i++) {
if (!u[i]) {
h[step] = i;
u[i] = true;
rec(step + 1, n, h, u);
u[i] = false;
}
}
}
}
private void solve(int n, int[] p, int ptr) {
int needSum = n - 1;
int l = 0, r = n - 1;
int first = 1, last = n - 1;
while (l + 1 < r - 1) {
p[l] = first;
p[l + 1] = last;
p[r] = needSum - first;
p[r - 1] = needSum - last;
first += 2;
last -= 2;
l += 2;
r -= 2;
}
}
private boolean check(int n, int[] p) {
for (int i = 0; i < n; i++) {
if (p[p[i]] != n - i - 1) {
// out.println(i + " " + p[i] + " " + p[p[i]] + " " + (n - i -
// 1));
// out.flush();
return false;
}
}
return true;
}
public static void main(String[] args) {
new A().run();
}
public void run() {
try {
final String className = this.getClass().getName().toLowerCase();
try {
in = new BufferedReader(new FileReader(className + ".in"));
out = new PrintWriter(new FileWriter(className + ".out"));
// in = new BufferedReader(new FileReader("input.txt"));
// out = new PrintWriter(new FileWriter("output.txt"));
} catch (FileNotFoundException e) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
rnd = new Random();
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(42);
}
}
String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String line = in.readLine();
if (line == null)
return null;
st = new StringTokenizer(line);
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
template <typename T>
inline T gcd(T a, T b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
template <typename T>
inline T lcm(T a, T b) {
return (a * b) / gcd(a, b);
}
template <typename T>
string toStr(T x) {
stringstream st;
st << x;
string s;
st >> s;
return s;
}
template <class T>
void splitStr(const string &s, vector<T> &out) {
istringstream in(s);
out.clear();
copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
inline int two(int n) { return 1 << n; }
inline int isOnBit(int n, int b) { return (n >> b) & 1; }
inline void onBit(int &n, int b) { n |= two(b); }
inline void offBit(int &n, int b) { n &= ~two(b); }
inline int lastBit(int n) { return n & (-n); }
inline int cntBit(int n) {
int res = 0;
while (n && ++res) n -= n & (-n);
return res;
}
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy8[] = {0, 1, 0, -1, -1, 1, -1, 1};
int n, ans[100000 + 5];
void solve() {
if (n % 4 == 2 || n % 4 == 3) {
puts("-1");
return;
} else if (n == 1) {
puts("1");
return;
}
int i = 1;
while (i <= (n + 1) / 2) {
if (i == (n + 1) / 2) {
ans[i] = i;
break;
}
ans[i] = i + 1;
ans[n - i + 1] = (n + 1) - ans[i];
i++;
ans[i] = n - i + 2;
ans[n - i + 1] = i - 1;
i++;
}
for (int i = (1), _b = (n); i <= _b; i++) printf("%d ", ans[i]);
}
int main() {
scanf("%d", &n);
solve();
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int p[200000];
int pp[200000];
int a[200000];
int n;
bool f() {
for (int(i) = (0); (i) < (n); ++(i))
if (a[a[i + 1]] != n - i) return 0;
return 1;
}
void ff(int id, int x) {
if (a[id]) return;
a[id] = x;
ff(x, n - id + 1);
}
int main() {
cin >> n;
if (n == 1) {
cout << 1 << endl;
return 0;
}
if (n % 4 > 1) {
cout << -1 << endl;
return 0;
}
int id = 1;
int x = 2, y = n;
for (int(i) = (0); (i) < (n / 4); ++(i)) {
a[id] = x;
x += 2;
id++;
a[id] = y;
y -= 2;
id++;
}
id = n;
x = 1;
y = n - 1;
for (int(i) = (0); (i) < (n / 4); ++(i)) {
a[id] = y;
y -= 2;
id--;
a[id] = x;
x += 2;
id--;
}
if (n % 2) a[(n + 1) / 2] = (n + 1) / 2;
for (int(i) = (1); (i) < (n + 1); ++(i)) cout << a[i] << " ";
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long k, a, b;
int main() {
long long n;
cin >> n;
long long ans[100000 + 1] = {};
if (n % 4 == 1) {
ans[(n + 1) / 2] = (n + 1) / 2;
long long mid = (n + 1) / 2;
for (long long i = 1; i <= (n + 1) / 2; i++) {
if (i % 2) {
ans[mid - i] = i;
ans[mid + i] = (n + 1 - i);
} else {
ans[mid - i] = (n + 1 - i);
ans[mid + i] = i;
}
}
for (long long i = 1; i <= n; i++) cout << ans[i] << " ";
cout << endl;
} else if ((n % 4) == 0) {
long long mid = n / 2;
for (long long i = 1; i <= (n) / 2; i++) {
if (i % 2) {
ans[mid - i + 1] = i;
ans[n + 1 - mid + i - 1] = (n + 1 - i);
} else {
ans[mid - i + 1] = n + 1 - i;
ans[n + 1 - mid + i - 1] = i;
}
}
for (long long i = 1; i <= n; i++) cout << ans[i] << " ";
cout << endl;
} else {
cout << "-1" << endl;
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
int m, n;
int a[100050];
int main() {
int i, j, k, x, y, z;
scanf("%d", &n);
if (n % 4 == 2 || n % 4 == 3)
puts("-1");
else {
j = n;
i = 1;
while (j >= i) {
if (j == i)
a[i] = i;
else {
a[i] = i + 1;
a[i + 1] = j;
a[j] = n - i;
a[j - 1] = n + 1 - j;
}
i += 2;
j -= 2;
}
for (i = 1; i <= n; i++) {
printf("%d ", a[i]);
}
}
getchar();
getchar();
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, Mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
if (n % 4 > 1) cout << "-1\n", exit(0);
int Arr[n + 2];
if (n & 1) Arr[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i <= n / 2; i += 2) {
Arr[i] = 2 + (i - 1);
Arr[i + 1] = n - (i - 1);
Arr[n - i + 1] = n - 1 - (i - 1);
Arr[n - i] = 1 + (i - 1);
}
for (int i = 1; i <= n; i++) {
if (i > 1) cout << " ";
cout << Arr[i];
}
cout << "\n";
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
inline void read(int &x) {
x = 0;
char ch = getchar();
while (ch < '0') ch = getchar();
while (ch >= '0') {
x = x * 10 + ch - 48;
ch = getchar();
}
}
int a[100010], ans;
bool f[100010];
int main() {
int n;
scanf("%d", &n);
if (n == 1)
puts("1");
else if (n == 2)
puts("-1");
else if (n % 4 > 1)
puts("-1");
else {
if (n & 1) a[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i <= n / 2; ++i) {
if (!a[i]) {
a[i] = i + 1;
a[i + 1] = n + 1 - i;
a[n + 1 - i] = n - i;
a[n - i] = i;
}
}
for (int i = 1; i <= n; ++i) printf("%d%c", a[i], (i == n) ? '\n' : ' ');
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int p[100005];
bitset<100005> viz, ap;
int main() {
int n, sf;
cin >> n;
sf = n;
if (n == 1) {
cout << 1;
return 0;
}
if (n % 4 == 2 || n % 4 == 3) {
cout << -1;
return 0;
}
for (int i = 1; i <= n; ++i)
if (viz[i] == 0) {
if (!(n % 4 == 1 && (i == n / 2 + 1))) {
p[i] = i + 1;
viz[i] = 1;
p[i + 1] = sf;
viz[i + 1] = 1;
p[sf] = sf - 1;
viz[sf] = 1;
p[sf - 1] = i;
viz[sf - 1] = 1;
sf -= 2;
}
}
for (int i = 1; i <= n; ++i)
if (viz[i] == 0) {
ap[n - i + 1] = 1;
p[i] = n - i + 1;
}
for (int i = 1; i <= n; ++i) cout << p[i] << " ";
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | # Legends Always Come Up with Solution
# Author: Manvir Singh
import os
import sys
from io import BytesIO, IOBase
from collections import deque
def main():
n=int(input())
if n%4==2 or n%4==3:
print(-1)
else:
a=deque(list(range(1,n+1)))
ans=[0]*n
for i in range(0,n//2,2):
ans[-(i+2)]=a.popleft()
ans[i]=a.popleft()
ans[i+1]=a.pop()
ans[-(i+1)]=a.pop()
if a:
ans[n//2]=a.pop()
print(*ans)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main() | PYTHON3 |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
public class C implements Runnable {
// leave empty to read from stdin/stdout
private static final String TASK_NAME_FOR_IO = "";
// file names
private static final String FILE_IN = TASK_NAME_FOR_IO + ".in";
private static final String FILE_OUT = TASK_NAME_FOR_IO + ".out";
BufferedReader in;
PrintWriter out;
StringTokenizer tokenizer = new StringTokenizer("");
public static void main(String[] args) {
new Thread(new C()).start();
}
boolean good(int n, int[] a) {
for (int i = 0; i < n; i++)
if (a[a[i] - 1] != n - i) {
return false;
}
return true;
}
private void gen(int pos, int n, int[] used, int[] a, List<int[]> all) {
if (pos == n) {
if (good(n, a)) {
all.add(a.clone());
}
return;
}
for (int i = 0; i < n; i++)
if (used[i] == 0) {
used[i] = 1;
a[pos] = i + 1;
gen(pos + 1, n, used, a, all);
used[i] = 0;
}
}
private void solveNaive(int n) {
List<int[]> all = new ArrayList<int[]>();
int[] used = new int[n];
int[] p = new int[n];
gen(0, n, used, p, all);
System.out.println("N = " + n);
for (int[] a : all) {
System.out.println(Arrays.toString(a));
}
}
private void solve() throws IOException {
/*
for (int n = 5; n <= 12; n++) {
solveNaive(n);
solveGood(n);
}
*/
int n = nextInt();
solveGood2(n);
}
private void solveGood2(int n) {
int[] a = new int[n];
int mod4 = n % 4;
if (n == 1) {
out.print("1");
return;
}
if (mod4 > 1) {
out.print("-1");
return;
}
int l = 0;
int r = n - 1;
int lo = 1;
for (;;) {
a[l] = n - 1;
a[l + 1] = lo;
a[r] = lo + 1;
a[r - 1] = n;
n -= 2;
lo += 2;
l += 2;
r -= 2;
if (l > r) {
break;
}
if (l == r) {
a[l] = n;
break;
}
}
for (int v : a) {
out.print(v);
out.print(' ');
}
}
private void solveGood(int n) {
System.out.println("N (GOOD) = " + n);
int[] a = new int[n];
int mod4 = n % 4;
if (n == 1) {
System.out.println(Arrays.toString(new int[] {1}));
return;
}
if (mod4 > 1) {
System.out.println(-1);
return;
}
int l = 0;
int r = n - 1;
int lo = 1;
for (;;) {
a[l] = n - 1;
a[l + 1] = lo;
a[r] = lo + 1;
a[r - 1] = n;
n -= 2;
lo += 2;
l += 2;
r -= 2;
if (l > r) {
break;
}
if (l == r) {
a[l] = n;
break;
}
}
System.out.println(Arrays.toString(a));
}
public void run() {
long timeStart = System.currentTimeMillis();
boolean fileIO = TASK_NAME_FOR_IO.length() > 0;
try {
if (fileIO) {
in = new BufferedReader(new FileReader(FILE_IN));
out = new PrintWriter(new FileWriter(FILE_OUT));
} else {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
}
solve();
in.close();
out.close();
} catch (IOException e) {
throw new IllegalStateException(e);
}
long timeEnd = System.currentTimeMillis();
if (fileIO) {
System.out.println("Time spent: " + (timeEnd - timeStart) + " ms");
}
}
private String nextToken() throws IOException {
while (!tokenizer.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
tokenizer = new StringTokenizer(line);
}
return tokenizer.nextToken();
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private BigInteger nextBigInt() throws IOException {
return new BigInteger(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
int p[100005];
int main() {
int n;
scanf("%d", &n);
memset(p, 0, sizeof(p));
if (n == 1) {
printf("1\n");
return 0;
}
if (n == 2 || n == 3 || n % 4 == 3 || n % 4 == 2) {
printf("-1\n");
return 0;
}
if (n >= 4) {
for (int i = 1; i <= n / 4; i++) {
p[2 * i - 1] = 2 * i;
p[2 * i] = n + 2 - 2 * i;
p[n + 2 - 2 * i] = n + 1 - 2 * i;
p[n + 1 - 2 * i] = 2 * i - 1;
}
if (n % 4 == 1) p[2 * (n / 4) + 1] = 2 * (n / 4) + 1;
for (int i = 1; i <= n; i++) {
printf("%d ", p[i]);
}
return 0;
}
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.util.*;
public class Main {
public static void main(String args[]) {
(new Main()).solve();
}
void solve() {
Scanner cin = new Scanner(System.in);
while( cin.hasNextInt() ) {
int N = cin.nextInt();
if( N % 4 >= 2 ) {
System.out.println(-1);
continue;
}
int arr[] = new int[N];
int cur = 1;
int rest = N;
int L = 0;
int R = N - 1;
while( rest >= 4 ) {
arr[L] = cur + 1;
arr[L + 1] = N - cur + 1;
arr[R - 1] = cur;
arr[R] = N - cur;
L += 2;
R -= 2;
rest -= 4;
cur += 2;
}
if( rest == 1 ) { arr[L] = cur; }
for(int i=0; i<N; ++i) {
if( i > 0 ) { System.out.print(" "); }
System.out.print(arr[i]);
}
System.out.println();
for(int i=0; i<N; ++i) {
if( arr[ arr[i] - 1 ] != N - i ) { throw new RuntimeException("assertion fail: " + i); }
}
}
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int a[100010];
int main() {
int i, j, k = 1, n, m;
cin >> n;
if (n % 4 == 2 || n % 4 == 3) {
cout << "-1\n";
return 0;
}
for (i = 0; i < n / 4; i++) {
int x = 2 * i + 1;
a[x] = x + 1;
a[n - x] = x;
a[n + 1 - x] = n - x;
a[x + 1] = n + 1 - x;
}
if (n % 2 != 0) a[n / 2 + 1] = n / 2 + 1;
for (i = 1; i <= n; i++) cout << a[i] << " ";
puts("");
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.*;
import java.util.*;
public class z3 {
public static void wiw(long a,long b)
{ if (a<=b) {if (a==b) System.out.print(""+a+" "); else
{
System.out.print(""+(a+1)+" "+b+" ");
wiw(a+2,b-2);
System.out.print(""+(a)+" "+(b-1)+" ");
}}
}
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
long n=in.nextLong();
if (((n%4)==2)||((n%4)==3)) System.out.println("-1"); else
{
wiw(1,n);
}
}
} | JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, i;
int num1, num2, cnt;
cin >> n;
if (n == 1) {
cout << 1 << endl;
return 0;
}
if (n % 4 == 0) {
cnt = n / 2;
num1 = 2, num2 = n;
while (num1 <= cnt) {
cout << num1 << " " << num2 << " ";
num1 += 2, num2 -= 2;
}
num1 -= 2, num2 += 2;
num1--, num2--;
while (num1 > 1) {
cout << num1 << " " << num2 << " ";
num1 -= 2, num2 += 2;
}
cout << num1 << " " << num2 << endl;
} else if (n % 4 == 1) {
cnt = n / 2;
num1 = 2, num2 = n;
while (num1 <= cnt) {
cout << num1 << " " << num2 << " ";
num1 += 2, num2 -= 2;
}
num1 -= 2, num2 += 2;
cout << n / 2 + 1 << " ";
num1--, num2--;
while (num1 > 1) {
cout << num1 << " " << num2 << " ";
num1 -= 2, num2 += 2;
}
cout << num1 << " " << num2 << endl;
} else
cout << -1 << endl;
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int output[100010];
int main() {
int n;
while (cin >> n) {
if (n % 4 == 2 || n % 4 == 3)
cout << -1 << endl;
else {
int front1 = 2, front2 = n, rear1 = n - 1, rear2 = 1;
int i, count = 0;
for (i = 1; count < n / 4; i += 2) {
output[i] = front1;
output[i + 1] = front2;
output[n - i + 1] = rear1;
output[n - i] = rear2;
front1 += 2;
front2 -= 2;
rear1 -= 2;
rear2 += 2;
count++;
}
for (i = 1; i <= n; i++) {
if (output[i])
cout << output[i] << " ";
else
cout << (n + 1) / 2 << " ";
}
cout << endl;
}
memset(output, 0, sizeof(output));
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author ocelopilli
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] p = new int[ n + 1 ];
if ( n % 4 > 1 ) out.println( -1 );
else
{
p[ (n+1) / 2 ] = (n + 1) / 2;
for (int le=1, ri=n; le<ri; le+=2, ri-=2)
{
p[ le ] = ri - 1;
p[ ri-1 ] = ri;
p[ ri ] = le + 1;
p[ le + 1 ] = le;
}
for (int i=1; i<=n; i++) out.print( p[i]+" " );
}
}
}
class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream in)
{
br = new BufferedReader(new InputStreamReader(in));
st = null;
}
public String next()
{
while (st==null || !st.hasMoreTokens())
{
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() { return Integer.parseInt(next()); }
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long Set(long long N, long long pos) { return N = N | (1 << pos); }
long long reset(long long N, long long pos) { return N = N & ~(1 << pos); }
bool check(long long N, long long pos) { return (bool)(N & (1 << pos)); }
void CI(long long &_x) { scanf("%d", &_x); }
void CO(long long &_x) { cout << _x; }
template <typename T>
void getarray(T a[], long long n) {
for (long long i = 0; i < n; i++) cin >> a[i];
}
template <typename T>
void prLLIarray(T a[], long long n) {
for (long long i = 0; i < n - 1; i++) cout << a[i] << " ";
cout << a[n - 1] << endl;
}
const double EPS = 1e-9;
const long long INF = 0x7f7f7f7f;
long long dr8[8] = {1, -1, 0, 0, 1, -1, -1, 1};
long long dc8[8] = {0, 0, -1, 1, 1, 1, -1, -1};
long long dr4[4] = {0, 0, 1, -1};
long long dc4[4] = {-1, 1, 0, 0};
long long kn8r[8] = {1, 2, 2, 1, -1, -2, -2, -1};
long long kn8c[8] = {2, 1, -1, -2, -2, -1, 1, 2};
int ans[100005];
void rec(int b, int e) {
if (b > e) return;
if (b == e) {
ans[b] = e;
return;
}
ans[b] = b + 1;
ans[b + 1] = e;
ans[e] = e - 1;
ans[e - 1] = b;
rec(b + 2, e - 2);
}
int main() {
int n;
cin >> n;
if (n % 4 == 2 || n % 4 == 3) {
puts("-1");
return 0;
}
rec(1, n);
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long n, i, f, m, a[211111], ans;
vector<long long> v;
double d;
int main() {
cin >> n;
if (n == 1)
cout << 1;
else if (n % 4 == 2 || n % 4 == 3)
cout << -1;
else {
for (i = 0; i < n / 2; i++) {
if (i % 2 == 0) {
a[i] = i + 1;
a[i + 1] = n - 1 - i;
a[n - 2 - i] = i;
a[n - 1 - i] = n - 2 - i;
}
}
if (n % 2 == 1) a[n / 2] = n / 2;
for (i = 0; i < n; i++) cout << a[i] + 1 << " ";
}
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int arr[100100];
int main() {
int n;
cin >> n;
if (n == 1) {
cout << "1" << endl;
return 0;
}
memset(arr, 0, sizeof(arr));
bool f = true;
int k = 0;
if (n % 2) arr[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i <= n; i++) {
if (arr[i] == 0 && k < n / 2) {
k += 2;
arr[i] = k;
}
arr[arr[i]] = n - i + 1;
}
for (int i = 1; i <= n && f; i++)
if (arr[arr[i]] != n - i + 1) f = false;
if (f)
for (int i = 1; i <= n; i++) cout << arr[i] << " ";
else
cout << "-1" << endl;
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
int main() {
int n, k = 1, i, j, a[100001];
scanf("%d", &n);
if (n == 1) {
printf("1");
return 0;
}
k = n / 2;
for (i = 1, j = n; i < j; i++, j--) {
if (i % 2 == 0) {
a[i] = k;
a[j] = n - k + 1;
} else {
a[i] = n - k + 1;
a[j] = k;
}
k--;
}
if (i == j) a[i] = i;
int f = 0;
for (i = 1; i <= n; i++)
if (a[a[i]] != n - i + 1) {
f = 1;
break;
}
if (f == 0)
for (i = 1; i <= n; i++) printf("%d ", a[i]);
else
printf("-1");
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int v[100001];
int main() {
int i, f, n;
cin >> n;
if (n % 4 == 2 || n % 4 == 3)
cout << -1 << endl;
else {
if (n % 4 == 1) v[(n + 1) / 2] = (n + 1) / 2;
i = 1;
f = n;
while (i < f) {
v[i] = f - 1;
v[f - 1] = f;
v[f] = i + 1;
v[i + 1] = i;
i += 2;
f -= 2;
}
for (i = 1; i <= n; i++) cout << v[i] << " ";
cout << endl;
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int p[MAXN];
int ans[MAXN];
int l, r;
void put_left(int x) {
ans[l] = x;
++l;
}
void put_right(int x) {
ans[r] = x;
--r;
}
int main() {
int n;
scanf("%d", &n);
if (n % 4 == 2 || n % 4 == 3) {
puts("-1");
return 0;
}
l = 0, r = n - 1;
for (int i = 0; i < n; ++i) {
p[i] = i + 1;
}
while (r - l > 0) {
int a = p[l], b = p[l + 1], c = p[r], d = p[r - 1];
put_left(b), put_left(c);
put_right(d), put_right(a);
}
if (r == l) put_right(p[r]);
for (int i = 0; i < n; ++i) printf("%d ", ans[i]);
puts("");
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, p[1010000];
int main() {
cin >> n;
if (n % 4 == 2 || n % 4 == 3) {
cout << -1;
exit(0);
}
if (n % 4 == 0) {
for (int i = 1; i <= n / 2; i++) {
if (i % 2 == 1)
p[i] = i + 1, p[n + 1 - i] = n - i;
else
p[i] = n + 2 - i, p[n + 1 - i] = i - 1;
}
for (int i = 1; i <= n; i++) cout << p[i] << " ";
} else {
for (int i = 1; i <= n / 2; i++) {
if (i % 2 == 1)
p[i] = i + 1, p[n + 1 - i] = n - i;
else
p[i] = n + 2 - i, p[n + 1 - i] = i - 1;
}
p[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i <= n; i++) cout << p[i] << " ";
}
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Dzmitry Paulenka
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
if (n%4 > 1) {
out.println(-1);
return;
}
int a[] = new int[n+1];
for (int i = 2; i <= n/2; i += 2) {
a[i-1] = i;
a[n-(i-1)] = i-1;
a[i] = n - i + 2;
a[n - i + 2] = n - i + 1;
}
if (n % 2 != 0) {
a[n/2+1] = n/2 + 1;
}
// System.out.println(check(a));
for (int i = 1; i < n; ++i) {
out.print(a[i] + " ");
}
out.println(a[n]);
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
int p[maxn];
bool mark[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
if (n % 4 > 1) return (cout << -1, 0);
if (n % 4 == 1) p[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i <= n / 4; i++) {
int s = 2 * i - 1;
int f = n - 2 * i + 2;
p[s] = s + 1, p[s + 1] = f, p[f] = f - 1, p[f - 1] = s;
}
for (int i = 0; i < n; i++) cout << p[i + 1] << " ";
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const double infd = 2e+9;
const int infi = INT_MAX;
template <class T>
inline T sqr(T x) {
return x * x;
}
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
if (n % 4 != 0 && n % 4 != 1) {
cout << -1;
return 0;
}
vector<int> p(n + 1, -1), c(n + 1, -1);
if (n % 4 == 1) {
int m = (n + 1) / 2;
p[m] = m;
c[m] = m;
}
int top = 1;
while (top <= n) {
int i, x;
while (top <= n && p[top] != -1) top++;
if (top > n) break;
i = top++;
while (top <= n && p[top] != -1) top++;
if (top > n) break;
x = top;
for (int j = 0; j < 4; j++) {
p[i] = x;
i = n - i + 1;
swap(i, x);
}
}
for (int i = 1; i <= n; i++) cout << p[i] << ' ';
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class P286A_LuckyPermutation {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
if (n % 4 > 1) {
System.out.println(-1);
return;
}
int k = n/4;
for (int i=0; i<k; i++) {
System.out.print((2*i+2) + " " + (n-2*i) + " ");
}
if (n % 4 == 1) {
System.out.print((n+1)/2 + " ");
}
for (int i=k-1; i>=0; i--) {
System.out.print((2*i+1) + " " + (n-2*i-1) + " ");
}
System.out.println();
}
} | JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int n, p[MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
if (n % 4 == 2 || n % 4 == 3) {
cout << -1;
return 0;
}
int l = 1, r = n, div = n;
while (div / 4) {
p[l] = l + 1;
p[l + 1] = r;
p[r] = r - 1;
p[r - 1] = l;
l += 2;
r -= 2;
div -= 4;
}
if (n % 4) p[(n + 1) / 2] = (n + 1) / 2;
for (int i = 1; i <= n; ++i) cout << p[i] << " ";
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.