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
4442f5ff480cf467ad8e7bf13e44f07e
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.*; import java.lang.*; public class Main2 implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public 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++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main2(), "Main", 1 << 27).start(); } static class Pair { int f; int s; int p; PrintWriter w; // int t; Pair(int f, int s) { // Pair(int f,int s, PrintWriter w){ this.f = f; this.s = s; // this.p = p; // this.w = w; // this.t = t; } public static Comparator<Pair> wc = new Comparator<Pair>() { public int compare(Pair e1,Pair e2){ // 1 for swap if(Math.abs(e1.f)-Math.abs(e2.f)!=0){ // e1.w.println("**"+e1.f+" "+e2.f); return (Math.abs(e1.f)-Math.abs(e2.f)); } else{ // e1.w.println("##"+e1.f+" "+e2.f); return (Math.abs(e1.s)-Math.abs(e2.s)); } } }; } public Integer[] sort(Integer[] a) { Arrays.sort(a); return a; } public Long[] sort(Long[] a) { Arrays.sort(a); return a; } public static ArrayList<Integer> sieve(int N) { int i, j, flag; ArrayList<Integer> p = new ArrayList<Integer>(); for (i = 1; i < N; i++) { if (i == 1 || i == 0) continue; flag = 1; for (j = 2; j <= i / 2; ++j) { if (i % j == 0) { flag = 0; break; } } if (flag == 1) { p.add(i); } } return p; } public static long gcd(long a, long b) { if (b == 0) return a; else return gcd(b, a % b); } public static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } //// recursive dfs public static int dfs(int s, ArrayList<Integer>[] g, long[] dist, boolean[] v, PrintWriter w, int p) { v[s] = true; int ans = 1; // int n = dist.length - 1; int t = g[s].size(); // int max = 1; for (int i = 0; i < t; i++) { int x = g[s].get(i); if (!v[x]) { // dist[x] = dist[s] + 1; ans = Math.min(ans, dfs(x, g, dist, v, w, s)); } else if (x != p) { // w.println("* " + s + " " + x + " " + p); ans = 0; } } // max = Math.max(max,(n-p)); return ans; } //// iterative BFS public static int bfs(int s, ArrayList<Integer>[] g, long[] dist, boolean[] b, PrintWriter w, int p) { b[s] = true; int siz = 1; // dist--; Queue<Integer> q = new LinkedList<>(); q.add(s); while (q.size() != 0) { int i = q.poll(); Iterator<Integer> it = g[i].listIterator(); int z = 0; while (it.hasNext()) { z = it.next(); if (!b[z]) { b[z] = true; // dist--; dist[z] = dist[i] + 1; // siz++; q.add(z); } else if (z != p) { siz = 0; } } } return siz; } public static int lower(int a[], int x) { // x is the target value or key int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } public static int upper(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; } public static int lower(ArrayList<Integer> a, int x) { // x is the target value or key int l = -1, r = a.size(); while (l + 1 < r) { int m = (l + r) >>> 1; if (a.get(m) >= x) r = m; else l = m; } return r; } public static int upper(ArrayList<Integer> a, int x) {// x is the key or target value int l = -1, r = a.size(); while (l + 1 < r) { int m = (l + r) >>> 1; if (a.get(m) <= x) l = m; else r = m; } return l + 1; } public static long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (p * p) % m; if (y % 2 == 0) return p; else return (x * p) % m; } public void yesOrNo(boolean f) { if (f) { w.println("YES"); } else { w.println("NO"); } } // Arrays.sort(rooms, (room1, room2) -> room1[1] == room2[1] ? room1[0]-room2[0] // : room2[1]-room1[1]); // Arrays.sort(A, (a, b) -> Integer.compare(a[0] , b[0])); ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// // code here int oo = (int) 1e9; int[] parent; int[] dist; int[][] val; boolean[][] vis; ArrayList<Integer>[] g; // int[] col; // HashMap<Long, Boolean>[] dp; //char[][] g; // boolean[][] v; Long[] a; // ArrayList<Integer[]> a; // int[][] ans; long[][] dp; long mod; int n; int m; int k; long[][] pre; // StringBuilder[] a; // StringBuilder[] b; // StringBuilder ans; int[][] col; int[][] row; PrintWriter w = new PrintWriter(System.out); public void run() { InputReader sc = new InputReader(System.in); int defaultValue = 0; mod = 1000000007; int test = 1; test = sc.nextInt(); while (test-- > 0) { int n =sc.nextInt(); int[] arr = new int[n]; for(int i=1;i<=n;i++){ arr[i-1] =i; } if(n%2==0) { for (int i = 1; i < n; i += 2) { int temp = arr[i]; arr[i] = arr[i - 1]; arr[i - 1] = temp; } } if(n%2!=0) { for (int i = 1; i < n-1; i += 2) { int temp = arr[i]; arr[i] = arr[i - 1]; arr[i - 1] = temp; } int temp = arr[n-1]; arr[n-1] = arr[n-2]; arr[n-2] = temp; } for(int i=0;i<n;i++){ System.out.print(arr[i] +" "); } System.out.println(); } w.flush(); w.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
14bb2ea098bf22c7b8cb87d095bce9fb
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.*; import java.lang.*; public class Main2 implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public 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++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main2(), "Main", 1 << 27).start(); } static class Pair { int f; int s; int p; PrintWriter w; // int t; Pair(int f, int s) { // Pair(int f,int s, PrintWriter w){ this.f = f; this.s = s; // this.p = p; // this.w = w; // this.t = t; } public static Comparator<Pair> wc = new Comparator<Pair>() { public int compare(Pair e1,Pair e2){ // 1 for swap if(Math.abs(e1.f)-Math.abs(e2.f)!=0){ // e1.w.println("**"+e1.f+" "+e2.f); return (Math.abs(e1.f)-Math.abs(e2.f)); } else{ // e1.w.println("##"+e1.f+" "+e2.f); return (Math.abs(e1.s)-Math.abs(e2.s)); } } }; } public Integer[] sort(Integer[] a) { Arrays.sort(a); return a; } public Long[] sort(Long[] a) { Arrays.sort(a); return a; } public static ArrayList<Integer> sieve(int N) { int i, j, flag; ArrayList<Integer> p = new ArrayList<Integer>(); for (i = 1; i < N; i++) { if (i == 1 || i == 0) continue; flag = 1; for (j = 2; j <= i / 2; ++j) { if (i % j == 0) { flag = 0; break; } } if (flag == 1) { p.add(i); } } return p; } public static long gcd(long a, long b) { if (b == 0) return a; else return gcd(b, a % b); } public static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } //// recursive dfs public static int dfs(int s, ArrayList<Integer>[] g, long[] dist, boolean[] v, PrintWriter w, int p) { v[s] = true; int ans = 1; // int n = dist.length - 1; int t = g[s].size(); // int max = 1; for (int i = 0; i < t; i++) { int x = g[s].get(i); if (!v[x]) { // dist[x] = dist[s] + 1; ans = Math.min(ans, dfs(x, g, dist, v, w, s)); } else if (x != p) { // w.println("* " + s + " " + x + " " + p); ans = 0; } } // max = Math.max(max,(n-p)); return ans; } //// iterative BFS public static int bfs(int s, ArrayList<Integer>[] g, long[] dist, boolean[] b, PrintWriter w, int p) { b[s] = true; int siz = 1; // dist--; Queue<Integer> q = new LinkedList<>(); q.add(s); while (q.size() != 0) { int i = q.poll(); Iterator<Integer> it = g[i].listIterator(); int z = 0; while (it.hasNext()) { z = it.next(); if (!b[z]) { b[z] = true; // dist--; dist[z] = dist[i] + 1; // siz++; q.add(z); } else if (z != p) { siz = 0; } } } return siz; } public static int lower(int a[], int x) { // x is the target value or key int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } public static int upper(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; } public static int lower(ArrayList<Integer> a, int x) { // x is the target value or key int l = -1, r = a.size(); while (l + 1 < r) { int m = (l + r) >>> 1; if (a.get(m) >= x) r = m; else l = m; } return r; } public static int upper(ArrayList<Integer> a, int x) {// x is the key or target value int l = -1, r = a.size(); while (l + 1 < r) { int m = (l + r) >>> 1; if (a.get(m) <= x) l = m; else r = m; } return l + 1; } public static long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (p * p) % m; if (y % 2 == 0) return p; else return (x * p) % m; } public void yesOrNo(boolean f) { if (f) { w.println("YES"); } else { w.println("NO"); } } // Arrays.sort(rooms, (room1, room2) -> room1[1] == room2[1] ? room1[0]-room2[0] // : room2[1]-room1[1]); // Arrays.sort(A, (a, b) -> Integer.compare(a[0] , b[0])); ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// // code here int oo = (int) 1e9; int[] parent; int[] dist; int[][] val; boolean[][] vis; ArrayList<Integer>[] g; // int[] col; // HashMap<Long, Boolean>[] dp; //char[][] g; // boolean[][] v; Long[] a; // ArrayList<Integer[]> a; // int[][] ans; long[][] dp; long mod; int n; int m; int k; long[][] pre; // StringBuilder[] a; // StringBuilder[] b; // StringBuilder ans; int[][] col; int[][] row; PrintWriter w = new PrintWriter(System.out); public void run() { InputReader sc = new InputReader(System.in); int defaultValue = 0; mod = 1000000007; int test = 1; test = sc.nextInt(); while (test-- > 0) { int n =sc.nextInt(); for(int i=1;i<n-2;i+=2) System.out.print((i+1)+" "+(i)+" "); if(n%2 ==0){ System.out.println(n+" "+(n-1)); }else{ System.out.println(n+" "+(n-2)+" "+(n-1)); } } w.flush(); w.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
c53b3e410262e88cc10b6f3238c4c608
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.*; import java.lang.*; public class Main2 implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public 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++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main2(), "Main", 1 << 27).start(); } static class Pair { int f; int s; int p; PrintWriter w; // int t; Pair(int f, int s) { // Pair(int f,int s, PrintWriter w){ this.f = f; this.s = s; // this.p = p; // this.w = w; // this.t = t; } public static Comparator<Pair> wc = new Comparator<Pair>() { public int compare(Pair e1,Pair e2){ // 1 for swap if(Math.abs(e1.f)-Math.abs(e2.f)!=0){ // e1.w.println("**"+e1.f+" "+e2.f); return (Math.abs(e1.f)-Math.abs(e2.f)); } else{ // e1.w.println("##"+e1.f+" "+e2.f); return (Math.abs(e1.s)-Math.abs(e2.s)); } } }; } public Integer[] sort(Integer[] a) { Arrays.sort(a); return a; } public Long[] sort(Long[] a) { Arrays.sort(a); return a; } public static ArrayList<Integer> sieve(int N) { int i, j, flag; ArrayList<Integer> p = new ArrayList<Integer>(); for (i = 1; i < N; i++) { if (i == 1 || i == 0) continue; flag = 1; for (j = 2; j <= i / 2; ++j) { if (i % j == 0) { flag = 0; break; } } if (flag == 1) { p.add(i); } } return p; } public static long gcd(long a, long b) { if (b == 0) return a; else return gcd(b, a % b); } public static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } //// recursive dfs public static int dfs(int s, ArrayList<Integer>[] g, long[] dist, boolean[] v, PrintWriter w, int p) { v[s] = true; int ans = 1; // int n = dist.length - 1; int t = g[s].size(); // int max = 1; for (int i = 0; i < t; i++) { int x = g[s].get(i); if (!v[x]) { // dist[x] = dist[s] + 1; ans = Math.min(ans, dfs(x, g, dist, v, w, s)); } else if (x != p) { // w.println("* " + s + " " + x + " " + p); ans = 0; } } // max = Math.max(max,(n-p)); return ans; } //// iterative BFS public static int bfs(int s, ArrayList<Integer>[] g, long[] dist, boolean[] b, PrintWriter w, int p) { b[s] = true; int siz = 1; // dist--; Queue<Integer> q = new LinkedList<>(); q.add(s); while (q.size() != 0) { int i = q.poll(); Iterator<Integer> it = g[i].listIterator(); int z = 0; while (it.hasNext()) { z = it.next(); if (!b[z]) { b[z] = true; // dist--; dist[z] = dist[i] + 1; // siz++; q.add(z); } else if (z != p) { siz = 0; } } } return siz; } public static int lower(int a[], int x) { // x is the target value or key int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } public static int upper(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; } public static int lower(ArrayList<Integer> a, int x) { // x is the target value or key int l = -1, r = a.size(); while (l + 1 < r) { int m = (l + r) >>> 1; if (a.get(m) >= x) r = m; else l = m; } return r; } public static int upper(ArrayList<Integer> a, int x) {// x is the key or target value int l = -1, r = a.size(); while (l + 1 < r) { int m = (l + r) >>> 1; if (a.get(m) <= x) l = m; else r = m; } return l + 1; } public static long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (p * p) % m; if (y % 2 == 0) return p; else return (x * p) % m; } public void yesOrNo(boolean f) { if (f) { w.println("YES"); } else { w.println("NO"); } } // Arrays.sort(rooms, (room1, room2) -> room1[1] == room2[1] ? room1[0]-room2[0] // : room2[1]-room1[1]); // Arrays.sort(A, (a, b) -> Integer.compare(a[0] , b[0])); ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// // code here int oo = (int) 1e9; int[] parent; int[] dist; int[][] val; boolean[][] vis; ArrayList<Integer>[] g; // int[] col; // HashMap<Long, Boolean>[] dp; //char[][] g; // boolean[][] v; Long[] a; // ArrayList<Integer[]> a; // int[][] ans; long[][] dp; long mod; int n; int m; int k; long[][] pre; // StringBuilder[] a; // StringBuilder[] b; // StringBuilder ans; int[][] col; int[][] row; PrintWriter w = new PrintWriter(System.out); public void run() { InputReader sc = new InputReader(System.in); int defaultValue = 0; mod = 1000000007; int test = 1; test = sc.nextInt(); while (test-- > 0) { int n =sc.nextInt(); for(int i=1;i<n-2;i+=2) System.out.print((i+1)+" "+(i)+" "); if(n%2 ==0){ System.out.println(n+" "+(n-1)); }else{ System.out.println(n+" "+(n-2)+" "+(n-1)); } } w.flush(); w.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
9630c081b4f1de5f10c536516ebf260e
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 permutations { public static void main(String args[]) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int t = Integer.parseInt(f.readLine()); for(int i = 0; i < t; i++) { int n = Integer.parseInt(f.readLine()); // ArrayList<Integer> cats = new ArrayList<Integer>(); // // for(int j = 0; j < n; j++) // { // cats.add(j); // } // int[] solution = permute(new int[n], 0, cats); // for(int j = 0; j < n; j++) // { // out.print((solution[j]+1) + " "); // } // out.println(); if(n % 2 == 1) { out.print("3 1 2 "); for(int j = 4; j < n; j +=2) { out.print((j + 1) + " " + j + " "); } } else { for(int j = 1; j < n; j +=2) { out.print((j + 1) + " " + j + " "); } } out.println(); } out.close(); } public static int[] permute(int[] order, int index, ArrayList<Integer> nums) { if(index == order.length) { return order; } int min = Integer.MAX_VALUE; int[] minorder = new int[0]; for(int i = 0; i < nums.size(); i++) { if(i != index) { order[index] = nums.get(i); nums.remove(i); int[] theorder = permute(order, index + 1, nums); int ans = findTotalDistance(theorder); if(ans < min) { min = ans; minorder = theorder; } nums.add(i, order[index]); } } return minorder; } public static int findTotalDistance(int[] order) { int total = 0; for(int i = 0; i < order.length; i++) { int dist = Math.abs(i - order[i]); total += dist; } return total; } }
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
2b36b89ad581bcff7d4425b519ba1aae
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.IOException; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t>0){ int x = sc.nextInt(); int [] arr = new int[x]; if(x==0){ System.out.println(0); } for(int i=0;i<x;i++){ arr[i] = i+1; } if(x%2!=0){ for(int i=0;i<x-2;i++){ int temp = arr[i+1]; arr[i+1] = arr[i]; arr[i] = temp; i++; } int tt = arr[x-1]; arr[x-1] = arr[x-2]; arr[x-2] = tt; }else{ for(int i=0;i<x-1;i++){ int temp = arr[i+1]; arr[i+1] = arr[i]; arr[i] = temp; i++; } } for(int j :arr){ System.out.print(j+" "); } 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
0d82eaa9c4c42f8edb5a5f8de3bbdf3e
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) { Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int tc = sc.nextInt(); while (tc-- > 0) { // int n = sc.nextInt(); // List<Integer> list = new ArrayList<>(); // for (int i = 0 ; i < n; i++) { // list.add(sc.nextInt()); // } int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 1; i <= n; i++) { arr[i - 1] = i; } if (n % 2 == 0) { for (int i = 0; i < n - 1; i += 2) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } else { arr[0] = 3; arr[1] = 1; arr[2] = 2; for (int i = 3; i < n - 1; i += 2) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } for (int i : arr) { sb.append(i + " "); } sb.append("\n"); } System.out.println(sb.toString()); 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
2a44d16bfd929744f9d11580022e2928
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 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()); } String nextLine(){ String sr = ""; try{ sr = br.readLine(); } catch(IOException e){ e.printStackTrace(); } return sr; } } public static void main(String args[]) throws Exception{ FastReader ft = new FastReader(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t = ft.nextInt(); while(t-->0){ int n = ft.nextInt(); if((n&1)==0) { for(int i = 1;i <= n; i+= 2) out.write((i+1)+" "+i+" "); } else{ for(int i = 1;i<(n-2);i+=2) out.write((i+1)+" "+i+" "); out.write(n+" "+(n-2)+" "+(n-1)); } out.write("\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 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
8488960bfad426ef580c5b850ec56971
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) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //BufferedReader br = new BufferedReader(new FileReader("src/input.txt")); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); for (int i = 0; i < t; i++) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); if (n % 2 == 0) { System.out.print(2); for (int x = 1; x < n; x++) { if (x % 2 == 0) { System.out.print(" " + (x + 2)); } else { System.out.print(" " + x); } } } else { int savepos = -1; for (int x = 0; x < n - 3; x++) { if (x % 2 == 0) { System.out.print((x + 2) + " "); } else { System.out.print(x + " "); } savepos = x; } System.out.print((savepos + 4) + " " + (savepos + 2) + " " + (savepos + 3)); } if (i < t - 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 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
0abdb94fdb76c7147368e2447588f1cd
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.*; import java.lang.*; public class Codeforces { 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) { FastReader s = new FastReader(); int t = s.nextInt(); try{ while (t>0) { int max=s.nextInt(); if(max%2==0){ for(int i=1;i<=max;i++) { if(i==max) System.out.println(i -1); else { if(i%2!=0) System.out.print(i + 1 + " "); else System.out.print(i-1+ " "); } } } else { int j=1; for(int i=1;i<=max-3;i++) { if(i%2!=0) System.out.print(i + 1 + " "); else System.out.print(i-1+ " "); j++; } System.out.print(max+" "); System.out.print(max-2+" " ); System.out.println(max-1); } t--; } } catch (Exception e) { } } }
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
8b60224764f3d560419c61fdd5b66b98
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 codeforces { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ boolean f = false; int n = sc.nextInt(); StringBuilder sb = new StringBuilder(); if(n%2==0) { for (int i = 1; i <= n; i++) { if (!f) sb.append((i + 1) + " "); else sb.append((i - 1) + " "); f = !f; } }else{ sb.append(3 + " "); sb.append(1 + " "); sb.append(2 + " "); for (int i = 4; i <= n; i++) { if (!f) sb.append((i + 1) + " "); else sb.append((i - 1) + " "); f = !f; } } System.out.println(sb); } } }
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
9828ba5b285710d0a6dc4f7d4ca97f71
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 DiagonalTraversal { public static void main(String[] args) { Scanner ref = new Scanner(System.in); int t = ref.nextInt(); while (t-- > 0) { long n = ref.nextLong(); if (n % 2 == 0) { for (int i = 1; i < n; i = i + 2) { System.out.print((i + 1) + " " + (i) + " "); } } else { if (n > 3) { for (int i = 1; i < n - 3; i = i + 2) { System.out.print((i + 1) + " " + (i) + " "); } } System.out.print((n) + " " + (n - 2) + " " + (n - 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 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
c43b5f0421ddfda1b2c7e5339f229c99
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){ Scanner scn=new Scanner (System.in); int t=scn.nextInt(); for(int k=1;k<=t;k++){ int noc=scn.nextInt(); int[]arr=new int[noc]; for(int i=0;i<noc;i++){ arr[i]=i+1; } if(noc%2==0){ int i=0; int j=1; while(j<noc){ int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; i=i+2; j=j+2; } }else{ int temporary=arr[0]; arr[0]=arr[1]; arr[1]=arr[2]; arr[2]=temporary; int i=3; int j=4; while( j<noc){ int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; i=i+2; j=j+2; } } for(int i=0;i<noc;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 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
8959fb88fef7365cc459bbf6124f19f2
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(); int[] a = new int[n]; int start = 0; if ((n & 1) == 1) { a[0] = 3; a[1] = 1; a[2] = 2; start = 3; } for (int i = start; i < n - 1; i+=2) { a[i] = i + 2; 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
c7e4033e39cd7c96148637824e9bb98d
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(); 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) { if (i >= n - 3 && n % 2 == 1) { int tmp1 = a[n - 3]; int tmp2 = a[n - 2]; a[n - 3] = a[n - 1]; a[n - 2] = tmp1; a[n - 1] = tmp2; break; } else { int tmp = a[i]; a[i] = a[i + 1]; a[i + 1] = tmp; } } 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
b53163dae168f2004fad9c651326f680
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(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = i + 1; } if (n % 2 == 0) { for (int i = 0; i < n - 1; i += 2) { int tmp = a[i]; a[i] = a[i + 1]; a[i + 1] = tmp; } } else { for (int i = 0; i < n - 3; i += 2) { int tmp = a[i]; a[i] = a[i + 1]; a[i + 1] = tmp; } int tmp1 = a[n - 3]; int tmp2 = a[n - 2]; a[n - 3] = a[n - 1]; a[n - 2] = tmp1; a[n - 1] = tmp2; } 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
5b8caf77e46a9ed128800c57457d7c93
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 arg[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); if(n % 2 == 0) System.out.println(generate(n)); else System.out.println("3 1 2 " + generate(n)); } } static String generate(int n) { String ans = ""; int x = 1; if(n % 2 != 0) x = 4; while(x <= n) { ans = ans + " " + (x + 1); ans = ans + " " + x; x = x + 2; } return ans.trim(); } }
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
40d55d4e7e6874c2523565286468e761
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 Solution{ 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 char nextChar() { return next().toCharArray()[0]; } } public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] arr = new int[n]; if (n % 2 == 0) { int switcher = -1; for (int i = 0; i < n; i++) { if(switcher == -1){ arr[i] = i+2; switcher *= -1; } else if(switcher == 1){ arr[i] = i; switcher *= -1; } } } else if(n%2==1){ int set = n-3; int switcher = -1; for (int i = 0; i < set; i++) { if(switcher == -1){ arr[i] = i+2; switcher *= -1; } else if(switcher == 1){ arr[i] = i; switcher *=-1; } } arr[set] = set+3; arr[set+1] = set+1; arr[set+2] = set+2; } for (int i : arr) { 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
1b29dab8b3882464026fc8a2c720aca8
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
//credit for scanner https://gist.github.com/alsuga/85ff07d7a080ec06c762 import java.io.*; import java.util.*; public class MyClass { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i = 0; i < t; i++){ int numOfCats = sc.nextInt(); if(numOfCats == 0) continue; int[] cats = new int[numOfCats]; for(int j = 0; j < cats.length; j++){ cats[j] = j+1; } int j = 0; for(; j < cats.length - (numOfCats % 2 == 0 ? 0 : 3); j++){ int temp = cats[j+1]; cats[j+1] = cats[j]; cats[j] = temp; j++; } if(numOfCats % 2 == 1){ int temp = cats[j+2]; cats[j+2] = cats[j + 1]; cats[j+1] = cats[j]; cats[j] = temp; } for(j = 0; j < cats.length; j++){ System.out.print(cats[j] + " "); } System.out.println(); } } } class Scanner { public BufferedReader reader; public StringTokenizer st; public Scanner(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); st = null; } public String nextLine() { while (st == null || !st.hasMoreTokens()) { try { String line = reader.readLine(); if (line == null) return null; st = new StringTokenizer(line); } catch (Exception e) { throw (new RuntimeException()); } } String ret = ""; while (st.hasMoreTokens()) { String curr = st.nextToken(); ret += curr + (st.hasMoreTokens() ? " " : ""); } return ret; } public boolean hasNext(){ return st.hasMoreTokens(); } public String next() { while (st == null || !st.hasMoreTokens()) { try { String line = reader.readLine(); if (line == null) return null; st = new StringTokenizer(line); } catch (Exception e) { throw (new RuntimeException()); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { 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 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
3c5a1a8514da8d96a3b9869e5316c13e
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 Permutations { static InputStream is = System.in; public static void main(String[] args) { StringBuilder output = new StringBuilder(); int m = ni(); for (int j = 0; j < m; j++) { int n = ni(); for (int i = 1; i < n - 2; i += 2) { output.append(i + 1).append(" "); output.append(i).append(" "); } if (n % 2 == 0) { output.append(n).append(" ").append(n - 1); } else { output.append(n).append(" ").append(n - 2).append(" ").append(n - 1); } output.append("\n"); } System.out.println(output); } static byte[] inbuf = new byte[1 << 24]; static int lenbuf = 0, ptrbuf = 0; static int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } static int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } static double nd() { return Double.parseDouble(ns()); } static char nc() { return (char) skip(); } static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } static char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } static int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } static long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } }
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
d8126cbaae529cc6c5b9964bc855dadd
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; /** * * @cancer_survivor */ public class Pretty { public static void main(String args[]){ Scanner input = new Scanner(System.in); int t = input.nextInt(); for(int i=0; i<t;i++){ int n = input.nextInt(); if(n%2==0){ PrintText(n); } else{ PrintText(n-3); System.out.print(n); System.out.print(" "); System.out.print(n-2); System.out.print(" "); System.out.print(n-1); } System.out.print("\n"); } } public static void PrintText(int n){ int j=2; while(j<=n){ System.out.print(j); System.out.print(" "); System.out.print(j-1); System.out.print(" "); j=j+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
af2fc47db8194aa42c233f44e3a71116
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 Main { static int N; static int[] arr; static int[] result; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int t = Integer.parseInt(br.readLine()); result = new int[N]; for (int T = 0; T < t; T++) { N = Integer.parseInt(br.readLine()); int insert = 2; int limit = 2; arr = new int[N + 1]; for (int i = 1; i <= N; i++) { arr[i] = insert--; if (arr[i] == i) swap(i); if (insert == limit - 2) { if (i + 1 == N) { insert = limit + 1; limit += 1; } else { insert = limit + 2; limit += 2; } } } for (int i = 1; i < arr.length; i++) { sb.append(arr[i] + " "); } sb.append("\n"); } System.out.print(sb); } public static void swap(int idx) { int tmp = arr[idx]; arr[idx] = arr[idx - 1]; arr[idx - 1] = tmp; } }
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
6f22274da40bbbd947248073fbb78bb7
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 code1 { public static void main(String[] args) { try { FScan scn = new FScan(); StringBuilder ans = new StringBuilder(); int testCase = scn.nextInt(); for (int i = 0; i < testCase; i++) { int n = scn.nextInt(); if (n % 2 == 0) { for (int j = 1; j <= n; j++) { ans.append(j + 1 + " " + j+" "); j++; } ans.append("\n"); } else { ArrayList<Integer> no=new ArrayList<>(); int last = 0; for (int j = 1; j < n - 1; j++) { no.add(j+1); no.add(j); last = j; j++; } no.remove(no.size()-1); no.add(n); no.add(last); for(int x:no){ ans.append(x+" "); } ans.append("\n"); } } System.out.println(ans); } catch (Exception e) { return; } } static class FScan { 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 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
bf9404f230e1f5c52f2ce9cfc90586a3
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 Codechef{ static class InputReader { public BufferedReader reader; public StringTokenizer tok; public InputReader(InputStream inputStream) { reader =new BufferedReader(new InputStreamReader(inputStream)); tok=null; } public InputReader(String inputFile) throws FileNotFoundException { reader=new BufferedReader(new FileReader(inputFile)); tok=null; } public String nextLine() { String c=""; try { c+=reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } return c; } public String next() { while(tok==null || !tok.hasMoreTokens()) { try { tok=new StringTokenizer(nextLine()); } catch(Exception e) { throw new RuntimeException(e); } } return tok.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] args){ Codechef cf = new Codechef(); InputReader scan = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t = scan.nextInt(); while(t-->0){ int n = scan.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++){ arr[i] = i+1; } int k =0; if((n&1)==0){ k = n; }else{ k = n-1; } for(int i=0;i<k;i+=2){ int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } if((n&1) != 0){ int temp = arr[n-2]; arr[n-2] = arr[n-1]; arr[n-1] = temp; } for(int i=0;i<n;i++){ out.print(arr[i] + " "); } out.println(); } 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 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
a059ea3c3e3d5ad34987a7757246d04c
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 abc { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n,i,tat,p; int t=sc.nextInt(); while(t-->0) { n=sc.nextInt(); int a[]=new int[n]; for(i=0;i<n;i++) { a[i]=i+1; } if(n%2==0) { for(i=0;i<n;i+=2) { tat=a[i]; a[i]=a[i+1]; a[i+1]=tat; } } else{ for(i=0;i<n-1;i+=2) { tat=a[i]; a[i]=a[i+1]; a[i+1]=tat; } tat=a[n-1]; a[n-1]=a[n-2]; a[n-2]=tat; } for(i=0;i<n;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
2769a28b1363835f608a0db430f0b32e
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 class44 { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); int t,n,i; t=sc.nextInt(); while(t-->0) {int temp; n=sc.nextInt(); int a[]=new int[n]; for(i=0;i<n;i++) { a[i]=i+1; } if(n%2==0) { for(i=0;i<n;i+=2) { temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } for(i=0;i<n;i++) { System.out.print(a[i]+" "); } } else { for(i=0;i<n-1;i+=2) { temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } temp=a[n-2]; a[n-2]=a[n-1]; a[n-1]=temp; for(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
110ddab2432547c62d4460270f272464
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 Solution2 { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); 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]; arr[i]=arr[i-1]; arr[i-1]=temp; } if(n%2!=0) { int temp2=arr[n-1]; arr[n-1]=arr[n-2]; arr[n-2]=temp2; for(int i=0;i<n;i++) { System.out.print(arr[i]+" "); } } else { 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 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
6620d438a21e7f0f2c61affd75574aa5
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; /** __ __ ( _) ( _) / / \\ / /\_\_ / / \\ / / | \ \ / / \\ / / |\ \ \ / / , \ , / / /| \ \ / / |\_ /| / / / \ \_\ / / |\/ _ '_| \ / / / \ \\ | / |/ 0 \0\ / | | \ \\ | |\| \_\_ / / | \ \\ | | |/ \.\ o\o) / \ | \\ \ | /\\`v-v / | | \\ | \/ /_| \\_| / | | \ \\ | | /__/_ `-` / _____ | | \ \\ \| [__] \_/ |_________ \ | \ () / [___] ( \ \ |\ | | // | [___] |\| \| / |/ /| [____] \ |/\ / / || ( \ [____ / ) _\ \ \ \| | || \ \ [_____| / / __/ \ / / // | \ [_____/ / / \ | \/ // | / '----| /=\____ _/ | / // __ / / | / ___/ _/\ \ | || (/-(/-\) / \ (/\/\)/ | / | / (/\/\) / / // _________/ / / \____________/ ( @author NTUDragons-Reborn */ public class Solution{ 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(); solver.solve(in, out); out.close(); } // main solver static class Task{ double eps= 0.00000001; static final int MAXN = 100005; static final int MOD= 1000000007; // stores smallest prime factor for every number static int spf[] = new int[MAXN]; static boolean[] prime; Map<Integer,Set<Integer>> dp= new HashMap<>(); // Calculating SPF (Smallest Prime Factor) for every // number till MAXN. // Time Complexity : O(nloglogn) public void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) // marking smallest prime factor for every // number to be itself. spf[i] = i; // separately marking spf for every even // number as 2 for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { // checking if i is prime if (spf[i] == i) { // marking SPF for all numbers divisible by i for (int j=i*i; j<MAXN; j+=i) // marking spf[j] if it is not // previously marked if (spf[j]==j) spf[j] = i; } } } void sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. prime= new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } } // A O(log n) function returning primefactorization // by dividing by smallest prime factor at every step public Set<Integer> getFactorization(int x) { if(dp.containsKey(x)) return dp.get(x); Set<Integer> ret = new HashSet<>(); while (x != 1) { if(spf[x]!=2) ret.add(spf[x]); x = x / spf[x]; } dp.put(x,ret); return ret; } // function to find first index >= x public int lowerIndex(List<Integer> arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) >= x) h = mid - 1; else l = mid + 1; } return l; } public int lowerIndex(int[] arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] >= x) h = mid - 1; else l = mid + 1; } return l; } // function to find last index <= y public int upperIndex(List<Integer> arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) <= y) l = mid + 1; else h = mid - 1; } return h; } public int upperIndex(int[] arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } // function to count elements within given range public int countInRange(List<Integer> arr, int n, int x, int y) { // initialize result int count = 0; count = upperIndex(arr, n, y) - lowerIndex(arr, n, x) + 1; return count; } public int _gcd(int a, int b) { if(b == 0) { return a; } else { return _gcd(b, a % b); } } public int add(int a, int b){ a+=b; if(a>=MOD) a-=MOD; else if(a<0) a+=MOD; return a; } public int mul(int a, int b){ long res= (long)a*(long)b; return (int)(res%MOD); } public int power(int a, int b) { int ans=1; while(b>0){ if((b&1)!=0) ans= mul(ans,a); b>>=1; a= mul(a,a); } return ans; } int[] fact= new int[MAXN]; int[] inv= new int[MAXN]; public int Ckn(int n, int k){ if(k<0 || n<0) return 0; return mul(mul(fact[n],inv[k]),inv[n-k]); } public int inverse(int a){ return power(a,MOD-2); } public void preprocess() { fact[0]=1; for(int i=1;i<MAXN;i++) fact[i]= mul(fact[i-1],i); inv[MAXN-1]= inverse(fact[MAXN-1]); for(int i=MAXN-2;i>=0;i--){ inv[i]= mul(inv[i+1],i+1); } } /** * return VALUE of lower bound for unsorted array */ public int lowerBoundNormalArray(int[] arr, int x){ TreeSet<Integer> set= new TreeSet<>(); for(int num: arr) set.add(num); return set.lower(x); } /** * return VALUE of upper bound for unsorted array */ public int upperBoundNormalArray(int[] arr, int x){ TreeSet<Integer> set= new TreeSet<>(); for(int num: arr) set.add(num); return set.higher(x); } public void debugArr(int[] arr){ for(int i: arr) out.print(i+" "); out.println(); } InputReader in; PrintWriter out; public void solve(InputReader in, PrintWriter out) { this.in=in; this.out=out; int t= in.nextInt(); while(t-->0){ solveB(); } } public void solveB(){ int n= in.nextInt(); if(n%2==0) { for(int i=2;i<=n;i+=2) out.print(i+" "+(i-1)+" "); out.println(); } else{ for(int i=2;i<=n-3;i+=2){ out.print(i+" "+(i-1)+" "); } out.print((n-1)+" "+n+" "+(n-2)); out.println(); } } public void solveA(){ int n=in.nextInt(); int[] a= in.nextIntArr(n); } public void solveC(){ int n= in.nextInt(); int[] h= in.nextIntArr(n); Arrays.sort(h); if(h.length==2) { out.println(h[0]+" "+h[1]); return; } int minDif=Integer.MAX_VALUE; for(int i=1;i<n;i++) minDif= Math.min(minDif,Math.abs(h[i]-h[i-1])); int i=0; for(;i<n-1;i++){ if(h[i+1]-h[i]==minDif) break; } for(int j=i+1;j<n;j++) out.print(h[j]+ " "); for(int j=0;j<=i;j++) out.print(h[j]+" "); out.println(); } public void solveE(){ } } static class Venice{ public Map<Long,Long> m= new HashMap<>(); public long base=0; public long totalValue=0; private int M= 1000000007; private long addMod(long a, long b){ a+=b; if(a>=M) a-=M; return a; } public void reset(){ m= new HashMap<>(); base=0; totalValue=0; } public void update(long add){ base= base+ add; } public void add(long key, long val){ long newKey= key-base; m.put(newKey, addMod(m.getOrDefault(newKey,(long)0),val)); } } static class Tuple implements Comparable<Tuple>{ int x, y, z; public Tuple(int x, int y, int z){ this.x= x; this.y= y; this.z=z; } @Override public int compareTo(Tuple o){ return this.x-o.x; } } static class Pair implements Comparable<Pair>{ public int x; public int y; public Pair(int x, int y){ this.x= x; this.y= y; } @Override public int compareTo(Pair o) { return this.x-o.x; } } // public static class compareL implements Comparator<Tuple>{ // @Override // public int compare(Tuple t1, Tuple t2) { // return t2.l - t1.l; // } // } // fast input reader class; static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public String nextToken() { while (st == null || !st.hasMoreTokens()) { String line = null; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble(){ return Double.parseDouble(nextToken()); } public long nextLong(){ return Long.parseLong(nextToken()); } public int[] nextIntArr(int n){ int[] arr= new int[n]; for(int i=0;i<n;i++) arr[i]= nextInt(); return arr; } public long[] nextLongArr(int n){ long[] arr= new long[n]; for(int i=0;i<n;i++) arr[i]= nextLong(); return arr; } public List<Integer> nextIntList(int n){ List<Integer> arr= new ArrayList<>(); for(int i=0;i<n;i++) arr.add(nextInt()); return arr; } public int[][] nextIntMatArr(int n, int m){ int[][] mat= new int[n][m]; for(int i=0;i<n;i++) for(int j=0;j<m;j++) mat[i][j]= nextInt(); return mat; } public List<List<Integer>> nextIntMatList(int n, int m){ List<List<Integer>> mat= new ArrayList<>(); for(int i=0;i<n;i++){ List<Integer> temp= new ArrayList<>(); for(int j=0;j<m;j++) temp.add(nextInt()); mat.add(temp); } return mat; } public char[] nextStringCharArr(){ return nextToken().toCharArray(); } } }
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
35d22790b08fde673a4af76474430acb
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.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; import java.io.*; 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 = 0;i<n-1;i+=2) { System.out.print((i+2)+" "+(i+1)+ " "); } }else { for(int i = 0;i<n-3;i+=2) { System.out.print((i+2)+" "+(i+1)+ " "); } System.out.print((n-1)+" "+ (n)+" "+(n-2)); } //System.out.print(false);t // for(int i =1 ;i <=n;i++) { // System.out.print(i%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 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
14cb8df304e63607b966b38f5a869964
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 s = new Scanner(System.in); int t = s.nextInt(); for(int m = 0 ; m < t ; m++) { int n = s.nextInt(); int odd = 1; if(n%2 != 0) { odd = 4; for(int i = 1 ; i <= 3 ; i++) { if(i==1) System.out.print(3+" "); else System.out.print((i-1)+" "); } if(n==3) { System.out.println(); continue; } } boolean change = true; for(int i = odd ; i <= n ; i++) { if(change) System.out.print((i+1)+" "); else System.out.print((i-1)+" "); change = !change; } 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
c188cce51a56ac755e3d42b5d5db6cf8
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(); while(t-->0){ int n = sc.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;++i){ arr[i] = i+1; } for(int i=0;i<n-1;i+=2){ int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } if(n%2 == 1){ int temp = arr[n-1]; arr[n-1] = arr[n-2]; arr[n-2] = 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 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
3f764878b70357231544262a171958db
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.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; // import java.io.InputStreamReader; // import java.util.Scanner; // import java.util.StringTokenizer; import java.util.Scanner; import javax.swing.text.Position; public class Solution { static long MAX_LIMIT = (long) 1e5; static long mod = (long) 1e9 + 7; static int MAX_INT = (int) 1e9; public static void main(String[] args) throws IOException { // Scanner sc = new Scanner(System.in); FastIOReader sc = new FastIOReader(); int tc = sc.nextInt(); while (tc-- > 0) { int n = sc.nextInt(); StringBuilder sb = new StringBuilder(""); if (n % 2 == 1) { // odd numbers. sb.append("3 1 2 "); for (int i = 2; i <= n / 2; i++) { sb.append((2 * i + 1) + " " + (2 * i) + " " ); } } else { for (int i = 1; i <= n / 2; i++) { sb.append((2 * i) + " " + (2 * i - 1) + " "); } } System.out.println(sb.toString().trim()); } } static boolean isPrimeNumber(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 void sieveOfEratosthenes(boolean[] prime) { // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. int n = prime.length - 1; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return; // // Print all prime numbers // for(int i = 2; i <= n; i++) // { // if(prime[i] == true) // System.out.print(i + " "); // } } // following FastIOReader class' implemntation is by Geeks-for-Geeks. // Reference link: // https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/ static class FastIOReader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public FastIOReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastIOReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[101]; // line length int cnt = 0, catfood; while ((catfood = read()) != -1) { if (catfood == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte) catfood; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte catfood = read(); while (catfood <= ' ') { catfood = read(); } boolean neg = (catfood == '-'); if (neg) catfood = read(); do { ret = ret * 10 + catfood - '0'; } while ((catfood = read()) >= '0' && catfood <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte catfood = read(); while (catfood <= ' ') catfood = read(); boolean neg = (catfood == '-'); if (neg) catfood = read(); do { ret = ret * 10 + catfood - '0'; } while ((catfood = read()) >= '0' && catfood <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte catfood = read(); while (catfood <= ' ') catfood = read(); boolean neg = (catfood == '-'); if (neg) catfood = read(); do { ret = ret * 10 + catfood - '0'; } while ((catfood = read()) >= '0' && catfood <= '9'); if (catfood == '.') { while ((catfood = read()) >= '0' && catfood <= '9') { ret += (catfood - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public 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 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
b6c4fb82d411dab36ae35ed1187387f2
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 AA { public static void main(String [] args) { Scanner in = new Scanner(System.in); 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)+(i==n?"\n":" ")); else { for(int i=2;i<=n-3;i+=2) System.out.print(i+" "+(i-1)+" "); System.out.println((n-1)+" "+n+" "+(n-2)); } } in.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
5abc31f5a248f5e1644bb7cc8024cd0e
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 codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class A_Pretty_Permutations { 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 java.lang.Exception { // your code goes here FastReader scn = new FastReader(); int test = scn.nextInt(); while (test-- > 0) { int n = scn.nextInt(); if(n%2==0){ for(int i=1;i<=n-1;i+=2){ System.out.print((i+1)+" "+i+" "); } } else{ System.out.print("3 1 2 "); for(int i=4;i<=n-1;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
f461be0832e2691aa19ebce46fc2e2b9
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 CF736 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int tc=sc.nextInt(); for(int i=0;i<tc;i++){ int n=sc.nextInt(); int arr[]=new int[n]; if(n%2==0){ for(int h=0;h<n;h=h+2){ arr[h]=h+2; arr[h+1]=h+1; } }else{ for(int h=0;h<n-2;h=h+2){ arr[h]=h+2; arr[h+1]=h+1; } arr[n-1]=n-2; arr[n-2]=n; } for(int u: arr){ System.out.print(u+" "); } 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
1cce897c562ad62f51eace09e3ecb901
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.security.cert.CollectionCertStoreParameters; import java.util.*; import static javax.swing.UIManager.get; public class Main { static class Pair implements Comparable<Pair>{ int x=0,y=0; public Pair(int x1,int y1) { x=x1; y=y1; } @Override public int compareTo(Pair o) { return this.x-o.x; } } static boolean checkPallindrome(int n) { ArrayList<Integer> list = new ArrayList<>(); while(n>0) { list.add(n%10); n/=10; } int low=0,high=list.size()-1; while(low<=high) { if(list.get(low)!=list.get(high)) return false; low++; high--; } return true; } public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); if (n % 2 != 0) { System.out.print(3+" "+1+" "+2+" "); if(n>3) { for(int i=4;i<=n;i+=2) System.out.print((i+1)+" "+i+" "); } System.out.println(); } else { for(int i=1;i<=n;i+=2) System.out.print((i+1)+" "+i+" "); System.out.println(); } } } //Things to check when u r getting wrong answer // syntax of conditional operator y=(x==1)?1:0; // 1- check the flow of the code //2- If ur stuck read the problem once again //3- before submitting always check the output format of ur code //4- don't check standings until problem B is done //5- if u r thinking ur concept is correct but still u r getting wrong answer try to implement it in another way //6- By default, java interpret all numeral literals as 32-bit integer values. // If you want to explicitely specify that this is something bigger then 32-bit integer you should use suffix L for long values. example long a = 600851475143L //All the functions static void debug(String s) { System.out.println(s); } //collections.sort use merge sort instead of quick sort but arrays.sort use quicksort whose worst time complexity is O(n^2) static int[] sort(int[] a) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<a.length;i++) list.add(a[i]); Collections.sort(list); int ind=0; for(int x:list) a[ind++]=x; return a; } //function to print an array for debugging static void print(int[] a) { for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } static void printc(char[] a) { for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } //normal gcd function, always put the greater number as a and the smaller number as b static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static int lcm(int a,int b) { return (a*b)/gcd(a,b); } //to find gcd and lcm for numbers of long data type static long gcdl(long a, long b) { if (b == 0) return a; return gcdl(b, a % b); } static long lcml(long a,long b) { return (a*b)/gcdl(a,b); } //Input Reader to read faster input 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[] readArray(int n) { int[] a = new int[n]; for(int i=0;i<n;i++) a[i]=nextInt(); 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 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
6f114734135fc3796eea0bbfccfd974a
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 Contest { public static void main(String[] args) { Scanner input = new Scanner(System.in); int x, t= input.nextInt(); while(t-->0) { x=input.nextInt(); if(x%2==0) { for(int i=1;i<x-1;i+=2) { System.out.print((i+1)+" "+i+" "); } System.out.println((x)+" "+(x-1)); } else { System.out.print(3+" "+1+" "+2); for(int i=4;i<x;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
6d8c39d870baf4bd0dbaa4ff9ec92d45
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 sbr{ 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==1) System.out.println("1"); else if(n==2) System.out.println("2 1"); else{ 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 "); 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 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
7b9521f7a621345a4a622d8829457d91
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.*; public class SolutionA extends Thread { 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; } } private static final FastReader scanner = new FastReader(); private static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { new Thread(null, new SolutionA(), "Main", 1 << 28).start(); } public void run() { int t = scanner.nextInt(); for (int i = 0; i < t; i++) { solve(); } out.close(); } private static void solve() { int n = scanner.nextInt(); if (n % 2 == 0) { StringBuilder s = new StringBuilder(); for (int i = 1; i <= n; i+=2) { s.append(i + 1) .append(" ") .append(i) .append(" "); } out.println(s); } else { StringBuilder s = new StringBuilder("3 1 2 "); for (int i = 4; i <= n; i+=2) { s.append(i + 1) .append(" ") .append(i) .append(" "); } out.println(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 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
77d24e43736737540466ff2f82c3d28b
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 MyScanner sc=new MyScanner(); public static void main(String[] args) { int t=sc.nextInt(); for(int x=0;x<t;x++) { 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-3;i+=2) System.out.print((i+1)+" "+i+" "); System.out.print(n+" "+(n-2)+" "+(n-1)); } System.out.println(); } } } class MyScanner { static Scanner sc=new Scanner(System.in); public int nextInt() { return (sc.nextInt()); } public long nextLong() { return (sc.nextLong()); } public double nextDouble() { return (sc.nextDouble()); } public String next() { return (sc.next()); } public String nextLine() { return (sc.nextLine()); } public int[] nextIntArr(int n) { int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); return a; } public long[] nextLongArr(int n) { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=sc.nextLong(); return a; } public double[] nextDoubleArr(int n) { double a[]=new double[n]; for(int i=0;i<n;i++) a[i]=sc.nextDouble(); return a; } public String[] nextArr(int n) { String a[]=new String[n]; for(int i=0;i<n;i++) a[i]=sc.next(); return a; } public String[] nextLineArr(int n) { String a[]=new String[n]; for(int i=0;i<n;i++) a[i]=sc.nextLine(); return a; } public ArrayList<Integer> nextIntList(int n) { ArrayList<Integer> a=new ArrayList<>(); for(int i=0;i<n;i++) a.add(sc.nextInt()); return a; } public ArrayList<Long> nextLongList(int n) { ArrayList<Long> a=new ArrayList<>(); for(int i=0;i<n;i++) a.add(sc.nextLong()); return a; } public ArrayList<Double> nextDoubleList(int n) { ArrayList<Double> a=new ArrayList<>(); for(int i=0;i<n;i++) a.add(sc.nextDouble()); return a; } public ArrayList<String> nextList(int n) { ArrayList<String> a=new ArrayList<>(); for(int i=0;i<n;i++) a.add(sc.next()); return a; } public ArrayList<String> nextLineList(int n) { ArrayList<String> a=new ArrayList<>(); for(int i=0;i<n;i++) a.add(sc.next()); 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 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
438095788299406fc18e8dae87b5fdf3
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 n = input.nextInt(); for(int i=0;i<n;i++){ int cur = input.nextInt(); if(cur%2==0){ int minus = cur-2; for (int j=1;j<=minus;j+=2){ System.out.print(j+1+" "+j+" "); } System.out.println(cur+" "+(cur-1)); } else{ System.out.print("3 1 2"); for (int j=4;j<cur;j+=2){ System.out.print(" "+(j+1)+" "+j); } 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
d73fd862686916056f0c7bda3ba8fd61
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
//1541A import java.util.*; public class PrettyPermutations { 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==0) { for(int i = 1; i<=n/2; i++) { System.out.print(2*i+" "+(2*i-1)+" "); } } else { System.out.print(3+" "+1+" "+2+" "); for(int i = 2; i<=n-2; i+=2) { System.out.print((i+3)+" "+(i+2)+" "); } } System.out.println(); } 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 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
1ab6b0ea2d1ff8387090f28008bb73ff
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 in = new Scanner(System.in); long t = in.nextLong(); for (long i=0; i<t; i++) { long n = in.nextLong(); if (n%2 == 1) { for (long j = 1; j <= n-3; j++) { if ( j%2 == 1) { System.out.format("%d ", j+1); } else { System.out.format("%d ", j-1); } } System.out.format("%d %d %d", n-1, n, n-2); System.out.println(); } else { for (long j = 1; j <= n; j++) { if ( j%2 == 1) { System.out.format("%d ", j+1); } else { System.out.format("%d ", 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 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
51aab3127e4ea0a2761e4d0becb18875
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.StringTokenizer; public class A{ public static void main(String[] args){ FS sc = new FS(); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); if((n&1)==1){ StringBuilder sb = new StringBuilder(); for(int i=1; i<=n-3; i+=2) sb.append((i+1)+" "+i+" "); sb.append(n+" "+(n-2)+" "+(n-1)); pw.println(sb.toString()); } else{ StringBuilder sb = new StringBuilder(); for(int i=1; i<=n; i+=2) sb.append((i+1)+" "+i+" "); pw.println(sb.toString().trim()); } } pw.flush(); pw.close(); } static class FS{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next(){ while(!st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(Exception ignored){ } } return st.nextToken(); } int[] nextArray(int n){ int[] a = new int[n]; for(int i = 0; i < n; i++){ a[i] = nextInt(); } return a; } long[] nextLongArray(int n){ long[] a = new long[n]; for(int i = 0; i < n; i++){ a[i] = nextLong(); } return a; } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ 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 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
71bd5a6dc359117e3744fa981a538c0d
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.StringTokenizer; public class A{ public static void main(String[] args){ FS sc = new FS(); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); if((n&1)==1){ StringBuilder sb = new StringBuilder(); for(int i=1; i<=n-3; i+=2) sb.append((i+1)+" "+i+" "); sb.append(n+" "+(n-2)+" "+(n-1)); pw.println(sb.toString()); } else{ StringBuilder sb = new StringBuilder(); for(int i=1; i<=n; i+=2) sb.append((i+1)+" "+i+" "); pw.println(sb.toString().trim()); } } pw.flush(); pw.close(); } static class FS{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next(){ while(!st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(Exception ignored){ } } return st.nextToken(); } int[] nextArray(int n){ int[] a = new int[n]; for(int i = 0; i < n; i++){ a[i] = nextInt(); } return a; } long[] nextLongArray(int n){ long[] a = new long[n]; for(int i = 0; i < n; i++){ a[i] = nextLong(); } return a; } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ 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 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
74ab6459d002069b6aa0343ba02e443b
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 MyClass { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int tc=sc.nextInt(); int i; while(tc>0) { int n=sc.nextInt(); if(n%2==0) { for(i=1;i<=n;i+=2) { System.out.print((i+1)+" "+i+" "); }} else { for(i=1;i<n-2;i+=2) System.out.print((i+1)+" "+i+" "); System.out.print((n-1)+" "+n+" "+(n-2)); } System.out.println(); --tc; }}}
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
8a2dea4cad2162c8c8146a3d1e8ea15f
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.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; // A. Pretty Permutations public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer st = new StringTokenizer(""); public static String next() { try { if (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } public static int ni() { return Integer.parseInt(next()); } public static void main(String[] args) { int t = ni(); while (t-- > 0) { int n = ni(); solve(n); } out.flush(); out.close(); } private static void solve(int n) { if ((n & 1) == 1) { // out.print(n); StringBuilder sb = new StringBuilder(); // sb.append(n); for (int i = 2; i < n - 2; i += 2) sb.append(i + " " + (i - 1) + " "); sb.append(n + " " + (n - 2) + " " + (n - 1)); out.println(sb); } else { StringBuilder sb = new StringBuilder(""); for (int i = 2; i <= n; i += 2) sb.append(i + " " + (i - 1) + " "); out.println(sb.toString().trim()); } } }
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
9307694bf85f45597cfa2ed5602bb652
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.Arrays; import java.util.Random; import java.util.StringTokenizer; public class codeforces728_A { private static void solve(FastIOAdapter ioAdapter) { int n = ioAdapter.nextInt(); if (n % 2 == 1) { ioAdapter.out.print("3 1 2 "); for (int i = 4; i <= n; i += 2) { ioAdapter.out.print(i + 1 + " " + i + " "); } } else { for (int i = 1; i <= n / 2; i++) { ioAdapter.out.print(2 * i + " " + (2 * i - 1) + " "); } } ioAdapter.out.println(); } public static void main(String[] args) throws Exception { try (FastIOAdapter ioAdapter = new FastIOAdapter()) { int count = 1; count = ioAdapter.nextInt(); while (count-- > 0) { solve(ioAdapter); } } } static void shuffleSort(int[] arr) { int n = arr.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } Arrays.sort(arr); } static class FastIOAdapter implements AutoCloseable { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out)))); 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()); } @Override public void close() throws Exception { out.flush(); out.close(); br.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
b09100b340421d29480d6c79b7d59d89
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 solve { public static void solve(int n){ if(n%2 == 0){ for(int i = 1; i<n; i+=2){ System.out.print((i+1) + " " + 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(); } System.out.println(); } public static void main(String[] args) { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } try{ Scanner scn = new Scanner(System.in); int testcases = scn.nextInt(); while(testcases > 0){ testcases--; int n = scn.nextInt(); solve(n); } } catch(Exception e){ System.out.println(e);; } } }
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
5be4108697b2a8bf7e1e92043c7dff6e
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 solve(int n) { if(n % 2 == 0) { for(int i = 1; i <= n; i++) { System.out.print(((i%2 == 0) ? (i - 1) : (i + 1)) + " "); } } else { for(int i = 1; i <= n-2; i++) { System.out.print(((i%2 == 0) ? (i - 1) : (i + 1)) + " "); } System.out.print((n) + " " + (n - 2)); } System.out.println(); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); long t = Long.parseLong(br.readLine()); while(t-- > 0) { int n = Integer.parseInt(br.readLine()); solve(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
PASSED
cc96e7d93684209b6af71876d90e17d7
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 test { public static void solve(int n) { if(n % 2 == 0) { for(int i = 1; i <= n; i++) { System.out.print(((i%2 == 0) ? (i - 1): (i+1))+ " "); } System.out.println(); } else { for(int i = 1; i <= n-2; i++) { System.out.print(((i%2 == 0) ? (i - 1): (i+1))+ " "); } System.out.print((n) + " " + (n-2)); System.out.println(); } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); long t = Long.parseLong(br.readLine()); while(t-- > 0) { int n = Integer.parseInt(br.readLine()); solve(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
PASSED
387b5724aef93b41063823dcf2c51b06
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; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author foysalmac */ public class A { /** * @param args the command line arguments */ public static void main(String args[]) throws InterruptedException { // TODO code application logic here Thread thread= new Thread(new Runnable(){ Scanner scanner =new Scanner(System.in); int t,n; @Override public void run(){ t= scanner.nextInt(); while(t!=0){ t--; n= scanner.nextInt(); if(n%2==1){ for(int i=1;i<n;i++){ if(n-2==i){ System.out.print(n+" "); System.out.print(i+" "); System.out.print(i+1+" "); break; } if(i%2==1){ System.out.print(i+1+" "); }else{ System.out.print(i-1+" "); } } }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(); } } }); thread.start(); // thread.join(); } }
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
c17b08bd17cd5880a2d0a4a110217cb7
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; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author foysalmac */ public class A { /** * @param args the command line arguments */ public static void main(String args[]) throws InterruptedException { // TODO code application logic here Thread thread= new Thread(new Runnable(){ Scanner scanner =new Scanner(System.in); int t,n; @Override public void run(){ t= scanner.nextInt(); while(t!=0){ t--; n= scanner.nextInt(); if(n%2==1){ for(int i=1;i<n;i++){ if(n-2==i){ System.out.print(n+" "); System.out.print(i+" "); System.out.print(i+1+" "); break; } if(i%2==1){ System.out.print(i+1+" "); }else{ System.out.print(i-1+" "); } } }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(); } } }); thread.start(); thread.join(); } }
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
e4a7fd8db87a2139e16492bab85728c9
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; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author foysalmac */ public class A { /** * @param args the command line arguments */ public static void main(String args[]) { // TODO code application logic here Scanner scanner =new Scanner(System.in); int t,n; t= scanner.nextInt(); while(t!=0){ t--; n= scanner.nextInt(); if(n%2==1){ for(int i=1;i<n;i++){ if(n-2==i){ System.out.print(n+" "); System.out.print(i+" "); System.out.print(i+1+" "); break; } if(i%2==1){ System.out.print(i+1+" "); }else{ System.out.print(i-1+" "); } } }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 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
dc8d22254514bd96fbb2588a0ffe4183
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 A { public void prayGod() throws IOException { int t = nextInt(); while (t-- > 0) { int n = nextInt(); if (n % 2 == 0) { for (int i = 1; i <= n; i += 2) { out.printf("%d %d ", i + 1, i); } out.println(); } else { for (int i = 1; i <= n - 3; i += 2) { out.printf("%d %d ", i + 1, i); } out.printf("%d %d %d\n", n, n - 2, n - 1); } } } public void printVerdict(boolean verdict) { if (verdict) out.println(VERDICT_YES); else out.println(VERDICT_NO); } static final String VERDICT_YES = "Yes"; static final String VERDICT_NO = "No"; static final boolean RUN_TIMING = true; static final boolean AUTOFLUSH = false; static final boolean FILE_INPUT = false; static final boolean FILE_OUTPUT = false; static int iinf = 0x3f3f3f3f; static long inf = (long) 1e18 + 10; static long mod = (long) 1e9 + 7; static char[] inputBuffer = new char[1 << 20]; static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1 << 20); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), AUTOFLUSH); // int data-type public int nextInt() throws IOException { return Integer.parseInt(next()); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } public void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } // long data-type public long nextLong() throws IOException { return Long.parseLong(next()); } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } public static void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } public void sort(long[] a) { shuffle(a); Arrays.sort(a); } // double data-type public double nextDouble() throws IOException { return Double.parseDouble(next()); } public double[] nextDoubleArray(int n) throws IOException { double[] arr = new double[n]; for (int i = 0; i < n; i++) arr[i] = nextDouble(); return arr; } public static void printArray(double[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } // Generic type public <T> void sort(T[] a) { shuffle(a); Arrays.sort(a); } public static <T> void printArray(T[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } public String next() throws IOException { int len = 0; int c; do { c = in.read(); } while (Character.isWhitespace(c) && c != -1); if (c == -1) { throw new NoSuchElementException("Reached EOF"); } do { inputBuffer[len] = (char) c; len++; c = in.read(); } while (!Character.isWhitespace(c) && c != -1); while (c != '\n' && Character.isWhitespace(c) && c != -1) { c = in.read(); } if (c != -1 && c != '\n') { in.unread(c); } return new String(inputBuffer, 0, len); } public String nextLine() throws IOException { int len = 0; int c; while ((c = in.read()) != '\n' && c != -1) { if (c == '\r') { continue; } inputBuffer[len] = (char) c; len++; } return new String(inputBuffer, 0, len); } public boolean hasNext() throws IOException { String line = nextLine(); if (line.isEmpty()) { return false; } in.unread('\n'); in.unread(line.toCharArray()); return true; } public void shuffle(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public void shuffle(long[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public void shuffle(Object[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); Object temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public static void main(String[] args) throws IOException { if (FILE_INPUT) in = new PushbackReader(new BufferedReader(new FileReader(new File("output.txt"))), 1 << 20); if (FILE_OUTPUT) out = new PrintWriter(new FileWriter(new File("output.txt"))); long time = 0; time -= System.nanoTime(); new A().prayGod(); time += System.nanoTime(); if (RUN_TIMING) System.err.printf("%.3f ms%n", time / 1000000.0); out.flush(); in.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
6f94201fe3a11294b2c8f5ce29bc9e8a
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 IncreaseKdecrease { public static void main(String args[]) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int n= Integer.parseInt(reader.readLine()); while(n>=1){ int a= Integer.parseInt(reader.readLine()); int arr[] = new int[a]; for(int i=0;i<a;i++){ arr[i]=i+1; } if(a%2==0){ for(int i=0;i<a-1;i=i+2){ int temp =arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } }else{ arr[0]=3; arr[1]=1; arr[2]=2; for(int i=3;i<a-1;i=i+2){ int temp=arr[i+1]; arr[i+1]=arr[i]; arr[i]=temp; } } for(int t:arr){ System.out.print(t+" "); } System.out.println(); 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
PASSED
47f04a1034538986882bc604e52faebd
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 Main { public static void main (String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); for(int i = 0;i<n;i++) { int l = s.nextInt(); if(l%2==0) { for(int j = 1;j<=l;j++) { int temp =j; if(j%2!=0) { System.out.print(temp+1 +" "); } else System.out.print(temp-1+" "); } } else { if(l==3) { System.out.println(3+" "+1+" "+2); } else { System.out.print(3+" "+1+" "+2+" "); for(int j = 4;j<=l;j++) { int temp =j; if(j%2==0) System.out.print(temp+1+" "); else System.out.print(temp-1+" "); } //System.out.print(3+" "+1+" "+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
89693b58168f7b51a1e80cb34c335c1b
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 Round728_A { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int tt = sc.nextInt(); while(tt-->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) + " " + (i) + " "); i++; } } }else{ for(int i=1;i<=n;i++){ if(i == (n-2)){ System.out.print(n + " " + (n-2) + " "); i++; } else if(i == n){ System.out.print((n-1) + " "); } else{ System.out.print((i+1) + " " + (i) + " "); 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
163797ad76c509e9802869907f84e637
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 s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0){ int n = s.nextInt(); int[] ar = new int[n+1]; if (n%2 == 0){ for (int i = 1; i <= n; i += 2){ ar[i] = i; ar[i-1] = i+1; } } else { ar[0] = 3; ar[1] = 1; ar[2] = 2; for (int i = 4; i <= n; i += 2){ ar[i] = i; ar[i-1] = i+1; } } for(int i = 0; i < n; i++){ System.out.print(ar[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
90b6f54b7ab9afb3e16e6fb482383189
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); long t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); for(int i=1;i<n-2;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 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
0c119fd08302cc5560b5f043aad5765a
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 Main { public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++) { arr[i] = i + 1; } int brr[] = new int[n]; Arrays.fill(brr,0); if(n%2==0) { for(int i=0;i<n-1;i++) { int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; i++; } for(int i=0;i<n;i++) { System.out.print(arr[i]+" "); } System.out.println(); } else { for(int i=0;i<n-2;i++) { int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; i++; } int e = arr[n-2]; arr[n-2] = arr[n-1]; arr[n-1] = e; for(int i=0;i<n;i++) { System.out.print(arr[i]+" "); } 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()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } static boolean isPalindrome(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } static boolean palindrome_array(int arr[], int n) { // Initialise flag to zero. int flag = 0; // Loop till array size n/2. for (int i = 0; i <= n / 2 && n != 0; i++) { // Check if first and last element are different // Then set flag to 1. if (arr[i] != arr[n - i - 1]) { flag = 1; break; } } // If flag is set then print Not Palindrome // else print Palindrome. if (flag == 1) return false; else return true; } static boolean allElementsEqual(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]==arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } static boolean allElementsDistinct(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]!=arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } public static void reverse(int[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily int temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static boolean isReverseSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { return false; } } return true; } static int[] rearrangeEvenAndOdd(int arr[], int n) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); int[] array = list.stream().mapToInt(i->i).toArray(); return array; } static long[] rearrangeEvenAndOddLong(long arr[], int n) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); long[] array = list.stream().mapToLong(i->i).toArray(); return array; } 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; } static int getSum(int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcdLong(long a, long b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcdLong(a-b, b); return gcdLong(a, b-a); } static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 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 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
c443dfcfab6d4885758ed1c844c4b615
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 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 a[] = new int[n]; for(int i=0;i<n;i++){ a[i] = i+1; } if(n%2==0){ for(int i=0;i<n-1;i+=2){ int temp = a[i+1]; a[i+1] = a[i]; a[i] = temp; } } else { for(int i=0;i<n-2;i+=2){ int temp = a[i+1]; a[i+1] = a[i]; a[i] = temp; } int x = a[n-1]; a[n-1] = a[n-2]; a[n-2] = x; } for(int item :a){ System.out.println(item); } } } }
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
eadb2a238faa73a0aaa87174c9decfd9
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 Codeforces { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); int arr[] = new int[test]; for(int i = 0; i<test ; i++) { arr[i] = sc.nextInt(); } int n = 0; for(int i = 0; i<test; i++) { n = arr[i]; if(n%2 == 0) { for(int j = 2; j<=n; j = j+2) { if(j!=n) System.out.print(j + " " + (j-1) + " "); else System.out.print(n + " " + (n-1)); } } else { for(int j = 2; j<=n; j = j+2) { if(j!=n-1) System.out.print(j + " " + (j-1) + " "); else { System.out.print(n + " " + (n-2) + " "+ (n-1)); break; } } } 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
11b9f9d50accaa7906f46c4fb27262d9
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 sc= new Scanner(System.in); int t= sc.nextInt(); while(t-->0) { int n= sc.nextInt(); if(n%2==0) { for(int i=2; i<=n; i+=2) { System.out.print(i+" "+(i-1)+" "); } } else { if(n==3) { System.out.print(n+" "); for(int i=1; i<=n-1; i++) { System.out.print(i+" "); } } else { System.out.print(3+" "+1+" "+2+" "); for(int i=5; i<=n; i+=2) { System.out.print(i+" "+(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 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
9fb61e3f442647625c28afe418260fb1
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 permutation { public static void main(String[] args) throws java.lang.Exception{ BufferedReader reader =new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(reader.readLine()); for (int i=0;i<t ;i++) { String[] s1 = reader.readLine().split(" "); int N = Integer.parseInt(s1[0]); 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=j+2) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } else { for (int j = 0; j < N-3; j=j+2) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } int temp = arr[N-3]; arr[N-3] = arr[N-1]; int temp2 = arr[N-2]; arr[N-2] = temp; arr[N-1] = temp2; } for (int j = 0; j < arr.length; j++) { System.out.print(arr[j] + " "); } 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
4d441175a04bb52d3bb190aa58070f79
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 Main { public static void main(String args[]) { Scanner scn = new Scanner(System.in); int test_cases = scn.nextInt(); for(int l = 0 ; l < test_cases ; l++){ int n = scn.nextInt(); // int[] arr = new int[n]; ArrayList<Integer> arr = new ArrayList<>(); for(int i = 0 ; i < n ; i++){ arr.add(i+1); } if(n%2==0){ for(int i = 0 ; i < n-1 ; i=i+2){ // swap(arr[i],arr[i+1]); Collections.swap(arr,i,i+1); } }else{ for(int i = 0 ; i <= n-1 ; i=i+2){ // System.out.print(1); if(i==0){ Collections.swap(arr,i,i+1); }else{ Collections.swap(arr,i,i-1); } } } for(int i = 0 ; i < n ; i++){ System.out.print(arr.get(i)+" "); } System.out.println(); } // System.out.println(); } // public static void swap(int i , int j){ // int temp = i; // i = j; // 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 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
fe581c65368b1af6a3136324f7c286a9
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(); while(t-->0){ int n = sc.nextInt(); int a[] = new int[n]; if(n%2==0){ for(int i=0;i<n;i+=2){ a[i] = i+2; a[i+1] = i+1; // System.out.print(i+1+" "+i+" "); } } if(n%2==1){ for(int i=0;i<n;i+=2){ if(i== n-1){ a[n-1] = n-2; a[n-2] = n; break; } a[i] = i+2; 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
150c4d5019f875506b46180d293b9abe
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 s=new Scanner(System.in); int t=s.nextInt(); while(t!=0){ int n=s.nextInt(); int[] ans=new int[n]; // System.out.print(n+" "); int k=0; // int m=n; // if(n%2!=0){ // m=n-1; // } for(int i=1; i<n; i=i+2){ ans[k++]=i+1; ans[k++]=i; } if(n%2!=0){ int temp=n; ans[n-1]=ans[n-2]; ans[n-2]=temp; } for(int i=0; i<n; i++){ System.out.println(ans[i]); } t--; } 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 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
0f487ec6649a2e783ae9c181a9ef1d68
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.*; /* 𝑛 cats in a line: 1 to 𝑛 2: test cases 2: number of cat 3: number of cat [1,2] -> [2,1] [1,2,3] -> [3,2,1] */ public class Main { public static void swap(int a, int b, int[] arr) { int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int testCases = sc.nextInt(); while (testCases-- > 0) { int cats = sc.nextInt(); if (cats <= 1) continue; int[] permutation = new int[cats]; // Create orignal array for (int i = 1; i <= cats; ++i) permutation[i - 1] = i; // Swap for (int i = 0; i < cats - 1; i += 2) swap(i, i + 1, permutation); // Odd if (cats % 2 != 0) { swap(cats - 2, cats - 1, permutation); } for (int p : permutation) { System.out.print(p); System.out.print(' '); } System.out.print('\n'); } /* int n = sc.nextInt(); // read input as integer long k = sc.nextLong(); // read input as long double d = sc.nextDouble(); // read input as double String str = sc.next(); // read input as String String s = sc.nextLine(); // read whole line as String int result = 3*n; out.println(result); // print via PrintWriter */ out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { 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 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
60003cab5527b54844bee68dbf3b2db9
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.*; public class Main{ public static void main(String []a) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t-->0) { int n = Integer.parseInt(br.readLine()),x=2,k=0; if(n%2==0) { while(x<=n) { System.out.print(x+" "+(x-1)+" "); x+=2; } }else { while(x<=n-2) { System.out.print(x+" "+(x-1)+" "); x+=2; } System.out.print((n-1)+" "+n+" "+(n-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
05c29a41b92dfeafbe6ebaf04f9529c7
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 pack; 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 class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; } static int depth(TreeNode root) { if(root == null)return 0; int a = 1+ depth(root.left); int b = 1+ depth(root.right); return Math.max(a,b); } static HashSet<Integer>set = new HashSet<>(); static void deepestLeaves(TreeNode root, int cur_depth, int depth) { if(root == null)return; if(cur_depth == depth)set.add(root.val); deepestLeaves(root.left,cur_depth+1,depth); deepestLeaves(root.right,cur_depth+1,depth); } public static void print(TreeNode root) { if(root == null)return; System.out.print(root.val+" "); System.out.println("er"); print(root.left); print(root.right); } public static HashSet<Integer>original(TreeNode root){ int d = depth(root); deepestLeaves(root,0,d); return set; } static HashSet<Integer>set1 = new HashSet<>(); static void leaves(TreeNode root) { if(root == null)return; if(root.left == null && root.right == null)set1.add(root.val); leaves(root.left); leaves(root.right); } public static boolean check(HashSet<Integer>s, HashSet<Integer>s1) { if(s.size() != s1.size())return false; for(int a : s) { if(!s1.contains(a))return false; } return true; } static TreeNode subTree; public static void smallest_subTree(TreeNode root) { if(root == null)return; smallest_subTree(root.left); smallest_subTree(root.right); set1 = new HashSet<>(); leaves(root); boolean smallest = check(set,set1); if(smallest) { subTree = root; return; } } public static TreeNode answer(TreeNode root) { smallest_subTree(root); return subTree; } } static class pair{ int first; int second; public pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(pair p) { if(first == p.first)return second-p.second; return first-p.first; } } static class Compare{ static void compare(ArrayList<pair>arr, long n) { Collections.sort(arr,new Comparator<pair>() { public int compare(pair p1, pair p2) { return (int) (p1.first-p2.first); } }); } } public static HashMap<Integer,Integer>sortByValue(HashMap<Integer,Integer>hm){ List<Map.Entry<Integer,Integer>>list = new LinkedList<Map.Entry<Integer,Integer>>(hm.entrySet()); Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>(){ public int compare(Map.Entry<Integer,Integer>o1, Map.Entry<Integer,Integer>o2) { return (o1.getValue()).compareTo(o2.getValue()); }}); HashMap<Integer,Integer>temp = new LinkedHashMap<Integer,Integer>(); for(Map.Entry<Integer,Integer>aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static class pairr implements Comparable<pairr>{ static Long value; Long index; public pairr(Long value, Long index) { this.value = value; this.index = index; } public int compareTo(pairr o) { return (int)(value-o.value); } } static class Key<K1, K2> { public K1 key1; public K2 key2; public Key(K1 key1, K2 key2) { this.key1 = key1; this.key2 = key2; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Key key = (Key) o; if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) { return false; } if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) { return false; } return true; } @Override public int hashCode() { int result = key1 != null ? key1.hashCode() : 0; result = 31 * result + (key2 != null ? key2.hashCode() : 0); return result; } @Override public String toString() { return "[" + key1 + ", " + key2 + "]"; } } public static int sumOfDigits (long n) { int sum = 0; while(n > 0) { sum += n%10; n /= 10; } return sum; } public static long binary_search(int s, int e, long num, long []ar) { if(s > e) { return -1; } int mid = (s+e)/2; if(s == e && ar[s] >= num) { return ar[s]; }else if(s == e && ar[s] < num) { return -1; }else if(ar[mid] < num) { return binary_search(mid+1,e,num,ar); }else if(ar[mid] >= num) { return binary_search(s,mid,num,ar); } return -1; } public static int index_search(int s, int e, long num, long []ar) { if(s > e) { return -1; } int mid = (s+e)/2; if(s == e && ar[s] >= num) { return s; }else if(s == e && ar[s] < num) { return -1; }else if(ar[mid] < num) { return index_search(mid+1,e,num,ar); }else if(ar[mid] >= num) { return index_search(s,mid,num,ar); } return -1; } public static void swap(int []ar, int i, int j) { for(int k= j; k >= i; k--) { int temp = ar[k]; ar[k] = ar[k+1]; ar[k+1] = temp; } } public static boolean digit_exists(long n) { while(n > 0) { if(n%10 == 9) return true; n = n/10; } return false; } public static int log(int n) { int c = 0; while(n > 0) { c++; n /=2; } return c; } public static int findOr(int[]bits){ int or=0; for(int i=0;i<32;i++){ or=or<<1; if(bits[i]>0) or=or+1; } return or; } static void simpleSieve(int limit, Vector<Integer> prime) { // Create a boolean array "mark[0..n-1]" and initialize // all entries of it as true. A value in mark[p] will // finally be false if 'p' is Not a prime, else true. boolean mark[] = new boolean[limit+1]; for (int i = 0; i < mark.length; i++) mark[i] = true; for (int p=2; p*p<limit; p++) { // If p is not changed, then it is a prime if (mark[p] == true) { // Update all multiples of p for (int i=p*p; i<limit; i+=p) mark[i] = false; } } // Print all prime numbers and store them in prime for (int p=2; p<limit; p++) { if (mark[p] == true) { prime.add(p); } } } // Prints all prime numbers smaller than 'n' public static void segmentedSieve(int n, ArrayList<Integer>l) { // Compute all primes smaller than or equal // to square root of n using simple sieve int limit = (int) (floor(sqrt(n))+1); Vector<Integer> prime = new Vector<>(); simpleSieve(limit, prime); // Divide the range [0..n-1] in different segments // We have chosen segment size as sqrt(n). int low = limit; int high = 2*limit; // While all segments of range [0..n-1] are not processed, // process one segment at a time while (low < n) { if (high >= n) high = n; // To mark primes in current range. A value in mark[i] // will finally be false if 'i-low' is Not a prime, // else true. boolean mark[] = new boolean[limit+1]; for (int i = 0; i < mark.length; i++) mark[i] = true; // Use the found primes by simpleSieve() to find // primes in current range for (int i = 0; i < prime.size(); i++) { // Find the minimum number in [low..high] that is // a multiple of prime.get(i) (divisible by prime.get(i)) // For example, if low is 31 and prime.get(i) is 3, // we start with 33. int loLim = (int) (floor(low/prime.get(i)) * prime.get(i)); if (loLim < low) loLim += prime.get(i); /* Mark multiples of prime.get(i) in [low..high]: We are marking j - low for j, i.e. each number in range [low, high] is mapped to [0, high-low] so if range is [50, 100] marking 50 corresponds to marking 0, marking 51 corresponds to 1 and so on. In this way we need to allocate space only for range */ for (int j=loLim; j<high; j+=prime.get(i)) mark[j-low] = false; } // Numbers which are not marked as false are prime for (int i = low; i<high; i++) if (mark[i - low] == true) l.add(i); // Update low and high for next segment low = low + limit; high = high + limit; } } public static int find_indexNum(long k) { long k1 = k; int power = 0; while(k > 0) { power++; k /=2 ; } long check = (long)Math.pow(2, power-1); if(k1 == check) { return power; } // System.out.println(power); long f = (long)Math.pow(2, power-1); long rem = k1-f; return find_indexNum(rem); } public static void sortPair(ArrayList<pair>l, int n) { n = l.size(); Compare obj = new Compare(); obj.compare(l, n); } public static void shuffle(int []array, int num,int t_index, boolean []vis, int m ) { for(int i = 0; i < m; i++) { if(vis[i] == false) { int temp = array[i]; if(i < t_index) { vis[i] = true; } array[i] = num; array[t_index] = temp; // System.out.println(array[t_index]+" "+array[i]); break; } } } public static void rotate(int []arr,int j, int times, int m) { if(j == 0) { int temp1 = arr[0]; arr[0] = arr[times]; arr[times] = temp1; }else { int temp = arr[j]; int z = arr[0]; arr[0] = arr[times]; arr[j] = z; arr[times] = temp; } } public static void recur(int i,int A, int B,int []dp,int []metal, int n, boolean took,int ind) { if(i-A <= 0 && i-B <= 0)return; int count = 0; for(int j = 1; j <= n; j++) { if(dp[j] >= metal[j]) { count++; } } if(count == n)return; if(i-A >= 0 && i-B >= 0 && dp[i] > 0 && dp[i] > metal[i]) { dp[i]--; dp[i-A]++; dp[i-B]++; } if(ind == 6) { // System.out.println(Arrays.toString(dp)); } recur(i-A,A,B,dp,metal,n,took,ind); recur(i-B,A,B,dp,metal,n,took,ind); } public static boolean isPrime(int 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; } public static boolean[] getSieve(int n) { boolean[] isPrime = new boolean[n+1]; for (int i = 2; i <= n; i++) isPrime[i] = true; for (int i = 2; i*i <= n; i++) if (isPrime[i]) for (int j = i; i*j <= n; j++) isPrime[i*j] = false; return isPrime; } static int gcd(int a, int b) { if(a == 0) return b; return gcd(b%a,a); } public static void dfs(LinkedList<Integer>[]list, HashMap<Integer,Integer>map, int parent, int n) { Stack<Integer>st = new Stack<>(); } public static boolean pos(int n) { int i = 1; boolean pos = false; while(i*i <= n) { if(i*i*2 == n || i*i*4 == n) { pos = true; break; } i++; } if(pos)return true; return false; } static long count = 0; public static void pairs (int []ar, int s, int e) { if(e <= s)return; // System.out.println(ar[s]+" "+ar[e]+" "+s+" "+e); if(ar[e]-ar[s] == e-s) { count++; //System.out.println("sdf"); } pairs(ar,s+1,e); pairs(ar,s,e-1); } public static long ways(long n) { return (n*(n-1))/2; } 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+= 6) { if(n%i == 0 || n%(i+2) == 0) return false; } return true; } static long nextPrime(long n) { boolean found = false; long prime = n; while(!found) { prime++; if(isPrime(prime)) found = true; } return prime; } public static boolean isValid(int h, int m, int hour, int minute) { int a = flip(hour / 10); if (a == -1) return false; int b = flip(hour % 10); if (b == -1) return false; int c = flip(minute / 10); if (c == -1) return false; int d = flip(minute % 10); if (d == -1) return false; if (10 * d + c >= h) return false; if (10 * b + a >= m) return false; return true; } public static int flip(int x) { if (x == 0) return 0; if (x == 1) return 1; if (x == 2) return 5; if (x == 5) return 2; if (x == 8) return 8; return -1; } static long maximum(long a, long b, long c, long d) { long m = Math.max(a, b); long m1 = Math.max(c, d); return Math.max(m1, m1); } static long minimum(long a, long b, long c,long d) { long m = Math.min(a, b); long m1 = Math.min(c, d); return Math.min(m, m1); } static long ans = 0; public static void solve1(boolean [][]vis,long [][]mat, int r, int c, int r2, int c2, int r1, int c1, int r3, int c3) { if(r > r1 || c > c1 || r > r2 || c > c2 || r1 > r3 || c1 > c3 || r3 < r2 || c3 < c2 || vis[r][c] || vis[r1][c1]|| vis[r2][c2] || vis[r3][c3]) return; vis[r][c] = true; vis[r1][c1] = true; vis[r2][c2] = true; vis[r3][c3] = true; long max = maximum(mat[r][c],mat[r1][c1],mat[r2][c2],mat[r3][c3]); long min = minimum(mat[r][c],mat[r1][c1],mat[r2][c2],mat[r3][c3]); long a = mat[r][c]; long b = mat[r1][c1]; long c4 = mat[r2][c2]; long d =mat[r3][c3]; long []p = {a,b,c4,d}; Arrays.sort(p); long temp = (p[2]+p[3]-p[0]-p[1]); if(r == r1 && r == r2 && r2 == r3 && r1 == r3) temp /= 2; System.out.println(Arrays.toString(p)); ans += temp; solve1(vis,mat,r+1,c,r2+1,c2,r1-1,c1,r3-1,c3); solve1(vis,mat,r,c+1,r2,c2-1,r1,c1+1,r3,c3-1); solve1 (vis,mat,r+1,c+1,r2+1,c2-1,r1-1,c1+1,r3-1,c3-1); } private static int solve(int[][] mat, int i, int c, int j, int c2, int k, int c1, int l, int c3) { // TODO Auto-generated method stub return 0; } public static int dfs(int parent, LinkedList<Integer>[]list) { for(int i : list[parent]) { if(list[parent].size() == 0) { return 0; }else { return 1 + dfs(i,list); } } return 0; } public static long answer = Integer.MAX_VALUE; public static void min_Time(int [][]dp, int i, HashSet<Integer>set, int min, int r, int c) { if(i > r) { answer = Math.min(answer, min); return; } if(min > answer)return; for(int j = i; j <= c; j++) { if(!set.contains(j)) { set.add(j); min += dp[i][j]; min_Time(dp,i+1,set,min,r,c); min -= dp[i][j]; set.remove(j); } } } public static void dp(int [][]dp, int r, int c, int o, int z, long sum) { if(r > o) { answer = Math.min(answer, sum); } if(r > o || c > z) { return; } if(sum > answer)return; sum += dp[r][c]; dp(dp,r+1,c+1,o,z,sum); sum -= dp[r][c]; dp(dp,r,c+1,o,z,sum); } static HashSet<ArrayList<Integer>>l = new HashSet<>(); public static void fourSum(Deque<Integer>ll, int i, int target, int []ar, int n) { if(ll.size() == 4) { int sum = 0; ArrayList<Integer>list = new ArrayList<>(); for(int a : ll) { sum += a; list.add(a); } if(sum == target) { Collections.sort(list); l.add(list); // System.out.println(ll); } return; } for(int j = i; j < n; j++) { ll.add(ar[j]); fourSum(ll,j+1,target,ar,n); ll.removeLast(); } } static int max_bottles(int cur, int exchange, int n){ if(cur == exchange){ cur = 0; n++; } if(n == 0)return 0; return 1+ max_bottles(cur+1,exchange,n-1); } public static void fill(int [][]mat, List<Integer>ans, int row_start, int row_end, int col_start, int col_end) { for(int i = col_start; i <= col_end; i++) { ans.add(mat[row_start][i]); } for(int i = row_start+1; i <= row_end; i++) { ans.add(mat[i][col_end]); } if(col_start == col_end)return; if(row_start == row_end)return; for(int i = col_end-1; i >= col_start; i--) { ans.add(mat[row_end][i]); } for(int i = row_end-1; i >= row_start+1; i--) { ans.add(mat[i][col_start]); } } public static void create(int [][]mat, int j, int i, int k) { if(i < 1 || j >= mat.length)return; mat[j][i] = k; create(mat,j+1,i-1,k+1); } public static long sum(int [][]mat, int x1, int y1, int x2, int y2) { long sum = 0; while(x1 <= x2) { sum += mat[x1][y1]; // System.out.println(mat[x1][y1]); x1++; } y1++; while(y1 <= y2) { sum += mat[x2][y1]; y1++; } return sum; } public static boolean allneg(int []ar, int n) { for(int i = 0; i < n; i++) { if(ar[i] >= 0)return false; } return true; } public static boolean allpos(int []ar, int n) { for(int i = 0; i < n; i++) { if(ar[i] <= 0)return false; } return true; } public static int max_pos(int []ar, int n) { int min = Integer.MAX_VALUE; for(int i = 1; i < n; i++) { if(ar[i] > 0) { break; } int a = Math.abs(ar[i]-ar[i-1]); min = Math.min(min, a); } int c = 0; boolean zero = false; TreeSet<Integer>set = new TreeSet<>(); int neg = 0; for(int i = 0; i < n; i++) { if(ar[i] <= 0) {neg++; if(ar[i] == 0) zero = true; continue;} if(ar[i] <= min) { c = 1; } } neg += c; return neg; } static final int MAX = 10000000; // prefix[i] is going to store count // of primes till i (including i). static int prefix[] = new int[MAX + 1]; static void buildPrefix() { // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. boolean prime[] = new boolean[MAX + 1]; Arrays.fill(prime, true); for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } // Build prefix array prefix[0] = prefix[1] = 0; for (int p = 2; p <= MAX; p++) { prefix[p] = prefix[p - 1]; if (prime[p]) prefix[p]++; } } static int query(int L, int R) { return prefix[R] - prefix[L - 1]; } static void alter(int n) { int ans = 0; boolean []vis = new boolean[n+1]; for(int i = 2; i <= n; i++){ boolean p = false; if(vis[i] == false) { for(int j = i; j <= n; j+=i) { if(vis[j] == true) { p = true; }else { vis[j] = true; } } if(!p)ans++; } } System.out.println(ans); } public static void solveDK(int []dp, int i, int D, int K) { int d = D/K; int ans = -1; int ind = d+1; while(ind < i) { int temp = i/ind; temp--; if(dp[temp*ind] == temp) { ans = dp[temp*ind]+1; dp[i] = ans; break; } ind = ind*2; } if(ans == -1) dp[i] = 1; } public static void solveKD(int []dp, int i, int D, int K) { int d = K/D; int ans = -1; int ind = d+1; while(ind < i) { int temp = i/ind; temp--; if(dp[temp*ind] == temp) { ans = dp[temp*ind]+1; dp[i] = ans; break; } ind = ind*2; } if(ans == -1) dp[i] = 1; } static int countGreater(int arr[], int n, int k) { int l = 0; int r = n - 1; // Stores the index of the left most element // from the array which is greater than k int leftGreater = n; // Finds number of elements greater than k while (l <= r) { int m = l + (r - l) / 2; // If mid element is greater than // k update leftGreater and r if (arr[m] > k) { leftGreater = m; r = m - 1; } // If mid element is less than // or equal to k update l else l = m + 1; } // Return the count of elements greater than k return (n - leftGreater); } static ArrayList<Integer>printDivisors(int n) { // Note that this loop runs till square root ArrayList<Integer>list = new ArrayList<>(); for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { // If divisors are equal, print only one if (n/i == i) list.add(i); else // Otherwise print both list.add(i); list.add(n/i); } } return list; } static ArrayList<List<Integer>>res = new ArrayList<>(); static void sum(int []ar, ArrayList<Integer>list,int target, int j) { int sum = 0; for(int a : list) { sum += a; } if(sum == target) { res.add(new ArrayList(list)); return; }else if(sum > target) return; for(int i = j; i < ar.length; i++) { list.add(ar[i]); sum(ar,list,target,i); list.remove(list.size()-1); } } public static void main(String args[])throws IOException{ // System.setIn(new FileInputStream("Case.txt")); 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 n = Integer.parseInt(ob.readLine()); 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-3; i+=2) { System.out.print((i+1)+" "+i+" "); } System.out.print(n+" "+(n-2)+" "+(n-1)); } System.out.println(); } } static class pairrr implements Comparable<pairrr> { int x; int y; public pairrr(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pairrr p = (pairrr) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Integer(x).hashCode() * 31 + new Integer(y).hashCode(); } public int compareTo(pairrr other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } }
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
580c566bb4060399ae5f20df7aaba398
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 : Shadman Shariar || Email : shadman.shariar@northsouth.edu || University : North South University (NSU) || Facebook : shadman.shahriar.007 || ==============================================*/ import java.io.*; import java.util.*; //import java.math.BigInteger; //import java.text.DecimalFormat; public class Main { public static Main obj = new Main(); //public static FastReader fr = new FastReader(); //public static Scanner input = new Scanner(System.in); //public static PrintWriter pw = new PrintWriter(System.out); //public static DecimalFormat df = new DecimalFormat(".000"); //public static BufferedReader br = new BufferedReader //(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception { Scanner input = new Scanner (System.in); //BigInteger bi1 = new BigInteger("000"); //StringBuilder sb = new StringBuilder(); //-----------------CODE STARTS FROM HERE------------------// int tt = input.nextInt(); for (int i = 0; i <tt; i++) { int n = input.nextInt(); if(n==1) { System.out.println(1); } else { int l = 2 ; int r = 1 ; for (int j = 0; j <n/2; j++) { System.out.print(l+" "); if(n%2!=0&&j==((n/2)-1)) { System.out.println(n+" "+r); break; } System.out.print(r+" "); l+=2; r+=2; } } System.out.println(); } //---------------------CODE ENDS HERE---------------------// //pw.close(); input.close(); System.exit(0); } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } public static long nCr(long n, long r) { return factorial(n) / (factorial(r) * factorial(n - r)); } public static long nPr(long n, long r) { return factorial(n) / factorial(n - r); } public static int countsubstring(String str) { int n = str.length(); return n * (n + 1) / 2; } public static boolean ispalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) { if (str.charAt(i) != str.charAt(j)) return false; i++; j--; } return true; } public static boolean perfectsquare(long x) { if (x >= 0) { long sr = (long)(Math.sqrt(x)); return ((sr * sr) == x); } return false; } public static boolean perfectcube(long N) { int cube; int c = 0; for (int i = 0; i <= N; i++) { cube = i * i * i; if (cube == N) { c=1; break; } else if (cube > N) { c=0 ; break ; } } if (c==1)return true; else return false; } public static boolean[] sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } prime[1]=false; return prime; } public static int removeduplicateelements(int arr[], int n){ Arrays.sort(arr); if (n==0 || n==1){ return n; } int[] temp = new int[n]; int j = 0; for (int i=0; i<n-1; i++){ if (arr[i] != arr[i+1]){ temp[j++] = arr[i]; } } temp[j++] = arr[n-1]; for (int i=0; i<j; i++){ arr[i] = temp[i]; } return j; } public static int fibon(int n) { if (n <= 1) { return n; } int[] array = new int[n + 1]; array[0] = 0; array[1] = 1; for (int i = 2; i <= n; i++) { array[i] = array[i - 2] + array[i - 1]; } return array[n]; } public static long sumofdigits(long n) { long sum = 0; while (n != 0) { sum = sum + n % 10; n = n / 10; } return sum; } public static int reversedigits(int num) { int rev_num = 0; while (num > 0) { rev_num = rev_num * 10 + num % 10; num = num / 10; } return rev_num; } public static int binarysearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarysearch(arr, l, mid - 1, x); return binarysearch(arr, mid + 1, r, x); } return -1; } public static void rangeofprimenumber(int a, int b) { int i, j, flag; for (i = a; i <= b; i++) { if (i == 1 || i == 0) continue; flag = 1; for (j = 2; j <= i / 2; ++j) { if (i % j == 0) { flag = 0; break; } } if (flag == 1) System.out.println(i); } } public static boolean isprime(long n) { if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (long i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } public static long factorial(long n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } public static int[] reversearrayinrange(int arr[], int start, int end) { int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } return arr; } public 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 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
d98e548f97f18dd6e4a04306d2edf943
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 MyClass { public static void main(String args[]) throws IOException { 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) { System.out.print(2+" "+1); } else if(n==3) { System.out.print(2+" "+3+" "+1); } else 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 { for(int i=1;i<=n-3;i++) { if(i%2!=0) System.out.print((i+1)+" "); else { System.out.print((i-1)+" "); } } System.out.print((n-1)+" "+(n)+" "+(n-2)); } System.out.println(); } } } // 1 2 3 4 5
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
3648fae52376453ea1d6f118cf8721b5
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 Sol{ static class Pair implements Comparable<Pair>{ int id;int value; public Pair(int id,int value) { this.id=id; this.value=value; } @Override public int compareTo(Pair p){return value-p.value; } } static int Max=Integer.MAX_VALUE; public static void main(String []args){ int t=ni(); while(t-->0){ int n=ni(); if(n%2==0){for(int i=1;i<=n;i+=2){out.print(i+1+" "+i+" ");}out.println();} else{for(int i=1;i<n-2;i+=2){out.print(i+1+" "+i+" ");}out.print(n+" "+(n-2)+" "+(n-1));out.println();} }out.close();} static int v(char c){return (int)(c-'a')+1;} static long mod=1000000007; 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; } //implements Comparable<Pair> static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static FastReader in=new FastReader(inputStream); static PrintWriter out=new PrintWriter(outputStream); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(InputStream in) { br = new BufferedReader(new InputStreamReader(System.in)); } 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()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int ni(){return in.nextInt();} static long nl(){return in.nextLong();} static String ns(){return in.nextLine();} static int[] na(int n){int a[]=new int[n];for(int i=0;i<n;i++){a[i]=ni();} 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 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
976f375009d3291fe72bd750f24adcaf
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
// * * * the goal is to be worlds best * * * // import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class A { static class Pair implements Comparable<Pair>{ int a; int b; Pair(int a , int b){ this.a = a; this.b = b; } public int compareTo(Pair o){ return this.a - o.a; } } //================================================================================================== static int countDer(int n) { // Create an array to store // counts for subproblems int der[] = new int[n + 1]; // Base cases der[1] = 0; der[2] = 1; // Fill der[0..n] in bottom up // manner using above recursive // formula for (int i = 3; i <= n; ++i) der[i] = (i - 1) * (der[i - 1] + der[i - 2]); // Return result for n return der[n]; } static void swap(int a[] , int i , int j){ int x = a[i]; int y = a[j]; a[i] = y; a[j] = x; } public static void main(String[] args) { FastScanner sc = new FastScanner(); 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; } boolean is = true; if(n % 2 == 0){ is = false; } for(int i = 0; i < n; i+=2){ if(i + 1 < n){ swap(a , i , i + 1); } } if(is){ swap(a , n - 2 , n - 1); } for(int e : a){ System.out.print(e + " "); } System.out.println(); } } //================================================================================================== // Use this instead of Arrays.sort() on an array of ints. Arrays.sort() is n^2 // worst case since it uses a version of quicksort. Although this would never // actually show up in the real world, in codeforces, people can hack, so // this is needed. static void sort(int[] a) { //ruffle 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; } //then sort Arrays.sort(a); } // Use this to input code since it is faster than a Scanner 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()); } double nextDouble() { return Double.parseDouble(next()); } String str = ""; String nextLine() { try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } // generates all the prime numbers upto n static void sieveOfEratosthenes(int n , ArrayList<Integer> al) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) al.add(i); } } static final int M = 1000_000_000 + 7; static final int imx = Integer.MAX_VALUE; static final int imi = Integer.MIN_VALUE; //fastPow static long fastPow(long base, long exp) { if (exp==0) return 1; long half=fastPow(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } // multiply two long numbers static long mul(long a, long b) { return a*b%M; } static int nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // Returns factorial of n static int fact(int n) { int res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } // to generate the lps array // lps means longest preffix that is also a suffix static void generateLPS(int lps[] , String p){ int l = 0; int r = 1; while(l < p.length() && l < r && r < p.length()){ if(p.charAt(l) == p.charAt(r)){ lps[r] = l + 1; l++; r++; } else{ if(l > 0) l = lps[l - 1]; else r++; } } } // returns the index of the element which is just smaller than or // equal to the tar in the given arraylist static int lowBound(ArrayList<Integer> ll,long tar ,int l,int r){ if(l>r) return l; int mid=l+(r-l)/2; if(ll.get(mid)>=tar){ return lowBound(ll,tar,l,mid-1); } return lowBound(ll,tar,mid+1,r); } // returns the index of the element which is just greater than or // equal to the tar in the given arraylist static int upBound(ArrayList<Integer> ll,long tar,int l ,int r){ if(l>r) return l; int mid=l+(r-l)/2; if(ll.get(mid)<=tar){ return upBound(ll,tar,mid+1 ,r); } return upBound(ll,tar,l,mid-1); } static void swap(int i , int j , int a[]){ int x = a[i]; int y = a[j]; a[j] = x; a[i] = y; } // a -> z == 97 -> 122 // String.format("%.9f", ans) ,--> to get upto 9 decimal places , (ans is double) // write }
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
7e566292c1506867e3fcda25e9277af8
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 q1 { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int tests = Integer.parseInt(br.readLine()); for (int test = 1;test <= tests;test++) { String[] parts = br.readLine().split(" "); int n = Integer.parseInt(parts[0]); if(n % 2 == 0){ for(int i = 1;i < n;i += 2){ System.out.print(i + 1 + " " + i + " "); } System.out.println(); }else{ for(int i = 1;i < n - 2;i += 2){ System.out.print(i + 1 + " " + i + " "); } System.out.println(n + " " + (n - 2) + " " + (n - 1)); System.out.println(); } } } public static long[] mergeSort(long[] arr, int lo, int hi) { if (lo == hi) { long[] base = new long[1]; base[0] = arr[lo]; return base; } int mid = (lo + hi) / 2; long[] a = mergeSort(arr, lo, mid); long[] b = mergeSort(arr, mid + 1, hi); return mergeTwoSortedArrays(a, b); } public static long[] mergeTwoSortedArrays(long[] a, long[] b) { int i = 0, j = 0, k = 0; long[] ans = new long[a.length + b.length]; while (i < a.length && j < b.length) { if (a[i] <= b[j]) { ans[k] = a[i]; i++; k++; } else { ans[k] = b[j]; j++; k++; } } while (i < a.length) { ans[k] = a[i]; k++; i++; } while (j < b.length) { ans[k] = b[j]; k++; j++; } return ans; } }
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
d2181189050a97b897c944650c34d35f
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 javax.print.attribute.IntegerSyntax; import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { static boolean[] primecheck = new boolean[1000000]; public static void main(String[] args) throws IOException { OutputStream outputStream = System.out; FastReader in = new FastReader(); PrintWriter out = new PrintWriter(outputStream); PROBLEM solver = new PROBLEM(); int t = 1; t = in.nextInt(); for (int i = 0; i < t; i++) { solver.solve(in, out); } out.close(); } static class PROBLEM { public void solve(FastReader in, PrintWriter out){ int n = in.nextInt(); int[] a = new int[n]; if(n%2 == 1) { for (int i = 3; i < n - 1; i += 2) { a[i] = i + 2; a[i + 1] = i + 1; } if (n >= 3) { a[0] = 3; a[1] = 1; a[2] = 2; } } else{ for (int i = 0; i < n - 1; i += 2) { a[i] = i + 2; a[i + 1] = i + 1; } if (n == 2) { a[0] = 2; a[1] = 1; } } for (int i = 0; i < n; i++) { out.print(a[i] + " "); } out.println(); } } static boolean isPalindrome(char[] s){ int flag = 1; for (int i = 0; i < s.length; i++) { if(s[i] != s[s.length-1-i]){ flag = 0; break; } } if(flag == 1) return true; else return false; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean isSquare(double a) { boolean isSq = false; double b = Math.sqrt(a); double c = Math.sqrt(a) - Math.floor(b); if (c == 0) isSq = true; return isSq; } static int exponentMod(int A, int B, int C) { // Base cases if (A == 0) return 0; if (B == 0) return 1; // If B is even long y; if (B % 2 == 0) { y = exponentMod(A, B / 2, C); y = (y * y) % C; } // If B is odd else { y = A % C; y = (y * exponentMod(A, B - 1, C) % C) % C; } return (int) ((y + C) % C); } public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } public int hashCode() { return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode()); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<U, V> p = (Pair<U, V>) o; return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y)); } public int compareTo(Pair<U, V> b) { int cmpU = x.compareTo(b.x); return cmpU != 0 ? cmpU : y.compareTo(b.y); } public String toString() { return String.format("(%s, %s)", x.toString(), y.toString()); } } 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()); } char nextChar() { return next().charAt(0); } boolean nextBoolean() { return !(nextInt() == 0); } // boolean nextBoolean(){return Boolean.parseBoolean(next());} String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int[] readArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } } private static int[] mergeSort(int[] array) { if (array.length <= 1) { return array; } int midpoint = array.length / 2; int[] left = new int[midpoint]; int[] right; if (array.length % 2 == 0) { right = new int[midpoint]; } else { right = new int[midpoint + 1]; } for (int i = 0; i < left.length; i++) { left[i] = array[i]; } for (int i = 0; i < right.length; i++) { right[i] = array[i + midpoint]; } int[] result = new int[array.length]; left = mergeSort(left); right = mergeSort(right); result = merge(left, right); return result; } private static int[] merge(int[] left, int[] right) { int[] result = new int[left.length + right.length]; int leftPointer = 0, rightPointer = 0, resultPointer = 0; while (leftPointer < left.length || rightPointer < right.length) { if (leftPointer < left.length && rightPointer < right.length) { if (left[leftPointer] < right[rightPointer]) { result[resultPointer++] = left[leftPointer++]; } else { result[resultPointer++] = right[rightPointer++]; } } else if (leftPointer < left.length) { result[resultPointer++] = left[leftPointer++]; } else { result[resultPointer++] = right[rightPointer++]; } } return result; } public static void Sieve(int n){ Arrays.fill(primecheck,true); primecheck[0]= false; primecheck[1] = false; for(int i = 2;i*i<n+1;i++){ if(primecheck[i]){ for(int j = i*2;j<n+1;j+=i){ primecheck[j] = false; } } } } }
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
658928d0b51fedb9339df878df3d8b90
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 void swap(int[]arr, int a,int b){ int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } static long abs(int[] arr, int a,int b){ if ( arr[a]>=arr[b]) return arr[a]-arr[b]; return arr[b]-arr[a]; } public static boolean isPrime(long n) { if (n <= 1) { return false; } for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } static long gcd(long a,long b){ if ( b==0) return a; return gcd(b,a%b); } static long lcm(long a,long b){ return (a*b)/gcd(a,b); } static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int test = sc.nextInt(); for ( int t=0; t<test; t++){ int n = sc.nextInt(); if ( n%2==0){ for ( int i=0; i<n; i++){ if ( i%2==0) System.out.print((i+2)+" "); else System.out.print((i)+" "); } System.out.println(); } else { for ( int i=0; i<n-2; i++){ if ( i%2==0) System.out.print((i+2)+" "); else System.out.print((i)+" "); } System.out.print(n+" "); System.out.print((n-2)); System.out.println(); } } } } // 5 // 7 1 // 0000000 // 7 3 // 0001000 // 5 2 // 00000 // 4 2 // 0000 // 6 4 // 000000 // 4 0 2 2 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
a15d64e91badecf39f6be5c9d082a688
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
/* Junaeid As Jknight Somoy paile dekha koiro alap-adda-gopposob hobe cha or coffee er sathe. ------<<<<<<*>>>>>>>>----- D-33 Problem - Pretty Permutation - 1541A */ import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.IOException; public class PrettyPermutation { static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null || !st.hasMoreTokens()){ 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().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } public static void main(String[] args){ try { FastReader s = new FastReader(); FastWriter out = new FastWriter(); int testCases = s.nextInt(),n,m; while(testCases-- > 0){ n = s.nextInt(); if(n%2==0){ for (int i = 2; i <= n; i+=2) { out.print(i+" "+(i-1)+" "); } out.println(""); }else{ out.print("3 1 2 "); for (int i = 5; i <= n ; i+=2) { out.print(i+" "+(i-1)+" "); } out.println(""); } } out.close(); } catch (Exception e) { return; } } }
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
f570d023c19af81203172edea062cf44
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.*; public class Codechef { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[200001]; // line length 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 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; } 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; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); }} static void cout(Object line) {System.out.println(line);} static void iop() { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); }} static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b);} static boolean isPerfectSquare(long n){ if (Math.ceil((double)Math.sqrt(n)) ==Math.floor((double)Math.sqrt(n))) return true; else return false;} static boolean isPrime(int n){ if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true;} static int fact(int n){ return (n == 1 || n == 0) ? 1 : n * fact(n - 1);} static boolean isPowerOfTwo (int x) { /* First x in the below expression is for the case when x is 0 */ return x!=0 && ((x&(x-1)) == 0);} static void printArray(int arr[]) { for(int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); }} public static void main(String[] args) throws IOException{ iop(); Reader sr = new Reader(); 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; } int i = 0; if(n % 2 != 0) { arr[0] = 3; arr[1] = 1; arr[2] = 2; i = 3; } for(;i < n-1; i++) { int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; i++; } for(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 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
f890d081f873ec6182acbd35bb1cf8f5
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
//Gaurav...... 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.io.*; import java.util.*; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import java.math.*; public class Main{ //start public static void main(String[] args) { FastScanner sc=new FastScanner(); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int a[]=new int[n]; if(n%2==0){ for(int i=1;i<=n;i++){ if(i%2!=0){ a[i-1]=i+1; }else{ a[i-1]=i-1; } } }else{ for(int i=n;i>1;i--){ if(i%2!=0){ a[i-1]=i-1; }else{ a[i-1]=i+1; } } a[0]=1; int temp=a[0]; a[0]=a[1]; a[1]=temp; } for(int i=0;i<n;i++){ System.out.print(a[i]+" "); } System.out.println(); } } //end 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()); } 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 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
68cfcd42483244bd0e9fbdb8c7a6a92e
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
//jiudge: 3905 import java.util.Scanner; public class J { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); for(int i = 1; i <= N; i ++ ) { int n = sc.nextInt(); if(n % 2 == 0) { for(int j = 1; j <= n / 2; j ++ ) { System.out.printf(j * 2 + " " + (j * 2 - 1) + " "); } }else { System.out.printf(3 + " " + 1 + " " + 2 + " "); for(int j = 2; j <= n / 2; j ++ ) { System.out.printf((j * 2 + 1) + " " + j * 2 + " "); } } 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
aedabbbae40f4f35f87c198d9f2c251f
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.Arrays; import java.util.Collections; // import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.ArrayList; import java.util.Set; import java.util.Stack; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.IntStream; // import java.util.Set; // import java.util.TreeSet; // import java.util.stream.Collectors; // import java.util.stream.Stream; // import java.util.Stack; // import java.util.Optional; // import java.util.HashSet; import java.io.*; 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!=0){ System.out.print("3 1 2 "); AtomicInteger at = new AtomicInteger(); at.set(4); IntStream.range(1,(n/2)).forEach(i->{ System.out.print((at.incrementAndGet())+" "+(at.incrementAndGet()-2)+" "); }); } else{ AtomicInteger at = new AtomicInteger(); at.set(1); IntStream.range(1,(n/2)+1).forEach(i->{ System.out.print((at.incrementAndGet())+" "+(at.incrementAndGet()-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
ff31098adae1e7fc6659c20efe51cc85
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.*; import static java.lang.Math.*; import static java.lang.Math.ceil; import static java.util.Arrays.sort; public class Round12 { public static void main(String[] args) { FastReader fastReader = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = fastReader.nextInt(); while (t-- > 0) { int n = fastReader.nextInt(); StringBuilder ans = new StringBuilder(); if (n % 2 == 0) { for (int i = 0; i < n - 1; i += 2) { ans.append(i + 2).append(" ").append(i + 1).append(" "); } } else { ans.append(3).append(" ").append(1).append(" ").append(2).append(" "); for (int i = 4; i <= n; i+=2) { ans.append(i + 1).append(" ").append(i ).append(" "); } } out.println(ans); } out.close(); } // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final long LMAX = 9223372036854775807L; static Random __r = new Random(); // math util static int minof(int a, int b, int c) { return min(a, min(b, c)); } static int minof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static long minof(long a, long b, long c) { return min(a, min(b, c)); } static long minof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static int maxof(int a, int b, int c) { return max(a, max(b, c)); } static int maxof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static long maxof(long a, long b, long c) { return max(a, max(b, c)); } static long maxof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static int powi(int a, int b) { if (a == 0) return 0; int ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static long powl(long a, int b) { if (a == 0) return 0; long ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static int fli(double d) { return (int) d; } static int cei(double d) { return (int) ceil(d); } static long fll(double d) { return (long) d; } static long cel(double d) { return (long) ceil(d); } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static int[] exgcd(int a, int b) { if (b == 0) return new int[]{1, 0}; int[] y = exgcd(b, a % b); return new int[]{y[1], y[0] - y[1] * (a / b)}; } static long[] exgcd(long a, long b) { if (b == 0) return new long[]{1, 0}; long[] y = exgcd(b, a % b); return new long[]{y[1], y[0] - y[1] * (a / b)}; } static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static long mix(long x) { x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31); } public static boolean[] findPrimes(int limit) { assert limit >= 2; final boolean[] nonPrimes = new boolean[limit]; nonPrimes[0] = true; nonPrimes[1] = true; int sqrt = (int) Math.sqrt(limit); for (int i = 2; i <= sqrt; i++) { if (nonPrimes[i]) continue; for (int j = i; j < limit; j += i) { if (!nonPrimes[j] && i != j) nonPrimes[j] = true; } } return nonPrimes; } // array util static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } 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; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } 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 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
d29fda8988e5a7c8076b869d1ce9acea
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 s = new Scanner(System.in); int t = s.nextInt(); while(t-->0) { int n = s.nextInt(); int arr[] = new int[n+1]; for(int i = 1; i<=n; i++) { arr[i] = i; } if(n%2 == 0) { for(int i = 1; i<n; i++) { int tem = arr[i]; arr[i] = arr[i+1]; arr[i+1] = tem; i++; } }else { if(n==3) { System.out.print(3+" "+1+" "+2+" "); continue; }else { arr[3] = 2; arr[2] = 1; arr[1] = 3; for(int i = 4; i<n; i++) { int tem = arr[i]; arr[i] = arr[i+1]; arr[i+1] = tem; i++; } } } for(int i = 1; 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 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
2d4f0e7c24e895208df732cd05dbcc6b
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 static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.Math.sqrt; import static java.lang.Math.pow; import static java.lang.System.out; import static java.lang.System.err; import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc; static long mod=((long)1e9)+7; // static FastWriter out; public static void main(String hi[]){ initializeIO(); sc=new FastReader(); int t=sc.nextInt(); // boolean[] seave=sieveOfEratosthenes((int)(1e7)); // int[] seave2=sieveOfEratosthenesInt((int)(1e4)); // int tp=1; while(t--!=0){ int n=sc.nextInt(); int[] arr=new int[n]; for(int i=1;i<=n;i++)arr[i-1]=i; debug(arr); for(int i=1;i<n;i+=2){ int tp=arr[i]; arr[i]=arr[i-1]; arr[i-1]=tp; } if(n%2!=0){ int tp=arr[n-1]; arr[n-1]=arr[n-2]; arr[n-2]=tp; } for(int x:arr)out.print(x+" "); out.println(); } // System.out.println(String.format("%.9f", max)); } static Boolean[][] dp; static List<Integer> res=new ArrayList<>(); private static boolean solve(long[] arr,long w,long c,int i,List<Integer> li){ int n=arr.length; long mid=(long)Math.ceil((1f*w)/(1f*2)); if(c>=(mid)&&c<=w){ res=new ArrayList<>(li); return true; } if(i>=n||c>w)return false; if(dp[i][(int)c]!=null)return dp[i][(int)c]; li.add(i+1); boolean left=false; if(c+arr[i]<=w)left=left|solve(arr,w,c+arr[i],i+1,li); li.remove(li.size()-1); boolean right=solve(arr,w,c,i+1,li); return dp[i][(int)c]=left|right; } private static int sizeOfSubstring(int st,int e){ int s=e-st+1; return (s*(s+1))/2; } private static int lessThen(long[] nums,long val){ int i=0,l=0,r=nums.length-1; while(l<=r){ int mid=l+((r-l)/2); if(nums[mid]<=val){ i=mid; l=mid+1; }else{ r=mid-1; } } return i; } private static int lessThen(List<Long> nums,long val){ int i=0,l=0,r=nums.size()-1; while(l<=r){ int mid=l+((r-l)/2); if(nums.get(mid)<=val){ i=mid; l=mid+1; }else{ r=mid-1; } } return i; } private static int greaterThen(long[] nums,long val){ int i=nums.length-1,l=0,r=nums.length-1; while(l<=r){ int mid=l+((r-l)/2); if(nums[mid]>=val){ i=mid; r=mid-1; }else{ l=mid+1; } } return i; } private static long sumOfAp(long a,long n,long d){ long val=(n*( 2*a+((n-1)*d))); return val/2; } //geometrics private static double areaOftriangle(double x1,double y1,double x2,double y2,double x3,double y3){ double[] mid_point=midOfaLine(x1,y1,x2,y2); // debug(Arrays.toString(mid_point)+" "+x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+" "+y3); double height=distanceBetweenPoints(mid_point[0],mid_point[1],x3,y3); double wight=distanceBetweenPoints(x1,y1,x2,y2); // debug(height+" "+wight); return (height*wight)/2; } private static double distanceBetweenPoints(double x1,double y1,double x2,double y2){ double x=x2-x1; double y=y2-y1; return sqrt(Math.pow(x,2)+Math.pow(y,2)); } private static double[] midOfaLine(double x1,double y1,double x2,double y2){ double[] mid=new double[2]; mid[0]=(x1+x2)/2; mid[1]=(y1+y2)/2; return mid; } private static long sumOfN(long n){ return (n*(n+1))/2; } private static long power(long x,long y,long p){ long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0){ // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } /* Function to calculate x raised to the power y in O(logn)*/ static long power(long x, long y){ long temp; if( y == 0) return 1l; temp = power(x, y / 2); if (y % 2 == 0) return (temp*temp); else return (x*temp*temp); } private static StringBuilder reverseString(String s){ StringBuilder sb=new StringBuilder(s); int l=0,r=sb.length()-1; while(l<=r){ char ch=sb.charAt(l); sb.setCharAt(l,sb.charAt(r)); sb.setCharAt(r,ch); l++; r--; } return sb; } private static void swap(long arr[],int i,int j){ long t=arr[i]; arr[i]=arr[j]; arr[j]=t; } private static void swap(List<Integer> li,int i,int j){ int t=li.get(i); li.set(i,li.get(j)); li.set(j,t); } private static void swap(int arr[],int i,int j){ int t=arr[i]; arr[i]=arr[j]; arr[j]=t; } private static void swap(double arr[],int i,int j){ double t=arr[i]; arr[i]=arr[j]; arr[j]=t; } private static void swap(float arr[],int i,int j){ float t=arr[i]; arr[i]=arr[j]; arr[j]=t; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long lcm(long a, long b){ return (a / gcd(a, b)) * b; } private static String decimalToString(int x){ return Integer.toBinaryString(x); } private static boolean isPallindrome(String s){ int l=0,r=s.length()-1; while(l<r){ if(s.charAt(l)!=s.charAt(r))return false; l++; r--; } return true; } private static StringBuilder removeLeadingZero(StringBuilder sb){ int i=0; while(i<sb.length()&&sb.charAt(i)=='0')i++; // debug("remove "+i); if(i==sb.length())return new StringBuilder(); return new StringBuilder(sb.substring(i,sb.length())); } private static StringBuilder removeEndingZero(StringBuilder sb){ int i=sb.length()-1; while(i>=0&&sb.charAt(i)=='0')i--; // debug("remove "+i); if(i<0)return new StringBuilder(); return new StringBuilder(sb.substring(0,i+1)); } private static int stringToDecimal(String binaryString){ // debug(decimalToString(n<<1)); return Integer.parseInt(binaryString,2); } private static int stringToInt(String s){ return Integer.parseInt(s); } private static String toString(int val){ return String.valueOf(val); } private static void debug(int[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(long[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(List<int[]> arr){ for(int[] a:arr){ err.println(Arrays.toString(a)); } } private static void debug(float[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(double[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(boolean[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void print(String s){ out.println(s); } private static void debug(String s){ err.println(s); } private static int charToIntS(char c){ return ((((int)(c-'0'))%48)); } private static void print(double s){ out.println(s); } private static void print(float s){ out.println(s); } private static void print(long s){ out.println(s); } private static void print(int s){ out.println(s); } private static void debug(double s){ err.println(s); } private static void debug(float s){ err.println(s); } private static void debug(long s){ err.println(s); } private static void debug(int s){ err.println(s); } private 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; } //read graph private static List<List<Integer>> readUndirectedGraph(int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<n;i++){ int x=sc.nextInt(); int y=sc.nextInt(); graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readUndirectedGraph(int[][] intervals,int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<intervals.length;i++){ int x=intervals[i][0]; int y=intervals[i][1]; graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int[][] intervals,int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<intervals.length;i++){ int x=intervals[i][0]; int y=intervals[i][1]; graph.get(x).add(y); // graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<n;i++){ int x=sc.nextInt(); int y=sc.nextInt(); graph.get(x).add(y); // graph.get(y).add(x); } return graph; } static String[] readStringArray(int n){ String[] arr=new String[n]; for(int i=0;i<n;i++){ arr[i]=sc.next(); } return arr; } private static Map<Character,Integer> freq(String s){ Map<Character,Integer> map=new HashMap<>(); for(char c:s.toCharArray()){ map.put(c,map.getOrDefault(c,0)+1); } return map; } static boolean[] sieveOfEratosthenes(long n){ boolean prime[] = new boolean[(int)n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true){ // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } static int[] sieveOfEratosthenesInt(long n){ boolean prime[] = new boolean[(int)n + 1]; Set<Integer> li=new HashSet<>(); for (int i = 1; i <= n; i++){ prime[i] = true; li.add(i); } for (int p = 2; p * p <= n; p++) { if (prime[p] == true){ for (int i = p * p; i <= n; i += p){ li.remove(i); prime[i] = false; } } } int[] arr=new int[li.size()+1]; int i=0; for(int x:li){ arr[i++]=x; } return arr; } public static long Kadens(List<Long> prices) { long sofar=0; long max_v=0; for(int i=0;i<prices.size();i++){ sofar+=prices.get(i); if (sofar<0) { sofar=0; } max_v=Math.max(max_v,sofar); } return max_v; } public static int Kadens(int[] prices) { int sofar=0; int max_v=0; for(int i=0;i<prices.length;i++){ sofar+=prices[i]; if (sofar<0) { sofar=0; } max_v=Math.max(max_v,sofar); } return max_v; } static boolean isMemberAC(int a, int d, int x){ // If difference is 0, then x must // be same as a. if (d == 0) return (x == a); // Else difference between x and a // must be divisible by d. return ((x - a) % d == 0 && (x - a) / d >= 0); } private static void sort(int[] arr){ int n=arr.length; List<Integer> li=new ArrayList<>(); for(int x:arr){ li.add(x); } Collections.sort(li); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sortReverse(int[] arr){ int n=arr.length; List<Integer> li=new ArrayList<>(); for(int x:arr){ li.add(x); } Collections.sort(li,Collections.reverseOrder()); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sort(double[] arr){ int n=arr.length; List<Double> li=new ArrayList<>(); for(double x:arr){ li.add(x); } Collections.sort(li); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sortReverse(double[] arr){ int n=arr.length; List<Double> li=new ArrayList<>(); for(double x:arr){ li.add(x); } Collections.sort(li,Collections.reverseOrder()); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sortReverse(long[] arr){ int n=arr.length; List<Long> li=new ArrayList<>(); for(long x:arr){ li.add(x); } Collections.sort(li,Collections.reverseOrder()); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sort(long[] arr){ int n=arr.length; List<Long> li=new ArrayList<>(); for(long x:arr){ li.add(x); } Collections.sort(li); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static long sum(int[] arr){ long sum=0; for(int x:arr){ sum+=x; } return sum; } private static long evenSumFibo(long n){ long l1=0,l2=2; long sum=0; while (l2<n) { long l3=(4*l2)+l1; sum+=l2; if(l3>n)break; l1=l2; l2=l3; } return sum; } private static void initializeIO(){ try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); System.setErr(new PrintStream(new FileOutputStream("error.txt"))); } catch (Exception e) { // System.err.println(e.getMessage()); } } private static int maxOfArray(int[] arr){ int max=Integer.MIN_VALUE; for(int x:arr){ max=Math.max(max,x); } return max; } private static long maxOfArray(long[] arr){ long max=Long.MIN_VALUE; for(long x:arr){ max=Math.max(max,x); } return max; } private static int[][] readIntIntervals(int n,int m){ int[][] arr=new int[n][m]; for(int j=0;j<n;j++){ for(int i=0;i<m;i++){ arr[j][i]=sc.nextInt(); } } return arr; } private static long gcd(long a,long b){ if(b==0)return a; return gcd(b,a%b); } private static int gcd(int a,int b){ if(b==0)return a; return gcd(b,a%b); } private static int[] readIntArray(int n){ int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } return arr; } private static double[] readDoubleArray(int n){ double[] arr=new double[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextDouble(); } return arr; } private static long[] readLongArray(int n){ long[] arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); } return arr; } private static void print(int[] arr){ out.println(Arrays.toString(arr)); } private static void print(long[] arr){ out.println(Arrays.toString(arr)); } private static void print(String[] arr){ out.println(Arrays.toString(arr)); } private static void print(double[] arr){ out.println(Arrays.toString(arr)); } private static void debug(String[] arr){ err.println(Arrays.toString(arr)); } private static void debug(Boolean[][] arr){ for(int i=0;i<arr.length;i++)err.println(Arrays.toString(arr[i])); } private static void debug(int[] arr){ err.println(Arrays.toString(arr)); } private static void debug(long[] arr){ err.println(Arrays.toString(arr)); } static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null || !st.hasMoreTokens()){ 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().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } static class Dsu { int[] parent, size; Dsu(int n) { parent = new int[n + 1]; size = new int[n + 1]; for (int i = 0; i <= n; i++) { parent[i] = i; size[i] = 1; } } private int findParent(int u) { if (parent[u] == u) return u; return parent[u] = findParent(parent[u]); } private boolean union(int u, int v) { // System.out.println("uf "+u+" "+v); int pu = findParent(u); // System.out.println("uf2 "+pu+" "+v); int pv = findParent(v); // System.out.println("uf3 " + u + " " + pv); if (pu == pv) return false; if (size[pu] <= size[pv]) { parent[pu] = pv; size[pv] += size[pu]; } else { parent[pv] = pu; size[pu] += size[pv]; } 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 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
4b300f2af97d8048cbfaa6dba3ac3546
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 a = sc.nextInt(); if(a % 2 == 0){ for (int j = 1; j <= a ; j+=2) { System.out.print(j+1 + " " + j + " "); } }else{ System.out.print(3 + " " + 1 + " " + 2 + " "); for (int j = 4; j <= a; j+=2) { System.out.print(j+1 + " " + j + " "); } } 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
d879a98949791151528dde5ecbc30c07
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.*; public class PrettyPerm { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for(int k = 0; k < t; k++) { int n = Integer.parseInt(br.readLine()); int dist; if(n % 2 == 0) { int index = 2; System.out.print(2); for(int i = 1; i < n; i++) { if(i % 2 == 0) index = i + 2; else index--; System.out.print(" " + index); } System.out.println(); } else { int index = 2; for(int i = 0; i < n - 3; i++) { if(i % 2 == 0) index = i + 2; else index--; System.out.print(index + " "); } System.out.println(n + " " + (n - 2) + " " + (n - 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 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
7423becc04a074992bb38cc32c420ed3
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 A { 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(); if (n%2==1) { for (int i=1;i<=n-3;i+=2) System.out.print(i+1+" "+i+" "); System.out.println(n+" "+(n-2)+" "+(n-1)); } else { for (int i=1;i<=n;i+=2) System.out.print(i+1+" "+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 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
e5447959a753723f2d39f4d522d81f3c
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 in = new Scanner(System.in); int nt, n, x; nt = in.nextInt(); while(nt > 0){ n = in.nextInt(); int [] arr = new int [n]; for(int i = 0; i < n; i ++){ arr[i] = i + 1; } for(int i = 0; i < n ; i ++){ if(i % 2 == 0 && i + 1 < n){ x = arr[i + 1]; arr[i + 1] = arr[i]; arr[i] = x; } } if(n % 2 != 0){ x = arr[n - 1]; arr[n - 1] = arr[n - 2]; arr[n - 2] = x; } for(int i = 0; i < n ; i ++){ System.out.print(arr[i] + " "); } System.out.println(); nt --; } } }
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 17
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
434ff5da14f1c9fbc06dcd08ce0d197e
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.util.ArrayList; public class A_Pretty_Permutations{ 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(); List<Integer> list=new ArrayList<>(); if(n%2==0){ for(int j=0;j<n;j++){ if(j%2==0){ list.add(j,j+2); } else{ list.add(j,j); } System.out.print(list.get(j)+" "); } System.out.println(); } else{ for(int j=0;j<n;j++){ if(j==0){ list.add(j,3); } else if(j==1){ list.add(j,1); } else if(j==2){ list.add(j,2); } else if(j%2==0){ list.add(j,j); } else if(j%2==1){ list.add(j,j+2); } System.out.print(list.get(j)+" "); } 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 17
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
b65cd3795106f3a826a3b6e7a5f10682
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; import java.util.stream.IntStream; public class Problem_1541A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); ArrayList<int[]> ans = new ArrayList<>(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int[] arr = IntStream.range(0, n).map(j -> j + 1).toArray(); int start = 0; if(n % 2 != 0) { int third = arr[2]; arr[2] = arr[1]; arr[1] = arr[0]; arr[0] = third; start = 3; } for (int j = start; j < n; j += 2) { int temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; } ans.add(arr); } for(int[] e: ans) { for(int f: e) { System.out.print(f + " "); } 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 17
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
d69d530be0e3102da1b7ef3a2f7c26fd
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 Solution{ public static void main(String[] args){ int test; Scanner sc = new Scanner(System.in); test = sc.nextInt(); for(int i = 0 ; i < test ; i++){ int size = sc.nextInt(); int[] arr = new int[size]; for(int j = 1 ; j <= size ; j++){ arr[j-1] = j; } for(int j = 0 ; j < size - 1 ; j += 2){ int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } if(size % 2 == 1){ int temp = arr[size - 2]; arr[size-2] = arr[size-1]; arr[size-1] = temp; } for(int j = 0 ; j < size ; j++){ System.out.print(arr[j] + " "); } 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 17
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
94dfc8ec44a6f5645dcc642dea10d97d
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 testCases = sc.nextInt(); while (testCases-- > 0) { int cats = sc.nextInt(); if (cats %2 == 0) { for (int i = 0; i < cats; i += 2) { System.out.print( (i+2) +" "+ (i+1) +" "); } }else{ for (int i = 0; i < cats - 3; i += 2) { System.out.print( (i+2) +" "+ (i+1) +" "); } System.out.print((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 17
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
8a30591752025866622f8d95e7b7977f
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 static java.lang.Math.*; public class Main { public static void main(String[] args) throws IOException { OutputStreamWriter osr = new OutputStreamWriter(System.out); PrintWriter o = new PrintWriter(osr); FastReader fr = new FastReader(); int t = fr.nextInt(); while (t-- != 0) { int n = fr.nextInt(); if(n%2==0) { for (int i = 1; i <= n; i++) { if (i % 2 == 0) o.print((i - 1) + " "); else o.print((i + 1) + " "); } } else { for (int i = 1; i <= n-2; i++) { if (i % 2 == 0) o.print((i - 1) + " "); else o.print((i + 1) + " "); } o.print(n + " " + (n-2)); } o.println(); } o.close(); } } class FastReader { // Attributes : BufferedReader br; StringTokenizer st; // Constructor : public FastReader() { InputStreamReader isr = new InputStreamReader(System.in); br = new BufferedReader(isr); } // Operations : // #01 : public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } // #02 : public String nextLine() throws IOException { return br.readLine(); } // #03 : public int nextInt() throws IOException { return Integer.parseInt(next()); } // #04 : public long nextLong() throws IOException { return Long.parseLong(next()); } // #05 : public double nextDouble() throws IOException { return Double.parseDouble(next()); } // #06 : public int [] intArray (int size) throws IOException{ int [] arr = new int[size]; for (int i = 0 ; i < size; i++) arr[i] = nextInt(); return arr; } // #07 : public char [] charArray() throws IOException { return nextLine().toCharArray(); } } class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } static class Compare implements Comparator<Pair> { @Override public int compare(Pair o1, Pair o2) { return (o1.y - o2.y); } } } /* if(arr[i] > arr[i+1] && !lid[i] && lid[i+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 17
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
b4cbec08003fca1922e9e26f02ada5d7
train_107.jsonl
1624635300
Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory.
256 megabytes
//package codeforces; import java.io.PrintWriter; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class solution { public static void main(String args[]){ Scanner s=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int t=s.nextInt(); for(int tt=0;tt<t;tt++) { int n=s.nextInt(); long a[]=new long[n]; for(int i=0;i<n;i++) { a[i]=s.nextInt(); } sort(a); long b[]=new long[n-1]; for(int i=0;i<n-1;i++) { b[i]=a[i+1]-a[i]; } long ans=0; for(int i=0;i<n-1;i++) { ans-=(((i+1)*(long)(n-i-1))-1)*b[i]; } out.println(ans); } out.close(); s.close(); } 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 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 int gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); } static long[][] sortcol(long a[][],int c) { Arrays.sort(a, (x, y) -> { if(x[c]>y[c]) { return 1; }else if(x[c]<y[c]) { return -1; }else { if(x[0]>y[0]) { return 1; }{ return -1; } } }); return a; } public static void printb(boolean ans) { if(ans) { System.out.println("Yes"); }else { System.out.println("No"); } } 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(); } double nextDouble() { return Double.parseDouble(next()); } 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 class Pair implements Comparable<Pair>{ int a , b; Pair(int x , int y){ a=x; b=y; } public int compareTo(Pair o) { return a != o.a ? a - o.a : b - o.b; } } }
Java
["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"]
2 seconds
["-3\n0\n0"]
NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$.
Java 11
standard input
[ "constructive algorithms", "graphs", "greedy", "shortest paths", "sortings" ]
7dfe0db5a99e6e4d71eb012fab07685b
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,400
For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory.
standard output
PASSED
81c1649627f3a31d490be8b9150140b3
train_107.jsonl
1624635300
Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory.
256 megabytes
import java.io.PrintWriter; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class solution { public static void main(String args[]){ Scanner s=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int t=s.nextInt(); for(int tt=0;tt<t;tt++) { int n=s.nextInt(); long a[]=new long[n]; for(int i=0;i<n;i++) { a[i]=s.nextInt(); } sort(a); long b[]=new long[n-1]; for(int i=0;i<n-1;i++) { b[i]=a[i+1]-a[i]; } long ans=0; n--; for(int i=0;i<n;i++) { ans-=((i+1)*(long)(n-i)*(b[i])); } out.println(ans+a[n]); } out.close(); s.close(); } 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 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 int gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); } static long[][] sortcol(long a[][],int c) { Arrays.sort(a, (x, y) -> { if(x[c]>y[c]) { return 1; }else if(x[c]<y[c]) { return -1; }else { if(x[0]>y[0]) { return 1; }{ return -1; } } }); return a; } public static void printb(boolean ans) { if(ans) { System.out.println("Yes"); }else { System.out.println("No"); } } 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(); } double nextDouble() { return Double.parseDouble(next()); } 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 class Pair implements Comparable<Pair>{ int a , b; Pair(int x , int y){ a=x; b=y; } public int compareTo(Pair o) { return a != o.a ? a - o.a : b - o.b; } } }
Java
["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"]
2 seconds
["-3\n0\n0"]
NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$.
Java 11
standard input
[ "constructive algorithms", "graphs", "greedy", "shortest paths", "sortings" ]
7dfe0db5a99e6e4d71eb012fab07685b
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,400
For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory.
standard output
PASSED
867fb3097d9f7420fa131819e9382d54
train_107.jsonl
1624635300
Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory.
256 megabytes
//package codeforces; import java.io.PrintWriter; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class solution { public static void main(String args[]){ Scanner s=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int t=s.nextInt(); for(int tt=0;tt<t;tt++) { int n=s.nextInt(); long a[]=new long[n]; for(int i=0;i<n;i++) { a[i]=s.nextInt(); } sort(a); long b[]=new long[n-1]; for(int i=0;i<n-1;i++) { b[i]=a[i+1]-a[i]; } long ans=0; n--; for(int i=0;i<n/2;i++) { ans-=((i+1)*(long)(n-i)*(b[i]+b[n-i-1])); } if(n%2!=0) { ans-=((n/2+1)*(long)(n-n/2)*b[n/2]); } out.println(ans+a[n]); } out.close(); s.close(); } 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 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 int gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); } static long[][] sortcol(long a[][],int c) { Arrays.sort(a, (x, y) -> { if(x[c]>y[c]) { return 1; }else if(x[c]<y[c]) { return -1; }else { if(x[0]>y[0]) { return 1; }{ return -1; } } }); return a; } public static void printb(boolean ans) { if(ans) { System.out.println("Yes"); }else { System.out.println("No"); } } 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(); } double nextDouble() { return Double.parseDouble(next()); } 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 class Pair implements Comparable<Pair>{ int a , b; Pair(int x , int y){ a=x; b=y; } public int compareTo(Pair o) { return a != o.a ? a - o.a : b - o.b; } } }
Java
["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"]
2 seconds
["-3\n0\n0"]
NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$.
Java 11
standard input
[ "constructive algorithms", "graphs", "greedy", "shortest paths", "sortings" ]
7dfe0db5a99e6e4d71eb012fab07685b
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,400
For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory.
standard output
PASSED
f1bc886b204b33ff2fdf2c63b3978919
train_107.jsonl
1624635300
Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory.
256 megabytes
//package codeforces; import java.io.PrintWriter; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class solution { public static void main(String args[]){ Scanner s=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int t=s.nextInt(); for(int tt=0;tt<t;tt++) { int n=s.nextInt(); long a[]=new long[n]; for(int i=0;i<n;i++) { a[i]=s.nextInt(); } sort(a); long b[]=new long[n-1]; for(int i=0;i<n-1;i++) { b[i]=a[i+1]-a[i]; } long ans=0; n--; for(int i=0;i<n/2;i++) { ans-=((i+1)*(long)(n-i)*(b[i]+b[n-i-1]))-b[i]-b[n-i-1]; } if(n%2!=0) { ans-=((n/2+1)*(long)(n-n/2)*b[n/2])-b[n/2]; } out.println(ans); } out.close(); s.close(); } 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 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 int gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); } static long[][] sortcol(long a[][],int c) { Arrays.sort(a, (x, y) -> { if(x[c]>y[c]) { return 1; }else if(x[c]<y[c]) { return -1; }else { if(x[0]>y[0]) { return 1; }{ return -1; } } }); return a; } public static void printb(boolean ans) { if(ans) { System.out.println("Yes"); }else { System.out.println("No"); } } 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(); } double nextDouble() { return Double.parseDouble(next()); } 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 class Pair implements Comparable<Pair>{ int a , b; Pair(int x , int y){ a=x; b=y; } public int compareTo(Pair o) { return a != o.a ? a - o.a : b - o.b; } } }
Java
["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"]
2 seconds
["-3\n0\n0"]
NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$.
Java 11
standard input
[ "constructive algorithms", "graphs", "greedy", "shortest paths", "sortings" ]
7dfe0db5a99e6e4d71eb012fab07685b
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,400
For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory.
standard output
PASSED
f56b2eece5feb5aae0b925e0aa3245d1
train_107.jsonl
1624635300
Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class ProblemC { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(in.readLine()); while (t-- > 0) { int n = Integer.parseInt(in.readLine()); String[] temp = in.readLine().split(" "); long[] d = new long[n]; for (int i = 0; i < n; i++) { d[i] = Long.parseLong(temp[i]); } Arrays.sort(d); long cost = d[n - 1]; for (int i = 1; i < n; i++) { cost -= (long) i * (n - i) * (d[i] - d[i - 1]); } System.out.println(cost); } } }
Java
["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"]
2 seconds
["-3\n0\n0"]
NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$.
Java 11
standard input
[ "constructive algorithms", "graphs", "greedy", "shortest paths", "sortings" ]
7dfe0db5a99e6e4d71eb012fab07685b
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,400
For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory.
standard output
PASSED
2d2afafd9ebeb3eb4bac9f998e38c2d8
train_107.jsonl
1624635300
Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class ProblemC { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(in.readLine()); while (t-- > 0) { int n = Integer.parseInt(in.readLine()); String[] temp = in.readLine().split(" "); long[] d = new long[n]; for (int i = 0; i < n; i++) { d[i] = Long.parseLong(temp[i]); } Arrays.sort(d); long cost = d[n - 1]; for (int i = 1; i < n; i++) { cost -= (d[i] - d[i - 1]) * i * (n - i); } System.out.println(cost); } } }
Java
["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"]
2 seconds
["-3\n0\n0"]
NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$.
Java 11
standard input
[ "constructive algorithms", "graphs", "greedy", "shortest paths", "sortings" ]
7dfe0db5a99e6e4d71eb012fab07685b
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,400
For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory.
standard output