exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
listlengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
difficulty
int64
-1
3.5k
prob_desc_output_spec
stringlengths
17
1.47k
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
be4bf8e0164effc0527b09c08b1273b3
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.StringTokenizer; import static java.lang.Double.parseDouble; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.exit; public class CodeForces { static BufferedReader in; static PrintWriter out; static StringTokenizer tok; static void solve() throws Exception { int t = nextInt(); while (t-- > 0) { int n = nextInt(); int len = n - 2; int n1, i1, i2; if (n % 2 == 0) { n1 = 2; i1 = 4; i2 = 3; } else { n1 = 3; i1 = 2; i2 = 5; } System.out.print(n1+" "+1); while (len > 0) { System.out.print(" " + i1); len--; i1 += 2; if (len > 0) { System.out.print(" " + i2); len--; i2 += 2; } } System.out.println(); } } public static void main(String[] args) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } static int nextInt() throws IOException { return parseInt(next()); } static long nextLong() throws IOException { return parseLong(next()); } static double nextDouble() throws IOException { return parseDouble(next()); } static String next() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
7850323b80949bb8c8a070399a0323cb
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.StringTokenizer; import static java.lang.Double.parseDouble; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.exit; public class CodeForces { static BufferedReader in; static PrintWriter out; static StringTokenizer tok; static void solve() throws Exception { int t = nextInt(); while (t-- > 0) { int n = nextInt(); if (n % 2 == 0) { System.out.print(2 + " " + 1); int len = n - 2; int index1 = 4; int index2 = 3; while (len > 0) { System.out.print(" "+index1); len--; index1+=2; if (len > 0) { System.out.print(" "+index2); len--; index2+=2; } } } else { System.out.print(3 + " " + 1); int len = n - 2; int index1 = 2; int index2 = 5; while (len > 0) { System.out.print(" "+index1); len--; index1+=2; if (len > 0) { System.out.print(" "+index2); len--; index2+=2; } } } System.out.println(); } } public static void main(String[] args) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } static int nextInt() throws IOException { return parseInt(next()); } static long nextLong() throws IOException { return parseLong(next()); } static double nextDouble() throws IOException { return parseDouble(next()); } static String next() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
8683911e8f7edfcca48435bd19c59520
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while(T-- > 0){ int n = sc.nextInt(); if(n%2 == 0){ for(int i=1; i<=n; i++) if(i%2 == 0) System.out.print((i-1) + " "); else System.out.print((i+1) + " "); } else { System.out.print("3 1 2 "); if(n>3) for(int i=4; i<=n; i++) if(i%2 != 0) System.out.print((i-1) + " "); else System.out.print((i+1) + " "); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
939b637140787990d6688f4817560c4e
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { int n = Integer.parseInt(br.readLine()); ArrayList<Integer> ans = new ArrayList<Integer>(); for (int i = 1; i <= n; i++) ans.add(i); if (n % 2 == 0) { for (int i = 0; i < n; i += 2) { Collections.swap(ans, i, i + 1); } } else { for (int i = 0; i < n - 1; i += 2) { Collections.swap(ans, i, i + 1); } Collections.swap(ans, ans.size() - 1, ans.size() - 2); } for (int x : ans) pw.print(x + " "); pw.println(); } pw.close(); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
074fa8569b40417a0f4bfe88908d9672
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.*; import java.math.BigInteger; import java.text.SimpleDateFormat; import java.text.DateFormat; public class Codeforces{ public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t-- > 0){ int n = Integer.parseInt(br.readLine()); if(n % 2 == 0){ int a = n + 1; int[] arr = new int[a]; for(int i = 0; i < n; i+=2){ arr[i] = i + 2; } for(int i = 1; i < n; i += 2){ arr[i] = i; } for(int i = 0; i < n; i++){ System.out.print(arr[i] + " "); } System.out.println(); }else{ if(n > 3){ int[] arr = new int[n]; arr[0] = 3; arr[1] = 1; arr[2] = 2; for(int i = 3 ; i < n; i += 2){ arr[i] = i + 2; } for(int i = 4; i < n; i += 2){ arr[i] = i; } for(int i = 0; i < n; i++){ System.out.print(arr[i] + " "); } System.out.println(); }else{ System.out.println(3 + " " + 1 + " " + 2); } } } }}
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
152df01096ab9c34e0990b012438dfc9
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class PrettyPermutations { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); for(int i=0;i<n;i++){ int a=sc.nextInt(); System.out.println(solve(a)); } } static String solve(int a){ StringBuilder s=new StringBuilder(); if(a==1){ return "1"; } if(a%2==1){ for(int i=1;i<=a-3;i+=2){ s.append(i+1+" "+i+" "); } s.append(String.format("%d %d %d ",a,a-2,a-1)); }else{ for(int i=1;i<=a;i+=2){ s.append(i+1+" "+i+" "); } } return String.format("%s",s); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
20d0bf1b0f2586ce0c246497e69d9a3e
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class cr728_A { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-->0) { int n = s.nextInt(); if(n==2) { System.out.println(2+" "+1); }else if(n==3) { System.out.println(3+" "+1+" "+2); }else { if(n%2==0) { for(int i=1;i<=n;i++) { if(i%2==1) { System.out.print(i+1+" "); }else { System.out.print(i-1+" "); } } System.out.println(); }else { System.out.print(3+" "+1+" "+2+" "); for(int i=4;i<=n;i++) { if(i%2==1) { System.out.print(i-1+" "); }else { System.out.print(i+1+" "); } } System.out.println(); } // System.out.print(3+" "+2+" "+1+" "); // for(int i=4;i<=n;i++) { // if(i%2==1) { // System.out.print(i+1+" "); // }else { // System.out.print(i+1+" "); // } // } //System.out.println(); } // if(n%2==1) { // System.out.print(n+" "); // for(int i=1;i<n;i++) { // System.out.print(i+" "); // } // System.out.println(); // }else { // for(int i=1;i<=n;i++) { // if(i%2==1) { // System.out.print(i+1+" "); // }else { // System.out.print(i-1+" "); // } // } // System.out.println(); // } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
72dd7a6cc821e6efac7d2057449ba258
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; import javafx.util.Pair; import jdk.nashorn.internal.ir.BreakNode; public class One { static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static StringBuilder out = new StringBuilder(); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); static int I(String s) { return Integer.parseInt(s); } static long L(String s) { return Long.parseLong(s); } static double D(String s) { return Double.parseDouble(s); } static String[] SA() throws IOException { return bf.readLine().split(" "); } public static void main(String[] args) throws IOException { int t = I(bf.readLine()); while (t-- > 0) { int n = I(bf.readLine()); if (n == 2) { System.out.println("2 1"); } else if (n == 3) { System.out.println("3 1 2"); } else { if (n % 2 == 0) { for (int i = 1; i <= n; i += 2) { System.out.print((i + 1) + " " + i + " "); } } else { if ((n/2)%2==0) { for (int i = 1; i <= n / 2; i += 2) { System.out.print((i + 1) + " " + i + " "); } System.out.print(((n / 2) + 3) + " " + ((n / 2) + 1) + " " + ((n / 2) + 2) + " "); for (int i = ((n / 2) + 4); i <= n; i += 2) { System.out.print((i + 1) + " " + i + " "); } }else{ for (int i = 1; i < n / 2; i += 2) { System.out.print((i + 1) + " " + i + " "); } System.out.print(((n / 2) + 2) + " " + ((n / 2) ) + " " + ((n / 2) + 1) + " "); for (int i = ((n / 2) + 3); i <= n; i += 2) { System.out.print((i + 1) + " " + i + " "); } } } System.out.println(""); } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
c2c7994e37d3d6304e061164d1d0f080
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
//package com.amoghghadge.compProgramming.CodeforcesUnder1300; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class PrettyPermutations { public static void main(String[] args) { FastReader in = new FastReader(); StringBuffer sb = new StringBuffer(); int t = in.nextInt(); while (t > 0) { int n = in.nextInt(); int start = 2; if (n % 2 == 1) { sb.append("3 1 2 "); start = 5; } while (start <= n) { sb.append(start + " " + (start - 1) + " "); start += 2; } sb.append("\n"); t--; } System.out.println(sb); } public static int search (int l, int r, int[] a, int key) { while (l <= r) { int middle = ((r - l) / 2) + l; if (a[middle] == key) { return middle; } else if (a[middle] < key) { l = middle + 1; } else { r = middle - 1; } } return -1; } public static void sort (int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void sort (long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static class FastReader { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public long[] readArrayLong(long n) { long[] a = new long[(int) n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
47395ba6adc009217469fbd080e18a1f
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; 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; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ASimpatichniePerestanovki solver = new ASimpatichniePerestanovki(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class ASimpatichniePerestanovki { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = i + 1; } for (int i = 0; i < n - 1; i += 2) { int z = a[i]; a[i] = a[i + 1]; a[i + 1] = z; } if (n % 2 != 0) { int z = a[n - 2]; a[n - 2] = a[n - 1]; a[n - 1] = z; } for (int i = 0; i < n; i++) { out.print(a[i] + " "); } out.println(); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
ea9333c2ebcd2a230f4ea1d953b544eb
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; 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; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ASimpatichniePerestanovki solver = new ASimpatichniePerestanovki(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class ASimpatichniePerestanovki { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = i + 1; } for (int i = 0; i < n - 1; i += 2) { int z = a[i]; a[i] = a[i + 1]; a[i + 1] = z; } if (n % 2 != 0) { int z = a[n - 2]; a[n - 2] = a[n - 1]; a[n - 1] = z; } for (int i = 0; i < n; i++) { out.print(a[i] + " "); } out.println(); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
e51b83e6c9a84b59122ec15317da76d1
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; 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; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ASimpatichniePerestanovki solver = new ASimpatichniePerestanovki(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class ASimpatichniePerestanovki { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = i + 1; } for (int i = 0; i < n - 1; i += 2) { int z = a[i]; a[i] = a[i + 1]; a[i + 1] = z; } if (n % 2 != 0) { int z = a[n - 2]; a[n - 2] = a[n - 1]; a[n - 1] = z; } for (int i = 0; i < n; i++) { out.print(a[i] + " "); } out.println(); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
c6016f9f378866a72adfef69ad0f0029
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; 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; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ASimpatichniePerestanovki solver = new ASimpatichniePerestanovki(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class ASimpatichniePerestanovki { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = i + 1; } for (int i = 0; i < n - 1; i += 2) { int z = a[i]; a[i] = a[i + 1]; a[i + 1] = z; } if (n % 2 != 0) { int z = a[n - 2]; a[n - 2] = a[n - 1]; a[n - 1] = z; } for (int i = 0; i < n; i++) { out.print(a[i] + " "); } out.println(); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
41a54843eb9010771259f0ccfd381cee
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; import java.util.*; public class CatProblem { public static void main(String[] args) { Scanner myObj = new Scanner(System.in); int tests = myObj.nextInt(); List<Integer> list=new ArrayList<Integer>(); for(int j=0;j<tests;j++) { list.add(myObj.nextInt()); } for(int cats : list){ { if(cats % 2 ==0 ) { for(int i=1 ; i<cats;i=i+2) { System.out.print(i+1 +" "+ i); if(i+2<cats) System.out.print(" "); System.out.println(); } }else { for(int i=1 ; i<cats-2;i=i+2) System.out.print(i+1 +" "+ i+" "); System.out.println((cats)+" "+(cats-2)+" "+(cats-1)); System.out.println(); } } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
5d2a189f0302fd6a18ef00d10886c69f
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.StringTokenizer; public class P1541A { static PrintWriter out = new PrintWriter(System.out); static MyFastReaderP1541A in = new MyFastReaderP1541A(); static long mod = (long) (1e9 + 7); public static void main(String[] args) throws Exception { int test = i(); while (test-- > 0) { int n = i(); int i; int ar[]=new int[n],p=0; for(i=2;i<=n;i+=2){ ar[p++]=i; ar[p++]=i-1; } if((n^1)==n-1){ ar[n-1]=n; ar[n-2]=(ar[n-2]+ar[n-1])-(ar[n-1]=ar[n-2]); } for(int val: ar) out.print(val+" "); out.print("\n"); out.flush(); } out.close(); } static class pair { long x, y; pair(long ar, long ar2) { x = ar; y = ar2; } } static void sort(long[] a) // check for long { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static class DescendingComparator implements Comparator<Integer> { public int compare(Integer a, Integer b) { return b - a; } } static class AscendingComparator implements Comparator<Integer> { public int compare(Integer a, Integer b) { return a - b; } } static boolean isPalindrome(char X[]) { int l = 0, r = X.length - 1; while (l <= r) { if (X[l] != X[r]) return false; l++; r--; } return true; } static long fact(long N) { long num = 1L; while (N >= 1) { num = ((num % mod) * (N % mod)) % mod; N--; } return num; } static long pow(long a, long b) { long mod = 1000000007; long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) pow = (pow * x) % mod; x = (x * x) % mod; b /= 2; } return pow; } static long toggleBits(long x)// one's complement || Toggle bits { int n = (int) (Math.floor(Math.log(x) / Math.log(2))) + 1; return ((1 << n) - 1) ^ x; } static int countBits(long a) { return (int) (Math.log(a) / Math.log(2) + 1); } static boolean isPrime(long N) { if (N <= 1) return false; if (N <= 3) return true; if (N % 2 == 0 || N % 3 == 0) return false; for (int i = 5; i * i <= N; i = i + 6) if (N % i == 0 || N % (i + 2) == 0) return false; return true; } static long GCD(long a, long b) { if (b == 0) { return a; } else return GCD(b, a % b); } // Debugging Functions Starts static void print(char A[]) { for (char c : A) System.out.print(c + " "); System.out.println(); } static void print(boolean A[]) { for (boolean c : A) System.out.print(c + " "); System.out.println(); } static void print(int A[]) { for (int a : A) System.out.print(a + " "); System.out.println(); } static void print(long A[]) { for (long i : A) System.out.print(i + " "); System.out.println(); } static void print(ArrayList<Integer> A) { for (int a : A) System.out.print(a + " "); System.out.println(); } // Debugging Functions END // ---------------------- // IO FUNCTIONS STARTS static HashMap<Integer, Integer> Hash(int A[]) { HashMap<Integer, Integer> mp = new HashMap<>(); for (int a : A) { int f = mp.getOrDefault(a, 0) + 1; mp.put(a, f); } return mp; } static String string() { return in.nextLine(); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] inputInt(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] inputLong(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) A[i] = in.nextLong(); return A; } } class MyFastReaderP1541A { BufferedReader br; StringTokenizer st; public MyFastReaderP1541A() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
98ead47d3ccf0d9911b51f3fa1743ff7
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class josh { // Driver program to test the above function public static void main(String []args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0) { int n = s.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++){ arr[i]=i+1; } if(n%2!=0){ arr[0]=2;arr[1]=3;arr[2]=1; for(int i=3;i<n-1;i=i+2){ int temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } else{ for(int i=0;i<n-1;i=i+2){ int temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } for(int i=0;i<n;i++){ System.out.print(arr[i]+" "); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
2d6766cf27e4f4d59f7dd26844f33af6
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.*; import java.lang.Math; public class Solution { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t>0){ int n = sc.nextInt(); int[] ans = new int[n]; int i; for(i=0;i<n;i++){ ans[i] = i+1; } int tmp; if(n%2==0){ for(i=0;i+1<n;i+=2){ tmp = ans[i]; ans[i] = ans[i+1]; ans[i+1] = tmp; } } else { int mid = n/2; for(i=0;i+1<mid;i+=2){ tmp = ans[i]; ans[i] = ans[i+1]; ans[i+1] = tmp; } for(i=mid+1;i+1<n;i+=2){ tmp = ans[i]; ans[i] = ans[i+1]; ans[i+1] = tmp; } tmp = ans[mid]; ans[mid] = ans[mid-1]; ans[mid-1] = tmp; if(n%4==3){ tmp = ans[n-1]; ans[n-1] = ans[n-2]; ans[n-2] = tmp; } } for(i=0;i<n;i++){ System.out.print(ans[i]+" "); } System.out.println(); t--; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
3522fb69962481ab4a6d7215de73fa5e
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.math.BigInteger; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; 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; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); int tests = in.nextInt(); for (int i = 0; i < tests; i++) { solver.solve(i, in, out); } out.close(); } static class Task { static final int DENOM = 100; static final long MODULO = (long) 1e9 + 7; private static final long INVDENOM = BigInteger.valueOf(DENOM).modInverse(BigInteger.valueOf(MODULO)) .longValue(); int n; long[][] p; int[][] s; public void solve(int testNumber, InputReader in, PrintWriter out) { n = in.nextInt(); int res = 0; if (n % 2 == 1) { out.print("3 1 2 "); n -= 3; res = 3; } for (int i = 0; i < n; i++) { if (i % 2 == 0) { out.print("" + (i+res + 1 + 1) + " "); } else { out.print("" + (i+res - 1 + 1) + " "); } } res += n; out.println(""); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
b57bee2d82150f1bacbc264f021869bb
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.*; public class Trials1 { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader inp=new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out)); int t=Integer.parseInt(inp.readLine()); while(t-- >0) { int n=Integer.parseInt(inp.readLine()); String s=""; if(n%2==0) { for(int i=1;i<=n;i+=2) { s=s+" "+(i+1)+" "+(i); } } else { int d=n/2 - 1; for(int i=1;i<=n-3;i+=2) { s=s+" "+(i+1)+" "+(i); } s=s+" "+n+" "+(n-2)+" "+(n-1); } s=s.trim(); out.write(s+"\n"); } out.flush(); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
de0d4853407762e82eb5436135a12ab0
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
//package java2; import java.util.Arrays; import java.util.Scanner; public class firstclass { public static void main(String[] args) { // Classes and objects /*class Box{ private int len,wid; public Box(int l,int b){ len=l; wid=b; } public void getdim() { System.out.println("The dimesnsions re: "+ len+" x "+wid); } } Box small=new Box(5,3); small.getdim();*/ // IO and Wrapper classes /*Scanner sc=new Scanner(System.in); Integer a=Integer.valueOf(sc.nextLine()); int x=Integer.parseInt(sc.nextLine()); int y=a.intValue(); if(x==y) { System.out.println("Same"); } */ // Inheritance /* class doctor{ protected int money; protected boolean fame; } class physician extends doctor{ public physician() { money=1000; fame=true; } } class dentist extends doctor{ public dentist() { money=500; fame=false; } } doctor d=new physician(); System.out.println("For physician "+d.money+" "+d.fame); doctor e=new dentist(); System.out.println("For dentist "+e.money+" "+e.fame);*/ // Super constructors /*class car{ protected int milage; public car(int x) { milage=x; System.out.println(milage); } } class dzire extends car{ public dzire() { super(5); } } dzire d=new dzire();*/ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t>0) { t--; int n=sc.nextInt(); int a[]=new int[n]; if(n%2==0) { for(int i=0;i<n;i++) { if(i%2==0) { a[i]=i+2; } else { a[i]=i; } } } else { for(int i=0;i<n-1;i++) { if(i%2==0) { a[i]=i+2; } else { a[i]=i; } } a[n-1]=a[n-2]; a[n-2]=n; } for(int i=0;i<n;i++) { System.out.print(a[i]); System.out.print(" "); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
0e9923adf856193ae6cf5d649805e473
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.ArrayList; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; import java.util.Arrays; import java.util.PriorityQueue; import java.util.HashSet; import java.util.HashMap; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.sqrt; import static java.lang.Math.abs; import static java.lang.Math.random; import static java.lang.Integer.MAX_VALUE; import static java.lang.Integer.MIN_VALUE; import static java.util.Collections.reverseOrder; public final class Test { static int mod1 = 998244353; public static void main(String[] args) { solve(); } static long gcd(int a, int b) {if (b == 0) return a; return gcd(b, a % b); } static long modAdd(long a, long b, long m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} static long modMul(long a, long b, long m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} static long modSub(long a, long b, long m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;} static long modDiv(long a, long b, long m) {a = a % m; b = b % m; return (modMul(a, mminvprime(b, m), m) + m) % m;} //only for prime m static long mminvprime(long a, long b) {return expo(a, b - 2, b);} static long expo(long a, long b, long mod) {long res = 1; while (b > 0) {if ((b & 1) != 0)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;} static long setBits(int n) {int cnt = 0; while (n != 0) { cnt++; n = n & (n - 1); } return cnt;} static int[] sieve(int n) {int[] arr = new int[n + 1]; for (int i = 2; i <= n; i++)if (arr[i] == 0) {for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return arr;} static void debug(int[] nums) { for (int i = 0; i < nums.length; i++) System.out.print(nums[i] + " "); System.out.println("");} static void debug(long[] nums) { for (int i = 0; i < nums.length; i++) System.out.print(nums[i] + " "); System.out.println("");} static void debug(String[] nums) { for (int i = 0; i < nums.length; i++) System.out.print(nums[i] + " "); System.out.println("");} static void reverse(int[] nums) { int start = 0, end = nums.length - 1; while (start < end) {int temp = nums[start]; nums[start] = nums[end]; nums[end] = temp; start++; end--; } } static void reverse(char[] nums) { int start = 0, end = nums.length - 1; while (start < end) {char temp = nums[start]; nums[start] = nums[end]; nums[end] = temp; start++; end--; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } // <------------------------- CODE START FROM HERE --------------------------------> public static void solve() { FastScanner sc = new FastScanner(); int t = sc.nextInt(); while (t-- != 0) { int n = sc.nextInt(); int res[] = new int[n]; for (int i = 0; i < n; i++) { res[i] = i + 1 ; } for (int i = 0; i + 1 < n; i = i + 2) { int temp = res[i]; res[i] = res[i + 1]; res[i + 1] = temp; } if (n % 2 == 1) { int temp = res[n - 1]; res[n - 1] = res[n - 2]; res[n - 2] = temp; } debug(res); //System.out.println(ans); } } // <----------------------------------CODE END HERE------------------------> // System.out.println(); // int b[] = a.clone(); }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
a8e1ca205ce2201e7d715db0fe8fa42c
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
// "static void main" must be defined in a public class. import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int tt=0;tt<t;tt++){ int n = sc.nextInt(); // int a[] = new int[n+1]; if(n==2){ System.out.println("2 1"); continue; } if(n%2==0){ for(int i=1;i<=n;i+=2){ System.out.print((i+1)+" "+i+" "); } }else{ boolean b = false; System.out.print("3 1 2 "); for(int i=4;i<n;i+=2){ System.out.print((i+1)+" "+i+" "); b=true; } // if(b){ System.out.print((n-2)+" "+(n-1)+" ");} } // for(int j=n;j>0;j-=2){ // System.out.print(j+" "); // } System.out.println(); //2 1 4 3 //3 1 2 5 4 } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
4c6a618286fd4242da038f1cee1f0365
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Pretty_Permutations { public static void main(String[] args) { ArrayList <Integer> mList = new ArrayList<>(); Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int m=0; m<t; m++){ int n = sc.nextInt(); if (n % 2 == 0) { for(int i=1; i<=n ; i+=2){ System.out.print(i+1 + " "+ i +" "); } } else { for(int i=1; i<n ; i+=2){ mList.add(i+1); mList.add(i); } mList.add(n-2,n); for(int i=0; i <mList.size(); i++){ System.out.print(mList.get (i)+ " "); } } mList.removeAll(mList); System.out.print("\n"); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
cd39b5b652a20b119ee96a75c16b510c
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class file { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for (int i = 0; i < t; i++) { int num = Integer.parseInt(br.readLine()); if (num%2 != 0) { if (num == 3) { System.out.print(num+" "); System.out.print((num-2)+" "); System.out.print((num-1)+" "); } else { int pairs = num/2-1; int cnt = 0; int idx = 1; while (cnt < pairs) { System.out.print((idx+1)+" "); System.out.print(idx+" "); idx += 2; cnt++; } System.out.print(num-1+" "); System.out.print(num+" "); System.out.print(num-2+" "); } } else { int j = 1; while (j <= num) { System.out.print((j+1)+" "); System.out.print(j+" "); j += 2; } } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
b86f3e76bb8124e9f97a969fc62a4de8
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.*; public final class CatReOrdering { static void minimumDistanceMoved(int[] positions) { int n = positions.length; if(n%2==1) n-=1; for(int i=0;i<n-1;) { int temp = positions[i]; positions[i] = positions[i+1]; positions[i+1] = temp; i+=2; } if(positions.length % 2 == 1) { int temp = positions[n-1]; positions[n-1] = positions[n]; positions[n] = temp; } for(int i=0;i<positions.length;i++) System.out.print(positions[i]+" "); System.out.println(); } public static void main (String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); while(T-->0) { int n = in.nextInt(); int[] positions = new int[n]; for(int i=0;i<n;i++) { positions[i] = i+1; } minimumDistanceMoved(positions); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
c66ca9ac664c2fe9e90fc24777b92148
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class A { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int[] ar; int t = scan.nextInt(); int n = 0; int odd=0, even=0; int swap = 0; while(t-->0){ n = scan.nextInt(); ar = new int[n+1]; for(int i=1;i<=n;i++){ ar[i] = i; } if(n%2==0){ for(int i=1;i<n;i+=2){ swap = ar[i]; ar[i] = ar[i+1]; ar[i+1] = swap; } } else{ for(int i=1;i<n-1;i+=2){ swap = ar[i]; ar[i] = ar[i+1]; ar[i+1] = swap; } swap = ar[n-1]; ar[n-1] = ar[n]; ar[n] = swap; } for(int i=1;i<=n;i++){ System.out.print(ar[i]+" "); } odd = 0; even = 0; System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
90e42006ac16459593fc47d763ac836b
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class A { public static void main(String[] args) { int T; Scanner s = new Scanner(System.in); //System.out.println("Enter Number of Test Cases:-"); T = s.nextInt(); int counter = 0; while(T > counter) { int N = s.nextInt(); int array[] = new int[N]; int j = 1; for(int i = 0; i < N; i++) { array[i] = j; j++; } //System.out.println(Arrays.toString(array)); int size = array.length; //If size is even if(size % 2 == 0) { for(int i = 0; i < N; i = i+2) { System.out.print(array[i+1] + " "); System.out.print(array[i] + " "); } } //If size is 3 else if(size == 3) { System.out.print(3 + " " + 1 + " " + 2); } //If size is odd else { for(int i = 0; i < N - 3; i = i+2) { System.out.print(array[i+1] + " "); System.out.print(array[i] + " "); } System.out.print(array[N-1] + " " + array[N-3] + " " + array[N-2]); } System.out.println(""); counter++; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
6e7dbcb31bc2e9ac402464f3d6eb245b
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class June25a { public static void main (String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for(int i=0;i<t;i++) { int n=Integer.parseInt(br.readLine()); if(n%2==1) { for (int j = 1; j <= n-3; j++) { // Find and print the ith term System.out.print(" "+((j % 2 == 0) ? (j - 1) : (j + 1))); } System.out.print(" "+n); System.out.print(" "+(n-2)); System.out.print(" "+(n-1)); } else { for (int j = 1; j <= n; j++) { // Find and print the ith term System.out.print(" "+((j % 2 == 0) ? (j - 1) : (j + 1))); } } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
515226cf98aa1d1173636b47112fedd3
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; public class PrettyPermuatations { public static void printPermuation(int N){ int[] arr = new int[N]; for(int i=0;i<N;i++){ arr[i] = i+1; } for(int i=1;i<N;i+=2){ int temp = arr[i-1]; arr[i-1] = arr[i]; arr[i] = temp; } if(N%2 != 0){ int temp = arr[N-2]; arr[N-2] = arr[N-1]; arr[N-1] = temp; } StringBuilder str = new StringBuilder(); for(int i=0;i<N;i++){ str.append( arr[i]); if(i != N-1){ str.append(" "); } } System.out.println(str.toString()); } public static void main(String[] args) { Scanner input = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int T = input.nextInt(); input.nextLine(); for(int i=0;i<T;i++){ int N = input.nextInt(); printPermuation(N); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
0adc259e828fb0796fcc0310fe9e2e63
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class Permutations { public static void main(String args[]) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-->0) { ArrayList<Integer> nums = new ArrayList<Integer>(); int a = in.nextInt(); for (int b = 1; b <= a; b++) { nums.add(b); } if (nums.size()%2==0) { for (int b = 0; b<nums.size()-1; b+=2) { nums.add (b, nums.remove (b+1)); } } else { nums.add (0,nums.remove (2)); for (int b = 3; b < nums.size()-1;b+=2) nums.add (b, nums.remove (b+1)); } for (int b = 0; b < nums.size(); b++) {System.out.print (nums.get(b) + " ");} System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
7f3eb9b79d8d6c88d2060348fe95dd6a
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.util.*; import java.math.*; import java.io.*; import java.text.*; import java.math.BigInteger; public class Main { static class pair{ Character x; int y; public pair(Character x,int y) { this.x=x; this.y=y; } } public static boolean[] sieve(long n) { boolean[] prime = new boolean[(int)n+1]; Arrays.fill(prime,true); prime[0] = false; prime[1] = false; long m = (long)Math.sqrt(n); for(int i=2;i<=m;i++) { if(prime[i]) { for(int k=i*i;k<=n;k+=i) { prime[k] = false; } } } return prime; } public static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } public static boolean[] primeseive(long n) { boolean prime[]=new boolean[(int)n+1]; Arrays.fill(prime, true); prime[0]=false; prime[1]=false; int m=(int) Math.sqrt(n); for(int i=2;i<=m;i++) { if(prime[i]) { for(int k=i*i;k<=n;k+=i) { prime[k]=false; } } } return prime; } public static String reverse(String input) { StringBuilder str = new StringBuilder("") ; for(int i =input.length()-1 ; i >= 0 ; i-- ) { str.append(input.charAt(i)); } return str.toString() ; } public static ArrayList<Integer> allFactors(int n) { ArrayList<Integer> list = new ArrayList<>() ; for(int i = 1; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n)list.add(i) ; else{ list.add(i); list.add(n/i); } } } return list ; } public static ArrayList<Long> allFactors(long n) { ArrayList<Long> list = new ArrayList<>() ; for(long i = 1L; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) list.add(i) ; else{ list.add(i) ; list.add(n/i) ; } } } return list ; } ////////////////////////////////////////////////////////////////////////////////// /* lowerBound - finds largest element equal or less than value paased upperBound - finds smallest element equal or more than value passed if not present return -1; */ public static long lowerBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k){ ans=arr[mid]; start=mid+1; } else{ end=mid-1; } } return ans; } public static int lowerBound(int[] arr,int k) { int ans=0;int start=0;int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=mid; start=mid+1; } else { end=mid-1; } } return ans; } public static long upperBound(long[] arr,long k) { long ans=-1;int start=0;int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=mid; end=mid-1; } else { start=mid+1; } } return ans; } public static int upperBound(int[] arr,int k) { int ans=arr.length-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=mid; end=mid-1; } else { start=mid+1; } } return ans; } //////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// static int GCD(int a,int b) { if(b==0) { return a; } return GCD(b,a%b); } static long gcdnik(long a,long b) { if(b==0) { return a; } return gcdnik(b,a%b); } static long CountCoPrimes(long n) { long res = n; for(int i=2;i*i<=n;i++) { if(n%i==0) { while(n%i==0) { n/=i; } res-=res/i; } } if(n>1) { res-=res/n; } return res; } static boolean prime(int n) { for(int i=2;i*i<=n;i++) { if(i%2==0 ||i%3==0) { return false; } } return true; } static int length(int len) { if(len==1) { return 1; } else if(len==2) { return 3; } else if(len==3) { return 6; } else if(len==4) { return 10; } return 0; } public static int LargestFour(int arr[]) { Arrays.sort(arr); int n=arr.length; int count=0; int sum=0; for(int i=n-1;i>=1;i--) { sum+=arr[i]; count++; if(count==4) { break; } } if(count<4) { sum+=arr[0]; } return sum; } //Nikunj Gupta public static void insertionSort(int array[]) { int n = array.length; for (int j = 1; j < n; j++) { int key = array[j]; int i = j-1; while ( (i > -1) && ( array [i] > key ) ) { array [i+1] = array [i]; i--; } array[i+1] = key; } } static String solve23(int n) { int k=0; while(true) { if(n-2020*k<0) { return "NO"; } if((n-2020*k)%2021==0) { return "YES"; } k++; } } public static String swapString(String a, int i, int j) { char[] b =a.toCharArray(); char ch; ch = b[i]; b[i] = b[j]; b[j] = ch; return String.valueOf(b); } public static void permutation(String str,int start,int end,ArrayList<String> l,Set<String>set) { if(start==end-1) { set.add(str); } for(int i=start;i<end;i++) { str=swapString(str,start,i); permutation(str,start+1,end,l,set); swapString(str,start,i); } } public static boolean solution(int j) { String str=Integer.toString(j); int zero=0; int one=0; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='7') { one++; }else if(str.charAt(i)=='4') { zero++; } } if(one==zero) { return true; } return false; } public static int lower_bound(ArrayList<Long> ar,long k) { int s=0; int e=ar.size(); while (s !=e) { int mid = s+e>>1; if (ar.get(mid) <k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return ar.size()-1; } return s; } private static void printsubsequences(String c, String st) { // TODO Auto-generated method stub if(c.length()==0) { System.out.println(st); return; } printsubsequences(c.substring(1),st+c.charAt(0)); printsubsequences(c.substring(1),st); } private static boolean compare(HashMap<Character, Integer> smap, HashMap<Character, Integer> pmap) { // TODO Auto-generated method stub for(char ch:smap.keySet()) { if(smap.get(ch)!=pmap.get(ch)) { return false; } } return true; } private static boolean BinarySearch(int search, int[] val, int n) { // TODO Auto-generated method stub int start=0; int end=val.length-1; while(start<=end) { int mid=(start+end)/2; if(val[mid]==search) { return true; } else if(val[mid]<search) { start=mid+1; } else { end=mid-1; } } return false; } static long re(long n) { n+=1; while(n%10==0) { n/=10; } return n; } static long xor(long n) { if(n%4==0) { return n; } if(n%4==1) { return 1; } if(n%4==2) { return n+1; } return 0; } static long xor(long a,long b) { return xor(b)^xor(a-1); } static void swap(char c,char p) { char t = c; c = p; p = t; } static long max(long n,long m) { return Math.max(n,m); } static long min(long n,long m) { return Math.min(n,m); } static int abs(int n) { return Math.abs(n); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready();} } public static char[] solve(char arr[]) { int i=0; char ans[]=new char[arr.length]; while(i<arr.length-1) { if(arr[i]=='B'&&arr[i+1]=='G') { ans[i]='G'; ans[i+1]='B'; i=i+2; }else { ans[i]=arr[i]; i++; } } if(i==arr.length-1) { ans[i]=arr[i]; } return ans; } public static void pairsort(ArrayList<pair>l){ l.sort(new Comparator<pair>() { public int compare(pair o1,pair o2) { if(o1.x>o2.x) { return 1; }else { return -1; } } }); } public static void solve(char[][]ch,int row,int col) { //System.out.println(row+" "+col); if(row !=-1&&col !=-1) { int f=0; if(ch[row][col]=='W') { f=1; } boolean flag=false; for(int j=col-1;j>=0;j--) { if(ch[row][j] == '.') { if(f==1) { ch[row][j]='R'; f=0; }else { ch[row][j]='W'; f=1; } } else if(ch[row][j]=='R') { if(f==0) { // System.out.println("hii"); flag=true; break; }else { f=0; } } else if(ch[row][j]=='W') { if(f==1) { // System.out.println("Hii4"); flag=true; break; }else { f=1; } }if(flag==true) { break; } } if(flag==true) { System.out.println("NO"); return; } f=0; if(ch[row][col]=='W') { f=1; } for(int j=col+1;j<ch[0].length;j++) { if(ch[row][j] == '.') { if(f==1) { ch[row][j]='R'; f=0; }else { ch[row][j]='W'; f=1; } } else if(ch[row][j]=='R') { if(f==0) { flag=true; break; }else { f=0; } } else if(ch[row][j]=='W') { if(f==1) { flag=true; break; }else { f=1; } } if(flag==true) { break; } } if(flag==true) { System.out.println("NO"); return; } for(int i=row-1;i>=0;i--) { } for(int i=row+1;i<ch.length;i++) { f=0;//red if(ch[i-1][0]=='W') { f=1; } for(int j=0;j<ch[0].length;j++) { if(ch[i][j] == '.') { if(f==1) { ch[i][j]='R'; f=0; }else { ch[i][j]='W'; f=1; } } else if(ch[i][j]=='R') { if(f==0){ flag=true; break; }else { f=0; }} else if(ch[i][j]=='W') { if(f==1) { flag=true; break; }else { f=1; }}} if(flag==true) { break; } } if(flag) { System.out.println("NO1"); return; } System.out.println("YES"); for(int i=0;i<ch.length;i++) { for(int j=0;j<ch[0].length;j++) { System.out.print(ch[i][j]); } System.out.println(); } }else { System.out.println("YES"); int f=0; for(int i=0;i<ch.length;i++) { if(i>0) { char c=ch[i-1][0]; if(c=='W') { f=0; }else { f=1; } } for(int j=0;j<ch[0].length;j++) { if(f==0) { ch[i][j]='R'; System.out.print("R"); f=1; }else { ch[i][j]='W'; System.out.print("W"); f=0; } } System.out.println(); } } } static class death implements Comparator<pair>{ public int compare(pair o1,pair o2) { if(o1.x<o2.x) { return -1; } else if(o1.x>o2.x) { return 1; } else { return 1; } } } public static void main(String[] args) throws IOException{ Scanner s=new Scanner(System.in); int op=s.nextInt(); while(op-- >0) { int n=s.nextInt(); if(n%2 ==0) { for(int i=2;i<=n;i+=2) { System.out.print(i+" "); System.out.print(i-1+" "); } }else { for(int i=2;i<=n;i+=2) { if(i !=n-1) { System.out.print(i+" "); System.out.print(i-1+" "); } else { System.out.print(i+" "); System.out.print(i+1+" "); System.out.print(i-1); break; } } } System.out.println(); } } static double solve(int or,int r,int c,int k,double dp[][]) { if(r==1&&c>1) { return 0; } if(r==1&&c==1) { Double ty=new Double(k); ty=ty-1; double div=2; double val=ty/div; // System.out.println("val is"+val); dp[r][c]=val; return val; } if(c==0) { return 0; } if(dp[r][c]!=-1) { return dp[r][c]; } double cal1=solve(or,r-1,c-1,k,dp); double cal2=solve(or,r-1,c,k,dp); // System.out.println(cal1+" "+cal2); double div=2; if(r !=or) { dp[r][c]=((cal1+cal2)-1)/div; }else { dp[r][c]=((cal1+cal2)); } return dp[r][c]; } public static long mod(long x){ //long M=1000000007; long M=998244353; return (int)((x%M+M)%M); } static long add(long a,long b){ return mod(mod(a)+mod(b)); } public static long mul(long a,long b){ return mod(mod(a)*mod(b)); } static void printAllKLengthRec(char[] set, String prefix, int n, int k,ArrayList<String>l) { // Base case: k is 0, // print prefix if (k == 0) { //System.out.println(prefix); l.add(prefix); return; } // One by one add all characters // from set and recursively // call for k equals to k-1 for (int i = 0; i < n; ++i) { // Next character of input added String newPrefix = prefix + set[i]; // k is decreased, because // we have added a new character printAllKLengthRec(set, newPrefix, n, k - 1,l); } } // class Pair implements Comparable<Pair> // { // int x,y; // public Pair(int x,int y) // { // this.x = x; // this.y = y; // } // public int compareTo(Pair o) // { // return this.y-o.y; // } // } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
b4e3cbd92407dd726d59d705864ad7db
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.io.*; public class saxxx { static FastReader sc = new FastReader(); public static void main(String[] args) throws IOException { try { int t = sc.nextInt(); while (t-- > 0) { solve(); } } catch (Exception e) { return; } } static void solve() { int n = sc.nextInt(); int a[] = new int[n]; if (n % 2 != 0) { if(n==3) { System.out.println("3 1 2"); return; }else { System.out.print("3 1 2 "); for (int i = 3; i < n; i++) { a[i] = i + 1; } for (int i = 3; i < n-1; i+=2) { int temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } for(int i=3;i<n;i++) { System.out.print(a[i] + " "); } return; } } else { for (int i = 0; i < n; i++) { a[i] = i + 1; } for (int i = 0; i < n-1; i+=2) { int temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } } for (int i : a) { System.out.print(i + " "); } System.out.println(); } static boolean[] seiveOfEratosthenes(int n) { boolean[] isPrime = new boolean[n + 1]; Arrays.fill(isPrime, true); for (int i = 2; i * i <= n; i++) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } return isPrime; } static boolean isPrime(long n) { if (n < 2) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(next()); } return a; } void printIntArray(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } long[] readLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = Long.parseLong(next()); } return a; } void printLongArray(long a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
0439fbe1c14d770bf42dc2c9877c5074
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.io.*; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.*; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.math.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import static java.lang.Math.sqrt; import static java.lang.Math.floor; public class topcoder { public static void main(String args[])throws IOException{ BufferedReader ob = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(ob.readLine()); while(t --> 0) { int num = Integer.parseInt(ob.readLine()); if(num%2 == 0) { for(int i = 1; i <= num; i+=2) { System.out.print((i+1)+" "+i+" "); } }else { for(int i = 1; i <= num-3; i+=2) { System.out.print((i+1)+" "+i+" "); } System.out.print(num+" "+(num-2)+" "+(num-1)); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
b6d47220f849083f06627b8ed20a04f4
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.*; public class solution { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t>0) { int n = sc.nextInt(); if(n%2==0) { for(int i=1;i<=n;i+=2) { System.out.print(i+1+" "); System.out.print(i+" "); } } else{ for(int i=1;i<=n-3;i+=2) { System.out.print(i+1+" "); System.out.print(i+" "); } System.out.print(n-1+" "); System.out.print(n+" "); System.out.print(n-2+" "); } System.out.println(""); t--; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
7002275b90bb954c99118e827475148a
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Solution { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for (int i = 0; i < t; i++) { int n = Integer.parseInt(br.readLine()); int evenN = n % 2 == 0 ? n : n - 1; int[] array = new int[n]; for (int j = 1; j <= evenN; j++) { array[j - 1] = j % 2 == 1 ? j + 1 : j - 1; } if (evenN != n) { array[n - 1] = array[n - 2]; array[n - 2] = n; } for (int j = 1; j <= n; j++) { if (j == n) { System.out.print(array[j - 1]); } else { System.out.print(array[j - 1] + " "); } } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
d90bc7d75149d2c640c4d7d8d8242c35
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { // *** ++ // +=-==+ +++=- //+-:---==+ *+=----= //+-:------==+ ++=------== //=-----------=++========================= //+--:::::---:-----============-=======+++==== //+---:..:----::-===============-======+++++++++ //=---:...---:-===================---===++++++++++ //+----:...:-=======================--==+++++++++++ //+-:------====================++===---==++++===+++++ //+=-----======================+++++==---==+==-::=++**+ //+=-----================---=======++=========::.:-+***** //+==::-====================--: --:-====++=+===:..-=+***** //+=---=====================-... :=..:-=+++++++++===++***** //+=---=====+=++++++++++++++++=-:::::-====+++++++++++++*****+ //+=======++++++++++++=+++++++============++++++=======+****** //+=====+++++++++++++++++++++++++==++++==++++++=:... . .+**** //++====++++++++++++++++++++++++++++++++++++++++-. ..-+**** //+======++++++++++++++++++++++++++++++++===+====:. ..:=++++ //+===--=====+++++++++++++++++++++++++++=========-::....::-=++* //====--==========+++++++==+++===++++===========--:::....:=++* //====---===++++=====++++++==+++=======-::--===-:. ....:-+++ //==--=--====++++++++==+++++++++++======--::::...::::::-=+++ //===----===++++++++++++++++++++============--=-==----==+++ //=--------====++++++++++++++++=====================+++++++ //=---------=======++++++++====+++=================++++++++ //-----------========+++++++++++++++=================+++++++ //=----------==========++++++++++=====================++++++++ //=====------==============+++++++===================+++==+++++ //=======------==========================================++++++ // created by : pritam kumar static long LowerBound(long[] a2, long x) { // x is the target value or key int l=-1,r=a2.length; while(l+1<r) { int m=(l+r)>>>1; if(a2[m]>=x) r=m; else l=m; } return r; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static void ruffleSort(long[] a) { int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { long oi=r.nextInt(n), temp=a[i]; a[i]=a[(int)oi]; a[(int)oi]=temp; } Arrays.sort(a); } static void ruffleSort(int[] a) { int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n), temp=a[i]; a[i]=a[oi]; a[oi]=temp; } Arrays.sort(a); } public static int findIndex(long arr[], long t) { // if array is Null if (arr == null) { return -1; } // find length of array int len = arr.length; int i = 0; // traverse in the array while (i < len) { // if the i-th element is t // then return the index if (arr[i] == t) { return i; } else { i = i + 1; } } return -1; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // method to return LCM of two numbers static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static int[] swap(int a[], int left, int right) { // Swap the data int temp = a[left]; a[left] = a[right]; a[right] = temp; // Return the updated array return a; } // public static pair swap(long max0,long max1) // { // pair p=new pair(0, 0); // long temp=max0; // max0=max1; // max1=temp; // p.x=max0; // p.y=max1; // return p; // // } public static void swap(int max0,int max1) { int temp=max0; max0=max1; max1=temp; } public static int[] reverse(int a[], int left, int right) { // Reverse the sub-array while (left < right) { int temp = a[left]; a[left++] = a[right]; a[right--] = temp; } // Return the updated array return a; } public static int[] findNextPermutation(int a[]) { int last = a.length - 2; // find the longest non-increasing suffix // and find the pivot while (last >= 0) { if (a[last] < a[last + 1]) { break; } last--; } // If there is no increasing pair // there is no higher order permutation if (last < 0) return a; int nextGreater = a.length - 1; // Find the rightmost successor to the pivot for (int i = a.length - 1; i > last; i--) { if (a[i] > a[last]) { nextGreater = i; break; } } // Swap the successor and the pivot a = swap(a, nextGreater, last); // Reverse the suffix a = reverse(a, last + 1, a.length - 1); // Return true as the next_permutation is done return a; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static double pow(double p,double tt) { double ii,q,r; q=1; r=p; while(tt>1) { for(ii=1;2*ii<=tt;ii*=2) p*=p; tt-=ii; q*=p; p=r; } if(tt==1) q*=r; return q; } static int factorial(int n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static boolean prime[] ; static void sieve(int n) { prime= new boolean[n+1]; for (int i = 0; i <= n; i++) prime[i] = true; prime[0]=false; prime[1]=false; 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 long[][] a; static ArrayList<Integer>[] tree; static long[][] dp; public static void main(String[] args) { FastReader s=new FastReader(); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); if(n%2==0) { for(int i=1;i<=n-1;i+=2) { System.out.print((i+1)+" "+i+" "); } System.out.println(); } else { int i=1; while(i<=n-3) { System.out.print((i+1)+" "+i+" "); i+=2; } System.out.println((i+2)+" "+(i)+" "+(i+1)+" "); System.out.println(); } } } static void dfs(int v, int p){ dp[0][v] = 0; dp[1][v]=0; for(int u: tree[v]) { if(u!=p) { dfs(u, v); dp[0][v] += Math.max(Math.abs(a[0][v] - a[1][u]) + dp[1][u], Math.abs(a[0][v] - a[0][u]) + dp[0][u]); dp[1][v] += Math.max(Math.abs(a[1][v] - a[1][u]) + dp[1][u], Math.abs(a[1][v] - a[0][u]) + dp[0][u]); } } } } class pair{ long x; long y; public pair(long x,long y) { this.x=x; this.y=y; } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
242f8d67a49a7a45664d893beae7ab69
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int i,j,k,l,m,n,t,p; t = Integer.parseInt(st.nextToken()); for(p=1;p<=t;p++) { st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); int b[] = new int[n]; for(i=0;i<n;i++) { if(i%2==0) { b[i] = i+2; } else b[i] = i; } if(n%2!=0) b[n-1] = n; if(n%2!=0) { l = b[n-1]; b[n-1] = b[n-2]; b[n-2] = l; } for (i = 0; i < b.length; i++) { System.out.print(b[i]+" "); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
eb54c816cdc9fd92f9fb5f93f7d74b76
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
// package com.company; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code here // BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t>0){ t--; int n = scan.nextInt(); ArrayList<Integer> arr = new ArrayList<Integer>(); int i = 1; while(i < n){ arr.add(i+1); arr.add(i); i+=2; } // System.out.println(arr); if(n % 2 == 1){ arr.add(n); int temp = arr.get(n-2); arr.set(n-2,n); arr.set(n-1,temp); } for(i = 0;i<arr.size();i++){ System.out.print(arr.get(i) +" "); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
531e6174450a6436a82d0927079ca15c
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.*; import java.lang.Math; public class Main{ static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter writer; static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } static long nextLong() throws IOException { return Long.parseLong(nextToken()); } static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } static String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static String nexts() throws IOException { tokenizer = new StringTokenizer(reader.readLine()); String s=""; while (tokenizer.hasMoreTokens()) { s+=tokenizer.nextElement()+" "; } return s; } public static int gcd(int x, int y){ if (y == 0) return x; else return gcd(y, x % y); } public static long gcd(long x, long y){ if (y == 0) return x; else return gcd(y, x % y); } static long digitSum(String x) { long a = 0; for (int i = 0; i < x.length(); i++) { a += x.charAt(i) - '0'; } return a; } static long digitSum(long x) { long a = 0; while (x > 0) { a += x % 10; x /= 10; } return a; } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int max(int x, int y, int z) { x = Math.max(x, y); x = Math.max(x, z); return x; } static int min(int x, int y, int z) { x = Math.min(x, y); x = Math.min(x, z); return x; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } static long max(long x, long y, long z) { x = Math.max(x, y); x = Math.max(x, z); return x; } static long min(long x, long y, long z) { x = Math.min(x, y); x = Math.min(x, z); return x; } static double max(double x, double y) { return Math.max(x, y); } static double min(double x, double y) { return Math.min(x, y); } static double max(double x, double y, double z) { x = Math.max(x, y); x = Math.max(x, z); return x; } static double min(double x, double y, double z) { x = Math.min(x, y); x = Math.min(x, z); return x; } static void rsort(int[] ar) { Arrays.sort(ar); int len = ar.length; for (int i = 0; i < len / 2; i++) { int tmp = ar[i]; ar[i] = ar[len - 1 - i]; ar[len - 1 - i] = tmp; } } static void rsort(long[] ar) { Arrays.sort(ar); int len = ar.length; for (int i = 0; i < len / 2; i++) { long tmp = ar[i]; ar[i] = ar[len - 1 - i]; ar[len - 1 - i] = tmp; } } static void rsort(double[] ar) { Arrays.sort(ar); int len = ar.length; for (int i = 0; i < len / 2; i++) { double tmp = ar[i]; ar[i] = ar[len - 1 - i]; ar[len - 1 - i] = tmp; } } static void rsort(char[] ar) { Arrays.sort(ar); int len = ar.length; for (int i = 0; i < len / 2; i++) { char tmp = ar[i]; ar[i] = ar[len - 1 - i]; ar[len - 1 - i] = tmp; } } static long lcm(long a, long b) throws IOException { return a * b / gcd(a, b); } static int lcm(int a, int b) throws IOException { return a * b / gcd(a, b); } static int rev(int x) { int a = 0; while (x > 0) { a = a * 10 + x % 10; x /= 10; } return a; } public static boolean isPrime(int nn) { if(nn<=1)return false; if(nn<=3){return true; } if (nn%2==0||nn%3==0){return false; } for (int i=5;i*i<=nn;i=i+6){ if(nn%i==0||nn%(i+2)==0){return false; }} return true; } public static void shuffle(int[] A) { for (int i = 0; i < A.length; i++) { int j = (int)(i * Math.random()); int tmp = A[i];A[i] = A[j];A[j] = tmp; } } public static void shufflel(Long[] A) { for (int i = 0; i < A.length; i++) { int j = (int)(i * Math.random()); long tmp = A[i];A[i] = A[j];A[j] = tmp; } } public static String reverse(String input) { StringBuilder input2=new StringBuilder(); input2.append(input); input2 = input2.reverse(); return input2.toString(); } public static long power(int x, long n) { // long mod = 1000000007; if (n == 0) { return 1; } long pow = power(x, n / 2); if ((n & 1) == 1) { return (x * pow * pow); } return (pow * pow); } public static long power(long x, long y, long p) //x^y%p { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) { res = (res * x) % p; } y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long ncr(int n, int k) { long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) { k = n - k; } // Calculate value of [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } public static int inv(int a, int m) //Returns modulo inverse of a with respect to m using extended euclid algorithm { int m0=m,t,q; int x0=0,x1=1; if(m==1){ return 0; } while(a>1) { q=a/m; t=m; m=a%m;a=t; t=x0; x0=x1-q*x0; x1=t; } if(x1<0){ x1+=m0; } return x1; } static int modInverse(int n, int p) { return (int)power((long)n, (long)(p - 2),(long)p); } static int ncrmod(int n, int r, int p) { if (r == 0) // Base case return 1; int[] fac = new int[n + 1]; // Fill factorial array so that we can find all factorial of r, n and n-r fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; //IMPORTANT } static int upper(int a[], int x) //Returns rightest index of x in sorted arr else -1 { //If not present returns index of just smaller element int n=a.length; int l=0; int r=n-1; int ans=-1; while(l<=r){ int m=(r-l)/2+l; if(a[m]<=x){ ans=m; l=m+1; } else{ r=m-1; } } return ans; } static int lower(int a[], int x) //Returns leftest index of x in sorted arr else n { //If not present returns index of just greater element int n=a.length; int l=0; int r=n-1; int ans=n; while(l<=r){ int m=(r-l)/2+l; if(a[m]>=x){ ans=m; r=m-1; } else{ l=m+1; } } return ans; } public static long stringtoint(String s){ // Convert bit string to number long a = 1; long ans = 0; StringBuilder sb = new StringBuilder(); sb.append(s); sb.reverse(); s=sb.toString(); for(int i=0;i<s.length();i++){ ans = ans + a*(s.charAt(i)-'0'); a = a*2; } return ans; } String inttostring(long a,long m){ // a is number and m is length of string required String res = ""; // Convert a number in bit string representation for(int i=0;i<(int)m;i++){ if((a&(1<<i))==1){ res+='1'; }else res+='0'; } StringBuilder sb = new StringBuilder(); sb.append(res); sb.reverse(); res=sb.toString(); return res; } static final int inf = Integer.MAX_VALUE / 2; static final long linf = Long.MAX_VALUE / 3; static final long mod = (long) 1e9 + 7; static class R implements Comparable<R>{ int x, y; public R(int x, int y) { //a[i]=new R(x,y); this.x = x; this.y = y; } public int compareTo(R o) { return x-o.x; //Increasing order(Which is usually required) } } //Check if palindrome string - System.out.print(A.equals(new StringBuilder(A).reverse().toString())?"Yes":"No"); public static void main(String[] args) throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } //public static boolean check() { // // } private static void solve() throws IOException { int t = nextInt(); while(t-->0){ //String s= nextToken(); //String[] s = str.split("\\s+"); ArrayList<Integer> ar=new ArrayList<Integer>(); //HashSet<Integer> set=new HashSet<Integer>(); //HashMap<Integer,String> h=new HashMap<Integer,String>(); //R[] a1=new R[n]; //char[] c=nextToken().toCharArray(); //PriorityQueue<Integer> pq=new PriorityQueue<Integer>(Collections.reverseOrder()); //NavigableSet<Integer> pq=new TreeSet<>(); int n = nextInt(); //long n = nextLong(); int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=i+1; } //long[] a=new long[n]; for(int i=1;i<n;i++){ int tt=arr[i]; arr[i]=arr[i-1]; arr[i-1]=tt; i++; } if(n%2!=0 && n>=2){ int tt=arr[n-1]; arr[n-1]=arr[n-2]; arr[n-2]=tt; } //shuffle(a); //shufflel(a);rsort(); //Arrays.sort(a); //StringBuilder ans=new StringBuilder(); for(int p:arr)writer.print(p+" "); writer.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
3341927d86bedce4d1ff9e1b6a272f43
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class contest728 { public static void main(String[] args) { FastScanner fs = new FastScanner(); int T = 1; T=fs.nextInt(); for (int tt = 0; tt < T; tt++) { int n= fs.nextInt(); StringBuilder sb= new StringBuilder(); // if(n==3){ // System.out.println(3+" "+1+" "+2); // continue; // } int end = n%2==0?n:n-3; for(int i=2;i<=end;i+=2){ sb.append(i+" "+(i-1)+" "); } if(end!=n){ sb.append(n+" "+(n-2)+" "+(n-1)); } System.out.println(sb.toString()); } } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
b98f593e04ddd5b151bdb3068c4867cb
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.io.*; public class A728 { public static void main(String[] args) { JS scan = new JS(); int t = scan.nextInt(); while(t-->0){ int n = scan.nextInt(); for(int i = 1;i<=n;i+=2){ if(n-i+1==3){ System.out.print((i+2)+" "+i+" "+(i+1)+" "); break; } System.out.print((i+1)+" "+(i+0)+" "); } System.out.println(); } } static class JS { public int BS = 1 << 16; public char NC = (char) 0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public long nextLong() { num = 1; boolean neg = false; if (c == NC) c = nextChar(); for (; (c < '0' || c > '9'); c = nextChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = nextChar()) { res = (res << 3) + (res << 1) + c - '0'; num *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / num; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c > 32) { res.append(c); c = nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c != '\n') { res.append(c); c = nextChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = nextChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
f374458414c42608b0977c853775ff3c
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
/* ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿ ⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿ ⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿ ⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿ ⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿ ⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺ ⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘ ⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆ ⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇ ⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇ ⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇ ⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇ ⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇ ⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀ ⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀ ⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸ ⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼ ⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿ ⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿ ⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿ ⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿ */ import java.io.*; import java.util.*; import java.util.StringTokenizer; public class Ex3 { public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int q = in.nextInt(); for (int t = 0; t < q; t++) { int x = in.nextInt(); int arr[] = new int[x]; for(int i = 0; i < x;i++){ arr[i] = i+1; } if (x % 2 == 0) { for(int i = 0; i < x-1; i+=2){ int temp=0; temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } for(int i = 0; i < x; i++){ System.out.print(arr[i]+" "); } }else { arr[0] = 3; arr[1] = 1; arr[2] = 2; for(int i = 3; i < x-1; i+=2){ int temp=0; temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } for(int i = 0; i < x; i++){ System.out.print(arr[i]+" "); } } out.flush(); } } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return a * b / gcd(a, b); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { init(); } public FastScanner(String name) { init(name); } public FastScanner(boolean isOnlineJudge) { if (!isOnlineJudge || System.getProperty("ONLINE_JUDGE") != null) { init(); } else { init("input.txt"); } } private void init() { br = new BufferedReader(new InputStreamReader(System.in)); } private void init(String name) { try { br = new BufferedReader(new FileReader(name)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
d783d8dbd9057e0f82097f40fd951e46
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
/* ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿ ⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿ ⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿ ⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿ ⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿ ⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺ ⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘ ⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆ ⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇ ⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇ ⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇ ⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇ ⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇ ⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀ ⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀ ⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸ ⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼ ⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿ ⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿ ⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿ ⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿ */ import java.io.*; import java.util.*; import java.util.StringTokenizer; public class Ex3 { public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int q = in.nextInt(); for (int t = 0; t < q; t++) { int x = in.nextInt(); int arr[] = new int[x]; for(int i = 0; i < x;i++){ arr[i] = i+1; } if (x % 2 == 0) { for(int i = 0; i < x-1; i+=2){ int temp=0; temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } for(int i = 0; i < x; i++){ System.out.print(arr[i]+" "); } }else { arr[0] = 3; arr[1] = 1; arr[2] = 2; for(int i = 3; i < x-1; i+=2){ int temp=0; temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } for(int i = 0; i < x; i++){ System.out.print(arr[i]+" "); } } out.flush(); } } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return a * b / gcd(a, b); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { init(); } public FastScanner(String name) { init(name); } public FastScanner(boolean isOnlineJudge) { if (!isOnlineJudge || System.getProperty("ONLINE_JUDGE") != null) { init(); } else { init("input.txt"); } } private void init() { br = new BufferedReader(new InputStreamReader(System.in)); } private void init(String name) { try { br = new BufferedReader(new FileReader(name)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
44b3cfc3155e406c20f85e7628bdffb9
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Random; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; public final class CFPS { static FastReader fr = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static final int gigamod = 1000000007; static final int mod = 998244353; static int t = 1; static double epsilon = 0.0000000001; static boolean[] isPrime; static int[] smallestFactorOf; static final int UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3; static long cmp; static int fastVar; static long zetamod = 999999999999989L; @SuppressWarnings({"unused"}) public static void main(String[] args) throws Exception { t = fr.nextInt(); OUTER: for (int tc = 0; tc < t; tc++) { int n = fr.nextInt(); int[] arr = new int[n]; for (int i = 1; i <= n; i++) { arr[i - 1] = i; } for (int i = 1; i < n; i += 2) { swap(arr, i, i - 1); } if (n % 2 == 1) { swap(arr, n - 1, n - 3); } out.println(toString(arr)); } out.close(); } static class Pair implements Comparable<Pair> { int first, second; int idx; Pair() { first = second = 0; } Pair (int ff, int ss) {first = ff; second = ss;} Pair (int ff, int ss, int ii) { first = ff; second = ss; idx = ii; } public int compareTo(Pair that) { cmp = first - that.first; if (cmp == 0) cmp = second - that.second; return (int) cmp; } } // (range add - segment min) segTree static int nn; static int[] arr; static int[] tree; static int[] lazy; static void build(int node, int leftt, int rightt) { if (leftt == rightt) { tree[node] = arr[leftt]; return; } int mid = (leftt + rightt) >> 1; build(node << 1, leftt, mid); build(node << 1 | 1, mid + 1, rightt); tree[node] = Math.min(tree[node << 1], tree[node << 1 | 1]); } static void segAdd(int node, int leftt, int rightt, int segL, int segR, int val) { if (lazy[node] != 0) { tree[node] += lazy[node]; if (leftt != rightt) { lazy[node << 1] += lazy[node]; lazy[node << 1 | 1] += lazy[node]; } lazy[node] = 0; } if (segL > rightt || segR < leftt) return; if (segL <= leftt && rightt <= segR) { tree[node] += val; if (leftt != rightt) { lazy[node << 1] += val; lazy[node << 1 | 1] += val; } lazy[node] = 0; return; } int mid = (leftt + rightt) >> 1; segAdd(node << 1, leftt, mid, segL, segR, val); segAdd(node << 1 | 1, mid + 1, rightt, segL, segR, val); tree[node] = Math.min(tree[node << 1], tree[node << 1 | 1]); } static int minQuery(int node, int leftt, int rightt, int segL, int segR) { if (lazy[node] != 0) { tree[node] += lazy[node]; if (leftt != rightt) { lazy[node << 1] += lazy[node]; lazy[node << 1 | 1] += lazy[node]; } lazy[node] = 0; } if (segL > rightt || segR < leftt) return Integer.MAX_VALUE / 10; if (segL <= leftt && rightt <= segR) return tree[node]; int mid = (leftt + rightt) >> 1; return Math.min(minQuery(node << 1, leftt, mid, segL, segR), minQuery(node << 1 | 1, mid + 1, rightt, segL, segR)); } static class Segment { int li, ri, wi, id; Segment(int ll, int rr, int ww, int ii) { li = ll; ri = rr; wi = ww; id = ii; } } static void compute_automaton(String s, int[][] aut) { s += '#'; int n = s.length(); int[] pi = prefix_function(s.toCharArray()); for (int i = 0; i < n; i++) { for (int c = 0; c < 26; c++) { int j = i; while (j > 0 && 'A' + c != s.charAt(j)) j = pi[j-1]; if ('A' + c == s.charAt(j)) j++; aut[i][c] = j; } } } static void timeDFS(int current, int from, UGraph ug, int[] time, int[] tIn, int[] tOut) { tIn[current] = ++time[0]; for (int adj : ug.adj(current)) if (adj != from) timeDFS(adj, current, ug, time, tIn, tOut); tOut[current] = ++time[0]; } static boolean areCollinear(long x1, long y1, long x2, long y2, long x3, long y3) { // we will check if c3 lies on line through (c1, c2) long a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); return a == 0; } static int[] treeDiameter(UGraph ug) { int n = ug.V(); int farthest = -1; int[] distTo = new int[n]; diamDFS(0, -1, 0, ug, distTo); int maxDist = -1; for (int i = 0; i < n; i++) if (maxDist < distTo[i]) { maxDist = distTo[i]; farthest = i; } distTo = new int[n + 1]; diamDFS(farthest, -1, 0, ug, distTo); distTo[n] = farthest; return distTo; } static void diamDFS(int current, int from, int dist, UGraph ug, int[] distTo) { distTo[current] = dist; for (int adj : ug.adj(current)) if (adj != from) diamDFS(adj, current, dist + 1, ug, distTo); } static class TreeDistFinder { UGraph ug; int n; int[] depthOf; LCA lca; TreeDistFinder(UGraph ug) { this.ug = ug; n = ug.V(); depthOf = new int[n]; depthCalc(0, -1, ug, 0, depthOf); lca = new LCA(ug, 0); } TreeDistFinder(UGraph ug, int a) { this.ug = ug; n = ug.V(); depthOf = new int[n]; depthCalc(a, -1, ug, 0, depthOf); lca = new LCA(ug, a); } private void depthCalc(int current, int from, UGraph ug, int depth, int[] depthOf) { depthOf[current] = depth; for (int adj : ug.adj(current)) if (adj != from) depthCalc(adj, current, ug, depth + 1, depthOf); } public int dist(int a, int b) { int lc = lca.lca(a, b); return (depthOf[a] - depthOf[lc] + depthOf[b] - depthOf[lc]); } } public static long[][] GCDSparseTable(long[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); long[][] ret = new long[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; } else { ret[i] = new long[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = gcd(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } public static long sparseRangeGCDQ(long[][] table, int l, int r) { // [a,b) if(l > r)return 1; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(r-l); return gcd(table[t][l], table[t][r-(1<<t)]); } static class Trie { TrieNode root; Trie(char[][] strings) { root = new TrieNode('A', false); construct(root, strings); } public Stack<String> set(TrieNode root) { Stack<String> set = new Stack<>(); StringBuilder sb = new StringBuilder(); for (TrieNode next : root.next) collect(sb, next, set); return set; } private void collect(StringBuilder sb, TrieNode node, Stack<String> set) { if (node == null) return; sb.append(node.character); if (node.isTerminal) set.add(sb.toString()); for (TrieNode next : node.next) collect(sb, next, set); if (sb.length() > 0) sb.setLength(sb.length() - 1); } private void construct(TrieNode root, char[][] strings) { // we have to construct the Trie for (char[] string : strings) { if (string.length == 0) continue; root.next[string[0] - 'a'] = put(root.next[string[0] - 'a'], string, 0); if (root.next[string[0] - 'a'] != null) root.isLeaf = false; } } private TrieNode put(TrieNode node, char[] string, int idx) { boolean isTerminal = (idx == string.length - 1); if (node == null) node = new TrieNode(string[idx], isTerminal); node.character = string[idx]; node.isTerminal |= isTerminal; if (!isTerminal) { node.isLeaf = false; node.next[string[idx + 1] - 'a'] = put(node.next[string[idx + 1] - 'a'], string, idx + 1); } return node; } class TrieNode { char character; TrieNode[] next; boolean isTerminal, isLeaf; boolean canWin, canLose; TrieNode(char c, boolean isTerminallll) { character = c; isTerminal = isTerminallll; next = new TrieNode[26]; isLeaf = true; } } } static class Edge implements Comparable<Edge> { int from, to; long weight, ans; int id; // int hash; Edge(int fro, int t, long wt, int i) { from = fro; to = t; id = i; weight = wt; // hash = Objects.hash(from, to, weight); } /*public int hashCode() { return hash; }*/ public int compareTo(Edge that) { return Long.compare(this.id, that.id); } } public static long[][] minSparseTable(long[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); long[][] ret = new long[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; }else { ret[i] = new long[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = Math.min(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } public static long sparseRangeMinQ(long[][] table, int l, int r) { // [a,b) if(l >= r)return Integer.MAX_VALUE; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(r-l); return Math.min(table[t][l], table[t][r-(1<<t)]); } public static long[][] maxSparseTable(long[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); long[][] ret = new long[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; }else { ret[i] = new long[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = Math.max(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } public static long sparseRangeMaxQ(long[][] table, int l, int r) { // [a,b) if(l >= r)return Integer.MIN_VALUE; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(r-l); return Math.max(table[t][l], table[t][r-(1<<t)]); } static class LCA { int[] height, first, segtree; ArrayList<Integer> euler; boolean[] visited; int n; LCA(UGraph ug, int root) { n = ug.V(); height = new int[n]; first = new int[n]; euler = new ArrayList<>(); visited = new boolean[n]; dfs(ug, root, 0); int m = euler.size(); segtree = new int[m * 4]; build(1, 0, m - 1); } void dfs(UGraph ug, int node, int h) { visited[node] = true; height[node] = h; first[node] = euler.size(); euler.add(node); for (int adj : ug.adj(node)) { if (!visited[adj]) { dfs(ug, adj, h + 1); euler.add(node); } } } void build(int node, int b, int e) { if (b == e) { segtree[node] = euler.get(b); } else { int mid = (b + e) / 2; build(node << 1, b, mid); build(node << 1 | 1, mid + 1, e); int l = segtree[node << 1], r = segtree[node << 1 | 1]; segtree[node] = (height[l] < height[r]) ? l : r; } } int query(int node, int b, int e, int L, int R) { if (b > R || e < L) return -1; if (b >= L && e <= R) return segtree[node]; int mid = (b + e) >> 1; int left = query(node << 1, b, mid, L, R); int right = query(node << 1 | 1, mid + 1, e, L, R); if (left == -1) return right; if (right == -1) return left; return height[left] < height[right] ? left : right; } int lca(int u, int v) { int left = first[u], right = first[v]; if (left > right) { int temp = left; left = right; right = temp; } return query(1, 0, euler.size() - 1, left, right); } } static class FenwickTree { long[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates public FenwickTree(int size) { array = new long[size + 1]; } public long rsq(int ind) { assert ind > 0; long 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 long rsq(int a, int b) { assert b >= a && a > 0 && b > 0; return rsq(b) - rsq(a - 1); } public void update(int ind, long 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; } } static class Point implements Comparable<Point> { long x; long y; long z; long id; // private int hashCode; Point() { x = z = y = 0; // this.hashCode = Objects.hash(x, y, cost); } Point(Point p) { this.x = p.x; this.y = p.y; this.z = p.z; this.id = p.id; // this.hashCode = Objects.hash(x, y, cost); } Point(long x, long y, long z, long id) { this.x = x; this.y = y; this.z = z; this.id = id; // this.hashCode = Objects.hash(x, y, id); } Point(long a, long b) { this.x = a; this.y = b; this.z = 0; // this.hashCode = Objects.hash(a, b); } Point(long x, long y, long id) { this.x = x; this.y = y; this.id = id; } @Override public int compareTo(Point o) { if (this.x < o.x) return -1; if (this.x > o.x) return 1; if (this.y < o.y) return -1; if (this.y > o.y) return 1; if (this.z < o.z) return -1; if (this.z > o.z) return 1; return 0; } @Override public boolean equals(Object that) { return this.compareTo((Point) that) == 0; } } static class BinaryLift { // FUNCTIONS: k-th ancestor and LCA in log(n) int[] parentOf; int maxJmpPow; int[][] binAncestorOf; int n; int[] lvlOf; // How this works? // a. For every node, we store the b-ancestor for b in {1, 2, 4, 8, .. log(n)}. // b. When we need k-ancestor, we represent 'k' in binary and for each set bit, we // lift level in the tree. public BinaryLift(UGraph tree) { n = tree.V(); maxJmpPow = logk(n, 2) + 1; parentOf = new int[n]; binAncestorOf = new int[n][maxJmpPow]; lvlOf = new int[n]; for (int i = 0; i < n; i++) Arrays.fill(binAncestorOf[i], -1); parentConstruct(0, -1, tree, 0); binConstruct(); } // TODO: Implement lvlOf[] initialization public BinaryLift(int[] parentOf) { this.parentOf = parentOf; n = parentOf.length; maxJmpPow = logk(n, 2) + 1; binAncestorOf = new int[n][maxJmpPow]; lvlOf = new int[n]; for (int i = 0; i < n; i++) Arrays.fill(binAncestorOf[i], -1); UGraph tree = new UGraph(n); for (int i = 1; i < n; i++) tree.addEdge(i, parentOf[i]); binConstruct(); parentConstruct(0, -1, tree, 0); } private void parentConstruct(int current, int from, UGraph tree, int depth) { parentOf[current] = from; lvlOf[current] = depth; for (int adj : tree.adj(current)) if (adj != from) parentConstruct(adj, current, tree, depth + 1); } private void binConstruct() { for (int node = 0; node < n; node++) for (int lvl = 0; lvl < maxJmpPow; lvl++) binConstruct(node, lvl); } private int binConstruct(int node, int lvl) { if (node < 0) return -1; if (lvl == 0) return binAncestorOf[node][lvl] = parentOf[node]; if (node == 0) return binAncestorOf[node][lvl] = -1; if (binAncestorOf[node][lvl] != -1) return binAncestorOf[node][lvl]; return binAncestorOf[node][lvl] = binConstruct(binConstruct(node, lvl - 1), lvl - 1); } // return ancestor which is 'k' levels above this one public int ancestor(int node, int k) { if (node < 0) return -1; if (node == 0) if (k == 0) return node; else return -1; if (k > (1 << maxJmpPow) - 1) return -1; if (k == 0) return node; int ancestor = node; int highestBit = Integer.highestOneBit(k); while (k > 0 && ancestor != -1) { ancestor = binAncestorOf[ancestor][logk(highestBit, 2)]; k -= highestBit; highestBit = Integer.highestOneBit(k); } return ancestor; } public int lca(int u, int v) { if (u == v) return u; // The invariant will be that 'u' is below 'v' initially. if (lvlOf[u] < lvlOf[v]) { int temp = u; u = v; v = temp; } // Equalizing the levels. u = ancestor(u, lvlOf[u] - lvlOf[v]); if (u == v) return u; // We will now raise level by largest fitting power of two until possible. for (int power = maxJmpPow - 1; power > -1; power--) if (binAncestorOf[u][power] != binAncestorOf[v][power]) { u = binAncestorOf[u][power]; v = binAncestorOf[v][power]; } return ancestor(u, 1); } } static class DFSTree { // NOTE: The thing is made keeping in mind that the whole // input graph is connected. UGraph tree; UGraph backUG; int hasBridge; int n; Edge backEdge; DFSTree(UGraph ug) { this.n = ug.V(); tree = new UGraph(n); hasBridge = -1; backUG = new UGraph(n); treeCalc(0, -1, new boolean[n], ug); } private void treeCalc(int current, int from, boolean[] marked, UGraph ug) { if (marked[current]) { // This is a backEdge. backUG.addEdge(from, current); backEdge = new Edge(from, current, 1, 0); return; } if (from != -1) tree.addEdge(from, current); marked[current] = true; for (int adj : ug.adj(current)) if (adj != from) treeCalc(adj, current, marked, ug); } public boolean hasBridge() { if (hasBridge != -1) return (hasBridge == 1); // We have to determine the bridge. bridgeFinder(); return (hasBridge == 1); } int[] levelOf; int[] dp; private void bridgeFinder() { // Finding the level of each node. levelOf = new int[n]; levelDFS(0, -1, 0); // Applying DP solution. // dp[i] -> Highest level reachable from subtree of 'i' using // some backEdge. dp = new int[n]; Arrays.fill(dp, Integer.MAX_VALUE / 100); dpDFS(0, -1); // Now, we will check each edge and determine whether its a // bridge. for (int i = 0; i < n; i++) for (int adj : tree.adj(i)) { // (i -> adj) is the edge. if (dp[adj] > levelOf[i]) hasBridge = 1; } if (hasBridge != 1) hasBridge = 0; } private void levelDFS(int current, int from, int lvl) { levelOf[current] = lvl; for (int adj : tree.adj(current)) if (adj != from) levelDFS(adj, current, lvl + 1); } private int dpDFS(int current, int from) { dp[current] = levelOf[current]; for (int back : backUG.adj(current)) dp[current] = Math.min(dp[current], levelOf[back]); for (int adj : tree.adj(current)) if (adj != from) dp[current] = Math.min(dp[current], dpDFS(adj, current)); return dp[current]; } } static class UnionFind { // Uses weighted quick-union with path compression. private int[] parent; // parent[i] = parent of i private int[] size; // size[i] = number of sites in tree rooted at i // Note: not necessarily correct if i is not a root node private int count; // number of components public UnionFind(int n) { count = n; parent = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } // Number of connected components. public int count() { return count; } // Find the root of p. public int find(int p) { while (p != parent[p]) p = parent[p]; return p; } public boolean connected(int p, int q) { return find(p) == find(q); } public int numConnectedTo(int node) { return size[find(node)]; } // Weighted union. public void union(int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return; // make smaller root point to larger one if (size[rootP] < size[rootQ]) { parent[rootP] = rootQ; size[rootQ] += size[rootP]; } else { parent[rootQ] = rootP; size[rootP] += size[rootQ]; } count--; } public static int[] connectedComponents(UnionFind uf) { // We can do this in nlogn. int n = uf.size.length; int[] compoColors = new int[n]; for (int i = 0; i < n; i++) compoColors[i] = uf.find(i); HashMap<Integer, Integer> oldToNew = new HashMap<>(); int newCtr = 0; for (int i = 0; i < n; i++) { int thisOldColor = compoColors[i]; Integer thisNewColor = oldToNew.get(thisOldColor); if (thisNewColor == null) thisNewColor = newCtr++; oldToNew.put(thisOldColor, thisNewColor); compoColors[i] = thisNewColor; } return compoColors; } } static class UGraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; @SuppressWarnings("unchecked") public UGraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); adj[to].add(from); } public HashSet<Integer> adj(int from) { return adj[from]; } public int degree(int v) { return adj[v].size(); } public int V() { return adj.length; } public int E() { return E; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static void dfsMark(int current, boolean[] marked, UGraph g) { if (marked[current]) return; marked[current] = true; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, marked, g); } public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) { if (marked[current]) return; marked[current] = true; if (from != -1) distTo[current] = distTo[from] + 1; HashSet<Integer> adj = g.adj(current); int alreadyMarkedCtr = 0; for (int adjc : adj) { if (marked[adjc]) alreadyMarkedCtr++; dfsMark(adjc, current, distTo, marked, g, endPoints); } if (alreadyMarkedCtr == adj.size()) endPoints.add(current); } public static void bfsOrder(int current, UGraph g) { } public static void dfsMark(int current, int[] colorIds, int color, UGraph g) { if (colorIds[current] != -1) return; colorIds[current] = color; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, colorIds, color, g); } public static int[] connectedComponents(UGraph g) { int n = g.V(); int[] componentId = new int[n]; Arrays.fill(componentId, -1); int colorCtr = 0; for (int i = 0; i < n; i++) { if (componentId[i] != -1) continue; dfsMark(i, componentId, colorCtr, g); colorCtr++; } return componentId; } public static boolean hasCycle(UGraph ug) { int n = ug.V(); boolean[] marked = new boolean[n]; boolean[] hasCycleFirst = new boolean[1]; for (int i = 0; i < n; i++) { if (marked[i]) continue; hcDfsMark(i, ug, marked, hasCycleFirst, -1); } return hasCycleFirst[0]; } // Helper for hasCycle. private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) { if (marked[current]) return; if (hasCycleFirst[0]) return; marked[current] = true; HashSet<Integer> adjc = ug.adj(current); for (int adj : adjc) { if (marked[adj] && adj != parent && parent != -1) { hasCycleFirst[0] = true; return; } hcDfsMark(adj, ug, marked, hasCycleFirst, current); } } } static class Digraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; @SuppressWarnings("unchecked") public Digraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); } public HashSet<Integer> adj(int from) { return adj[from]; } public int V() { return adj.length; } public int E() { return E; } public Digraph reversed() { Digraph dg = new Digraph(V()); for (int i = 0; i < V(); i++) for (int adjVert : adj(i)) dg.addEdge(adjVert, i); return dg; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static int[] KosarajuSharirSCC(Digraph dg) { int[] id = new int[dg.V()]; Digraph reversed = dg.reversed(); // Gotta perform topological sort on this one to get the stack. Stack<Integer> revStack = Digraph.topologicalSort(reversed); // Initializing id and idCtr. id = new int[dg.V()]; int idCtr = -1; // Creating a 'marked' array. boolean[] marked = new boolean[dg.V()]; while (!revStack.isEmpty()) { int vertex = revStack.pop(); if (!marked[vertex]) sccDFS(dg, vertex, marked, ++idCtr, id); } return id; } private static void sccDFS(Digraph dg, int source, boolean[] marked, int idCtr, int[] id) { marked[source] = true; id[source] = idCtr; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) sccDFS(dg, adjVertex, marked, idCtr, id); } public static Stack<Integer> topologicalSort(Digraph dg) { // dg has to be a directed acyclic graph. // We'll have to run dfs on the digraph and push the deepest nodes on stack first. // We'll need a Stack<Integer> and a int[] marked. Stack<Integer> topologicalStack = new Stack<Integer>(); boolean[] marked = new boolean[dg.V()]; // Calling dfs for (int i = 0; i < dg.V(); i++) if (!marked[i]) runDfs(dg, topologicalStack, marked, i); return topologicalStack; } static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) { marked[source] = true; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) runDfs(dg, topologicalStack, marked, adjVertex); topologicalStack.add(source); } } static class FastReader { private BufferedReader bfr; private StringTokenizer st; public FastReader() { bfr = new BufferedReader(new InputStreamReader(System.in)); } String next() { if (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bfr.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return next().toCharArray()[0]; } String nextString() { return next(); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } double[] nextDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextDouble(); return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } int[][] nextIntGrid(int n, int m) { int[][] grid = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) grid[i][j] = fr.nextInt(); } return grid; } } @SuppressWarnings("serial") static class CountMap<T> extends TreeMap<T, Integer>{ CountMap() { } CountMap(Comparator<T> cmp) { } CountMap(T[] arr) { this.putCM(arr); } public Integer putCM(T key) { return super.put(key, super.getOrDefault(key, 0) + 1); } public Integer removeCM(T key) { int count = super.getOrDefault(key, -1); if (count == -1) return -1; if (count == 1) return super.remove(key); else return super.put(key, count - 1); } public Integer getCM(T key) { return super.getOrDefault(key, 0); } public void putCM(T[] arr) { for (T l : arr) this.putCM(l); } } static long dioGCD(long a, long b, long[] x0, long[] y0) { if (b == 0) { x0[0] = 1; y0[0] = 0; return a; } long[] x1 = new long[1], y1 = new long[1]; long d = dioGCD(b, a % b, x1, y1); x0[0] = y1[0]; y0[0] = x1[0] - y1[0] * (a / b); return d; } static boolean diophantine(long a, long b, long c, long[] x0, long[] y0, long[] g) { g[0] = dioGCD(Math.abs(a), Math.abs(b), x0, y0); if (c % g[0] > 0) { return false; } x0[0] *= c / g[0]; y0[0] *= c / g[0]; if (a < 0) x0[0] = -x0[0]; if (b < 0) y0[0] = -y0[0]; return true; } static long[][] prod(long[][] mat1, long[][] mat2) { int n = mat1.length; long[][] prod = new long[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) // determining prod[i][j] // it will be the dot product of mat1[i][] and mat2[][i] for (int k = 0; k < n; k++) prod[i][j] += mat1[i][k] * mat2[k][j]; return prod; } static long[][] matExpo(long[][] mat, long power) { int n = mat.length; long[][] ans = new long[n][n]; if (power == 0) return null; if (power == 1) return mat; long[][] half = matExpo(mat, power / 2); ans = prod(half, half); if (power % 2 == 1) { ans = prod(ans, mat); } return ans; } static int KMPNumOcc(char[] text, char[] pat) { int n = text.length; int m = pat.length; char[] patPlusText = new char[n + m + 1]; for (int i = 0; i < m; i++) patPlusText[i] = pat[i]; patPlusText[m] = '^'; // Seperator for (int i = 0; i < n; i++) patPlusText[m + i] = text[i]; int[] fullPi = piCalcKMP(patPlusText); int answer = 0; for (int i = 0; i < n + m + 1; i++) if (fullPi[i] == m) answer++; return answer; } static int[] piCalcKMP(char[] s) { int n = s.length; int[] pi = new int[n]; for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]) j++; pi[i] = j; } return pi; } static boolean[] prefMatchesSuff(char[] s) { int n = s.length; boolean[] res = new boolean[n + 1]; int[] pi = prefix_function(s); res[0] = true; for (int p = n; p != 0; p = pi[p]) res[p] = true; return res; } static int[] prefix_function(char[] s) { int n = s.length; int[] pi = new int[n]; for (int i = 1; i < n; i++) { int j = pi[i-1]; while (j > 0 && s[i] != s[j]) j = pi[j-1]; if (s[i] == s[j]) j++; pi[i] = j; } return pi; } static long hash(long key) { long h = Long.hashCode(key); h ^= (h >>> 20) ^ (h >>> 12) ^ (h >>> 7) ^ (h >>> 4); return h & (gigamod-1); } static int mapTo1D(int row, int col, int n, int m) { // Maps elements in a 2D matrix serially to elements in // a 1D array. return row * m + col; } static int[] mapTo2D(int idx, int n, int m) { // Inverse of what the one above does. int[] rnc = new int[2]; rnc[0] = idx / m; rnc[1] = idx % m; return rnc; } static boolean[] primeGenerator(int upto) { // Sieve of Eratosthenes: isPrime = new boolean[upto + 1]; smallestFactorOf = new int[upto + 1]; Arrays.fill(smallestFactorOf, 1); Arrays.fill(isPrime, true); isPrime[1] = isPrime[0] = false; for (long i = 2; i < upto + 1; i++) if (isPrime[(int) i]) { smallestFactorOf[(int) i] = (int) i; // Mark all the multiples greater than or equal // to the square of i to be false. for (long j = i; j * i < upto + 1; j++) { if (isPrime[(int) j * (int) i]) { isPrime[(int) j * (int) i] = false; smallestFactorOf[(int) j * (int) i] = (int) i; } } } return isPrime; } static HashMap<Integer, Integer> smolNumPrimeFactorization(int num) { if (smallestFactorOf == null) primeGenerator(num + 1); HashMap<Integer, Integer> fnps = new HashMap<>(); while (num != 1) { fnps.put(smallestFactorOf[num], fnps.getOrDefault(smallestFactorOf[num], 0) + 1); num /= smallestFactorOf[num]; } return fnps; } static HashMap<Long, Integer> primeFactorization(long num) { // Returns map of factor and its power in the number. HashMap<Long, Integer> map = new HashMap<>(); while (num % 2 == 0) { num /= 2; Integer pwrCnt = map.get(2L); map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1); } for (long i = 3; i * i <= num; i += 2) { while (num % i == 0) { num /= i; Integer pwrCnt = map.get(i); map.put(i, pwrCnt != null ? pwrCnt + 1 : 1); } } // If the number is prime, we have to add it to the // map. if (num != 1) map.put(num, 1); return map; } static HashSet<Long> divisors(long num) { HashSet<Long> divisors = new HashSet<Long>(); divisors.add(1L); divisors.add(num); for (long i = 2; i * i <= num; i++) { if (num % i == 0) { divisors.add(num/i); divisors.add(i); } } return divisors; } static void coprimeGenerator(int m, int n, ArrayList<Point> coprimes, int limit, int numCoprimes) { if (m > limit) return; if (m <= limit && n <= limit) coprimes.add(new Point(m, n)); if (coprimes.size() > numCoprimes) return; coprimeGenerator(2 * m - n, m, coprimes, limit, numCoprimes); coprimeGenerator(2 * m + n, m, coprimes, limit, numCoprimes); coprimeGenerator(m + 2 * n, n, coprimes, limit, numCoprimes); } static long nCr(long n, long r, long[] fac) { long p = gigamod; if (r == 0) return 1; return (fac[(int)n] * modInverse(fac[(int)r], p) % p * modInverse(fac[(int)n - (int)r], p) % p) % p; } static long modInverse(long n, long p) { return power(n, p - 2, p); } static long modDiv(long a, long b){return mod(a * power(b, mod - 2, mod), mod);} static long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static int logk(long n, long k) { return (int)(Math.log(n) / Math.log(k)); } static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static long gcd(long[] arr) { int n = arr.length; long gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static int gcd(int[] arr) { int n = arr.length; int gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long lcm(long[] arr) { long lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(long a, long b) { return (a * b)/gcd(a, b); } static boolean less(int a, int b) { return a < b ? true : false; } static boolean isSorted(int[] a) { for (int i = 1; i < a.length; i++) { if (less(a[i], a[i - 1])) return false; } return true; } static boolean isSorted(long[] a) { for (int i = 1; i < a.length; i++) { if (a[i] < a[i - 1]) return false; } return true; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(double[] a, int i, int j) { double temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(char[] a, int i, int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; } static void sort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void reverseSort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverse(char[] arr) { int n = arr.length; for (int i = 0; i < n / 2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void shuffleArray(long[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(int[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(double[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { double tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(char[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { char tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static boolean isPrime(long n) {if (n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(long i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;} static String toString(int[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();} static String toString(boolean[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();} static String toString(long[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();} static String toString(char[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+"");return sb.toString();} static String toString(int[][] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++){for(int j=0;j<dp[i].length;j++){sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();} static String toString(long[][] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++){for(int j=0;j<dp[i].length;j++) {sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();} static String toString(double[][] dp){StringBuilder sb=new StringBuilder();for(int i = 0;i<dp.length;i++){for(int j = 0;j<dp[i].length;j++){sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();} static String toString(char[][] dp){StringBuilder sb = new StringBuilder();for(int i = 0;i<dp.length;i++){for(int j = 0;j<dp[i].length;j++){sb.append(dp[i][j]+"");}sb.append('\n');}return sb.toString();} static long mod(long a, long m){return(a%m+1000000L*m)%m;} } /* * * int[] arr = new int[] {1810, 1700, 1710, 2320, 2000, 1785, 1780 , 2130, 2185, 1430, 1460, 1740, 1860, 1100, 1905, 1650}; int n = arr.length; sort(arr); int bel1700 = 0, bet1700n1900 = 0, abv1900 = 0; for (int i = 0; i < n; i++) if (arr[i] < 1700) bel1700++; else if (1700 <= arr[i] && arr[i] < 1900) bet1700n1900++; else if (arr[i] >= 1900) abv1900++; out.println("COUNT: " + n); out.println("PERFS: " + toString(arr)); out.println("MEDIAN: " + arr[n / 2]); out.println("AVERAGE: " + Arrays.stream(arr).average().getAsDouble()); out.println("[0, 1700): " + bel1700 + "/" + n); out.println("[1700, 1900): " + bet1700n1900 + "/" + n); out.println("[1900, 2400): " + abv1900 + "/" + n); * * */ // NOTES: // ASCII VALUE OF 'A': 65 // ASCII VALUE OF 'a': 97 // Range of long: 9 * 10^18 // ASCII VALUE OF '0': 48 // Primes upto 'n' can be given by (n / (logn)).
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
ef3e52f34457ed4a0e4bfdf5f7ea6fc6
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.lang.reflect.Array; import java.util.*; public class ques { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t= sc.nextInt(); for (int i=0; i<t; i++){ int n= sc.nextInt(); int[] arr= new int[n]; for (int j=1;j<=n;j++ ){ arr[j-1]= j; } if (n%2==0){ for (int j=0; j<n; j+=2){ int temp = arr[j]; arr[j]= arr[j+1]; arr[j+1]=temp; }} else { if (n/2 %2!=0){ for (int j=0; j<(n/2) ; j+=2){ int temp = arr[j]; arr[j]= arr[j+1]; arr[j+1]=temp; } for (int j=(n/2)+2; j<n-1; j+=2){ int temp = arr[j]; arr[j]= arr[j+1]; arr[j+1]=temp; } int temp = arr[n/2 +1]; arr[n/2 +1] = arr[n/2]; arr[n/2]= temp; }else { for (int j=0; j<n/2 ; j+=2){ int temp = arr[j]; arr[j]= arr[j+1]; arr[j+1]=temp; } for (int j=(n/2)+1; j<n-1; j+=2){ int temp = arr[j]; arr[j]= arr[j+1]; arr[j+1]=temp; } int temp = arr[n/2 -1]; arr[n/2 -1] = arr[n/2]; arr[n/2]= temp; }} for (int k:arr){ System.out.print(k+" "); } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
4216f7e52b3b1fe8c80c549172dfe44d
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int n; int[] solution; int temp; while (t > 0) { n = sc.nextInt(); solution = new int[n]; for (int i = 0 ; i < n ; i++) { solution[i] = i + 1; } temp = 0; for (int i = 0 ; i < n - 1 ; i += 2) { temp = solution[i]; solution[i] = solution[i + 1]; solution[i + 1] = temp; } if (n % 2 == 1) { temp = solution[n - 1]; solution[n - 1] = solution[n - 2]; solution[n - 2] = temp; } for (int i = 0 ; i < n ; i++) { System.out.print(solution[i] + " "); } System.out.println(); t -= 1; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
d01c2d0429058d38bca5ba8e35f5d5f2
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class Codefoecesp1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i =0 ; i<n;i++) { int a = sc.nextInt(); if(a%2 ==0 ){ for(int k =1 ; k<=a ;k=k+2){ System.out.print((k+1)+" "+(k)+" "); } } else{ System.out.print(3+" "+ 1 + " "+ 2+" "); if(a>3){ for(int k = 4 ; k<=a ;k=k+2){ System.out.print((k+1)+" "+(k)+" "); } } } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
4dc49d2a656c6f19792d71b14ccb7609
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.*; public class Rough { public static void main(String[] args) throws Exception { Scanner s = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tc = s.nextInt(); for (int t = 1; t <= tc; t++) { int n=s.nextInt(); if(n%2==1) { pw.print(3+" "+1+" "+2+" "); for(int i=4;i<=n;i+=2)pw.print(i+1+" "+i+" "); } else { for(int i=1;i<=n;i+=2) { pw.print(i+1+" "+i+" "); } } pw.println(); } pw.close(); } static final Random ran = new Random(); static void sort(int[] ar) { int n = ar.length; for (int i = 0; i < n; i++) { int doer = ran.nextInt(n), temp = ar[doer]; ar[doer] = ar[i]; ar[i] = temp; } Arrays.sort(ar); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
91a90576c62f2eb67b5256157ff1ae67
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { long max = (long) 1e5+10; long[] factorials = new long[(int) max]; long[] primes = new long[(int) max]; long[] fibonacci = new long[(int) max]; Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); scanner.nextLine(); while(t-- > 0){ int n = scanner.nextInt(); ArrayList<Integer> arr = new ArrayList<>(); for(int i = 1; i<=n; i++){ arr.add(i); } if(n%2 == 0){ for(int i = 1; i<arr.size(); i+=2){ int temp = arr.get(i); arr.set(i, arr.get(i-1)); arr.set(i-1, temp); } }else{ for(int i = 1; i<arr.size(); i+=2){ int temp = arr.get(i); arr.set(i, arr.get(i-1)); arr.set(i-1, temp); } int temp = arr.get(arr.size()-1); arr.set(arr.size()-1, arr.get(arr.size()-2)); arr.set(arr.size()-2, temp); } for(int i = 0; i<arr.size(); i++){ System.out.print(arr.get(i) + " "); } System.out.println(); } } public static boolean isPalindrome(long n) { long reversed = 0; long original = n; while (n > 0) { reversed = reversed * 10 + n % 10; n /= 10; } return reversed == original; } public static boolean isPalindrome(String s) { int i = 0, j = s.length() - 1; while (i < j) { if (s.charAt(i) != s.charAt(j)) { return false; } i++; j--; } return true; } public static boolean isPalindrome(int n) { int reversed = 0; int original = n; while (n > 0) { reversed = reversed * 10 + n % 10; n /= 10; } return reversed == original; } public static Map<Integer, Integer> SortByvalueMap(Map<Integer, Integer> map) { Map<Integer, Integer> result = new LinkedHashMap<>(); map.entrySet() .stream() .sorted(Map.Entry.comparingByValue()) .forEachOrdered(x -> result.put(x.getKey(), x.getValue())); return result; } public static Map<Integer, Integer> SortByKeyMap(Map<Integer, Integer> map) { Map<Integer, Integer> result = new LinkedHashMap<>(); map.entrySet() .stream() .sorted(Map.Entry.comparingByKey()) .forEachOrdered(x -> result.put(x.getKey(), x.getValue())); return result; } public static long[] countFibonacciTilln(int n, long[] fibonacci) { fibonacci[0] = 0; fibonacci[1] = 1; for(int i = 2; i<=n; i++){ fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]; } return fibonacci; } public static long[] getPrimes(long n, long[] primes) { int i = 0; for (i = 2; i <= n; i++) { if (isPrime(i)) { primes[i] = i; } } return primes; } public static long[] countFactorials(int n, long[] factorials) { factorials[0] = 1; for (int i = 1; i <= n; i++) { factorials[i] = factorials[i - 1] * i; } return factorials; } public static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } public static boolean isPrime(int n) { if (n <= 1) { return false; } for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } public static int pow(int a, int b) { int result = 1; while (b > 0) { if ((b & 1) == 1) { result *= a; } a *= a; b >>= 1; } return result; } public static int mod(int a, int m) { return (a % m + m) % m; } public static int modPow(int a, int b, int m) { int result = 1; while (b > 0) { if ((b & 1) == 1) { result = mod(result * a, m); } a = mod(a * a, m); b >>= 1; } return result; } public static int gcdExtended(int a, int b, int[] x, int[] y) { if (a == 0) { x[0] = 0; y[0] = 1; return b; } int[] temp = new int[2]; int gcd = gcdExtended(b % a, a, temp, x); y[0] = temp[1] - (b / a) * temp[0]; return gcd; } public static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static int modLcm(int a, int b, int m) { return (a / gcd(a, b)) * b % m; } public static int modInverse(int a, int m) { int[] x = new int[1]; int[] y = new int[1]; int g = gcdExtended(a, m, x, y); if (g != 1) { return -1; } return (x[0] % m + m) % m; } public static int modMul(int a, int b, int m) { return ((a % m) * (b % m)) % m; } public static int modSubtract(int a, int b, int m) { return modAdd(a, modMul(b, modInverse(gcd(a, m), m), m), m); } public static int modAdd(int a, int b, int m) { return ((a % m) + (b % m)) % m; } public static int modSquare(int a, int m) { return modMul(a, a, m); } public static int modCube(int a, int m) { return modMul(a, modMul(a, a, m), m); } public static int modFactorial(int a, int m) { int result = 1; while (a > 0) { result = modMul(result, a, m); a--; } return result; } public static int modExp(int a, int b, int m) { int result = 1; while (b > 0) { if ((b & 1) == 1) { result = modMul(result, a, m); } a = modMul(a, a, m); b >>= 1; } return result; } public static int modLog(int a, int b, int m) { int result = 0; while (modSquare(a, m) != b) { result++; a = modMul(a, a, m); } return result; } public static int modDivide(int a, int b, int m) { return modMul(a, modInverse(b, m), m); } public static int modDivide(int a, int b, int m, int[] x, int[] y) { int gcd = gcdExtended(a, m, x, y); if (b % gcd != 0) { return -1; } x[0] = (x[0] * (b / gcd)) % m; return 0; } public static int modDivide(int a, int b, int m, int[] x, int[] y, int[] z) { int gcd = gcdExtended(a, m, x, y); if (b % gcd != 0) { return -1; } x[0] = (x[0] * (b / gcd)) % m; z[0] = (y[0] * (b / gcd)) % m; return 0; } public static int modDivide(int a, int b, int m, int[] x, int[] y, int[] z, int[] w) { int gcd = gcdExtended(a, m, x, y); if (b % gcd != 0) { return -1; } x[0] = (x[0] * (b / gcd)) % m; z[0] = (y[0] * (b / gcd)) % m; w[0] = (w[0] * (b / gcd)) % m; return 0; } public static int modDivide(int a, int b, int m, int[] x, int[] y, int[] z, int[] w, int[] v) { int gcd = gcdExtended(a, m, x, y); if (b % gcd != 0) { return -1; } x[0] = (x[0] * (b / gcd)) % m; z[0] = (y[0] * (b / gcd)) % m; w[0] = (w[0] * (b / gcd)) % m; v[0] = (v[0] * (b / gcd)) % m; return 0; } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
19356b70d7718e3e9c5473cd57e24b32
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; /** * https://codeforces.com/problemset/problem/1541/A * @author ey * */ public class P1514A { public static void main(String[] args) throws IOException{ BufferedReader io = new BufferedReader(new InputStreamReader(System.in)); Integer cnt = Integer.valueOf(io.readLine()); List<String> rs = new ArrayList<String>(); while (cnt > 0) { cnt--; Integer input = Integer.valueOf(io.readLine()); String tmp = ""; if (input % 2 == 0) { for(int i = 2; i <= input; i=i+2) { tmp = tmp + String.valueOf(i) + " " + String.valueOf(i - 1) + " "; } } else { tmp = "3 1 2 "; if (input > 3) { for(int i = 5; i <=input; i = i+2) { tmp = tmp + String.valueOf(i) + " " + String.valueOf(i - 1) + " "; } } } rs.add(tmp); } for (String string : rs) { System.out.println(string); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
1021eaddfb57453bf32eaec30c88ccd9
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class PrettyCombinations { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); while (test-- > 0) { int n = sc.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = i + 1; } for (int i = 0; i < n - 1; i += 2) { swap(nums, i, i + 1); } if (n % 2 == 1) swap(nums, n - 1, n - 2); // if (n % 2 == 1) { // for (int i = 0; i < n - 1; i++) { // swap(nums, i, i + 1); // } // } // else { // for (int i = 0; i < n; i += 2) { // swap(nums, i, i + 1); // } // } for (int number : nums) { System.out.print(number + " "); } System.out.println(); } } private static void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
a67f12d95b45a2cbfbaf76cd8b27e903
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class Solution { public static void solve(int start, int n){ for(int i = start; i <= n; i+=2){ if(start+1 != n) System.out.print((i+1) + " " + i + " "); else System.out.print(i+1 + " " + i); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i = 0; i < t; i++){ int n = sc.nextInt(); if(n % 2 == 0){ solve(1, n); } else{ System.out.print("3 1 2"); if(n > 3){ System.out.print(" "); solve(4, n); } } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
a09f814cef9459de36e34d6b052a91f1
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.*; public class Solution{ static PrintWriter out = new PrintWriter(System.out); static int mod = 1000000007; public static void main(String[] args) throws IOException { FastReader fs = new FastReader(); int t = fs.nextInt(); while(t > 0){ int n = fs.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++){ a[i] = i+1; } for(int i = 0; i < n-1; i++){ if(a[i] == i+1){ int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } } if(a[n-1] == n){ int temp = a[n-1]; a[n-1] = a[n-2]; a[n-2] = temp; } for(int i = 0; i < n; i++){out.print(a[i] + " ");} out.println(); t--; } out.flush(); } static void op(int[] a, int n){ int max = Integer.MIN_VALUE; for(int i = 0; i < n; i++){ max = Math.max(max, a[i]); } for(int i = 0; i < n; i++){ a[i] = max - a[i]; } } static int isCs(String x, String y, int n, int m, int[][] dp){ if(n == 0 || m == 0){ return 0; } if(dp[n-1][m-1] != 0) {return dp[n-1][m-1];} if(x.charAt(n-1) == y.charAt(m-1)){ return dp[n-1][m-1] = isCs(x, y, n-1, m-1, dp) + 1; }else{ return dp[n-1][m-1] = Math.max(isCs(x, y, n-1, m, dp), isCs(x, y, n, m-1, dp)); } } static int binarySearch(int[] a){ int n = a.length; return 0; } static boolean distinct(String s){ for(int i = 0; i < s.length()-1; i++){ for(int j = i+1; j < s.length(); j++){ if(s.charAt(i) == s.charAt(j)){ return false; } } } return true; } static long sumOfDigit(long n){ long sum = 0; while(n > 0){ long rem = n % 10; sum += rem; n = n / 10; } return sum; } static void swap(int a, int b){ int temp = a; a = b; b = temp; } static int[] numArr(int n){ int len = countDigit(n); int[] a = new int[len]; int i = 0; while(i < len){ a[i] = n % 10; n = n / 10; i++; } return a; } static void lcs(String X, String Y, int m, int n, int temp) { int[][] L = new int[m+1][n+1]; // Following steps build L[m+1][n+1] in bottom up fashion. Note // that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] for (int i=0; i<=m; i++) { for (int j=0; j<=n; j++) { if (i == 0 || j == 0) L[i][j] = 0; else if (X.charAt(i-1) == Y.charAt(j-1)) L[i][j] = L[i-1][j-1] + 1; else L[i][j] = Math.max(L[i-1][j], L[i][j-1]); } } // Following code is used to print LCS int index = L[m][n]; temp = index; // Create a character array to store the lcs string char[] lcs = new char[index+1]; lcs[index] = '\u0000'; // Set the terminating character // Start from the right-most-bottom-most corner and // one by one store characters in lcs[] int i = m; int j = n; while (i > 0 && j > 0) { // If current character in X[] and Y are same, then // current character is part of LCS if (X.charAt(i-1) == Y.charAt(j-1)) { // Put current character in result lcs[index-1] = X.charAt(i-1); // reduce values of i, j and index i--; j--; index--; } // If not same, then find the larger of two and // go in the direction of larger value else if (L[i-1][j] > L[i][j-1]) i--; else j--; } for(int k=0;k<=temp;k++) System.out.print(lcs[k]); } static void sortDe(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); int list = l.size() - 1; for (int i = 0; i < a.length; i++) { a[i] = l.get(list); list--; } } static void bracket(int n){ for(int i = 0; i < n; i++){ out.print("("); } for(int i = 0; i < n; i++){ out.print(")"); } } static int countDigit(int n){ return (int) Math.floor(Math.log10(n)) + 1; } static int countDigit(long n){ return (int) Math.floor(Math.log10(n)) + 1; } static void print(int[] ar){ for(int i = 0; i < ar.length; i++){ out.print(ar[i] + " "); } } static long countEven(long n){ long c = 0; long rem = 0; while(n > 1){ rem = n % 10; if(rem % 2 == 0){ c++; } n = n / 10; } return c; } static boolean divisor(long n){ int count = 2; for(int i = 2; i*i <= n; i++){ if(n % i == 0){ count++; } if(count < 3){ break; } } if(count == 3){return true;} else{return false;} } static String reverseString(String s){ int j = s.length() - 1; String st = new String(); while(j >= 0){ st += s.charAt(j); j--; } return st; } static boolean isPallindrome(String s){ int i = 0, j = s.length() - 1; while(i < j){ if(s.charAt(i) != s.charAt(j)){ return false; } i++; j--; } return true; } static boolean lcsPallindrom(String s){ int i = 0, j = s.length()-1; while(i < j){ if(s.charAt(i) != s.charAt(j)){ return false; } i++; j--; } return true; } static int[] nprimeNumbers(int n){ int[] mark = new int[n+1]; Arrays.fill(mark, 1); mark[0] = 0; mark[1] = 0; for(int i = 2; i < mark.length; i++){ if(mark[i] == 1){ for(int j = i*i; j < mark.length; j += i){ mark[j] = 0; } } } return mark; } static long gcd(long a, long b){ if(b > a){return gcd(b, a);} if(b == 0){return a;} return gcd(b, a % b); } static int gcd(int a, int b){ if(b > a){return gcd(b, a);} if(b == 0){return a;} return gcd(b, a % b); } static class Pair { int lvl; int str; Pair(int lvl, int str){ this.lvl = lvl; this.str = str; } } static class lvlcompare implements Comparator<Pair> { public int compare(Pair p1, Pair p2){ if(p1.lvl == p2.lvl){ return 0; }else if(p1.lvl > p2.lvl){ return 1; }else { return -1; } } } static long count(long n){ long ct = 0; while(n > 0){ n = n / 10; ct++; } return ct; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() {br = new BufferedReader(new InputStreamReader(System.in));} String next() { while (st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();}} return st.nextToken();} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble(){return Double.parseDouble(next());} String nextLine() { String str = ""; try {str = br.readLine();} catch (IOException e) {e.printStackTrace();} return str; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
21384cc5266973ed54a43dfdce9afcc3
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.Math; import java.util.Scanner; import java.io.*; import java.util.*; import java.util.StringTokenizer; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) throws Exception { // TODO Auto-generated method stub FastReader sc = new FastReader(); BufferedWriter ou = new BufferedWriter(new OutputStreamWriter(System.out)); // Start coding int t = sc.nextInt(); for (int o = 0; o < t; o++) { int n = sc.nextInt(); if (n%2 == 0) { for (int i = 1; i<=n ; i++) { if (i%2 == 0) { ou.write(i-1 +" "); } else { ou.write(i+1 +" "); } } } else { ou.write(3 + " " + 1 +" "+ 2 +" "); for (int i = 4 ; i<=n ; i++) { if (i%2 == 0) { ou.write(i+1 +" "); } else { ou.write(i-1 +" "); } } } ou.write("\n"); } ou.flush(); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
5c9347ed09c53a9527ffac9be2096e37
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.io.*; public class PrettyPermutations { public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-->0){ int n = sc.nextInt(); int[] a = new int[n]; for(int i=0; i<n ;i++){ a[i] = i+1; if(i>=1 && (i%2==1 || i == n-1)){ int tmp = a[i]; a[i] = a[i-1]; a[i-1] = tmp; } } for (int i=0; i<n ;i++) pw.print(a[i]+" "); pw.println(); } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int[] nextIntArr(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
ce589802b156e587d365bc4d16d92153
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.*; public class PrettyPermutations { public static void main(String[] args) { new PrettyPermutations().run(); } BufferedReader br; PrintWriter out; long mod = (long) (1e9 + 7), inf = (long) (3e18); class pair { int F, S; pair(int f, int s) { F = f; S = s; } } void solve() { int t = ni(); while(t-- > 0) { // TODO: int n=ni(); for(int i=1; i<n; i+=2) { if(n%2==1 && i==n-2) { out.print((i+1)+" "+(i+2)+" "+i); } else { out.print((i+1)+" "+i+" "); } } out.println(); } } // -------- I/O Template ------------- char nc() { return ns().charAt(0); } String nLine() { try { return br.readLine(); } catch(IOException e) { return "-1"; } } double nd() { return Double.parseDouble(ns()); } long nl() { return Long.parseLong(ns()); } int ni() { return Integer.parseInt(ns()); } int[] na(int n) { int a[] = new int[n]; for(int i = 0; i < n; i++) a[i] = ni(); return a; } StringTokenizer ip; String ns() { if(ip == null || !ip.hasMoreTokens()) { try { ip = new StringTokenizer(br.readLine()); if(ip == null || !ip.hasMoreTokens()) ip = new StringTokenizer(br.readLine()); } catch(IOException e) { throw new InputMismatchException(); } } return ip.nextToken(); } void run() { try { if (System.getProperty("ONLINE_JUDGE") == null) { br = new BufferedReader(new FileReader("/media/ankanchanda/Data/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/input.txt")); out = new PrintWriter("/media/ankanchanda/Data/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/output.txt"); } else { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } catch (FileNotFoundException e) { System.out.println(e); } solve(); out.flush(); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
4c89afb1cf3f8b19334d96b09c53e8ea
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i = 0; i < t; i++){ int n = sc.nextInt(); if(n % 2 == 1){ System.out.print("3 1 2 "); for(int j = 4; j <= n; j++){ if(j % 2 != 0){ System.out.print(j-1 + " "); }else System.out.print(j+1 + " "); } }else{ for(int j = 1; j <= n; j++){ if(j % 2 != 0){ System.out.print(j+1 + " "); }else System.out.print(j-1 + " "); } } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
ba0f623dbebe129be5c69de8297d1314
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; import java.util.Set; import org.omg.CORBA.MARSHAL; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int len = s.nextInt(); long a = 0; long b = 0; long c = 0; long res = 0; int odd = 0; int even = 0; int floor = 0; int max = 0; int min = 0; int d = 0; StringBuilder sb = new StringBuilder(); String str; boolean placeFound = false; for (int i = 0; i <len; i++) { a = s.nextLong(); if(a%2 == 0) { for (int k = 1; k < a; k = k + 2) { System.out.print(k+1 + " " + k+ " "); } }else { System.out.print("3 1 2 "); for (int k = 4; k < a; k = k + 2) { System.out.print(k+1 + " " + k+ " "); } } System.out.println(); } /* str = s.next(); for(char ch : str.toCharArray()){ if(ch == 'L') a--; else b++; } System.out.println((b-a) + 1); */ s.close(); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
579876701d6ec811d015e47a50c68bd1
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class pretty { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t,n; t=sc.nextInt(); while(t-->0) { n=sc.nextInt(); if(n%2==0) { int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=i+1; } for(int i=0;i<n-1;i=i+2) { int temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } for(int i=0;i<n;i++) { System.out.print(a[i]+" "); } System.out.println(); } else { int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=i+1; } System.out.print(3+" "+1+" "+2+" "); for(int i=3;i<n-1;i=i+2) { int temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } for(int i=3;i<n;i++) { System.out.print(a[i]+" "); } System.out.println(); } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
290722a2a2cf2518bb9d61d9a2e5d3ac
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.io.*; public class c1 { static FastScanner scan; static SlowPrinter out; public static void main(String[] args) throws Exception { scan=new FastScanner(System.in); out=new SlowPrinter(); // int T=1; int T=scan.nextInt(); while(T-->0) { int n=scan.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++) a[i]=i+1; for(int i=0;i<n-1;i+=2) { a[i]^=a[i+1]; a[i+1]^=a[i]; a[i]^=a[i+1]; } if(n%2!=0) { a[n-1]^=a[n-2]; a[n-2]^=a[n-1]; a[n-1]^=a[n-2]; } out.println(a); } out.close(); } } class Pair { int a,b; Pair(int a,int b) { this.a=a; this.b=b; } } class SlowPrinter { static PrintWriter print; SlowPrinter() { print=new PrintWriter(System.out); } void println(int[][] a) { for(int i=0;i<a.length;i++) { for(int j=0;j<a[i].length;j++) print.print(a[i][j]+" "); print.println(); } } void println(long[][] a) { for(int i=0;i<a.length;i++) { for(int j=0;j<a[i].length;j++) print.print(a[i][j]+" "); print.println(); } } void println(double[][] a) { for(int i=0;i<a.length;i++) { for(int j=0;j<a[i].length;j++) print.print(a[i][j]+" "); print.println(); } } void println(int[] a) { for(int i=0;i<a.length;i++) print.print(a[i]+" "); print.println(); } void println(long[] a) { for(int i=0;i<a.length;i++) print.print(a[i]+" "); print.println(); } void println(double[] a) { for(int i=0;i<a.length;i++) print.print(a[i]+" "); print.println(); } void println(String text) { print.println(text); } void println(int text) { print.println(text); } void println(long text) { print.println(text); } void println(double text) { print.println(text); } void print(String text) { print.print(text); } void print(int text) { print.print(text); } void print(long text) { print.print(text); } void print(double text) { print.print(text); } void close() { print.close(); } void flush() { print.flush(); } } class FastScanner { private InputStream stream; private byte[] buf=new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream=stream; } int read() { if(numChars==-1) throw new InputMismatchException(); if(curChar>=numChars) { curChar=0; try { numChars=stream.read(buf); } catch(IOException e) { throw new InputMismatchException(); } if(numChars<=0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1; } boolean isEndline(int c) { return c=='\n'||c=='\r'||c==-1; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { int c=read(); while(isSpaceChar(c)) c=read(); StringBuilder res=new StringBuilder(); do { res.appendCodePoint(c); c=read(); } while(!isSpaceChar(c)); return res.toString(); } String nextLine() { int c=read(); while(isEndline(c)) c=read(); StringBuilder res=new StringBuilder(); do { res.appendCodePoint(c); c=read(); } while(!isEndline(c)); return res.toString(); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
e0e4aea2b543670e39582c9859b95b28
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.io.*; public class s1 { public static void main(String[] args) throws IOException { scan=new BessieBeam(); out=new PrintWriter(System.out); // int T=1; int T=scan.nextInt(); while(T-->0) { int n=scan.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++) a[i]=i+1; for(int i=0;i<n-1;i+=2) { a[i]^=a[i+1]; a[i+1]^=a[i]; a[i]^=a[i+1]; } if(n%2!=0) { a[n-1]^=a[n-2]; a[n-2]^=a[n-1]; a[n-1]^=a[n-2]; } for(int i=0;i<n;i++) out.print(a[i]+" "); out.println(); } out.close(); } /* Delete */ /* Delete */ public static PrintWriter out; public static BessieBeam scan; public static class BessieBeam { final private int bufferSize=1<<16; private DataInputStream din; private byte[] buffer; private int bufferPointer,bytesRead; public BessieBeam() { din=new DataInputStream(System.in); buffer=new byte[bufferSize]; bufferPointer=bytesRead=0; } public BessieBeam(String file) throws IOException { din=new DataInputStream(new FileInputStream(file+".in")); buffer=new byte[bufferSize]; bufferPointer=bytesRead=0; } public String nextLine() throws IOException{ byte[] buf=new byte[1024]; int cnt=0,c; while((c=read())!=-1) { if(c=='\n') { if(cnt!=0) break; else continue; } buf[cnt++]=(byte) c; } return new String(buf,0,cnt); } public int nextInt() throws IOException{ int ret=0; byte c=read(); while(c<=' ') { c=read(); } boolean neg=(c=='-'); if(neg) c=read(); do { ret=ret*10+c-'0'; } while((c=read())>='0'&&c<='9'); if(neg) return -ret; return ret; } public double nextDouble() throws IOException{ double ret=0,div=1; byte c=read(); while(c<=' ') c=read(); boolean neg=(c=='-'); if(neg) c=read(); do { ret=ret*10+c-'0'; } while((c=read())>='0'&&c<='9'); if(c=='.') { while((c=read())>='0'&&c<='9') { ret+=(c-'0')/(div*=10); } } if(neg) return -ret; return ret; } public long nextLong() throws IOException{ long ret=0; byte c=read(); while(c<=' ') c=read(); boolean neg=(c=='-'); if(neg) c=read(); do { ret=ret*10+c-'0'; } while((c=read())>='0'&&c<='9'); if(neg) return -ret; return ret; } private byte read() throws IOException{ if(bufferPointer==bytesRead) fillBuffer(); return buffer[bufferPointer++]; } private void fillBuffer() throws IOException{ bytesRead=din.read(buffer,bufferPointer=0,bufferSize); if(bytesRead==-1) buffer[0]=-1; } private void close() throws IOException{ if(din==null) return; din.close(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
ca439ff692dc27e7af26e99c4e558208
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class Pretty_Permutations{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); long input_count=sc.nextInt(); while(input_count-->0){ long cat_cost = sc.nextInt(); if(cat_cost==1){ System.out.println("1"); break; } ArrayList<Long> cat_map = new ArrayList<Long>(); for(long i =0;i<cat_cost;i++){ cat_map.add(i+1); } if(cat_cost%2==0){ //odd List<Long> subList = cat_map.subList(0, (int)cat_cost); cat_move(subList); } else{ //even System.out.print("3 1 2 "); List<Long> subList = cat_map.subList(3, (int)cat_cost); if(cat_map.size()>3){ cat_move(subList); } } } } public static void cat_move(List<Long> map){ for(int i = 0;i<map.size()-2;i+=2){ Collections.swap(map, i, i+1); System.out.print(map.get(i)+" "+map.get(i+1)+" "); } Collections.swap(map, map.size()-2, map.size()-1); System.out.print(map.get(map.size()-2)+" "+map.get(map.size()-1)); System.out.println(); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
2be589415a36663a72efcb8da801c178
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.io.*; import java.util.Arrays; import java.util.Map.Entry; public class codeforces { public static void main(String[] args) throws Exception { int t=sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int[] arr = new int[n];for(int i = 1; i<=n; i++)arr[i - 1] = i; int[] ans = new int[n]; for(int i = 0; i<n-1; i+=2){ int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; // i += 2; } if(n % 2 != 0){ int temp = arr[n - 1]; arr[n - 1] = arr[n - 2]; arr[n - 2] = temp; } for(int i = 0; i<n; i++)pw.print(arr[i] + " "); pw.println(); } pw.close(); } public static long eval(String s) { long p=1; long res=0; for (int i = 0; i < s.length(); i++) { res+=p*(s.charAt(s.length()-1-i)=='1'?1:0); p*=2; } return res; } public static String binary(long x) { String s=""; while(x!=0) { s=(x%2)+s; x/=2; } return s; } public static boolean allSame(String s) { char x=s.charAt(0); for (int i = 0; i < s.length(); i++) { if(s.charAt(i)!=x)return false; } return true; } public static boolean isPalindrom(String s) { int l=0; int r=s.length()-1; while(l<r) { if(s.charAt(r--)!=s.charAt(l++))return false; } return true; } public static String putAtFront(String s,char c) { if(s.length()==0)return s; if(s.charAt(s.length()-1)==c) { return s.charAt(s.length()-1)+putAtFront(s.substring(0,s.length()-1), c); }else { return putAtFront(s.substring(0,s.length()-1), c)+s.charAt(s.length()-1); } } public static boolean isSubString(String s,String t) { int ls=s.length(); int lt=t.length(); boolean res=false; for (int i = 0; i <=lt-ls; i++) { if(t.substring(i, i+ls).equals(s)) { res=true; break; } } return res; } public static boolean isSorted(int[]a) { for (int i = 0; i < a.length-1; i++) { if(a[i]>a[i+1])return false; } return true; } public static boolean isPrime(int n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } public static long power(long x,long k) { long res=1; long mod=((int)1e9)+7; for (int i = 0; i < k; i++) { res*=x; res=res%mod; } return res; } public static int whichPower(int x) { int res=0; for (int j = 0; j < 31; j++) { if((1<<j&x)!=0) { res=j; break; } } return res; } public static long evaln(String x,int n) { long res=0; for (int i = 0; i < x.length(); i++) { res+=Long.parseLong(x.charAt(x.length()-1-i)+"")*Math.pow(n, i); } return res; } static void merge(int[] arr,int b,int m,int e) { int len1=m-b+1,len2=e-m; int[] l=new int[len1]; int[] r=new int[len2]; for(int i=0;i<len1;i++)l[i]=arr[b+i]; for(int i=0;i<len2;i++)r[i]=arr[m+1+i]; int i=0,j=0,k=b; while(i<len1 && j<len2) { if(l[i]<r[j])arr[k++]=l[i++]; else arr[k++]=r[j++]; } while(i<len1)arr[k++]=l[i++]; while(j<len2)arr[k++]=r[j++]; return; } static void mergesortidx(int[] arr,int[]idx,int b,int e) { if(b<e) { int m=b+(e-b)/2; mergesortidx(arr,idx,b,m); mergesortidx(arr,idx,m+1,e); mergeidx(arr,idx,b,m,e); } return; } static void mergeidx(int[] arr,int[]idx,int b,int m,int e) { int len1=m-b+1,len2=e-m; int[] l=new int[len1]; int[] lidx=new int[len1]; int[] r=new int[len2]; int[] ridx=new int[len2]; for(int i=0;i<len1;i++) { l[i]=arr[b+i]; lidx[i]=idx[b+i]; } for(int i=0;i<len2;i++) { r[i]=arr[m+1+i]; ridx[i]=idx[m+1+i]; } int i=0,j=0,k=b; while(i<len1 && j<len2) { if(l[i]<r[j]) { arr[k++]=l[i++]; idx[k-1]=lidx[i-1]; } else { arr[k++]=r[j++]; idx[k-1]=ridx[j-1]; } } while(i<len1) { idx[k]=lidx[i]; arr[k++]=l[i++]; } while(j<len2) { idx[k]=ridx[j]; arr[k++]=r[j++]; } return; } static void mergesort(int[] arr,int b,int e) { if(b<e) { int m=b+(e-b)/2; mergesort(arr,b,m); mergesort(arr,m+1,e); merge(arr,b,m,e); } return; } static long mergen(int[] arr,int b,int m,int e) { int len1=m-b+1,len2=e-m; int[] l=new int[len1]; int[] r=new int[len2]; for(int i=0;i<len1;i++)l[i]=arr[b+i]; for(int i=0;i<len2;i++)r[i]=arr[m+1+i]; int i=0,j=0,k=b; long c=0; while(i<len1 && j<len2) { if(l[i]<r[j])arr[k++]=l[i++]; else { arr[k++]=r[j++]; c=c+(long)(len1-i); } } while(i<len1)arr[k++]=l[i++]; while(j<len2)arr[k++]=r[j++]; return c; } static long mergesortn(int[] arr,int b,int e) { long c=0; if(b<e) { int m=b+(e-b)/2; c=c+(long)mergesortn(arr,b,m); c=c+(long)mergesortn(arr,m+1,e); c=c+(long)mergen(arr,b,m,e); } return c; } public static long fac(int n) { if(n==0)return 1; return n*fac(n-1); } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static long summ(long x) { long sum=0; while(x!=0) { sum+=x%10; x=x/10; } return sum; } public static void sort2darray(Integer[][]a){ Arrays.sort(a,Comparator.<Integer[]>comparingInt(x -> x[0]).thenComparingInt(x -> x[1])); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } public int[] nextArrint(int size) throws IOException { int[] a=new int[size]; for (int i = 0; i < a.length; i++) { a[i]=sc.nextInt(); } return a; } public long[] nextArrlong(int size) throws IOException { long[] a=new long[size]; for (int i = 0; i < a.length; i++) { a[i]=sc.nextLong(); } return a; } public int[][] next2dArrint(int rows,int columns) throws IOException{ int[][]a=new int[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { a[i][j]=sc.nextInt(); } } return a; } public long[][] next2dArrlong(int rows,int columns) throws IOException{ long[][]a=new long[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { a[i][j]=sc.nextLong(); } } return a; } } static Scanner sc=new Scanner(System.in); static PrintWriter pw=new PrintWriter(System.out); } //int[]help=new int[max+1]; //boolean flag=true; //for (int i = 0; i < a.length; i++) { // if((idx[i]-i)%2==1)help[a[i]]++; // else { // if(help[a[i]]%2==1) { // flag=false; // }else { // help[a[i]]=0; // } // } //} //if(flag) { // ans=true; // for (int i = 0; i < help.length; i++) { // ans=ans&&((help[i]%2==0)); // } // pw.println(ans?"YES":"NO"); //}else pw.println("NO");
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
63d2681bd526f5176a5c8133ed0d8889
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class Main { static AReader scan = new AReader(); static Scanner scan2 = new Scanner(System.in); static int N = 100010; static void slove() { int n = scan.nextInt(); if(n % 2 == 0){ for(int i=2;i<=n; i +=2){ System.out.print(i+" " + (i-1) + " "); } }else{ System.out.print(3+" " + 1 + " " + 2+" "); for(int i=5;i<=n; i +=2){ System.out.print(i+" " + (i-1) + " "); } } System.out.println(); } public static void main(String[] args) { int t = scan.nextInt(); while (t-- > 0) { slove(); } } } class AReader { private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer tokenizer = new StringTokenizer(""); private String innerNextLine() { try { return reader.readLine(); } catch (IOException ex) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String nextLine = innerNextLine(); if (nextLine == null) { return false; } tokenizer = new StringTokenizer(nextLine); } return true; } public String nextLine() { tokenizer = new StringTokenizer(""); return innerNextLine(); } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } class Node{ int x; int y; public Node(int x, int y) { this.x = x; this.y = y; } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
d8290025d14f03434c1f8b15efaccfb1
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); while(t-->0){ int n = input.nextInt(); int i =1; if (n%2!=0){ System.out.print("3 1 2 "); i =4; } while(i < n){ if (i+1<=n) System.out.print(i+1 + " "); System.out.print(i+ " "); i+=2; } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
5298f9b152a07292b3a3284da7bad577
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
//package practice; import java.util.*; public class Solution { public static void main (String[] args){ Scanner scan = new Scanner(System.in); int T = scan.nextInt(); while(T-->0) { int N = scan.nextInt(); int arr[] = new int[N]; if(N%2 != 0) { arr[0] = 3; arr[1] = 1; arr[2] = 2; for(int i=3; i<N; i++) { if(i%2 != 0) arr[i] = i+2; else arr[i] = i; } }else { for(int i=0; i<N; i++) { if(i%2 == 0) arr[i] = i+2; else arr[i] = i; } } for(int i=0; i<N; i++) System.out.print(arr[i]+" "); System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
e01ba52d7869ba0490e470bcac365c7d
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); if (n % 2 == 0) { for (int j = 1; j <= n; j++) { if (j % 2 == 1) { System.out.printf("%d", j + 1); } else { System.out.printf("%d", j - 1); } if (j != n) { System.out.print(" "); } } System.out.println(); } else { System.out.print("3 1 2"); if (n > 3) { System.out.print(" "); for (int j = 4; j <= n; j++) { if (j % 2 == 1) { System.out.printf("%d", j - 1); } else { System.out.printf("%d", j + 1); } if (j != n) { System.out.print(" "); } } } System.out.println(); } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
4910eb5f154ac524d0733f9a049f1e90
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.*; public class A { public static void main(String[] args) { FastReader in = new FastReader(); int T = in.nextInt(); while(T-- > 0) { int n = in.nextInt(); if(n==2) System.out.println("2 1"); else if(n==3) System.out.println("3 1 2"); else { if(n%2==0) { for(int i = 2; i <= n; i+=2) System.out.print(i + " " + (i-1) + " "); System.out.println(); } else { System.out.print("3 1 2 "); for(int i = 5; i <= n; i+=2) System.out.print(i + " " + (i-1) + " "); System.out.println(); } } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } int[] readInt(int size) { int[] arr = new int[size]; for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(next()); return arr; } long[] readLong(int size) { long[] arr = new long[size]; for(int i = 0; i < size; i++) arr[i] = Long.parseLong(next()); return arr; } int[][] read2dArray(int rows, int cols) { int[][] arr = new int[rows][cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) arr[i][j] = Integer.parseInt(next()); } return arr; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
5f530b84b0b57c37b908ede5abb56e27
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.*; public class A { public static void main(String[] args) { FastReader in = new FastReader(); int T = in.nextInt(); while(T-- > 0) { int n = in.nextInt(); if(n%2==0) { for(int i = 2; i <= n; i+=2) { System.out.print(i + " " + (i-1) + " "); } } else { System.out.print("3 1 2 "); for(int i = 5; i <= n; i+=2) { System.out.print(i + " " + (i-1) + " "); } } System.out.println(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } int[] readInt(int size) { int[] arr = new int[size]; for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(next()); return arr; } long[] readLong(int size) { long[] arr = new long[size]; for(int i = 0; i < size; i++) arr[i] = Long.parseLong(next()); return arr; } int[][] read2dArray(int rows, int cols) { int[][] arr = new int[rows][cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) arr[i][j] = Integer.parseInt(next()); } return arr; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
3e56bc6597da90ab8237d907b831af04
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class Hello { public static void main(String[] args) { Scanner ss=new Scanner(System.in); int t=ss.nextInt(); for(int it=0;it<t;it++) { int n=ss.nextInt(); int[] arr=new int[105]; for(int i=1;i<=n;i++) { arr[i-1]=i; } for(int i=0;i<n-1;i++) { if(i%2==0) { int x=arr[i]; int y=arr[i+1]; arr[i]=y; arr[i+1]=x; } } if(n%2==0) { for(int i=0;i<n;i++) { System.out.print(arr[i]+" "); } System.out.println(); } else { int x=arr[n-1]; int y=arr[n-2]; arr[n-1]=y; arr[n-2]=x; for(int i=0;i<n;i++) { System.out.print(arr[i]+" "); } System.out.println(); } } /*Bicycle bike1=new Bicycle(); Bicycle bike2=new Bicycle(); bike1.changeCadence(5); bike1.changeGear(50); bike1.speedUp(5); bike1.speedDown(4); bike2.changeCadence(10); bike2.changeGear(10); bike2.speedUp(6); bike2.speedDown(4); bike1.printStates(); bike2.printStates();*/ } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
87f725ee7760e4c622b76f20c3740b7a
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Solution { static class Algebra { public static int GCD(int a, int b) { return b==0?a:GCD(b,a%b); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } int[] nextintArr(int n, FastReader fs) { int[] arr = new int[n]; for(int i=0;i<n;i++) { arr[i] = fs.nextInt(); } return arr; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void ln(String s){ System.out.println(s); } static int abs(int a,int b){ if(b>a) return b-a; return a-b; } public static char nextChar(char curr) { if(curr == 'z') return 'a'; else return (char)((int)curr+1); } public static int factorial(int i) { int res = 1; while(i>0) res*=i--; return res; } public static void main(String[] args) { FastReader fs = new FastReader(); int t = fs.nextInt(); while(t-->0) { int n = fs.nextInt(); for (int i = 1; i <=n-3; i+=2) { System.out.print(i+1+" "+(i)+" "); } if(n%2==0) { System.out.print(n+" "+(n-1)); } else { System.out.print((n)+" "+(n-2)+" "+(n-1)); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
905fd4e378fda9c907c38b9f6752e642
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
// package cp; import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public class Main{ public static void main(String[] args) throws Exception{ Reader.init(System.in); Main mm=new Main(); int t=Reader.nextInt(); while(t>0) { int n=Reader.nextInt(); int odd=1; if(n%2==0) { odd=0; } else { n-=3; } for(int i=1;i<=n;i+=2) { System.out.print((i+1)+" "+i+" "); } if(odd==1) { System.out.println((n+3)+" "+(n+1)+" "+(n+2)); } t--; } } } class a implements Comparator<pair>{ public int compare(pair p1,pair p2) { return p1.key-p2.key; } } class pair{ int key; int value; pair(int key,int value){ this.key=key; this.value=value; } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; // / call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } static String nextLine() throws IOException{ return reader.readLine(); } static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
32472db2622c434f925a7577ad3814c3
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class PrettyPermutations { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); for(int i=1; i<n; i+=2) { if(n%2==1 && i==n-2) { System.out.print((i+1)+" "+(i+2)+" "+i); } else { System.out.print((i+1)+" "+i+" "); } } System.out.println(); } } }/* 4 2 3 4 5 1 2 3 4 5 2 1 4 3 5 1 2 3 2 1 3 2 3 1 */
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
d0879064a961d804ed6332121a842515
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Solution { static final int MOD = (int)1e9+7; public static void main(String[] args)throws Exception { int T = nextInt(); while(T-- > 0) { int n = nextInt(); int[] a = new int[n]; for (int i=0;i<n-1;i+=2) { a[i] = i+2; a[i+1] = i+1; } if (n%2==1) { a[n-1] = a[n-2]; a[n-2] = n; } for (int i=0;i<n;i++) { System.out.print(a[i]+" "); } System.out.println(); } } static void swap(int[] a, int i, int j) { int t = a[i]; a[i] = a[j]; a[j] = t; } static int nextInt()throws IOException{ InputStream in=System.in; int ans=0; boolean flag=true; byte b=0; boolean neg=false; while ((b>47 && b<58) || flag){ if(b==45) neg=true; if(b>=48 && b<58){ ans=ans*10+(b-48); flag=false; } b=(byte)in.read(); } if(neg) return -ans; return ans; } static long nextLong()throws IOException{ InputStream in=System.in; long ans=0; boolean flag=true; byte b=0; while ((b>47 && b<58) || flag){ if(b>=48 && b<58){ ans=ans*10+(b-48); flag=false; } b=(byte)in.read(); } return ans; } static String next()throws Exception{ StringBuilder sb=new StringBuilder(1<<16); InputStream in=System.in; byte b=0; do{ if(!isWhiteSpace(b)) sb.append((char)b); b=(byte)in.read(); }while(!isWhiteSpace(b) || sb.length()==0); return sb.toString(); } static boolean isWhiteSpace(byte b){ char ch=(char)b; return ch=='\0' || ch==' ' || ch=='\n'; } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
78cb3664051af45ce0ef6156758af8a5
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class Sol{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); if(n%2==1){ if(n==1) System.out.println("1"); else{ System.out.print("3 "); System.out.print("1 "); System.out.print("2 "); for(int i=4;i<=n-1;i+=2){ System.out.print(i+1+" "); System.out.print(i+" "); } } } else{ for(int i=1;i<=n-1;i+=2){ System.out.print(i+1+" "); System.out.print(i+" "); } System.out.println(); } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
88cd399cd39b56ad63ae836956d1952b
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; import javafx.util.Pair; public class Main { public static void main(String[] args) { FastScanner input = new FastScanner(); int tc = input.nextInt(); while (tc-- > 0) { int n = input.nextInt(); if(n%2==0) { int i=1; while(i<n) { System.out.print((i+1)+" "+i+" "); i+=2; } System.out.println(); } else { System.out.print("3 1 2 "); int i = 4; while(i<n) { System.out.print((i+1)+" "+i+" "); i+=2; } System.out.println(""); } } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
d63f2b52b961ab30117bdc18fa9a2abf
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /* * @author : Imtiaz Adar */ public class Pretty_Permutations { public static void main(String[] args) throws IOException{ InputReader scan = new InputReader(); PrintWriter out = new PrintWriter(System.out); Runner run = new Runner(); int n = scan.nextInt(); for(int i=1; i<=n; i++) run.Raw(scan, out); out.close(); } static class Runner{ void Raw(InputReader scan, PrintWriter out) { StringBuilder sb = new StringBuilder(); long a = scan.nextLong(); if(a%2==0) { for(long index=1; index<=a; index+=2) { sb.append(index+1+" "+ index+" "); } } else { for(long index = 1; index<=a-3; index+=2) { sb.append((index+1)+" "+(index)+" "); } sb.append((a)+" "+(a-2)+" "+(a-1)+" "); } out.println(sb); } } static class InputReader { BufferedReader readfile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer token = new StringTokenizer(""); String next() { while(!token.hasMoreTokens()) { try { token = new StringTokenizer(readfile.readLine()); } catch(IOException e) {}; } return token.nextToken(); } String nextLine() throws IOException { return readfile.readLine(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] nextIntArray(int size) throws IOException { int [] arr = new int[size]; token = new StringTokenizer(readfile.readLine()); for(int i=0; i<arr.length; i++) { arr[i] = Integer.parseInt(token.nextToken()); } return arr; } double[] nextDoubleArray(int size) throws IOException { double [] arr = new double[size]; token = new StringTokenizer(readfile.readLine()); for(int i=0; i<arr.length; i++) { arr[i] = Double.parseDouble(token.nextToken()); } return arr; } String[] nextStringArray(int size) throws IOException { String [] arr = new String[size]; token = new StringTokenizer(readfile.readLine()); for(int i=0; i<arr.length; i++) { arr[i] = token.nextToken(); } return arr; } int[][] nextIntArray2(int size1, int size2) throws IOException { int [][] arr = new int[size1][size2]; for(int i=0; i<size1; i++) { token = new StringTokenizer(readfile.readLine()); for(int j=0; j<size2; j++) { arr[i][j] = Integer.parseInt(token.nextToken()); } } return arr; } String[][] nextStringArray2(int size1, int size2) throws IOException { String [][] arr = new String[size1][size2]; for(int i=0; i<size1; i++) { token = new StringTokenizer(readfile.readLine()); for(int j=0; j<size2; j++) { arr[i][j] = token.nextToken(); } } return arr; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
12f6aebf824213030c05cdece49574dd
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; /* * @author : Imtiaz Adar */ public class Pretty_Permutations { public static void main(String[] args) throws IOException{ InputReader scan = new InputReader(); Runner run = new Runner(); int n = scan.nextInt(); for(int i=1; i<=n; i++) run.Raw(scan); } static class Runner{ void Raw(InputReader scan) { StringBuilder sb = new StringBuilder(); long a = scan.nextLong(); if(a%2==0) { for(long index=1; index<=a; index+=2) { sb.append(index+1+" "+ index+" "); } } else { for(long index = 1; index<=a-3; index+=2) { sb.append((index+1)+" "+(index)+" "); } sb.append((a)+" "+(a-2)+" "+(a-1)+" "); } System.out.println(sb); } } static class InputReader { BufferedReader readfile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer token = new StringTokenizer(""); String next() { while(!token.hasMoreTokens()) { try { token = new StringTokenizer(readfile.readLine()); } catch(IOException e) {}; } return token.nextToken(); } String nextLine() throws IOException { return readfile.readLine(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] nextIntArray(int size) throws IOException { int [] arr = new int[size]; token = new StringTokenizer(readfile.readLine()); for(int i=0; i<arr.length; i++) { arr[i] = Integer.parseInt(token.nextToken()); } return arr; } double[] nextDoubleArray(int size) throws IOException { double [] arr = new double[size]; token = new StringTokenizer(readfile.readLine()); for(int i=0; i<arr.length; i++) { arr[i] = Double.parseDouble(token.nextToken()); } return arr; } String[] nextStringArray(int size) throws IOException { String [] arr = new String[size]; token = new StringTokenizer(readfile.readLine()); for(int i=0; i<arr.length; i++) { arr[i] = token.nextToken(); } return arr; } int[][] nextIntArray2(int size1, int size2) throws IOException { int [][] arr = new int[size1][size2]; for(int i=0; i<size1; i++) { token = new StringTokenizer(readfile.readLine()); for(int j=0; j<size2; j++) { arr[i][j] = Integer.parseInt(token.nextToken()); } } return arr; } String[][] nextStringArray2(int size1, int size2) throws IOException { String [][] arr = new String[size1][size2]; for(int i=0; i<size1; i++) { token = new StringTokenizer(readfile.readLine()); for(int j=0; j<size2; j++) { arr[i][j] = token.nextToken(); } } return arr; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
8d3d9431f6adf6d68f77424b218545f8
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; /* * @author : Imtiaz Adar */ public class Pretty_Permutations { public static void main(String[] args) throws IOException{ InputReader scan = new InputReader(); Runner run = new Runner(); int n = scan.nextInt(); for(int i=1; i<=n; i++) run.Raw(scan); } static class Runner{ void Raw(InputReader scan) { StringBuilder sb = new StringBuilder(); long a = scan.nextLong(); if(a%2==0) { for(long index=1; index<=a; index+=2) { sb.append(index+1+" "+ index+" "); } } else { for(long index = 1; index<=a-3; index+=2) { sb.append((index+1)+" "+(index)+" "); } sb.append((a)+" "+(a-2)+" "+(a-1)+" "); } System.out.println(sb); } void printArray(StringBuilder sb, long[] dp) { for(long item: dp) { sb.append(item+" "); } System.out.println(sb); } } static class InputReader { BufferedReader readfile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer token = new StringTokenizer(""); String next() { while(!token.hasMoreTokens()) { try { token = new StringTokenizer(readfile.readLine()); } catch(IOException e) {}; } return token.nextToken(); } String nextLine() throws IOException { return readfile.readLine(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] nextIntArray(int size) throws IOException { int [] arr = new int[size]; token = new StringTokenizer(readfile.readLine()); for(int i=0; i<arr.length; i++) { arr[i] = Integer.parseInt(token.nextToken()); } return arr; } double[] nextDoubleArray(int size) throws IOException { double [] arr = new double[size]; token = new StringTokenizer(readfile.readLine()); for(int i=0; i<arr.length; i++) { arr[i] = Double.parseDouble(token.nextToken()); } return arr; } String[] nextStringArray(int size) throws IOException { String [] arr = new String[size]; token = new StringTokenizer(readfile.readLine()); for(int i=0; i<arr.length; i++) { arr[i] = token.nextToken(); } return arr; } int[][] nextIntArray2(int size1, int size2) throws IOException { int [][] arr = new int[size1][size2]; for(int i=0; i<size1; i++) { token = new StringTokenizer(readfile.readLine()); for(int j=0; j<size2; j++) { arr[i][j] = Integer.parseInt(token.nextToken()); } } return arr; } String[][] nextStringArray2(int size1, int size2) throws IOException { String [][] arr = new String[size1][size2]; for(int i=0; i<size1; i++) { token = new StringTokenizer(readfile.readLine()); for(int j=0; j<size2; j++) { arr[i][j] = token.nextToken(); } } return arr; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
380b45fa9016ebb67b4ee0c1cbf56f21
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
// package first; import java.util.*; import java.io.*; import java.math.*; public class first { public static void main(String hi[]) throws Exception { Scanner sc=new Scanner(System.in); int testcase=sc.nextInt(); while(testcase>=1) { // System.out.print("enter a number \n"); int n=sc.nextInt(); int arr[]=new int[n]; int count=0; int i=0; for(int j=1;j<=n;j++) { arr[i]=j; i++; } // System.out.print("elements in the array are \n"); if(n%2==0) { for(int j=0;j<n;j+=2) { int temp; temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; count+=2; } for(int j=0;j<n;j++) { System.out.print(arr[j]+" "); } } else { for(int j=0;j<n-1;j+=2) { int temp; temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; count+=2; } int temp=arr[n-1]; arr[n-1]=arr[n-2]; arr[n-2]=temp; count+=2; // System.out.print(count); for(int l=0;l<n;l++) { System.out.print(arr[l]+" "); // System.out.print(" "); } } testcase--; } // } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
d35e0918c8236bc91b275ab0411fa2a2
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
// package solve; /* stream Butter! eggyHide eggyVengeance I need U xiao rerun when */ import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class solve { public static void main(String hi[]) throws Exception { FastScanner fs=new FastScanner(); int testcase=fs.nextInt(); while(testcase>=1) { // System.out.print("enter a number \n"); int n=fs.nextInt(); int arr[]=new int[n]; int count=0; int i=0; for(int j=1;j<=n;j++) { arr[i]=j; i++; } // System.out.print("elements in the array are \n"); if(n%2==0) { for(int j=0;j<n;j+=2) { int temp; temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; count+=2; } for(int j=0;j<n;j++) { System.out.print(arr[j]+" "); } } else { for(int j=0;j<n-1;j+=2) { int temp; temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; count+=2; } int temp=arr[n-1]; arr[n-1]=arr[n-2]; arr[n-2]=temp; count+=2; // System.out.print(count); for(int l=0;l<n;l++) { System.out.print(arr[l]+" "); // System.out.print(" "); } } testcase--; } // } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } public static void print(int[] arr) { //for debugging only for(int x: arr) out.print(x+" "); out.println(); } public static long[] readArr2(int N, BufferedReader infile, StringTokenizer st) throws Exception { long[] arr = new long[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Long.parseLong(st.nextToken()); return arr; } public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static long gcd(long a, long b) { if(a > b) a = (a+b)-(b=a); if(a == 0L) return b; return gcd(b%a, a); } public static long totient(long n) { long result = n; for (int p = 2; p*p <= n; ++p) if (n % p == 0) { while(n%p == 0) n /= p; result -= result/p; } if (n > 1) result -= result/n; return result; /* find phi(i) from 1 to N fast O(N*loglogN) long[] arr = new long[N+1]; for(int i=1; i <= N; i++) arr[i] = i; for(int v=2; v <= N; v++) if(arr[v] == v) for(int a=v; a <= N; a+=v) arr[a] -= arr[a]/v; */ } public static ArrayList<Integer> findDiv(int N) { //gens all divisors of N ArrayList<Integer> ls1 = new ArrayList<Integer>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++) if(N%i == 0) { ls1.add(i); ls2.add(N/i); } Collections.reverse(ls2); for(int b: ls2) if(b != ls1.get(ls1.size()-1)) ls1.add(b); return ls1; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } //custom multiset (replace with HashMap if needed) public static void push(TreeMap<Integer, Integer> map, int k, int v) { //map[k] += v; if(!map.containsKey(k)) map.put(k, v); else map.put(k, map.get(k)+v); } public static void pull(TreeMap<Integer, Integer> map, int k, int v) { //assumes map[k] >= v //map[k] -= v int lol = map.get(k); if(lol == v) map.remove(k); else map.put(k, lol-v); } public static int[] compress(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int boof = 1; //min value for(int x: ls) if(!map.containsKey(x)) map.put(x, boof++); int[] brr = new int[arr.length]; for(int i=0; i < arr.length; i++) brr[i] = map.get(arr[i]); return brr; } public static long[][] multiply(long[][] left, long[][] right) { long MOD = 1000000007L; int N = left.length; int M = right[0].length; long[][] res = new long[N][M]; for(int a=0; a < N; a++) for(int b=0; b < M; b++) for(int c=0; c < left[0].length; c++) { res[a][b] += (left[a][c]*right[c][b])%MOD; if(res[a][b] >= MOD) res[a][b] -= MOD; } return res; } public static long[][] power(long[][] grid, long pow) { long[][] res = new long[grid.length][grid[0].length]; for(int i=0; i < res.length; i++) res[i][i] = 1L; long[][] curr = grid.clone(); while(pow > 0) { if((pow&1L) == 1L) res = multiply(curr, res); pow >>= 1; curr = multiply(curr, curr); } return res; } } class DSU { public int[] dsu; public int[] size; public DSU(int N) { dsu = new int[N+1]; size = new int[N+1]; for(int i=0; i <= N; i++) { dsu[i] = i; size[i] = 1; } } //with path compression, no find by rank public int find(int x) { return dsu[x] == x ? x : (dsu[x] = find(dsu[x])); } public void merge(int x, int y) { int fx = find(x); int fy = find(y); dsu[fx] = fy; } public void merge(int x, int y, boolean sized) { int fx = find(x); int fy = find(y); size[fy] += size[fx]; dsu[fx] = fy; } } class FenwickTree { //Binary Indexed Tree //1 indexed public int[] tree; public int size; public FenwickTree(int size) { this.size = size; tree = new int[size+5]; } public void add(int i, int v) { while(i <= size) { tree[i] += v; i += i&-i; } } public int find(int i) { int res = 0; while(i >= 1) { res += tree[i]; i -= i&-i; } return res; } public int find(int l, int r) { return find(r)-find(l-1); } } class SegmentTree { //Tlatoani's segment tree //iterative implementation = low constant runtime factor //range query, non lazy final int[] val; final int treeFrom; final int length; public SegmentTree(int treeFrom, int treeTo) { this.treeFrom = treeFrom; int length = treeTo - treeFrom + 1; int l; for (l = 0; (1 << l) < length; l++); val = new int[1 << (l + 1)]; this.length = 1 << l; } public void update(int index, int delta) { //replaces value int node = index - treeFrom + length; val[node] = delta; for (node >>= 1; node > 0; node >>= 1) val[node] = comb(val[node << 1], val[(node << 1) + 1]); } public int query(int from, int to) { //inclusive bounds if (to < from) return 0; //0 or 1? from += length - treeFrom; to += length - treeFrom + 1; //0 or 1? int res = 0; for (; from + (from & -from) <= to; from += from & -from) res = comb(res, val[from / (from & -from)]); for (; to - (to & -to) >= from; to -= to & -to) res = comb(res, val[(to - (to & -to)) / (to & -to)]); return res; } public int comb(int a, int b) { //change this return Math.max(a,b); } } class LazySegTree { //definitions private int NULL = -1; private int[] tree; private int[] lazy; private int length; public LazySegTree(int N) { length = N; int b; for(b=0; (1<<b) < length; b++); tree = new int[1<<(b+1)]; lazy = new int[1<<(b+1)]; } public int query(int left, int right) { //left and right are 0-indexed return get(1, 0, length-1, left, right); } private int get(int v, int currL, int currR, int L, int R) { if(L > R) return NULL; if(L <= currL && currR <= R) return tree[v]; propagate(v); int mid = (currL+currR)/2; return comb(get(v*2, currL, mid, L, Math.min(R, mid)), get(v*2+1, mid+1, currR, Math.max(L, mid+1), R)); } public void update(int left, int right, int delta) { add(1, 0, length-1, left, right, delta); } private void add(int v, int currL, int currR, int L, int R, int delta) { if(L > R) return; if(currL == L && currR == R) { //exact covering tree[v] += delta; lazy[v] += delta; return; } propagate(v); int mid = (currL+currR)/2; add(v*2, currL, mid, L, Math.min(R, mid), delta); add(v*2+1, mid+1, currR, Math.max(L, mid+1), R, delta); tree[v] = comb(tree[v*2], tree[v*2+1]); } private void propagate(int v) { //tree[v] already has lazy[v] if(lazy[v] == 0) return; tree[v*2] += lazy[v]; lazy[v*2] += lazy[v]; tree[v*2+1] += lazy[v]; lazy[v*2+1] += lazy[v]; lazy[v] = 0; } private int comb(int a, int b) { return Math.max(a,b); } } class RangeBit { //FenwickTree and RangeBit are faster than LazySegTree by constant factor final int[] value; final int[] weightedVal; public RangeBit(int treeTo) { value = new int[treeTo+2]; weightedVal = new int[treeTo+2]; } private void updateHelper(int index, int delta) { int weightedDelta = index*delta; for(int j = index; j < value.length; j += j & -j) { value[j] += delta; weightedVal[j] += weightedDelta; } } public void update(int from, int to, int delta) { updateHelper(from, delta); updateHelper(to + 1, -delta); } private int query(int to) { int res = 0; int weightedRes = 0; for (int j = to; j > 0; j -= j & -j) { res += value[j]; weightedRes += weightedVal[j]; } return ((to + 1)*res)-weightedRes; } public int query(int from, int to) { if (to < from) return 0; return query(to) - query(from - 1); } } class SparseTable { public int[] log; public int[][] table; public int N; public int K; public SparseTable(int N) { this.N = N; log = new int[N+2]; K = Integer.numberOfTrailingZeros(Integer.highestOneBit(N)); table = new int[N][K+1]; sparsywarsy(); } private void sparsywarsy() { log[1] = 0; for(int i=2; i <= N+1; i++) log[i] = log[i/2]+1; } public void lift(int[] arr) { int n = arr.length; for(int i=0; i < n; i++) table[i][0] = arr[i]; for(int j=1; j <= K; j++) for(int i=0; i + (1 << j) <= n; i++) table[i][j] = Math.min(table[i][j-1], table[i+(1 << (j - 1))][j-1]); } public int query(int L, int R) { //inclusive, 1 indexed L--; R--; int mexico = log[R-L+1]; return Math.min(table[L][mexico], table[R-(1 << mexico)+1][mexico]); } } class LCA { public int N, root; public ArrayDeque<Integer>[] edges; private int[] enter; private int[] exit; private int LOG = 17; //change this private int[][] dp; public LCA(int n, ArrayDeque<Integer>[] edges, int r) { N = n; root = r; enter = new int[N+1]; exit = new int[N+1]; dp = new int[N+1][LOG]; this.edges = edges; int[] time = new int[1]; //change to iterative dfs if N is large dfs(root, 0, time); dp[root][0] = 1; for(int b=1; b < LOG; b++) for(int v=1; v <= N; v++) dp[v][b] = dp[dp[v][b-1]][b-1]; } private void dfs(int curr, int par, int[] time) { dp[curr][0] = par; enter[curr] = ++time[0]; for(int next: edges[curr]) if(next != par) dfs(next, curr, time); exit[curr] = ++time[0]; } public int lca(int x, int y) { if(isAnc(x, y)) return x; if(isAnc(y, x)) return y; int curr = x; for(int b=LOG-1; b >= 0; b--) { int temp = dp[curr][b]; if(!isAnc(temp, y)) curr = temp; } return dp[curr][0]; } private boolean isAnc(int anc, int curr) { return enter[anc] <= enter[curr] && exit[anc] >= exit[curr]; } } class BitSet { private int CONS = 62; //safe public long[] sets; public int size; public BitSet(int N) { size = N; if(N%CONS == 0) sets = new long[N/CONS]; else sets = new long[N/CONS+1]; } public void add(int i) { int dex = i/CONS; int thing = i%CONS; sets[dex] |= (1L << thing); } public int and(BitSet oth) { int boof = Math.min(sets.length, oth.sets.length); int res = 0; for(int i=0; i < boof; i++) res += Long.bitCount(sets[i] & oth.sets[i]); return res; } public int xor(BitSet oth) { int boof = Math.min(sets.length, oth.sets.length); int res = 0; for(int i=0; i < boof; i++) res += Long.bitCount(sets[i] ^ oth.sets[i]); return res; } } class MaxFlow { //Dinic with optimizations (see magic array in dfs function) public int N, source, sink; public ArrayList<Edge>[] edges; private int[] depth; public MaxFlow(int n, int x, int y) { N = n; source = x; sink = y; edges = new ArrayList[N+1]; for(int i=0; i <= N; i++) edges[i] = new ArrayList<Edge>(); depth = new int[N+1]; } public void addEdge(int from, int to, long cap) { Edge forward = new Edge(from, to, cap); Edge backward = new Edge(to, from, 0L); forward.residual = backward; backward.residual = forward; edges[from].add(forward); edges[to].add(backward); } public long mfmc() { long res = 0L; int[] magic = new int[N+1]; while(assignDepths()) { long flow = dfs(source, Long.MAX_VALUE/2, magic); while(flow > 0) { res += flow; flow = dfs(source, Long.MAX_VALUE/2, magic); } magic = new int[N+1]; } return res; } private boolean assignDepths() { Arrays.fill(depth, -69); ArrayDeque<Integer> q = new ArrayDeque<Integer>(); q.add(source); depth[source] = 0; while(q.size() > 0) { int curr = q.poll(); for(Edge e: edges[curr]) if(e.capacityLeft() > 0 && depth[e.to] == -69) { depth[e.to] = depth[curr]+1; q.add(e.to); } } return depth[sink] != -69; } private long dfs(int curr, long bottleneck, int[] magic) { if(curr == sink) return bottleneck; for(; magic[curr] < edges[curr].size(); magic[curr]++) { Edge e = edges[curr].get(magic[curr]); if(e.capacityLeft() > 0 && depth[e.to]-depth[curr] == 1) { long val = dfs(e.to, Math.min(bottleneck, e.capacityLeft()), magic); if(val > 0) { e.augment(val); return val; } } } return 0L; //no flow } private class Edge { public int from, to; public long flow, capacity; public Edge residual; public Edge(int f, int t, long cap) { from = f; to = t; capacity = cap; } public long capacityLeft() { return capacity-flow; } public void augment(long val) { flow += val; residual.flow -= val; } } } class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } //use for codeforces!
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
aadcd2e75d1321f4f35995d4dd235548
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
//package com.company; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number=input.nextInt(); for (int i=0;i<number;i++){ int numbers= input.nextInt(); int[]arr=new int[numbers]; int[]arr1=new int[numbers]; int[]arr2=new int[numbers]; int[]arr3=new int[numbers]; for (int j=0;j<numbers;j++){ arr[j]=j+1; } arr1=arr; arr2=arr; arr3=arr2; int original=arr1[numbers-1]; // System.out.println(Arrays.toString(arr)); for (int m=0;m<numbers;m++){ if (m+1==numbers){ continue; } int test=arr1[m]; arr[m]=arr2[m+1]; arr[m+1]=test; m=m+1; } if (numbers%2==1){ arr[numbers-1]=arr[numbers-2]; arr[numbers-2]=original; } for (int l=0;l<numbers;l++){ System.out.print(arr[l]+" "); } System.out.println(" "); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
d745c9aa2ec5cf577709c32e433a46cd
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public class A { /* */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); //sc.nextLine(); while(T-->0) { int num = sc.nextInt(); if(num%2==0) { for(int i=1;i<=num;i=i+2) { System.out.print(i+1+" "+i+' '); } }else if(num==3) { System.out.print(num + " "+ (num-2)+ " "+ (num-1)); } else { for(int i=1;i<=num-3;i=i+2) { System.out.print(i+1+" "+i+' '); } System.out.print(num + " "+ (num-2)+ " "+ (num-1)); } System.out.println(); } sc.close(); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
3f34d5536b88987cbe893c4f06f25083
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { private static boolean arr[] = sieve(1000001); private static ArrayList<Long> primes = new ArrayList<>(); private static int freq[] = new int[200005]; private static FastScanner c; private static PrintWriter pw; private static int mod = (int) (1e9 + 7); private static final int IMAX = 2147483647; private static final int IMIN = -2147483648; private static final long LMAX = 9223372036854775807L; private static final long LMIN = -9223372036854775808L; private static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() throws FileNotFoundException { if (System.getProperty("os.name").equals("Mac OS X")) { // Input is a file br = new BufferedReader(new FileReader("input.txt")); // PrintWriter class prints formatted representations // of objects to a text-output stream. PrintStream pw = new PrintStream(new FileOutputStream("output.txt")); System.setOut(pw); } else { // Input is System.in br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int i() { return Integer.parseInt(next()); } int[] intArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) ret[i] = i(); return ret; } long l() { return Long.parseLong(next()); } long[] longArray(int n) { long[] ret = new long[n]; for (int i = 0; i < n; i++) ret[i] = l(); return ret; } double d() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // **********************************Code Begins From // Here*************************************** // DecimalFormat df = new DecimalFormat("#.000000000000"); // map.put(key,map.getOrDefault(key,0)+1); public static void solve() { int n = c.i(); if (n == 1) pn("1"); else if (n % 2 == 0) for (int i = 1; i <= n; i += 2) p((i + 1) + " " + i + " "); else { p("3 1 2 "); for (int i = 4; i <= n; i += 2) p((i + 1) + " " + i + " "); } pn(""); } public static void main(String[] args) throws FileNotFoundException { // Scanner sc = new Scanner(System.in); c = new FastScanner(); pw = new PrintWriter(System.out); int tc = c.i(); long start = System.currentTimeMillis(); for (int t = 0; t < tc; t++) { // p("Case #" + (t + 1) + ": "); solve(); } long end = System.currentTimeMillis(); if (System.getProperty("os.name").equals("Mac OS X")) { pn("The Program takes " + (end - start) + "ms"); } // for (long i = 0; i < arr.length; i++) { // if (arr[(int) i]) { // primes.add(i); // } // } pw.close(); } // ArrayList<Integer> al = new ArrayList<>(); // Set<Integer> set = new TreeSet<>(); // Pair<Integer, Integer> pair; // Map<Integer, Integer> map = new HashMap<>(); // for(Map.Entry<Integer,Integer> e:map.entrySet())pn(e.getKey()+" // "+e.getValue()); // LinkedList<Integer> ll = new LinkedList<>(); // math util private static int bs(int[] a, int x) { int start = 0, end = a.length - 1; while (start <= end) { int mid = start + ((end - start) / 2); if (a[mid] == x) return mid; else if (a[mid] < x) start = mid + 1; else end = mid - 1; } return -1; } private static int lower(int a[], int x) { int l = 0, r = a.length - 1, mid, ans = r; while (l <= r) { mid = (l + r) >> 1; if (a[mid] >= x) { ans = mid; r = mid - 1; } else l = mid + 1; } return ans; } private static int upper(int a[], int x) { int l = 0, r = a.length - 1, mid, ans = l; while (l <= r) { mid = (l + r) >> 1; if (a[mid] <= x) { ans = mid; l = mid + 1; } else r = mid - 1; } return ans; } private static int fli(double d) { return (int) d; } private static int cei(double d) { return (int) Math.ceil(d); } private static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } private static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } private static int lcm(int a, int b) { return a * b / gcd(a, b); } private static long lcm(long a, long b) { return a * b / gcd(a, b); } private static int abs(int a, int b) { return (int) Math.abs(a - b); } private static int abs(int a) { return (int) Math.abs(a); } private static long abs(long a) { return (long) Math.abs(a); } private static long abs(long a, long b) { return (long) Math.abs(a - b); } private static int max(int a, int b) { if (a > b) { return a; } else { return b; } } private static int min(int a, int b) { if (a > b) { return b; } else { return a; } } private static long max(long a, long b) { if (a > b) { return a; } else { return b; } } private static long min(long a, long b) { if (a > b) { return b; } else { return a; } } private static long pow(long n, long p, long m) { long result = 1; if (p == 0) { return 1; } while (p != 0) { if (p % 2 == 1) { result *= n; } if (result >= m) { result %= m; } p >>= 1; n *= n; if (n >= m) { n %= m; } } return result; } private static int pow(int base, int exp) { int result = 1; while (exp != 0) { if ((exp & 1) == 1) result *= base; exp >>= 1; base *= base; } return result; } private static long pow(long n, long m) { if (m == 0) return 1; if (m == 1) return n; if (m % 2 == 0) { long ans = pow(n, m / 2); return (ans * ans) % mod; } else { long ans = pow(n, ((m - 1) / 2)); return ((n * ans) % mod * ans) % mod; } } // array util private static void sortList(ArrayList<Integer> al) { Collections.sort(al); } static void reverseList(ArrayList<Integer> al) { Collections.reverse(al); } private static void sort(int[] a) { Arrays.parallelSort(a); } private static void sort(long[] a) { Arrays.parallelSort(a); } private static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } // output private static void pn(Object o) { pw.println(o); } private static void p(Object o) { pw.print(o); } private static void pry() { pn("Yes"); } private static void pY() { pn("YES"); } private static void prn() { pn("No"); } private static void pN() { pn("NO"); } private static void flush() { pw.flush(); } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } private static long factorial(int n) { if (n <= 0) return 1; else return (long) ((n * factorial(n - 1)) % mod); } private static long factorial(long n) { if (n <= 0) return 1L; else return (long) ((n * factorial(n - 1)) % mod); } private static void watch(int[] a) { for (int e : a) p(e + " "); pn(""); } private static void watch(char[] a) { for (char e : a) p(e + " "); pn(""); } private static void watch(long[] a) { for (long e : a) p(e + " "); pn(""); } private static void watchList(ArrayList<Integer> al) { for (int e : al) p(e + " "); pn(""); } private static void watchSet(Set<Integer> set) { for (int e : set) p(e + " "); pn(""); } private static Set<Integer> putSet(int[] a) { Set<Integer> set = new TreeSet<>(); for (int e : a) set.add(e); return set; } private static boolean isPalindromeString(String s) { String temp = ""; for (int i = s.length() - 1; i >= 0; i--) { temp += s.charAt(i) + ""; } return ((s.equals(temp)) ? true : false); } private static boolean isPalindrome(int[] a) { for (int i = 0; i < a.length / 2; i++) { if (a[i] != a[a.length - 1 - i]) return false; } return true; } private static String sortString(String inputString) { // convert input string to Character array Character tempArray[] = new Character[inputString.length()]; for (int i = 0; i < inputString.length(); i++) { tempArray[i] = inputString.charAt(i); } // Sort, ignoring case during sorting Arrays.sort(tempArray, new Comparator<Character>() { @Override public int compare(Character c1, Character c2) { // ignoring case return Character.compare(Character.toLowerCase(c1), Character.toLowerCase(c2)); } }); // using StringBuilder to convert Character array to String StringBuilder sb = new StringBuilder(tempArray.length); for (Character c : tempArray) sb.append(c.charValue()); return sb.toString(); } private static String reverseString(String s, int x) { String ans = "", temp = s.substring(0, x); for (int i = x; i < s.length(); i++) { ans = s.charAt(i) + ans; } return temp + ans; } private static boolean palindrome(int n) { int temp = n, rev = 0; while (n > 0) { int digit = n % 10; n /= 10; rev = rev * 10 + digit; } return (temp == rev) ? true : false; } private static boolean[] sieve(int n) { boolean[] isPrime = new boolean[n + 1]; Arrays.fill(isPrime, true); isPrime[0] = false; isPrime[1] = false; for (int i = 2; i * i <= n; i++) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } return isPrime; } private static String rev(String s) { StringBuilder sb = new StringBuilder(s); sb.reverse(); return sb.toString(); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
c09417d4ccddc0da6f6ba78434364aa7
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class A_Pretty_Premutation_V3 { public static void main(String[] args) { FastReader fr = new FastReader(); PrintWriter pw = new PrintWriter(System.out); int count = fr.nextInt(); while (count-- > 0) { int cat = fr.nextInt(); ArrayList<Integer> arr = createSpace(cat); for (int newCat : arr) { pw.print(newCat + " "); } pw.println(); } pw.close(); } private static ArrayList<Integer> createSpace(int cat) { ArrayList<Integer> space = new ArrayList<Integer>(); for (int index = 0; index < cat; index++) { space.add(index + 1); } Collections.sort(space); if (space.size() == 1) { return space; } if (space.size() == 3) { space.set(0, 3); space.set(1, 1); space.set(2, 2); return space; } if (space.size() % 2 == 0) { for (int index = 0, index2 = 1; index2 < space.size(); index += 2, index2 += 2) { if (index2 <= space.size() - 1) { int a = space.get(index); space.set(index, space.get(index2)); space.set(index2, a); } } } else { //F@king damn! space.set(0, 3); space.set(1, 1); space.set(2, 2); for (int index = 3, index2 = 4; index2 < space.size(); index += 2, index2 += 2) { if (index2 <= space.size() - 1) { int a = space.get(index); space.set(index, space.get(index2)); space.set(index2, a); } } } return space; } static class FastReader { private BufferedReader br; private StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
07a69a6ec9231476be60943132c3c093
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class prettyperm { public static void main(String args[]) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-- > 0) { int n = s.nextInt(); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] = i+1; } for(int i=0;i<n-1;i=i+2) { int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } if(n%2==1) { int temp = a[n-1]; a[n-1] = a[n-2]; a[n-2] = temp; } for(int i=0;i<n;i++) { System.out.print(a[i] + " "); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
87c4adb0ee80ce68604478cc46525c4f
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
/** * @author Luminous * @date 2022/1/2 20:59 */ import java.io.*; import java.math.*; import java.util.*; public class Main { static final int INF = 0x3f3f3f3f; static final long LNF = 0x3f3f3f3f3f3f3f3fL; public static void main(String[] args) throws IOException { initReader(); int T = nextInt(); for (int i = 1; i <= T; i++) solve(); pw.flush(); } /***************************************************************************************/ public static void solve() throws IOException { int n = nextInt(); if ((n & 1) == 1) { pw.print("3 1 2 "); for (int i = 4; i <= n; i += 2) { pw.print((i + 1) + " " + i + " "); } pw.println(); } else { for (int i = 1; i <= n; i += 2) { pw.print((i + 1) + " " + i + " "); } pw.println(); } } /***************************************************************************************/ /***************************************************************************************/ static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter pw; public static void initReader() throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = new StringTokenizer(""); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); // 从文件读写 // reader = new BufferedReader(new FileReader("test.in")); // tokenizer = new StringTokenizer(""); // pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out"))); } public static boolean hasNext() { try { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (Exception e) { return false; } return true; } public static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static String nextLine() { try { return reader.readLine(); } catch (Exception e) { return null; } } public static int nextInt() throws IOException { return Integer.parseInt(next()); } public static long nextLong() throws IOException { return Long.parseLong(next()); } public static double nextDouble() throws IOException { return Double.parseDouble(next()); } public static char nextChar() throws IOException { return next().charAt(0); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
2d6c16b9704edad71475f70a15979a77
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class Main { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { try { Slove(); } catch(Exception e){ System.exit(0); } } public static void Slove() { int t = sc.nextInt(); while(t -- > 0) { int n = sc.nextInt(); int[] a = new int[n]; for(int i=0; i<a.length; i++) { a[i] = i+1; } if(n % 2 == 0) { for(int i=0; i<n; i+=2) { int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } for(int i=0; i<a.length; i++) { System.out.print(a[i]+" "); } } else { for(int i=0; i<n-1; i+=2) { int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } int temp = a[n-1]; a[n-1] = a[n-2]; a[n-2] = temp; for(int i=0; i<a.length; i++) { System.out.print(a[i]+" "); } } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
d2db5658c955e7755d52e04bb934a886
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class CPEB20211007_E { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); while(n-->0) { int x =sc.nextInt(); if(x%2==1) { System.out.print(3+" "); System.out.print(1+" "); System.out.print(2+" "); if(x>3) { for(int i=4;i<=x;i+=2) { System.out.print((i+1)+" "); System.out.print(i+" "); } } System.out.println(); }else { for(int i=1;i<x;i+=2) { System.out.print((i+1)+" "); System.out.print(i+" "); } System.out.println(); } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
cbc2c53106ce30cf3005dce63597a8f2
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
/* Rohit CF Target100 questions 25 days*/ import java.util.*; public class Solution{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); if(n%2==0) { for(int i=1;i<=n;i+=2) { System.out.print((i+1)+" "+i+" "); } } else{ System.out.print("3 1 2 "); for(int i=4;i<=n;i+=2) { System.out.print((i+1)+" "+i+" "); } } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
11edbf5805e6406e39cf1beea8a23c25
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class PrettyPermutations { static Scanner scn = new Scanner(System.in); public static void main(String[] args) { int t = scn.nextInt(); while(t-- >0) { int n = scn.nextInt(); int i = 1; if (n % 2 == 0) { while (i <= n) { System.out.print((i + 1) + " "); System.out.print(i + " "); i += 2; } } else { if(n>=3) { System.out.print("3 1 2 "); } i = 4; while (i <= n) { System.out.print((i + 1) + " "); System.out.print(i + " "); i += 2; } } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
f746957b85292dae7becd38251b4e074
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class cf { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i=0; i<n; i++) { int t = sc.nextInt(); if(t%2==0) { for(int j=1; j<=t; j+=2) System.out.print(j+1 + " " + j + " "); } else { int j = 1; for(; j<t-2; j+=2) System.out.print(j+1 + " " + j + " "); int arr[] = new int[3]; arr[0] = j+2; arr[1] = j; arr[2] = j+1; System.out.print(arr[0] + " " + arr[1] + " " + arr[2]); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
f11ff4d8ade3ad4b079fdeefe9ffbadd
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { // write your code here Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t>0){ int n = sc.nextInt(); // for even no. of cats if(n%2 == 0){ for(int i=1; i<=n; i++){ // every odd cat -> print next cat(even) if(i%2==1){ System.out.print(i+1 + " "); } // every even cat -> print prev cat(odd) else{ System.out.print(i-1 + " "); } } } // for odd no. of cats -> leave out last 3 cats else{ for(int i=1; i<n-2; i++){ if(i%2==1){ System.out.print(i+1 + " "); } else{ System.out.print(i-1 + " "); } } // swapping last 3 cats System.out.print((n-1) + " " + (n) + " " + (n-2)); } System.out.println(""); t--; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
c5e1672c4bc5b760f9c03489c8019159
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; //import template.Template.FastScanner; public class Contest728 { public static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next () { while(!st.hasMoreTokens()) { try { st=new StringTokenizer(br.readLine()); }catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] = nextInt(); } return a; } long nextLong() { return Long.parseLong(next()); } } //prime sieve public static boolean sieve[]; public static void sievePrime(int n) { sieve = new boolean[n+1]; for(int i=2;i*i<=n;i++) { if(sieve[i] == false) { //means prime hai for(int j=i+i;j<sieve.length;j+= i) { sieve[j] = true; } } } } //swap public static void swap(int a,int b) { int temp= a; a=b; b=temp; } //Longest Palindromic String public String manacher(String s){ StringBuilder sb = new StringBuilder(); sb.append("@"); for(int i=0;i<s.length();i++){ sb.append("#"); sb.append(s.charAt(i)); } sb.append("#"); sb.append("$"); String ans = manacher_Util(sb.toString(),s); return ans; } public String manacher_Util(String s,String org){ int lps[] = new int[s.length()]; int c = 0; int r = 0; for(int i=1;i<s.length()-1;i++){ int mirror = c - (i-c); if(i<r){ lps[i] = Math.min(lps[mirror],r-i); } while(s.charAt(1+i+lps[i]) == s.charAt(i-1-lps[i])){ lps[i]++; } if(i+lps[i] > r){ c= i ; r = i+lps[i]; } } int maxlen = Integer.MIN_VALUE; int maxindex = Integer.MIN_VALUE; for(int i=1;i<lps.length-1;i++){ if(lps[i] > maxlen){ maxlen = lps[i]; maxindex = i; } } int firstindex = maxindex - maxlen + 1; int actualFirstIndex = (firstindex-2)/2; return org.substring(actualFirstIndex,actualFirstIndex+maxlen); } //kmp public static int lps[]; public static void kmp(String s) { lps = new int[s.length()]; int i = 1; int len = 0; while(i<s.length()) { if(s.charAt(i) == s.charAt(len)) { len++; lps[i] = len; i++; } else { if(len>0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } } public static void main(String[] args) { FastScanner sc = new FastScanner(); int test = sc.nextInt(); PrintWriter o = new PrintWriter(System.out); while(test-->0) { int n = sc.nextInt(); if(n%2==0) { for(int i = 1;i<=n;i = i+2) { System.out.print((i+1)+" "+i+" "); } System.out.println(); }else { for(int i = 1;i<n-2;i = i+2) { System.out.print((i+1)+" "+i+" "); } System.out.print(n-1+" "); System.out.print(n+" "); System.out.print(n-2+" "); } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
cfb76ce2372574e3638681f5c7896481
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
// Problem: A. Pretty Permutations // Contest: Codeforces - Codeforces Round #728 (Div. 2) // URL: https://codeforces.com/contest/1541/problem/A // Memory Limit: 256 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws Exception{ sc=new MScanner(System.in); pw = new PrintWriter(System.out); int tc=sc.nextInt(); while(tc-- >0) { int n = sc.nextInt(); if(n%2==0) { for(int i=1;i<n;i=i+2) { pw.print(i+1+" "+i+" "); } } else { pw.print(3+" "+1+" "+2+" "); for(int i=4;i<n;i=i+2) { pw.print(i+1+" "+i+" "); } } pw.println(); } pw.flush(); // 2,1,5,3,4 } static PrintWriter pw; static MScanner sc; static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int[] intArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public long[] longArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public Integer[] IntegerArr(int n) throws IOException { Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public Long[] LongArr(int n) throws IOException { Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
d7421cef62a01282ff51282c087db0fc
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; /** * @author jeetscmaker * contest: Codeforces Round #728 (Div. 2), A. Pretty Permutations */ public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); short t = sc.nextShort(); for (int tt = 0; tt < t; tt++) { short n = sc.nextShort(); if (n % 2 == 0) { for (int i = 1, j = 2; i < n && j <= n; i += 2, j += 2) { System.out.print(j + " " + i + " "); } System.out.println(); } else { System.out.print(3 + " " + 1 + " " + 2 + " "); for (int i = 4; i < n; i += 2) { System.out.print((i + 1) + " " + (i) + " "); } System.out.println(); } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
6da0b307ac84a80303a48b1eac5da2ee
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; /** * @author jeetscmaker * contest: Codeforces Round #728 (Div. 2), A. Pretty Permutations */ public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); short t = sc.nextShort(); for (int tt = 0; tt < t; tt++) { short n = sc.nextShort(); if (n % 2 == 0) { for (int i = 1, j = 2; i < n && j <= n; i += 2, j += 2) { System.out.print(j + " " + i + " "); } System.out.println(); } else { short[] a = new short[n]; for (int i = 0; i < n; i++) { a[i] = (short) (i + 1); } a[0] = 3; a[1] = 1; a[2] = 2; System.out.print(a[0] + " " + a[1] + " " + a[2] + " "); for (int i = 3; i < n; i += 2) { System.out.print(a[i + 1] + " " + a[i] + " "); } System.out.println(); } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
5afe0e90ecec87b3f60427bfead129dd
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class Matr { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc= new Scanner(System.in); int t=sc.nextInt(); while(t--!=0) { int n=sc.nextInt(); int a[]=new int[n]; a[0]=n; int k=0; if(n%2!=0) { a[0]=3; a[1]=1; a[2]=2; for(int i=3;i<n;i++) { if(i%2!=0)a[i]=i+2; else a[i]=i; } } else { for(int i=1;i<=n;i++) { if(i%2!=0)a[i-1]=i+1; else a[i-1]=i-1; } } for(int i=0;i<n;i++) { System.out.print(a[i]+" "); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
3d679104659d1ef9751260f5345d518c
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class PrettyPermutations { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0){ int n = sc.nextInt(); int a[] = new int[n]; for(int i = 0; i < n; i++){ a[i] = i+1; } if(n%2 == 1){ a[0] = 3; a[1] = 1; a[2] = 2; shuffle(a, 3); }else{ shuffle(a, 0); } for (int i : a) { System.out.print(i + " "); } System.out.println(); } } public static void shuffle(int[] a, int i) { for(int j = i; j < a.length-1; j += 2){ int t = a[j]; a[j] = a[j+1]; a[j+1] = t; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
23d5e59c181c222f09413d1b32b8aed1
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
//package com.company; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Prob_1A { static void swap(int[] arr, int x, int y){ int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } static void Print(int[] arr,int n){ for(int i=0; i< n;i++){ System.out.print(arr[i] + " "); } System.out.println(""); } public static void main(String[] args)throws IOException { InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); int t = Integer.parseInt(br.readLine()); int n; int[] arr; for(int i=0; i<t;i++){ n = Integer.parseInt(br.readLine()); arr = new int[n]; for(int j=0; j<=n-1;j++){ arr[j] = j+1; } if(n>3) { int x=0; if(n%2==0){ while(x<n-1){ swap(arr,x,x+1); x+=2; } } int k = 0; if (n % 2 == 1) { while (k < 2) { swap(arr, k, k + 1); k++; } int a=3; while(a<n-1) { swap(arr, a, a+1); a+=2; } } } else{ int y=0; while(y<n-1){ swap(arr,y,y+1); y++; } } Print(arr,n); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output