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
ae3a3897e66c078e27ebd9cd053499bc
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} < p_i > p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} > p_i < p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { // write your code here boolean readFromLocal = true; //readFromLocal = false; String filepath = "src/input.txt"; //FileInputStream fileInputStream = new FileInputStream(filepath); InputReader inputReader = new InputReader(System.in); Solve s = new Solve(); s.solve(inputReader); } } class Solve { public void solve(InputReader inputReader) { int t = inputReader.nextInt(); long mod = 1000_000_000 + 9; while (t > 0) { t--; long n = inputReader.nextLong(); long a = inputReader.nextLong(); long b = inputReader.nextLong(); int[] res = new int[(int) n]; int[] val = new int[res.length]; printResult(n, a, b, res, val); } } private void printResult(long n, long a, long b, int[] res, int[] val) { if (permutationExists(n, a, b)) { for (int i = 0; i < val.length; i++) { val[i] = i + 1; } if (b > a) { int index = 0; int permutationIndex = 1; for (int pos = 1; pos <= b; pos++) { res[permutationIndex] = val[index++]; permutationIndex += 2; } for (permutationIndex = 0; permutationIndex < res.length; permutationIndex++) { if (res[permutationIndex] != 0) { continue; } res[permutationIndex] = val[index++]; } } else { int index = (int) n - 1; int permutationIndex = 1; for (int pos = 1; pos <= a; pos++) { res[permutationIndex] = val[index--]; permutationIndex += 2; } for (permutationIndex = 0; permutationIndex < res.length; permutationIndex++) { if (res[permutationIndex] != 0) { continue; } res[permutationIndex] = val[index--]; } if (a == b && a!=0){ swap(res,(int)(n)-2,(int)(n)-1); } } for (int i = 0; i < res.length; i++) { System.out.print(res[i]+" "); } System.out.println(); } else System.out.println(-1); } private void swap(int[] res, int i, int j) { int temp = res[i]; res[i] = res[j]; res[j] = temp; } private boolean permutationExists(long n, long a, long b) { if (a < b) { return permutationExists(n, b, a); } boolean isPossible; if (a - b > 1) { isPossible = false; } else { isPossible = a == b ? a * 2 + 1 < n : a * 2 < n; } return isPossible; } private long pow(int n, long mod) { long res = 1L; long num = 2L; while (n > 0) { if ((n & 1) != 0) { res = (res * num) % mod; } num = (num * num) % mod; n >>= 1; } return res; } } class InputReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; 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 int peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) { return -1; } } return buf[curChar]; } 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 String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return readLine(); } else { return readLine0(); } } public BigInteger readBigInteger() { try { return new BigInteger(nextString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } 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 boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) { read(); } return value == -1; } public String next() { return nextString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public int[] nextSortedIntArray(int n) { int array[] = nextIntArray(n); Arrays.sort(array); return array; } public int[] nextSumIntArray(int n) { int[] array = new int[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; ++i) array[i] = nextLong(); return array; } public long[] nextSumLongArray(int n) { long[] array = new long[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextSortedLongArray(int n) { long array[] = nextLongArray(n); Arrays.sort(array); return array; } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
67a43a72e6e5a154a57c162cbd5085be
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.lang.Math.ceil; import static java.util.Arrays.sort; public class C { public static void main(String[] args) throws IOException { int t = ri(); while (t-- > 0) { int n = rni(); int a = ni(); int b = ni(); if ((n + 1) / 2 <= a || (n + 1) / 2 <= b || Math.abs(a - b) > 1) { prln(-1); } else if (a > b) { int temp = n; int total = 1; temp -= 3; total += (temp+1) / 2; total += total - 1; if (total < a+b) { prln(-1); } else { StringBuilder ans = new StringBuilder(); int low = 1, high = n; int cnt = 0; while (cnt < a) { ans.append(low).append(" ").append(high).append(" "); low++; high--; cnt++; } for (int i = high; i >= low; i--) { ans.append(i).append(" "); } prln(ans); } } else { int temp = n; int total = 1; temp -= 3; total += (temp+1) / 2; total += total - 1; if (total < a+b) { prln(-1); } else { StringBuilder ans = new StringBuilder(); int low = 1, high = n; int cnt = 0; while (cnt < b) { ans.append(high).append(" ").append(low).append(" "); low++; high--; cnt++; } boolean isEqual = false; if (a == b) { ans.append(high).append(" ").append(high - 1).append(" "); high -= 2; isEqual = true; } if (!isEqual) { for (int i = low; i <= high; i++) { ans.append(i).append(" "); } } else { for (int i = high; i >= low; i--) { ans.append(i).append(" "); } } prln(ans); } } } close(); } static class DisjointSetUnion { int p[]; int count[]; public DisjointSetUnion(int n) { p = new int[n]; count = new int[n]; for (int i = 0; i < n; i++) { count[i] = 1; } clear(n); } public void clear(int n) { for (int i = 0; i < n; i++) { p[i] = i; } } public int get(int x) { return x != p[x] ? p[x] = get(p[x]) : x; } public int getCount(int x) { return count[x]; } public boolean union(int a, int b) { a = get(a); b = get(b); p[a] = b; if (a != b) { count[b] += count[a]; count[a] = 0; } return a != b; } } static BufferedReader __i = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __o = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static boolean[] isPrime; static Random __r = new Random(); // references // IBIG = 1e9 + 7 // IMAX ~= 2e9 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final long LMAX = 9223372036854775807L; // 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 long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % 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); } static void setTrue(int n) { for (int i = 0; i < n; i++) { isPrime[i] = true; } } static void prime(int n) { for (int i = 2; i * i < n; i++) { if (isPrime[i]) { for (int j = i * i; j < n; j += i) isPrime[j] = false; } } } // 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 void resetBoolean(boolean[] vis, int n) { for (int i = 0; i < n; i++) { vis[i] = false; } } static void setMinusOne(int[][] matrix) { int row = matrix.length; int col = matrix[0].length; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { matrix[i][j] = -1; } } } // input static void r() throws IOException { input = new StringTokenizer(rline()); } static int ri() throws IOException { return Integer.parseInt(rline().split(" ")[0]); } static long rl() throws IOException { return Long.parseLong(rline()); } static double rd() throws IOException { return Double.parseDouble(rline()); } static int[] ria(int n) throws IOException { int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a; } static void ria(int[] a) throws IOException { int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni(); } static int[] riam1(int n) throws IOException { int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a; } static void riam1(int[] a) throws IOException { int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; } static long[] rla(int n) throws IOException { long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a; } static void rla(long[] a) throws IOException { int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nl(); } static double[] rda(int n) throws IOException { double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a; } static void rda(double[] a) throws IOException { int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nd(); } static char[] rcha() throws IOException { return rline().toCharArray(); } static void rcha(char[] a) throws IOException { int n = a.length, i = 0; for (char c : rline().toCharArray()) a[i++] = c; } static String rline() throws IOException { return __i.readLine(); } static String n() { return input.nextToken(); } static int rni() throws IOException { r(); return ni(); } static int ni() { return Integer.parseInt(n()); } static long rnl() throws IOException { r(); return nl(); } static long nl() { return Long.parseLong(n()); } static double rnd() throws IOException { r(); return nd(); } static double nd() { return Double.parseDouble(n()); } // output static void pr(int i) { __o.print(i); } static void prln(int i) { __o.println(i); } static void pr(long l) { __o.print(l); } static void prln(long l) { __o.println(l); } static void pr(double d) { __o.print(d); } static void prln(double d) { __o.println(d); } static void pr(char c) { __o.print(c); } static void prln(char c) { __o.println(c); } static void pr(char[] s) { __o.print(new String(s)); } static void prln(char[] s) { __o.println(new String(s)); } static void pr(String s) { __o.print(s); } static void prln(String s) { __o.println(s); } static void pr(Object o) { __o.print(o); } static void prln(Object o) { __o.println(o); } static void prln() { __o.println(); } static void pryes() { prln("yes"); } static void pry() { prln("Yes"); } static void prY() { prln("YES"); } static void prno() { prln("no"); } static void prn() { prln("No"); } static void prN() { prln("NO"); } static boolean pryesno(boolean b) { prln(b ? "yes" : "no"); return b; } ; static boolean pryn(boolean b) { prln(b ? "Yes" : "No"); return b; } static boolean prYN(boolean b) { prln(b ? "YES" : "NO"); return b; } static void prln(int... a) { for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i) ; if (a.length > 0) prln(a[a.length - 1]); else prln(); } static void prln(long... a) { for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i) ; if (a.length > 0) prln(a[a.length - 1]); else prln(); } static void prln(double... a) { for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i) ; if (a.length > 0) prln(a[a.length - 1]); else prln(); } static <T> void prln(Collection<T> c) { int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i) ; if (n >= 0) prln(iter.next()); else prln(); } static void h() { prln("hlfd"); flush(); } static void flush() { __o.flush(); } static void close() { __o.close(); } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
05eb49c750816184e477d7b8ed6d7472
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.util.*; public class B { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); // if(a + b > n-2){ // System.out.println(-1); // continue; // } // // if(Math.abs(a-b) > 1){ // System.out.println(-1); // continue; // } if ((a == 0 && b == 0)) { for (int i = 1; i <= n; i++) { pw.print(i + " "); } pw.println(); } else if (n % 2 == 0 && (a > ((n - 2) / 2) || b > ((n - 2) / 2))) { pw.println(-1); } else if (n % 2 == 1 && (a > ((n - 2) / 2) && b > ((n - 2) / 2))) { pw.println(-1); } else if (Math.abs(a - b) > 1) { pw.println(-1); } else { if (a > b) { pw.print(1 + " "); int i = 2; int j = n; while (i < j) { if (a == 1) break; pw.print(j + " "); pw.print(i + " "); a--; j--; i++; } if (i < j) { for (int k = j; k >= i; k--) { pw.print(k + " "); } } pw.println(); } else if (a < b) { pw.print(n + " "); int i = 1; int j = n - 1; while (i < j) { if (b == 1) break; pw.print(i + " "); pw.print(j + " "); b--; j--; i++; } if (i < j) { for (int k = i; k <= j; k++) { pw.print(k + " "); } } pw.println(); } else { pw.print(1 + " "); int i = 2; int j = n; while (i < j) { if (a == 1) break; pw.print(j + " "); pw.print(i + " "); a--; j--; i++; } pw.print(j + " "); j--; if (i < j) { for (int k = i; k <= j; k++) { pw.print(k + " "); } } pw.println(); } } } pw.close(); } // --------------------sTufF ---------------------- static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
a0daf77cb8f86912760732ebcd6b238e
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
//Harsh Gour code import java.util.*; import java.io.*; import java.lang.*; public class Solution { // Arrays.sort(arr, (a, b) -> a[1] - b[1]); // this is used to to sort value pairs /* String str ="3"; int p = Integer.parseInt(str); System.out.println(p); */ // Using stringBuilder functions /* StringBuilder sb =new StringBuilder(); sb.append("Hii my name is Harsh "); int age = 21; sb.append("My age is :"+age+"\n"); System.out.println(sb); */ public static void main(String[] args) throws IOException{ FastReader sc = new FastReader(System.in); int test = sc.nextInt(); while (test-- > 0) { int n =sc.nextInt(); int a= sc.nextInt(); int b =sc.nextInt(); int c= n-2; int min = (int)Math.floor((double)(n-2)/2); int max = (int)Math.ceil((double)(n-2)/2); int []arr =new int [n]; if(a==b || a==b+1 || a+1==b) { //System.out.println("aa"); if(a==b && min>=a && a<=max) { arr[0] = 1; for(int i=1;i<n;i++) { if(i<=a+b) { if(i%2==1) { arr[i]= i+2; }else { arr[i]= i; } }else { arr[i]=i+1; } } for(int i=0;i<n;i++) { System.out.print(arr[i]+" "); } System.out.println(); } else if(a==b+1 && min>=b && a <=max) { arr[0] = 1; int j=0; for(int i=1;i<n;i++) { if(i<=a+b-1) { if(i%2==1) { arr[i]= i+2; }else { arr[i]= i; } }else { arr[i]=n-j; j++; } } for(int i=0;i<n;i++) { System.out.print(arr[i]+" "); } System.out.println(); } else if(a+1==b && min>=a && b<=max) { arr[0] = 2; for(int i=1;i<n;i++) { if(i<=a+b) { if(i%2==1) { arr[i]= i; }else { arr[i]= i+2; } }else { arr[i]=i+1; } } for(int i=0;i<n;i++) { System.out.print(arr[i]+" "); } System.out.println(); }else { System.out.println("-1"); } }else { System.out.println("-1"); } } } static boolean check(int n ,int[]brr, int[]crr) { boolean a= true; for(int i=0;i<2*n;i++) { if(brr[i]!=crr[i]) { a=false; break; } } return a; } // GCD static int gcd(int a, int b) { if (a == 0) return b; else return gcd(b % a, a); } // optimal power function static int power(int x, int y) { if (y == 0) return 1; else if (y % 2 == 0) return power(x, y / 2) * power(x, y / 2); else return x * power(x, y / 2) * power(x, y / 2); } // optimal power function end static int minimumadjacentswapstomakearraysidentical(char []s1, char[] s2, int size) { int i = 0, j = 0; int result = 0; // Iterate over the first string and convert // every element equal to the second string while (i < size) { j = i; // Find index element of first string which // is equal to the ith element of second string while (s1[j] != s2[i]) { j += 1; } // Swap adjacent elements in first string so // that element at ith position becomes equal while (i < j) { // Swap elements using temporary variable char temp = s1[j]; s1[j] = s1[j - 1]; s1[j - 1] = temp; j -= 1; result += 1; } i += 1; } return result; } //end// // sorted pair by its key value /* StringBuilder sb =new StringBuilder(); Map<Integer , Integer> map = new HashMap<>(); TreeMap<Integer,Integer> sorted=new TreeMap<Integer,Integer>(); sorted.putAll(map); for(Map.Entry<Integer , Integer>entry : sorted.entrySet()) { sb.append(entry.getKey()+" "+ entry.getValue()+ "\n"); } */ } //Pair class class Pair { int x; int y; //Constructor public Pair(int x, int y) { this.x = x; this.y = y; } } // Fast Reader // FastReader in = new FastReader(System.in); class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
61d08c4074f4ed281750d4eb223ea283
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); StringTokenizer s = new StringTokenizer(br.readLine()); int tc = Integer.parseInt(s.nextToken()); while(tc-->0) { s = new StringTokenizer(br.readLine()); int n =Integer.parseInt(s.nextToken()); int a = Integer.parseInt(s.nextToken()); int b = Integer.parseInt(s.nextToken()); int ar[] = new int[n]; for(int i=0;i<n;i++) ar[i]=i+1; if(a==0 && b==0) { for(int i=0;i<n;i++) pw.print(ar[i]+" "); pw.println(); }else if((Math.abs(a-b)==0 || Math.abs(a-b)==1) && (a+b<=n-2)) { if(a>=b) { int x =a; int y =b; int i=n-1; while(a-->0) { swap(i,i-1,ar); i-=2; if(a==0 && x==y) { swap(0,1,ar); } } }else if(b>a) { int i=0; while(b-->0) { swap(i,i+1,ar); i+=2; } } for(int i=0;i<n;i++) pw.print(ar[i]+" "); pw.println(); }else { pw.println(-1); } } pw.close(); } public static void swap(int i, int j,int[] ar) { int temp = ar[i]; ar[i]=ar[j]; ar[j]=temp; } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
3070a6e3594ac96df3baba4cf74f1ca0
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
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 a = s.nextInt(); int b = s.nextInt(); if ((Math.abs(a-b) > 1) || (a+b) > n-2){ System.out.println("-1"); continue; } List<Integer> ar = new ArrayList<>(); int left = 1, right = n; if (a == b){ int h = 0, v = 0; ar.add(left); left++; while (h < a){ ar.add(right); right--; ar.add(left); left++; h++; } for (int i = left; i <= right; i++){ ar.add(i); } } else if (a > b){ int h = 0; ar.add(left); left++; while (h < a-1){ ar.add(right); right--; ar.add(left); left++; h++; } for (int i = right; i >= left; i--){ ar.add(i); } } else { ar.add(right); right--; int v = 0; while (v < b-1){ ar.add(left); left++; ar.add(right); right--; v++; } for (int i = left; i <= right; i++){ ar.add(i); } } for (int e: ar){ System.out.print(e+" "); } System.out.println(); } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
a981104853f57857d8c20068116c9685
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; import java.io.*; // THIS TEMPLATE MADE BY AKSH BANSAL. 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; } } static void sort(int a[]){ // int -> long ArrayList<Integer> arr=new ArrayList<>(); // Integer -> Long for(int i=0;i<a.length;i++) arr.add(a[i]); Collections.sort(arr); for(int i=0;i<a.length;i++) a[i]=arr.get(i); } private static long gcd(long a, long b){ if(b==0)return a; return gcd(b,a%b); } private static long pow(long x,long y){ if(y==0)return 1; long temp = pow(x, y/2); if(y%2==1){ return x*temp*temp; } else{ return temp*temp; } } static int log(long n){ int res = 0; while(n>0){ res++; n/=2; } return res; } static int mod = (int)1e9+7; static PrintWriter out; static FastReader sc ; public static void main(String[] args) throws IOException { sc = new FastReader(); out = new PrintWriter(System.out); // primes(); // ________________________________ int test = sc.nextInt(); StringBuilder output = new StringBuilder(); while (test-- > 0) { int n =sc.nextInt(); int a =sc.nextInt(); int b =sc.nextInt(); solver(n, a, b); } out.print(output); // _______________________________ // int n = sc.nextInt(); // out.println(solver()); // ________________________________ out.flush(); } public static void solver(int n, int a, int b) { if(Math.abs(a-b)>1 || a>n/2 || b>n/2 || a+b>n-2){ out.println(-1); return; } int aa = a; int bb = b; int[] res= new int[n]; if(a==b){ for(int i=0;i<n;i++)res[i] = i+1; for(int i=1;i<n-1;i+=2){ if(a==0)break; int temp = res[i]; res[i] = res[i+1]; res[i+1] = temp; a--; b--; } } else if(a>b){ for(int i=0;i<n;i++)res[i] = i+1; int pos = 0; for(int i=1;i<n-1;i+=2){ if(a==0)break; int temp = res[i]; res[i] = res[i+1]; res[i+1] = temp; a--; b--; pos+=2; } for(int i=0;i<(n-pos)/2;i++){ int temp = res[pos+i]; res[pos+i] = res[n-i-1]; res[n-i-1] = temp; } } else{ for(int i=n-1;i>=0;i--)res[i] = n-i; int pos = 0; for(int i=1;i<n-1;i+=2){ if(b==0)break; int temp = res[i]; res[i] = res[i+1]; res[i+1] = temp; a--; b--; pos+=2; } for(int i=0;i<(n-pos)/2;i++){ int temp = res[pos+i]; res[pos+i] = res[n-i-1]; res[n-i-1] = temp; } } // if(!check(aa,bb,res)){ // out.println("Wrong!"); // } for(int i:res) out.print(i+" "); out.println(); } static boolean check(int a, int b, int[] res){ int high = 0, low = 0; int n = res.length; for(int i=1;i<n-1;i++){ if(res[i]>res[i-1] && res[i]>res[i+1])high++; if(res[i]<res[i-1] && res[i]<res[i+1])low++; } return a==high && b==low; } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
15f2ea775e1b51bd55707207cdacef98
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.FileNotFoundException; import java.util.StringTokenizer; import java.io.Writer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Asgar Javadov */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); BBuildThePermutation solver = new BBuildThePermutation(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class BBuildThePermutation { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); if (Math.abs(a - b) > 1) { out.minusOne(); return; } if (a + b + 2 > n) { out.minusOne(); return; } var p = ArrayUtils.arange(n); ArrayUtils.increaseByOne(p); if (a == b) { for (int i = 0; i + 2 < p.length && a > 0; i += 2) { ArrayUtils.swap(p, i + 1, i + 2); a--; } out.println(p); return; } if (a < b) { int min = 1; int start = b + 1; for (int i = 0; i < b << 1; i += 2) { p[i] = start; p[i + 1] = min; min++; start++; } out.println(p); return; } else { int start = n; int min = 1; for (int i = 0; i < a << 1; i += 2) { p[i] = min; p[i + 1] = start; min++; start--; } for (int i = a << 1; i < n; i++) { p[i] = start--; } out.println(p); return; } } } static class InputReader extends BufferedReader { StringTokenizer tokenizer; public InputReader(InputStream inputStream) { super(new InputStreamReader(inputStream), 32768); } public InputReader(String filename) { super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename))); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { String line = readLine(); if (line == null) continue; tokenizer = new StringTokenizer(line); } catch (IOException e) { throw new RuntimeException(); } } return tokenizer.nextToken(); } public Integer nextInt() { return Integer.valueOf(next()); } } static class ArrayUtils { public static int[] arange(int length) { int[] array = new int[length]; for (int i = 0; i < length; ++i) array[i] = i; return array; } public static void swap(int[] array, int i, int j) { if (i == j) return; int temp = array[i]; array[i] = array[j]; array[j] = temp; } public static void increaseByOne(int[] array) { for (int i = 0; i < array.length; ++i) ++array[i]; } } static class OutputWriter extends PrintWriter { public OutputWriter(OutputStream outputStream) { super(outputStream); } public OutputWriter(Writer writer) { super(writer); } public OutputWriter(String filename) throws FileNotFoundException { super(filename); } public void println(int[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) print(' '); print(array[i]); } println(); } public void minusOne() { println(-1); } public void close() { super.close(); } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
df323a305628f9557542ca444d9642d1
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.util.*; import java.util.ArrayList; import java.util.Arrays.*; import java.util.Map.Entry; import org.xml.sax.InputSource; import static java.util.Collections.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class Main { static final int SCAN_LINE_LENGTH = 1_000_002; private static final boolean thread = false; @SuppressWarnings("all") static final boolean HAS_TEST_CASES = (1 == 1) ? true : false; static int n, a[], e[][], vis[], m, b[]; @SuppressWarnings("all") private static ArrayList<Integer> adj[], v = new ArrayList<>(); static long gcd = 1; static HashMap<Integer, Integer> pos; @SuppressWarnings("unchecked") static void solve() throws Exception { int n = ni(), a = ni(), b = ni(); int[] ar = new int[n]; for (int i = 0; i < ar.length; i++) { ar[i] = i + 1; } if (abs(a - b) > 1 || (n == 2 && (a > 0 || b > 0)) || (a + b >= n - 1)) { pn(-1); return; } boolean rev = true; if (b > a) { rev = false; int m = n; for (int i = 0; i < ar.length; i++) { ar[i] = m--; } } int i = 0, j = n - 1; if (a > 0 || b > 0) p(ar[i++] + " "); else p(ar[j--] + " "); boolean kt = true; for (int k = 1; k < ar.length; k++) { if (a > 0 || b > 0) { if (k % 2 == 0) { p(ar[i++] + " "); int t = rev ? b-- : a--; kt = false; } else { p(ar[j--] + " "); int t = rev ? a-- : b--; kt = true; } } else { // p(ar[rev ? j-- : i++] + " "); p(ar[kt ? j-- : i++] + " "); } } pn(); } public static void main(final String[] args) throws Exception { if (!thread) { final int testcases = HAS_TEST_CASES ? ni() : 1; for (int i = 1; i <= testcases; i++) { // pni(i + " t"); // out.print("Case #" + (i) + ": "); try { solve(); } catch (final Exception e) { // pni("idk Exception"); // e.printStackTrace(System.err); // System.exit(0); throw e; } } out.flush(); } r.close(); out.close(); } @SuppressWarnings("all") private static final int MOD = (int) (1e9 + 7), MOD_FFT = 998244353; private static final Reader r = new Reader(); private static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static long[] enumPows(int a, int n, int mod) { a %= mod; long[] pows = new long[n + 1]; pows[0] = 1; for (int i = 1; i <= n; i++) pows[i] = pows[i - 1] * a % mod; return pows; } @SuppressWarnings("all") private static ArrayList<Integer> seive(int n) { ArrayList<Integer> p = new ArrayList<>(); int[] f = new int[n + 1]; for (int i = 2; i <= n; i++) { if (f[i] == 1) continue; for (int j = i * i; j <= n && j < f.length; j += i) { f[j] = 1; } } for (int i = 2; i < f.length; i++) { if (f[i] == 0) p.add(i); } return p; } @SuppressWarnings("all") private static void s(long[] a) { Long[] t = new Long[a.length]; for (int i = 0; i < t.length; i++) { t[i] = a[i]; } sort(t); for (int i = 0; i < t.length; i++) { a[i] = t[i]; } } @SuppressWarnings("all") private static void s(int[] a) { Integer[] t = new Integer[a.length]; for (int i = 0; i < t.length; i++) { t[i] = a[i]; } sort(t); for (int i = 0; i < t.length; i++) { a[i] = t[i]; } } int pow(int a, int b, int m) { int ans = 1; while (b != 0) { if ((b & 1) != 0) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } int modinv(int k) { return pow(k, MOD - 2, MOD); } @SuppressWarnings("all") private static void swap(final int i, final int j) { final int temp = a[i]; a[i] = a[j]; a[j] = temp; } @SuppressWarnings("all") private static boolean isMulOverflow(final long A, final long B) { if (A == 0 || B == 0) return false; final long result = A * B; if (A == result / B) return true; return false; } @SuppressWarnings("all") private static int gcd(final int a, final int b) { if (b == 0) return a; return gcd(b, a % b); } @SuppressWarnings("all") private static long gcd(final long a, final long b) { if (b == 0) return a; return gcd(b, a % b); } @SuppressWarnings("all") private static class Pair<T, E> implements Comparable<Pair<T, E>> { T fir; E snd; Pair() { } Pair(final T x, final E y) { this.fir = x; this.snd = y; } // @Override // @SuppressWarnings("unchecked") // public int compareTo(final Pair<T, E> o) { // final int c = ((Comparable<T>) fir).compareTo(o.fir); // return c != 0 ? c : ((Comparable<E>) snd).compareTo(o.snd); // } @Override @SuppressWarnings("unchecked") public int compareTo(final Pair<T, E> o) { final int c = ((Comparable<T>) fir).compareTo(o.fir); return c; } } @SuppressWarnings("all") private static class pi implements Comparable<pi> { int fir, snd; pi() { } pi(final int a, final int b) { fir = a; snd = b; } public int compareTo(final pi o) { return fir == o.fir ? snd - o.snd : fir - o.fir; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + fir; result = prime * result + snd; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; pi other = (pi) obj; if (fir != other.fir) return false; if (snd != other.snd) return false; return true; } } @SuppressWarnings("all") private static <T> void checkV(final Vector<T> adj[], final int i) { adj[i] = adj[i] == null ? new Vector<>() : adj[i]; } private static <T> void checkV(final ArrayList<T> adj[], final int i) { adj[i] = adj[i] == null ? new ArrayList<>() : adj[i]; } @SuppressWarnings("all") private static int[] na(final int n) throws Exception { final int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } @SuppressWarnings("all") private static int[] na1(final int n) throws Exception { final int[] a = new int[n + 1]; for (int i = 1; i < a.length; i++) { a[i] = ni(); } return a; } @SuppressWarnings("all") private static String n() throws IOException { return r.readToken(); } @SuppressWarnings("all") private static char[] ns() throws IOException { return r.readToken().toCharArray(); } @SuppressWarnings("all") private static String nln() throws IOException { return r.readLine(); } private static int ni() throws IOException { return r.nextInt(); } @SuppressWarnings("all") private static long nl() throws IOException { return r.nextLong(); } @SuppressWarnings("all") private static double nd() throws IOException { return r.nextDouble(); } @SuppressWarnings("all") private static void p(final Object o) { out.print(o); } @SuppressWarnings("all") private static void pn(final Object o) { out.println(o); } @SuppressWarnings("all") private static void pn() { out.println(""); } @SuppressWarnings("all") private static void pi(final Object o) { out.print(o); out.flush(); } @SuppressWarnings("all") private static void pni() { out.println(""); out.flush(); } private static void pni(final Object o) { out.println(o); out.flush(); } private static class Reader { private final int BUFFER_SIZE = 1 << 17; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; // private StringTokenizer st; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } @SuppressWarnings("all") public Reader(final String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } final byte[] buf = new byte[SCAN_LINE_LENGTH]; public String readLine() throws IOException { int cnt = 0; int c; o: while ((c = read()) != -1) { if (c == '\n') if (cnt == 0) continue o; else break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public String readToken() throws IOException { int cnt = 0; int c; o: while ((c = read()) != -1) { if (!(c >= 33 && c <= 126)) if (cnt == 0) continue o; else break; 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(); final 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(); final 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(); final 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 { // if (thread) // new Thread(null, new Runnable() { // @Override // public void run() { // try { // final int testcases = HAS_TEST_CASES ? ni() : 1; // for (int i = 1; i <= testcases; i++) { // // out.print("Case #" + (i + 1) + ": "); // try { // solve(); // } catch (final ArrayIndexOutOfBoundsException e) { // e.printStackTrace(System.err); // System.exit(-1); // } catch (final Exception e) { // pni("idk Exception in solve"); // e.printStackTrace(System.err); // System.exit(-1); // } // } // out.flush(); // } catch (final Throwable t) { // t.printStackTrace(System.err); // System.exit(-1); // } // } // }, "rec", (1L << 28)).start(); // } @SuppressWarnings({ "all", }) private static <T> T deepCopy(final T old) { try { return (T) deepCopyObject(old); } catch (final IOException | ClassNotFoundException e) { e.printStackTrace(System.err); System.exit(-1); } return null; } private static Object deepCopyObject(final Object oldObj) throws IOException, ClassNotFoundException { ObjectOutputStream oos = null; ObjectInputStream ois = null; try { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A oos = new ObjectOutputStream(bos); // B // serialize and pass the object oos.writeObject(oldObj); // C oos.flush(); // D final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); // E ois = new ObjectInputStream(bin); // F // return the new object return ois.readObject(); // G } catch (final ClassNotFoundException e) { pni("Exception in ObjectCloner = " + e); throw (e); } finally { oos.close(); ois.close(); } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
5623f7fc9c82752b24bfe4210c282281
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.util.*; public class ProblemA{ static long mod = 1000000007L; // map.put(a[i],map.getOrDefault(a[i],0)+1); static MyScanner sc = new MyScanner(); //<----------------------------------------------WRITE HERE-------------------------------------------> static void solve(){ int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); if(Math.abs(a-b) > 1) { out.println(-1); return; } int places = 0; if(n%2 == 0) { places = n/2-1; }else { places = n/2; } if(n%2 == 0 && (a>places || b>places)) { out.println(-1); return; } if(n%2 != 0 && ((a>=places && b>=places) || a>places || b>places)) { out.println(-1); return; } int[] arr = new int[n]; if(a == 0 && b == 0) { for(int i=0;i<n;i++) { arr[i] = i+1; } } else if(a>b) { int v = n; int i = 1; int l = a; while(l-->0) { arr[i] = v; i+=2; v--; } l = a; i = 0; while(l-->0) { arr[i] = v; v--; i+=2; } for(;i<n;i++) { arr[i] = v; v--; } // if(n%2 == 0)arr[n-1] = v; } else if(a == b) { int v = n; int i = 1; int l = a; while(l-->0) { arr[i] = v; i+=2; v--; } v=1; l = b; i=0; while(l-->0) { arr[i] = v; v++; i+=2; } for(;i<n;i++) { arr[i] = v; v++; } // if(n%2 == 0)arr[n-1] = v; } else { int v = 1; int i = 1; int l = b; while(l-->0) { arr[i] = v; i+=2; v++; } l = b; i=0; while(l-->0) { arr[i] = v; v++; i+=2; } for(;i<n;i++) { arr[i] = v; v++; } // if(n%2 == 0)arr[n-1] = v; } priArr(arr); } public static int solve(List<Pair> li, int f) { int lo = 0; int hi = li.size()-1; while(lo<=hi) { int mid = (lo+hi)>>1; if(li.get(mid).val>f) { hi = mid-1; }else lo = mid+1; } return lo; } //<----------------------------------------------WRITE HERE-------------------------------------------> static void swap(char[] a,int i,int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; } static int LowerBound(int a[], int x) { 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; } static int UpperBound(int a[], int x) { int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } static boolean isPali(String str){ int i = 0; int j = str.length()-1; while(i<j){ if(str.charAt(i)!=str.charAt(j)){ return false; } i++; j--; } return true; } static void priArr(int[] a) { for(int i=0;i<a.length;i++) { out.print(a[i] + " "); } out.println(); } static long gcd(long a,long b){ if(b==0) return a; return gcd(b,a%b); } static String reverse(String str){ char arr[] = str.toCharArray(); int i = 0; int j = arr.length-1; while(i<j){ char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } String st = new String(arr); return st; } static void reverse(int[] arr){ int i = 0; int j = arr.length-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static boolean isprime(int n){ if(n==1) return false; if(n==3 || n==2) 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 class Pair implements Comparable<Pair>{ int val; int ind; Pair(int v,int f){ val = v; ind = f; } public int compareTo(Pair p){ return this.ind - p.ind; } } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); // int t= 1; while(t-- >0){ solve(); } // Stop writing your solution here. ------------------------------------- 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(); } public int[] readIntArray(int n){ int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(next()); } return arr; } int[] reverse(int arr[]){ int n= arr.length; int i = 0; int j = n-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; j--;i++; } return arr; } long[] readLongArray(int n){ long arr[] = new long[n]; for(int i = 0;i<n;i++){ arr[i] = Long.parseLong(next()); } return arr; } 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 void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
36fcaf425c6fc833cea88dd13910ccb4
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA import java.util.*; import java.util.Map.Entry; import java.util.stream.*; import java.lang.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.io.*; public class CodeForces { static private final String INPUT = "input.txt"; static private final String OUTPUT = "output.txt"; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); static DecimalFormat df = new DecimalFormat("0.00000"); final static int MAX = Integer.MAX_VALUE, MIN = Integer.MIN_VALUE, mod = (int) (1e9 + 7); final static long LMAX = Long.MAX_VALUE, LMIN = Long.MIN_VALUE; final static long INF = (long) 1e18, Neg_INF = (long) -1e18; static Random rand = new Random(); // ======================= MAIN ================================== public static void main(String[] args) throws IOException { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; // ==== start ==== input(); preprocess(); int t = 1; t = readInt(); while (t-- > 0) { solve(); } out.flush(); // ==== end ==== if (!oj) System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } private static void solve() throws IOException { int n = readInt(), a = readInt(), b = readInt(); if (Math.abs(a - b) > 1 || a + b > n - 2) { out.println(-1); return; } int[] arr = new int[n]; int val = n - a - b - 1; for (int i = 0; i < a + b + 2; i += 2) arr[i] = val++; for (int i = 1; i < a + b + 2; i += 2) arr[i] = val++; int[] ans = new int[n]; for (int i = 0; i < n - a - b - 2; i++) ans[i] = i + 1; for (int i = 0; i < a + b + 2; i++) ans[n - a - b - 2 + i] = arr[i]; if (b > a) for (int i = 0; i < n; i++) ans[i] = n - ans[i] + 1; printIArray(ans); } private static void preprocess() throws IOException { } // cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces // javac CodeForces.java // java CodeForces // javac CodeForces.java && java CodeForces // change Stack size -> java -Xss16M CodeForces.java // ==================== CUSTOM CLASSES ================================ static class Pair implements Comparable<Pair> { int first, second; Pair(int f, int s) { first = f; second = s; } public int compareTo(Pair o) { if (this.first == o.first) return this.second - o.second; return this.first - o.first; } @Override public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (this.getClass() != obj.getClass()) return false; Pair other = (Pair) (obj); if (this.first != other.first) return false; if (this.second != other.second) return false; return true; } @Override public int hashCode() { return this.first ^ this.second; } @Override public String toString() { return this.first + " " + this.second; } } static class DequeNode { DequeNode prev, next; int val; DequeNode(int val) { this.val = val; } DequeNode(int val, DequeNode prev, DequeNode next) { this.val = val; this.prev = prev; this.next = next; } } // ======================= FOR INPUT ================================== private static void input() { FileInputStream instream = null; PrintStream outstream = null; try { instream = new FileInputStream(INPUT); outstream = new PrintStream(new FileOutputStream(OUTPUT)); System.setIn(instream); System.setOut(outstream); } catch (Exception e) { System.err.println("Error Occurred."); } br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(readLine()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readString() throws IOException { return next(); } static String readLine() throws IOException { return br.readLine().trim(); } static int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = readInt(); return arr; } static int[][] read2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = readIntArray(m); return arr; } static List<Integer> readIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readInt()); return list; } static long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = readLong(); return arr; } static long[][] read2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = readLongArray(m); return arr; } static List<Long> readLongList(int n) throws IOException { List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readLong()); return list; } static char[] readCharArray() throws IOException { return readString().toCharArray(); } static char[][] readMatrix(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = readCharArray(); return mat; } // ========================= FOR OUTPUT ================================== private static void printIList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIArray(arr[i]); } private static void printLArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLArray(arr[i]); } private static void yes() { out.println("YES"); } private static void no() { out.println("NO"); } // ====================== TO CHECK IF STRING IS NUMBER ======================== private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // ==================== FASTER SORT ================================ private static void sort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void sort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // ==================== MATHEMATICAL FUNCTIONS =========================== private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static int mod_pow(long a, long b, int mod) { if (b == 0) return 1; int temp = mod_pow(a, b >> 1, mod); temp %= mod; temp = (int) ((1L * temp * temp) % mod); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % mod); return temp; } private static int multiply(int a, int b) { return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod); } private static int divide(int a, int b) { return multiply(a, mod_pow(b, mod - 2, mod)); } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } private static long nCr(long n, long r) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans *= i; for (long i = 2; i <= n - r; i++) ans /= i; return ans; } private static List<Integer> factors(int n) { List<Integer> list = new ArrayList<>(); for (int i = 1; 1L * i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } private static List<Long> factors(long n) { List<Long> list = new ArrayList<>(); for (long i = 1; i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } // ==================== Primes using Seive ===================== private static List<Integer> getPrimes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); for (int i = 2; 1L * i * i <= n; i++) if (prime[i]) for (int j = i * i; j <= n; j += i) prime[j] = false; // return prime; List<Integer> list = new ArrayList<>(); for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } private static int[] SeivePrime(int n) { int[] primes = new int[n]; for (int i = 0; i < n; i++) primes[i] = i; for (int i = 2; 1L * i * i < n; i++) { if (primes[i] != i) continue; for (int j = i * i; j < n; j += i) if (primes[j] == j) primes[j] = i; } return primes; } // ==================== STRING FUNCTIONS ================================ private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } // check if a is subsequence of b private static boolean isSubsequence(String a, String b) { int idx = 0; for (int i = 0; i < b.length() && idx < a.length(); i++) if (a.charAt(idx) == b.charAt(i)) idx++; return idx == a.length(); } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // ==================== LIS & LNDS ================================ private static int LIS(int arr[], int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find1(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // =============== Lower Bound & Upper Bound =========== // less than or equal private static int lower_bound(List<Integer> list, int val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(List<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(List<Long> list, long val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== UNION FIND ===================== private static int find(int x, int[] parent) { if (parent[x] == x) return x; return parent[x] = find(parent[x], parent); } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) return false; if (rank[lx] > rank[ly]) parent[ly] = lx; else if (rank[lx] < rank[ly]) parent[lx] = ly; else { parent[lx] = ly; rank[ly]++; } return true; } // ==================== TRIE ================================ static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } void insert(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) curr.children[ch - 'a'] = new Node(); curr = curr.children[ch - 'a']; } curr.isEnd = true; } boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) return false; curr = curr.children[ch - 'a']; } return curr.isEnd; } } // ================== SEGMENT TREE (RANGE SUM & RANGE UPDATE) ================== public static class SegmentTree { int n; long[] arr, tree, lazy; SegmentTree(long arr[]) { this.arr = arr; this.n = arr.length; this.tree = new long[(n << 2)]; this.lazy = new long[(n << 2)]; build(1, 0, n - 1); } void build(int id, int start, int end) { if (start == end) tree[id] = arr[start]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; build(left, start, mid); build(right, mid + 1, end); tree[id] = tree[left] + tree[right]; } } void update(int l, int r, long val) { update(1, 0, n - 1, l, r, val); } void update(int id, int start, int end, int l, int r, long val) { distribute(id, start, end); if (end < l || r < start) return; if (start == end) tree[id] += val; else if (l <= start && end <= r) { lazy[id] += val; distribute(id, start, end); } else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; update(left, start, mid, l, r, val); update(right, mid + 1, end, l, r, val); tree[id] = tree[left] + tree[right]; } } long query(int l, int r) { return query(1, 0, n - 1, l, r); } long query(int id, int start, int end, int l, int r) { if (end < l || r < start) return 0L; distribute(id, start, end); if (start == end) return tree[id]; else if (l <= start && end <= r) return tree[id]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r); } } void distribute(int id, int start, int end) { if (start == end) tree[id] += lazy[id]; else { tree[id] += lazy[id] * (end - start + 1); lazy[(id << 1)] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; } lazy[id] = 0; } } // ==================== FENWICK TREE ================================ static class FT { int n; int[] arr; int[] tree; FT(int[] arr, int n) { this.arr = arr; this.n = n; this.tree = new int[n + 1]; for (int i = 1; i <= n; i++) { update(i, arr[i - 1]); } } FT(int n) { this.n = n; this.tree = new int[n + 1]; } // 1 based indexing void update(int idx, int val) { while (idx <= n) { tree[idx] += val; idx += idx & -idx; } } // 1 based indexing long query(int l, int r) { return getSum(r) - getSum(l - 1); } long getSum(int idx) { long ans = 0L; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } } // ==================== BINARY INDEX TREE ================================ static class BIT { long[][] tree; int n, m; BIT(int[][] mat, int n, int m) { this.n = n; this.m = m; tree = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { update(i, j, mat[i - 1][j - 1]); } } } void update(int x, int y, int val) { while (x <= n) { int t = y; while (t <= m) { tree[x][t] += val; t += t & -t; } x += x & -x; } } long query(int x1, int y1, int x2, int y2) { return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1); } long getSum(int x, int y) { long ans = 0L; while (x > 0) { int t = y; while (t > 0) { ans += tree[x][t]; t -= t & -t; } x -= x & -x; } return ans; } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
a35a1d8da4e3f0f6102510cffdfbdf26
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Solution { static long[] fac; static int[][] temp = new int[8][8]; public static void main(String[] args) throws IOException { Reader.init(System.in); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); /* // Do not delete this // Uncomment this before using nCrModPFermat fac = new long[200000 + 1]; fac[0] = 1; for (int i = 1; i <= 200000; i++) fac[i] = (fac[i - 1] * i % 1000000007); */ int te = Reader.nextInt(); // int te = 1; while(te-->0) { int n = Reader.nextInt(); int a = Reader.nextInt(); int b = Reader.nextInt(); if(Math.abs(a-b)>1 || a+b>n-2){ output.write("-1\n"); } else{ int[] arr = new int[n]; int idx = 0; if(a>b){ int l = 2, h = n; arr[idx++] = 1; for(int i = 0;i<b;i++){ arr[idx++] = h--; arr[idx++] = l++; } for(int i = h;i>=l;i--){ arr[idx++] = i; } } else if(a==b){ int l = 1, h = n-1; arr[idx++] = n; for(int i = 0;i<a;i++){ arr[idx++] = l++; arr[idx++] = h--; } for(int i = h;i>=l;i--){ arr[idx++] = i; } } else{ int l = 1, h = n-1; arr[idx++] = n; for(int i = 0;i<a;i++){ arr[idx++] = l++; arr[idx++] = h--; } for(int i = l;i<=h;i++){ arr[idx++] = i; } } int c_a = 0, c_b = 0; for(int i = 1;i<n-1;i++){ if(arr[i]<arr[i-1] && arr[i]<arr[i+1]) c_b++; else if(arr[i]>arr[i-1] && arr[i]>arr[i+1]) c_a++; } if(a==c_a && b==c_b){ for(int i = 0;i<n;i++){ output.write(arr[i]+" "); } output.write("\n"); } else{ output.write("-1\n"); } } } output.close(); } public static boolean isP(int n){ StringBuilder s1 = new StringBuilder(n+""); StringBuilder s2 = new StringBuilder(n+""); s2.reverse(); for(int i = 0;i<s1.length();i++){ if(s1.charAt(i)!=s2.charAt(i)) return false; } return true; } public static long[] factorial(int n){ long[] factorials = new long[n+1]; factorials[0] = 1; factorials[1] = 1; for(int i = 2;i<=n;i++){ factorials[i] = (factorials[i-1]*i); } return factorials; } public static long numOfBits(long n){ long ans = 0; while(n>0){ n = n & (n-1); ans++; } return ans; } public static long ceilOfFraction(long x, long y){ // ceil using integer division: ceil(x/y) = (x+y-1)/y // using double may go out of range. return (x+y-1)/y; } public static long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } // Returns n^(-1) mod p public static long modInverse(long n, long p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. public static long nCrModPFermat(int n, int r, int p) { if (n<r) return 0; if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } public static long ncr(long n, long r) { long p = 1, k = 1; if (n - r < r) { r = n - r; } if (r != 0) { while (r > 0) { p *= n; k *= r; long m = gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } return p; } public 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; (long) i * i <= n; i = i + 6){ if (n % i == 0 || n % (i + 2) == 0) { return false; } } return true; } public static int powOf2JustSmallerThanN(int n) { n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return n ^ (n >> 1); } public static long merge(int[] arr, int[] aux, int low, int mid, int high) { int k = low, i = low, j = mid + 1; long inversionCount = 0; // while there are elements in the left and right runs while (i <= mid && j <= high) { if (arr[i] <= arr[j]) { aux[k++] = arr[i++]; } else { aux[k++] = arr[j++]; inversionCount += (mid - i + 1); // NOTE } } // copy remaining elements while (i <= mid) { aux[k++] = arr[i++]; } /* no need to copy the second half (since the remaining items are already in their correct position in the temporary array) */ // copy back to the original array to reflect sorted order for (i = low; i <= high; i++) { arr[i] = aux[i]; } return inversionCount; } // Sort array `arr[low…high]` using auxiliary array `aux` public static long mergesort(int[] arr, int[] aux, int low, int high) { if (high <= low) { // if run size <= 1 return 0; } int mid = (low + ((high - low) >> 1)); long inversionCount = 0; // recursively split runs into two halves until run size <= 1, // then merges them and return up the call chain // split/merge left half inversionCount += mergesort(arr, aux, low, mid); // split/merge right half inversionCount += mergesort(arr, aux, mid + 1, high); // merge the two half runs inversionCount += merge(arr, aux, low, mid, high); return inversionCount; } public static void reverseArray(int[] arr,int start, int end) { int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } 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){ if(a>b) return a/gcd(b,a) * b; return b/gcd(a,b) * a; } public static long largeExponentMod(long x,long y,long mod){ // computing (x^y) % mod x%=mod; long ans = 1; while(y>0){ if((y&1)==1){ ans = (ans*x)%mod; } x = (x*x)%mod; y = y >> 1; } return ans; } public static boolean[] numOfPrimesInRange(long L, long R){ boolean[] isPrime = new boolean[(int) (R-L+1)]; Arrays.fill(isPrime,true); long lim = (long) Math.sqrt(R); for (long i = 2; i <= lim; i++){ for (long j = Math.max(i * i, (L + i - 1) / i * i); j <= R; j += i){ isPrime[(int) (j - L)] = false; } } if (L == 1) isPrime[0] = false; return isPrime; } public static ArrayList<Long> primeFactors(long n){ ArrayList<Long> factorization = new ArrayList<>(); if(n%2==0){ factorization.add(2L); } while(n%2==0){ n/=2; } if(n%3==0){ factorization.add(3L); } while(n%3==0){ n/=3; } if(n%5==0){ factorization.add(5L); } while(n%5==0){ // factorization.add(5L); n/=5; } int[] increments = {4, 2, 4, 2, 4, 6, 2, 6}; int i = 0; for (long d = 7; d * d <= n; d += increments[i++]) { if(n%d==0){ factorization.add(d); } while (n % d == 0) { // factorization.add(d); n /= d; } if (i == 8) i = 0; } if (n > 1) factorization.add(n); return factorization; } } class DSU { int[] size, parent; int n; public DSU(int n){ this.n = n; size = new int[n]; parent = new int[n]; for(int i = 0;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int u){ if(parent[u]==u){ return u; } return parent[u] = find(parent[u]); } public void merge(int u, int v){ u = find(u); v = find(v); if(u!=v){ if(size[u]>size[v]){ parent[v] = u; size[u] += size[v]; } else{ parent[u] = v; size[v] += size[u]; } } } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static String nextLine() throws IOException { return reader.readLine(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException{ return Long.parseLong(next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } } class SegTree{ int n; // array size // Max size of tree public int[] tree; public SegTree(int n){ this.n = n; tree = new int[4*n]; } // function to build the tree void update(int pos, int val, int s, int e, int treeIdx){ if(pos < s || pos > e){ return; } if(s == e){ tree[treeIdx] = val; return; } int mid = s + (e - s) / 2; update(pos, val, s, mid, 2 * treeIdx); update(pos, val, mid + 1, e, 2 * treeIdx + 1); tree[treeIdx] = tree[2 * treeIdx] + tree[2 * treeIdx + 1]; } void update(int pos, int val){ update(pos, val, 1, n, 1); } int query(int qs, int qe, int s, int e, int treeIdx){ if(qs <= s && qe >= e){ return tree[treeIdx]; } if(qs > e || qe < s){ return 0; } int mid = s + (e - s) / 2; int subQuery1 = query(qs, qe, s, mid, 2 * treeIdx); int subQuery2 = query(qs, qe, mid + 1, e, 2 * treeIdx + 1); int res = subQuery1 + subQuery2; return res; } int query(int l, int r){ return query(l, r, 1, n, 1); } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
7ad0496d7b0cc9e42ae41d8b5c670e72
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; public class B_Build_the_Permutation { public static void main(String[] args) { OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); FastReader f = new FastReader(); int t = f.nextInt(); while(t-- > 0){ solve(f, out); } out.close(); } public static void solve(FastReader f, PrintWriter out) { int n = f.nextInt(); int a = f.nextInt(); int b = f.nextInt(); if(abs(a-b) > 1) { out.println(-1); return; } if(max(a, b) > (n-1)/2 || min(a, b) > (n-2)/2) { out.println(-1); return; } if(a == b) { int i = 1; int j = n; for(int k = 0; k < a; k++) { out.print(i + " " + j + " "); i++; j--; } for(int k = i; k <= j; k++) { out.print(k + " "); } } else if(a > b) { int i = 1; int j = n; for(int k = 0; k < max(a, b); k++) { out.print(i + " " + j + " "); i++; j--; } for(int k = j; k >= i; k--) { out.print(k + " "); } } else { int i = n; int j = 1; for(int k = 0; k < max(a, b); k++) { out.print(i + " " + j + " "); i--; j++; } for(int k = j; k <= i; k++) { out.print(k + " "); } } out.println(); } public static void sort(int arr[]) { ArrayList<Integer> al = new ArrayList<>(); for(int i: arr) { al.add(i); } Collections.sort(al); for(int i = 0; i < arr.length; i++) { arr[i] = al.get(i); } } public static void allDivisors(int n) { for(int i = 1; i*i <= n; i++) { if(n%i == 0) { System.out.println(i + " "); if(i != n/i) { System.out.println(n/i + " "); } } } } public static boolean isPrime(int n) { if(n < 1) return false; if(n == 2 || 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; } public static int gcd(int a, int b) { int dividend = a > b ? a : b; int divisor = a < b ? a : b; while(divisor > 0) { int reminder = dividend % divisor; dividend = divisor; divisor = reminder; } return dividend; } public static int lcm(int a, int b) { int lcm = gcd(a, b); int hcf = (a * b) / lcm; return hcf; } public static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } 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()); } boolean nextBoolean() { return Boolean.parseBoolean(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextArray(int n) { int[] a = new int[n]; for(int i=0; i<n; i++) { a[i] = nextInt(); } return a; } } } /** Dec Char Dec Char Dec Char Dec Char --------- --------- --------- ---------- 0 NUL (null) 32 SPACE 64 @ 96 ` 1 SOH (start of heading) 33 ! 65 A 97 a 2 STX (start of text) 34 " 66 B 98 b 3 ETX (end of text) 35 # 67 C 99 c 4 EOT (end of transmission) 36 $ 68 D 100 d 5 ENQ (enquiry) 37 % 69 E 101 e 6 ACK (acknowledge) 38 & 70 F 102 f 7 BEL (bell) 39 ' 71 G 103 g 8 BS (backspace) 40 ( 72 H 104 h 9 TAB (horizontal tab) 41 ) 73 I 105 i 10 LF (NL line feed, new line) 42 * 74 J 106 j 11 VT (vertical tab) 43 + 75 K 107 k 12 FF (NP form feed, new page) 44 , 76 L 108 l 13 CR (carriage return) 45 - 77 M 109 m 14 SO (shift out) 46 . 78 N 110 n 15 SI (shift in) 47 / 79 O 111 o 16 DLE (data link escape) 48 0 80 P 112 p 17 DC1 (device control 1) 49 1 81 Q 113 q 18 DC2 (device control 2) 50 2 82 R 114 r 19 DC3 (device control 3) 51 3 83 S 115 s 20 DC4 (device control 4) 52 4 84 T 116 t 21 NAK (negative acknowledge) 53 5 85 U 117 u 22 SYN (synchronous idle) 54 6 86 V 118 v 23 ETB (end of trans. block) 55 7 87 W 119 w 24 CAN (cancel) 56 8 88 X 120 x 25 EM (end of medium) 57 9 89 Y 121 y 26 SUB (substitute) 58 : 90 Z 122 z 27 ESC (escape) 59 ; 91 [ 123 { 28 FS (file separator) 60 < 92 \ 124 | 29 GS (group separator) 61 = 93 ] 125 } 30 RS (record separator) 62 > 94 ^ 126 ~ 31 US (unit separator) 63 ? 95 _ 127 DEL */
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
fb982e340f204c2f5619b677b198d1ba
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; public class class376 { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); if(a+b+2>n || Math.abs(a-b)>1) { System.out.println(-1); } else { if(a>=b) { int i=n,j=1; int ans[]=new int[n+1]; int k=0; int flag=0; if(a==b) { flag=1; } for(k=2;k<=n-1;k+=2) { if(a==0) { break; } ans[k]=i--; a--; if(b!=0) { ans[k+1]=j++; b--; } } if(flag==1) { for(k=1;k<=n;k++) { if(ans[k]==0) { ans[k]=j++; } System.out.print(ans[k]+" "); } System.out.println(); continue; } for(k=1;k<=n;k++) { if(ans[k]==0) { ans[k]=i--; } System.out.print(ans[k]+" "); } System.out.println(); } else { int i=n,j=1; int ans[]=new int[n+1]; int k=0; for(k=2;k<=n-1;k+=2) { if(b==0) { break; } ans[k]=j++; b--; if(a!=0) { ans[k+1]=i--; a--; } } for(k=1;k<=n;k++) { if(ans[k]==0) { ans[k]=j++; } System.out.print(ans[k]+" "); } System.out.println(); } } } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
04d43ffce27aab439ca0402df8aa48d8
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
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 FastWriter out; public static void main(String hi[]){ initializeIO(); sc=new FastReader(); // FastWriter out=new FastWriter(); int t=sc.nextInt(); // boolean[] seave=sieveOfEratosthenes((int)(1e6)); int[] seave2=sieveOfEratosthenesInt((int)(1e4)); // int t=1; // debug(seave2); while(t--!=0){ int n=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); if (((a+b)>n-2)||abs(a-b)>1) { print(-1); continue; } solve(n,a,b); } // System.out.println(String.format("%.10f", max)); } private static void solve(int n,int a,int b){ int[]arr=new int[n+1]; if(a==b){ int val=n; for(int i=2;i<=n&&b>0;i=i+2){ // debug(val+" "+i+" "+b); arr[i]=val; b--; val--; } int rem=n-val; // debug(val+" "+rem+" "+Arrays.toString(arr)); int v=1; rem++; for(int i=1;i<=n;i+=(rem>0)?2:1){ arr[i]=v++; rem--; } // debug(arr); }else if(a>b){ int val=n; for(int i=2;i<=n&&a>0;i=i+2){ // debug(val+" "+i+" "+b); arr[i]=val; a--; val--; } int rem=n-val; // debug(val+" "+rem+" "+Arrays.toString(arr)); rem++; for(int i=1;i<=n;i+=(rem>0)?2:1){ arr[i]=val--; rem--; } // debug(arr); }else { int val=1; for(int i=2;i<=n&&b>0;i=i+2){ debug(val+" "+i+" "+b); arr[i]=val; b--; val++; } // val--; int rem=val; // debug(val+" "+Arrays.toString(arr)); // rem++; for(int i=1;i<=n;i+=(rem>0)?2:1){ debug(i+" "+rem); arr[i]=val++; rem--; } // debug(arr); } for(int i=1;i<=n;i++){ out.print(arr[i]+" "); } print(""); } 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 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 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 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 print(String s){ out.println(s); } private static void debug(String s){ err.println(s); } private static int charToInt(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); } // debug(li+" "); 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){ 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; } 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(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
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
e4c18497090674608a313d7d418d90ff
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void reverse(int arr[]) { int l=0,r=arr.length-1; while(l<r) { int t=arr[l]; arr[l++]=arr[r]; arr[r--]=t; } } 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=sc.nextInt(); int b=sc.nextInt(); int arr[]=new int[n]; for(int i=1;i<=n;i++) arr[i-1]=i; if(Math.abs(a-b)>1 || a+b>n-2) { System.out.println(-1); } else { if(a==b) { for(int i=1;a-->0;i+=2) { int temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } else if(a>b) { for(int i=(n-(a*2));i<n-1;i+=2) { int temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } else { reverse(arr); for(int i=(n-(b*2))+1;i<n;i+=2) { int temp=arr[i]; arr[i]=arr[i-1]; arr[i-1]=temp; } } for(int x:arr) System.out.print(x+" "); System.out.println(); } } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
8cfe670740693bdad6493384a24aae4d
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; import java.lang.*; import java.math.BigInteger; import java.io.*; public class Main implements Runnable { public static void main(String[] args) { new Thread(null, new Main(), "whatever", 1 << 26).start(); } private FastScanner sc; private PrintWriter pw; public void run() { try { boolean isSumitting = true; // isSumitting = false; if (isSumitting) { pw = new PrintWriter(System.out); sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); } else { pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); sc = new FastScanner(new BufferedReader(new FileReader("input.txt"))); } } catch (Exception e) { throw new RuntimeException(); } int t = sc.nextInt(); // int t = 1; while (t-- > 0) { // sc.nextLine(); // System.out.println("for t=" + t); solve(); } pw.close(); } public long mod = 1_000_000_007; public void solve() { int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); boolean found = false; int start = -1, end = -1; StringBuilder sb = new StringBuilder(); boolean lastMaxima = false; if (a >= b) { sb.append("1 "); start = 2; end = n; lastMaxima = false; } else { sb.append(n + " "); start = 1; end = n - 1; lastMaxima = true; } for (int i = 1; i < n; i++) { if (lastMaxima && b == 0 && a > 0) { pw.println(-1); return; } if (!lastMaxima && a == 0 && b > 0) { pw.println(-1); return; } if (!lastMaxima && a > 0) { a--; sb.append(end + " "); end--; lastMaxima = !lastMaxima; } else if (lastMaxima && b > 0) { b--; sb.append(start + " "); start++; lastMaxima = !lastMaxima; } else if (lastMaxima && a == 0 && b == 0) { found = true; sb.append(end + " "); end--; } else if (!lastMaxima && a == 0 && b == 0) { found = true; sb.append(start + " "); start++; } } if (found) pw.println(sb.toString()); else pw.println(-1); } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(BufferedReader bf) { reader = bf; tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String[] nextStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } } private static class Sorter { public static <T extends Comparable<? super T>> void sort(T[] arr) { Arrays.sort(arr); } public static <T> void sort(T[] arr, Comparator<T> c) { Arrays.sort(arr, c); } public static <T> void sort(T[][] arr, Comparator<T[]> c) { Arrays.sort(arr, c); } public static <T extends Comparable<? super T>> void sort(ArrayList<T> arr) { Collections.sort(arr); } public static <T> void sort(ArrayList<T> arr, Comparator<T> c) { Collections.sort(arr, c); } public static void normalSort(int[] arr) { Arrays.sort(arr); } public static void normalSort(long[] arr) { Arrays.sort(arr); } public static void sort(int[] arr) { timSort(arr); } public static void sort(int[] arr, Comparator<Integer> c) { timSort(arr, c); } public static void sort(int[][] arr, Comparator<Integer[]> c) { timSort(arr, c); } public static void sort(long[] arr) { timSort(arr); } public static void sort(long[] arr, Comparator<Long> c) { timSort(arr, c); } public static void sort(long[][] arr, Comparator<Long[]> c) { timSort(arr, c); } private static void timSort(int[] arr) { Integer[] temp = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(int[] arr, Comparator<Integer> c) { Integer[] temp = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(int[][] arr, Comparator<Integer[]> c) { Integer[][] temp = new Integer[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; } private static void timSort(long[] arr) { Long[] temp = new Long[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(long[] arr, Comparator<Long> c) { Long[] temp = new Long[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(long[][] arr, Comparator<Long[]> c) { Long[][] temp = new Long[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; } } public long fastPow(long x, long y, long mod) { if (y == 0) return 1; if (y == 1) return x % mod; long temp = fastPow(x, y / 2, mod); long ans = (temp * temp) % mod; return (y % 2 == 1) ? (ans * (x % mod)) % mod : ans; } public long fastPow(long x, long y) { if (y == 0) return 1; if (y == 1) return x; long temp = fastPow(x, y / 2); long ans = (temp * temp); return (y % 2 == 1) ? (ans * x) : ans; } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
2b5a755ec0d1015da7941da0734dfefd
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; import java.lang.*; import java.math.BigInteger; import java.io.*; public class Main implements Runnable { public static void main(String[] args) { new Thread(null, new Main(), "whatever", 1 << 26).start(); } private FastScanner sc; private PrintWriter pw; public void run() { try { boolean isSumitting = true; // isSumitting = false; if (isSumitting) { pw = new PrintWriter(System.out); sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); } else { pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); sc = new FastScanner(new BufferedReader(new FileReader("input.txt"))); } } catch (Exception e) { throw new RuntimeException(); } int t = sc.nextInt(); // int t = 1; while (t-- > 0) { // sc.nextLine(); // System.out.println("for t=" + t); solve(); } pw.close(); } public long mod = 1_000_000_007; public void solve() { int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); // int temp = n - 2; // int min = -1, max = -1; // if (temp % 2 == 0) { // min = temp / 2; // max = temp / 2; // } else { // if (a >= b) { // max = (temp / 2) + 1; // min = temp / 2; // } else { // min = (temp / 2) + 1; // max = temp / 2; // } // } // if (a > max || b < min) { // pw.println(-1); // return; // } // max = a; // min = b; boolean found = false; int start = -1, end = -1; StringBuilder sb = new StringBuilder(); boolean lastMaxima = false; if (a >= b) { sb.append("1 "); start = 2; end = n; lastMaxima = false; } else { sb.append(n + " "); start = 1; end = n - 1; lastMaxima = true; } for (int i = 1; i < n; i++) { if (lastMaxima && b == 0 && a > 0) { pw.println(-1); return; } if (!lastMaxima && a == 0 && b > 0) { pw.println(-1); return; } if (!lastMaxima && a > 0) { a--; sb.append(end + " "); end--; lastMaxima = !lastMaxima; } else if (lastMaxima && b > 0) { b--; sb.append(start + " "); start++; lastMaxima = !lastMaxima; } else if (lastMaxima && a == 0 && b == 0) { found = true; sb.append(end + " "); end--; } else if (!lastMaxima && a == 0 && b == 0) { found = true; sb.append(start + " "); start++; } } if (found) pw.println(sb.toString()); else pw.println(-1); } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(BufferedReader bf) { reader = bf; tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String[] nextStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } } private static class Sorter { public static <T extends Comparable<? super T>> void sort(T[] arr) { Arrays.sort(arr); } public static <T> void sort(T[] arr, Comparator<T> c) { Arrays.sort(arr, c); } public static <T> void sort(T[][] arr, Comparator<T[]> c) { Arrays.sort(arr, c); } public static <T extends Comparable<? super T>> void sort(ArrayList<T> arr) { Collections.sort(arr); } public static <T> void sort(ArrayList<T> arr, Comparator<T> c) { Collections.sort(arr, c); } public static void normalSort(int[] arr) { Arrays.sort(arr); } public static void normalSort(long[] arr) { Arrays.sort(arr); } public static void sort(int[] arr) { timSort(arr); } public static void sort(int[] arr, Comparator<Integer> c) { timSort(arr, c); } public static void sort(int[][] arr, Comparator<Integer[]> c) { timSort(arr, c); } public static void sort(long[] arr) { timSort(arr); } public static void sort(long[] arr, Comparator<Long> c) { timSort(arr, c); } public static void sort(long[][] arr, Comparator<Long[]> c) { timSort(arr, c); } private static void timSort(int[] arr) { Integer[] temp = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(int[] arr, Comparator<Integer> c) { Integer[] temp = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(int[][] arr, Comparator<Integer[]> c) { Integer[][] temp = new Integer[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; } private static void timSort(long[] arr) { Long[] temp = new Long[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(long[] arr, Comparator<Long> c) { Long[] temp = new Long[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(long[][] arr, Comparator<Long[]> c) { Long[][] temp = new Long[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; } } public long fastPow(long x, long y, long mod) { if (y == 0) return 1; if (y == 1) return x % mod; long temp = fastPow(x, y / 2, mod); long ans = (temp * temp) % mod; return (y % 2 == 1) ? (ans * (x % mod)) % mod : ans; } public long fastPow(long x, long y) { if (y == 0) return 1; if (y == 1) return x; long temp = fastPow(x, y / 2); long ans = (temp * temp); return (y % 2 == 1) ? (ans * x) : ans; } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
6bdc84b1c99d09567e48941c62773b35
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.util.*; // import static j'>>>ava.lang.Math.min; public final class Main { static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(0, 1), new Pair(1, 0), new Pair(0, -1)}; static int mod = (int) (1e9 + 7); static int mod2 = 998244353; public static void main(String[] args) { int tt = i(); while (tt-- > 0) { solve(); } out.flush(); } public static void solve() { int n = i(); int a = i(); int b = i(); if(a == b+1) { if(a+b+2>n) out.println(-1); else { int[] arr = new int[n]; int aa = a; for(int i=n;i>0;i--) { if(aa == 0) { arr[i-1] = i; } else { aa--; arr[i-1] = i-1; arr[i-2] = i; i--; } } print(arr); } } else if(a==b) { if(a+b+2>n) out.println(-1); else { int[] arr = new int[n]; int aa = a; for(int i=n;i>0;i--) { if(aa == 0) { arr[i-1] = i; } else { aa--; arr[i-1] = i-1; arr[i-2] = i; i--; } } if(a!=0){ int temp = arr[0]; arr[0] = arr[1]; arr[1] = temp; } print(arr); } } else if(b==a+1) { if(a+b+2>n) out.println(-1); else { int[] arr = new int[n]; int bb = b; for(int i=0;i<n;i++) { if(bb == 0) arr[i] = i+1; else { arr[i] = i+2; arr[i+1] = i+1; i++; bb--; } } print(arr); } } else out.println(-1); } static int searchLT(int i, int j, long[] arr, long tar) { if(i==j) return i; else if(i==j-1) { return arr[j]<tar?j:i; } int mid = (i+j)/2; if(arr[mid]>=tar) return searchLT(i, mid-1, arr, tar); else return searchLT(mid, j, arr, tar); } static int searchGT(int i, int j, long[] arr, long tar) { // out.println(i+" "+j+" "+tar); if(i==j) return i; else if(i==j-1) { // out.println(i+" "+j+" "+tar); return arr[i]<tar?j:i; } int mid = (i+j)/2; if(arr[mid]<tar) return searchGT(mid+1, j, arr, tar); else if(arr[mid]>tar) return searchGT(i, mid, arr, tar); else { // out.println("equal case"); return mid;} } static long[] pre(int[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] pre(long[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static void print(char A[]) { for (char c : A) { out.print(c); } out.println(); } static void print(boolean A[]) { for (boolean c : A) { out.print(c + " "); } out.println(); } static void print(int A[]) { for (int c : A) { out.print(c + " "); } out.println(); } static void print(long A[]) { for (long i : A) { out.print(i + " "); } out.println(); } static void print(List<Integer> A) { for (int a : A) { System.out.print(a + " "); } System.out.println(); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static double d() { return in.nextDouble(); } static String s() { return in.nextLine(); } static String c() { return in.next(); } static int[][] inputWithIdx(int N) { int A[][] = new int[N][2]; for (int i = 0; i < N; i++) { A[i] = new int[]{i, in.nextInt()}; } return A; } static int[] input(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] inputLong(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) { A[i] = in.nextLong(); } return A; } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long GCD(long a, long b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long LCM(int a, int b) { return (long) a / GCD(a, b) * b; } static long LCM(long a, long b) { return a / GCD(a, b) * b; } static void shuffle(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } } static void shuffleAndSort(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static void shuffleAndSort(int[][] arr, Comparator<? super int[]> comparator) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int[] temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr, comparator); } static void shuffleAndSort(long[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); long temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static boolean isPerfectSquare(double number) { double sqrt = Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static void swap(int A[], int a, int b) { int t = A[a]; A[a] = A[b]; A[b] = t; } static void swap(char A[], int a, int b) { char t = A[a]; A[a] = A[b]; A[b] = t; } static long pow(long a, long b, int mod) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow = (pow * x) % mod; } x = (x * x) % mod; b /= 2; } return pow; } static long pow(long a, long b) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow *= x; } x = x * x; b /= 2; } return pow; } static long modInverse(long x, int mod) { return pow(x, mod - 2, mod); } static boolean isPrime(long N) { if (N <= 1) { return false; } if (N <= 3) { return true; } if (N % 2 == 0 || N % 3 == 0) { return false; } for (int i = 5; i * i <= N; i = i + 6) { if (N % i == 0 || N % (i + 2) == 0) { return false; } } return true; } public static String reverse(String str) { if (str == null) { return null; } return new StringBuilder(str).reverse().toString(); } public static void reverse(int[] arr) { for (int i = 0; i < arr.length / 2; i++) { int tmp = arr[i]; arr[arr.length - 1 - i] = tmp; arr[i] = arr[arr.length - 1 - i]; } } public static String repeat(char ch, int repeat) { if (repeat <= 0) { return ""; } final char[] buf = new char[repeat]; for (int i = repeat - 1; i >= 0; i--) { buf[i] = ch; } return new String(buf); } public static int[] manacher(String s) { char[] chars = s.toCharArray(); int n = s.length(); int[] d1 = new int[n]; for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1); while (0 <= i - k && i + k < n && chars[i - k] == chars[i + k]) { k++; } d1[i] = k--; if (i + k > r) { l = i - k; r = i + k; } } return d1; } public static int[] kmp(String s) { int n = s.length(); int[] res = new int[n]; for (int i = 1; i < n; ++i) { int j = res[i - 1]; while (j > 0 && s.charAt(i) != s.charAt(j)) { j = res[j - 1]; } if (s.charAt(i) == s.charAt(j)) { ++j; } res[i] = j; } return res; } } class Pair { int i; int j; Pair(int i, int j) { this.i = i; this.j = j; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Pair pair = (Pair) o; return i == pair.i && j == pair.j; } @Override public int hashCode() { return Objects.hash(i, j); } } 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; } } class Node { int val; public Node(int val) { this.val = val; } } class ST { int n; Node[] st; ST(int n) { this.n = n; st = new Node[4 * Integer.highestOneBit(n)]; } void build(Node[] nodes) { build(0, 0, n - 1, nodes); } private void build(int id, int l, int r, Node[] nodes) { if (l == r) { st[id] = nodes[l]; return; } int mid = (l + r) >> 1; build((id << 1) + 1, l, mid, nodes); build((id << 1) + 2, mid + 1, r, nodes); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } void update(int i, Node node) { update(0, 0, n - 1, i, node); } private void update(int id, int l, int r, int i, Node node) { if (i < l || r < i) { return; } if (l == r) { st[id] = node; return; } int mid = (l + r) >> 1; update((id << 1) + 1, l, mid, i, node); update((id << 1) + 2, mid + 1, r, i, node); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } Node get(int x, int y) { return get(0, 0, n - 1, x, y); } private Node get(int id, int l, int r, int x, int y) { if (x > r || y < l) { return new Node(0); } if (x <= l && r <= y) { return st[id]; } int mid = (l + r) >> 1; return comb(get((id << 1) + 1, l, mid, x, y), get((id << 1) + 2, mid + 1, r, x, y)); } Node comb(Node a, Node b) { if (a == null) { return b; } if (b == null) { return a; } return new Node(a.val | b.val); } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
fbcd847503760443e9751e8ba3c277be
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
/* بسم الله الرحمن الرحيم /$$$$$ /$$$$$$ /$$ /$$ /$$$$$$ |__ $$ /$$__ $$ |$$ |$$ /$$__ $$ | $$| $$ \ $$| $$|$$| $$ \ $$ | $$| $$$$$$$$| $$ / $$/| $$$$$$$$ / $$ | $$| $$__ $$ \ $$ $$/ | $$__ $$ | $$ | $$| $$ | $$ \ $$$/ | $$ | $$ | $$$$$$/| $$ | $$ \ $/ | $$ | $$ \______/ |__/ |__/ \_/ |__/ |__/ /$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$$$ /$$$$$$$ | $$__ $$| $$__ $$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$$ /$$$| $$$ /$$$| $$_____/| $$__ $$ | $$ \ $$| $$ \ $$| $$ \ $$| $$ \__/| $$ \ $$| $$ \ $$| $$$$ /$$$$| $$$$ /$$$$| $$ | $$ \ $$ | $$$$$$$/| $$$$$$$/| $$ | $$| $$ /$$$$| $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$ $$/$$ $$| $$$$$ | $$$$$$$/ | $$____/ | $$__ $$| $$ | $$| $$|_ $$| $$__ $$| $$__ $$| $$ $$$| $$| $$ $$$| $$| $$__/ | $$__ $$ | $$ | $$ \ $$| $$ | $$| $$ \ $$| $$ \ $$| $$ | $$| $$\ $ | $$| $$\ $ | $$| $$ | $$ \ $$ | $$ | $$ | $$| $$$$$$/| $$$$$$/| $$ | $$| $$ | $$| $$ \/ | $$| $$ \/ | $$| $$$$$$$$| $$ | $$ |__/ |__/ |__/ \______/ \______/ |__/ |__/|__/ |__/|__/ |__/|__/ |__/|________/|__/ |__/ */ import java.util.*; //import java.lang.*; import java.io.*; public class BuildthePermutation { public static void main(String[] args) throws java.lang.Exception { // your code goes here try { // Scanner sc=new Scanner(System.in); FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int n=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); if((a+b)>n-2)System.out.println(-1); else if(Math.abs(a-b)>1)System.out.println(-1); else{ int[] ans=new int[n]; if(a>b){ int l=1; int r=n; for(int i=1;i<n;i+=2){ if(a==0)break; ans[i]=r; r--; a--; if(a==0)break; } for(int i=2;i<n;i+=2){ if(b==0)break; ans[i]=l; l++; b--; if(b==0)break; } for(int i=0;i<n;i++){ if(ans[i]==0){ ans[i]=r; r--; } } } else if(b>a){ int l=1; int r=n; for(int i=1;i<n;i+=2){ if(b==0)break; ans[i]=l; l++; b--; if(b==0)break; } for(int i=2;i<n;i+=2){ if(a==0)break; ans[i]=r; r--; a--; if(a==0)break; } for(int i=0;i<n;i++){ if(ans[i]==0){ ans[i]=l; l++; } } } else{ int l=1; int r=n; for(int i=1;i<n;i+=2){ if(a==0)break; ans[i]=r; r--; a--; if(a==0)break; } for(int i=2;i<n;i+=2){ if(b==0)break; ans[i]=l; l++; b--; if(b==0)break; } for(int i=0;i<n;i++){ if(ans[i]==0){ ans[i]=l; l++; } } } for(int i:ans)System.out.print(i+" "); System.out.println(); } /* int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } */ /* * long[] arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); } */ } } catch (Exception e) { return; } } public static class pair { int ff; int ss; pair(int ff, int ss) { this.ff = ff; this.ss = ss; } } public static void rsa(int[] arr2){ Arrays.sort(arr2); int size = arr2.length; for (int left = 0; left < size / 2; left++) { int right = size - left - 1; int temp = arr2[right]; arr2[right] = arr2[left]; arr2[left] = temp; } } public static boolean checkBothTheArraysSame(int[] a, int[] b, int i, int j) { int n = a.length; int[] a1 = a; int[] b1 = b; if (i >= 0 && j >= 0) { for (int k = i; k <= j; k++) { a1[k] += 1; } } Arrays.sort(a1); Arrays.sort(b1); for (int ii = 0; ii < n; ii++) { if (a1[ii] != b1[ii]) return false; } return true; } static int BS(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] < element) { low = mid + 1; } else { high = mid; } } if (arr[low] == element) { return low; } else if (arr[high] == element) { return high; } return -1; } static int lower_bound(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] < element) { low = mid + 1; } else { high = mid; } } if (arr[low] >= element) { return low; } else if (arr[high] >= element) { return high; } return -1; } static int upper_bound(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] <= element) { low = mid + 1; } else { high = mid; } } if (arr[low] > element) { return low; } else if (arr[high] > element) { return high; } return -1; } public static long gcd_long(long a, long b) { // a/b,a-> dividant b-> divisor if (b == 0) return a; return gcd_long(b, a % b); } public static int gcd_int(int a, int b) { // a/b,a-> dividant b-> divisor if (b == 0) return a; return gcd_int(b, a % b); } public static int lcm(int a, int b) { int gcd = gcd_int(a, b); return (a * b) / gcd; } 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()); } double nextDouble() { return Double.valueOf(Integer.parseInt(next())); } String nextLine() { String s = ""; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } Long nextLong() { return Long.parseLong(next()); } } /* * JAVA STRING CONTAINS METHOD--------->str1.contains(str2)-->if(str2 is present * in str1){ return true; } else return false; */ public static boolean contains(String main, String Substring) { boolean flag = false; if (main == null && main.trim().equals("")) { return flag; } if (Substring == null) { return flag; } char fullstring[] = main.toCharArray(); char sub[] = Substring.toCharArray(); int counter = 0; if (sub.length == 0) { flag = true; return flag; } for (int i = 0; i < fullstring.length; i++) { if (fullstring[i] == sub[counter]) { counter++; } else { counter = 0; } if (counter == sub.length) { flag = true; return flag; } } return flag; } //FACTORISATION OF A NUMBER ---> OPTIMISED CODE //TC -->O(sqrt(N)); public static void factorize(int n){ for(int i=2;i*i<=n;i++){ if(n%i==0){ int cnt=0; while(n%i==0){ n/=i; cnt++; } System.out.println("DIVISOR is "+i+" "+"COUNT is "+cnt); } } if(n!=1){ //n is prime number System.out.println("DIVISOR is "+n+" "+"COUNT is "+1); } } //DISJOINT SET UNINION OPTIMALLY IMPLEMENTED USING PATH COMPRESSION AND RANK ARRAY------>>>>>>>>>>>>>>>>>>>>> //parent[i].ff==parent of i and parent[i].ss gives the rant of the set in which i belongs to public static int find_set(int a,pair[] parent){ if(parent[a].ff==a)return a; return parent[a].ff=find_set(parent[a].ff,parent); } public static void union_set(int a,int b,pair[] parent){ int a_root=find_set(a, parent); int b_root=find_set(b, parent); if(a_root==b_root)return; if(parent[a_root].ss<parent[b_root].ss){ parent[a_root].ff=b_root; } else if(parent[a_root].ss>parent[b_root].ss){ parent[b_root].ff=a_root; } else{ parent[b_root].ff=a_root; parent[a_root].ss++; } } } /* * public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){ * return true; } if(n<1 || m<1 || k<0){ return false; } boolean * tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ return true; } * return false; } */
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
4dbb06cc9bcc9cfd899852f65a9ee818
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author saikat021 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, OutputWriter out) { int T = in.nextInt(); while (T-- > 0) { int n = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); if (n >= (a + b + 2) && Math.abs(a - b) <= 1) { //solution exists int[] arr = new int[n + 1]; if (a > b) { for (int i = 1; i <= n; i++) { arr[i] = i; } int i = 2; while (b-- > 0) { swap(arr, i, i + 1); i = i + 2; } swap(arr, n, n - 1); } else if (a < b) { for (int i = 1; i <= n; i++) { arr[i] = n + 1 - i; } int i = 2; while (a-- > 0) { swap(arr, i, i + 1); i = i + 2; } swap(arr, n, n - 1); } else { for (int i = 1; i <= n; i++) { arr[i] = i; } int i = 2; while (a-- > 0) { swap(arr, i, i + 1); i += 2; } } for (int i = 1; i <= n; i++) { out.print(arr[i] + " "); } out.println(); } else { out.println(-1); } } } public void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println() { writer.println(); } public void close() { writer.close(); } public void println(int i) { writer.println(i); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; 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 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 boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
28e722b01f2dd4e4702462734f607435
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException; import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter; import java.security.AccessControlException;import java.util.List;import java.util.stream.IntStream; public class _B {static public void main(final String[] args) throws IOException {B._main(args);} static class B extends Solver{public B(){}static class Node{int value=0;Node prev =null,next=null;}@Override public void solve()throws IOException{int n=sc.nextInt();int a =sc.nextInt();int b=sc.nextInt();sc.nextLine();Node res=null;if(Math.abs(a-b)<2 && a+b+2<=n){int[]ints=IntStream.rangeClosed(1,n).toArray();int min=0;int max=ints.length -1;Node tail=null;int hi=Math.max(a,b);for(int i=0;i<hi;i++){Node node=new Node(); node.value=ints[a>=b?max:min];if(res==null){res=node;tail=node;}else{tail.next=node; node.prev=tail;tail=node;}if(a>=b){max--;}else{min++;}if(i<hi-1 || a==b){Node node1 =new Node();node1.value=ints[a>=b?min:max];node1.prev=node;node.next=node1;tail= node1;if(a>=b){min++;}else{max--;}}}Node node2=new Node();node2.value=ints[max--]; if(res==null){res=node2;tail=node2;}else{node2.next=res;res.prev=node2;res=node2; }node2=new Node();node2.value=ints[min++];tail.next=node2;node2.prev=tail;tail=node2; while(max>=min){node2=new Node();node2.value=ints[min];for(Node p=res;p.next!=null; p=p.next){if(p.value<node2.value && p.next.value>node2.value || p.value>node2.value && p.next.value<node2.value){node2.prev=p;node2.next=p.next;p.next=node2;break;} }min++;}}if(res==null){pw.println(-1);}else{for(Node p=res;p!=null;p=p.next){pw.print(p.value +" ");}pw.println();}}static public void _main(String[]args)throws IOException{new B().run();}}static class MyScanner{private StringBuilder cache=new StringBuilder(); int cache_pos=0;private int first_linebreak=-1;private int second_linebreak=-1;private StringBuilder sb=new StringBuilder();private InputStream is=null;public MyScanner(final InputStream is){this.is=is;}public String charToString(final int c){return String.format("'%s'", c=='\n'?"\\n":(c=='\r'?"\\r":String.valueOf((char)c)));}public int get(){int res =-1;if(cache_pos<cache.length()){res=cache.charAt(cache_pos);cache_pos++;if(cache_pos ==cache.length()){cache.delete(0,cache_pos);cache_pos=0;}}else{try{res=is.read(); }catch(IOException ex){throw new RuntimeException(ex);}}return res;}private void unget(final int c){if(cache_pos==0){cache.insert(0,(char)c);}else{cache_pos--;}} public String nextLine(){sb.delete(0,sb.length());int c;boolean done=false;boolean end=false;while((c=get())!=-1){if(check_linebreak(c)){done=true;if(c==first_linebreak) {if(!end){end=true;}else{cache.append((char)c);break;}}else if(second_linebreak!= -1 && c==second_linebreak){break;}}if(end && c!=first_linebreak && c!=second_linebreak) {cache.append((char)c);break;}if(!done){sb.append((char)c);}}return!done && sb.length() ==0?null:sb.toString();}private boolean check_linebreak(int c){if(c=='\n'|| c=='\r') {if(first_linebreak==-1){first_linebreak=c;}else if(c!=first_linebreak && second_linebreak ==-1){second_linebreak=c;}return true;}return false;}public int nextInt(){return Integer.parseInt(next());}public long nextLong(){return Long.parseLong(next());} public boolean hasNext(){boolean res=false;int c;while((c=get())!=-1){if(!check_linebreak(c) && c!=' '&& c!='\t'){res=true;unget(c);break;}}return res;}public String next(){ sb.delete(0,sb.length());boolean started=false;int c;while((c=get())!=-1){if(check_linebreak(c) || c==' '|| c=='\t'){if(started){unget(c);break;}}else{started=true;sb.append((char)c); }}return sb.toString();}public int nextChar(){return get();}public boolean eof() {int c=get();boolean res=false;if(c!=-1){unget(c);}else{res=true;}return res;}public double nextDouble(){return Double.parseDouble(next());}}static abstract class Solver {protected String nameIn=null;protected String nameOut=null;protected boolean singleTest =false;protected MyScanner sc=null;protected PrintWriter pw=null;private int current_test =0;private int count_tests=0;protected int currentTest(){return current_test;}protected int countTests(){return count_tests;}private void process()throws IOException{if(!singleTest) {count_tests=sc.nextInt();sc.nextLine();for(current_test=1;current_test<=count_tests; current_test++){solve();pw.flush();}}else{count_tests=1;current_test=1;solve();} }abstract protected void solve()throws IOException;public void run()throws IOException {boolean done=false;try{if(nameIn!=null){if(new File(nameIn).exists()){try(FileInputStream fis=new FileInputStream(nameIn);PrintWriter pw0=select_output();){select_output(); done=true;sc=new MyScanner(fis);pw=pw0;process();}}else{throw new RuntimeException("File " +new File(nameIn).getAbsolutePath()+" does not exist!");}}}catch(IOException | AccessControlException ex){}if(!done){try(PrintWriter pw0=select_output();){sc=new MyScanner(System.in); pw=pw0;process();}}}private PrintWriter select_output()throws FileNotFoundException {if(nameOut!=null){return new PrintWriter(nameOut);}return new PrintWriter(System.out); }}static abstract class Tester{static public int getRandomInt(final int min,final int max){return(min+(int)Math.floor(Math.random()*(max-min+1)));}static public long getRandomLong(final long min,final long max){return(min+(long)Math.floor(Math.random() *(max-min+1)));}static public double getRandomDouble(final double min,final double maxExclusive){return(min+Math.random()*(maxExclusive-min));}abstract protected void testOutput(final List<String>output_data);abstract protected void generateInput(); abstract protected String inputDataToString();private boolean break_tester=false; protected void beforeTesting(){}protected void breakTester(){break_tester=true;} public boolean broken(){return break_tester;}}}
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
9e33d4de2b7b338ecd7f430c7e1272fd
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; 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()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void sort(long[] a) { ArrayList<Long> arr = new ArrayList<>(); for (int i = 0; i < a.length; i++) arr.add(a[i]); Collections.sort(arr); for (int i = 0; i < arr.size(); i++) a[i] = arr.get(i); } public static void revsort(long[] a) { ArrayList<Long> arr = new ArrayList<>(); for (int i = 0; i < a.length; i++) arr.add(a[i]); Collections.sort(arr, Collections.reverseOrder()); for (int i = 0; i < arr.size(); i++) a[i] = arr.get(i); } //Cover the small test cases like for n=1 . public static void main(String[] args) { FastReader obj = new FastReader(); PrintWriter out = new PrintWriter(System.out); int len = obj.nextInt(); while (len-- != 0) { int n = obj.nextInt(); int a=obj.nextInt(); int b=obj.nextInt(); int tell=Math.abs(a-b); int max=Math.max(a, b); Vector<Integer> v=new Vector<>(); if(a==b) { int i=1,j=a+2; if(n>=(a+1)*2){ while(i<=a+1) { v.add(i); v.add(j); i++; j++; } while(j<=n) { v.add(j); j+=1; } for(int nd:v)out.print(nd+" "); out.println(); } else out.println(-1); } else if(tell==1) { if(max*2+1<=n) { if(a>b) { int i=n-a+1,j=1; while(i<=n) { v.add(j); v.add(i); i++; j++; } i=n-a; while(i>=j) { v.add(i); i--; } } else { int i=1,j=b+1; while(i<=b) { v.add(j); v.add(i); i++; j++; } while(j<=n) { v.add(j); j++; } } for(int nd:v)out.print(nd+" "); out.println(); } else out.println(-1); } else out.println(-1); } out.flush(); } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
da89302cc16fb41bfaaf3d8b45583e27
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class cf1608b { public static void main(String[] args) throws IOException { int t = ri(); while (t --> 0) { int n = rni(), a = ni(), b = ni(); if (abs(a - b) > 1 || a + b > n - 2) { prln(-1); continue; } int ans[] = new int[n], lp = 1, hp = n; if (b > a) { for (int i = 0; i < n; ++i) { if (a > 0 || b > 0) { if (i % 2 == 0) { ans[i] = hp--; } else { ans[i] = lp++; } if (i >= 1) { if (ans[i - 1] < ans[i]) { --a; } else { --b; } } } else if (i == 0 || ans[i - 1] < lp) { ans[i] = lp++; } else { ans[i] = hp--; } } } else { for (int i = 0; i < n; ++i) { if (a > 0 || b > 0) { if (i % 2 == 0) { ans[i] = lp++; } else { ans[i] = hp--; } if (i >= 1) { if (ans[i - 1] < ans[i]) { --a; } else { --b; } } } else if (i == 0 || ans[i - 1] < lp) { ans[i] = lp++; } else { ans[i] = hp--; } } } prln(ans); } close(); } static BufferedReader __i = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __o = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random __r = new Random(); // references // IBIG = 1e9 + 7 // IMAX ~= 2e9 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final long LMAX = 9223372036854775807L; // 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 long gcd(long a, long b) {return b == 0 ? a : gcd(b, a % 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);} // 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;} // input static void r() throws IOException {input = new StringTokenizer(rline());} static int ri() throws IOException {return Integer.parseInt(rline());} static long rl() throws IOException {return Long.parseLong(rline());} static double rd() throws IOException {return Double.parseDouble(rline());} static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;} static void ria(int[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni();} static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;} static void riam1(int[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1;} static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;} static void rla(long[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nl();} static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;} static void rda(double[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nd();} static char[] rcha() throws IOException {return rline().toCharArray();} static void rcha(char[] a) throws IOException {int n = a.length, i = 0; for (char c : rline().toCharArray()) a[i++] = c;} static String rline() throws IOException {return __i.readLine();} static String n() {return input.nextToken();} static int rni() throws IOException {r(); return ni();} static int ni() {return Integer.parseInt(n());} static long rnl() throws IOException {r(); return nl();} static long nl() {return Long.parseLong(n());} static double rnd() throws IOException {r(); return nd();} static double nd() {return Double.parseDouble(n());} // output static void pr(int i) {__o.print(i);} static void prln(int i) {__o.println(i);} static void pr(long l) {__o.print(l);} static void prln(long l) {__o.println(l);} static void pr(double d) {__o.print(d);} static void prln(double d) {__o.println(d);} static void pr(char c) {__o.print(c);} static void prln(char c) {__o.println(c);} static void pr(char[] s) {__o.print(new String(s));} static void prln(char[] s) {__o.println(new String(s));} static void pr(String s) {__o.print(s);} static void prln(String s) {__o.println(s);} static void pr(Object o) {__o.print(o);} static void prln(Object o) {__o.println(o);} static void prln() {__o.println();} static void pryes() {prln("yes");} static void pry() {prln("Yes");} static void prY() {prln("YES");} static void prno() {prln("no");} static void prn() {prln("No");} static void prN() {prln("NO");} static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;}; static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;} static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;} static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();} static void h() {prln("hlfd"); flush();} static void flush() {__o.flush();} static void close() {__o.close();} }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
a1a1f91c5cff605d2e008f41c6d5e84a
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.text.DecimalFormat; import java.util.*; public class Codeforces { static int mod= 998244353; public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); int t=fs.nextInt(); outer:while(t-->0) { int n=fs.nextInt(), a=fs.nextInt(), b=fs.nextInt(); if(a+b>n-2||Math.abs(a-b)>1) { out.println(-1); continue outer; } int arr[]=new int[n]; int cur=n; if(a>b) { for(int i=1;a>0;i+=2) { arr[i]=cur--; a--; } for(int i=0;i<n;i++) { if(arr[i]==0) arr[i]=cur--; } } else if(a<b){ cur=1; for(int i=1;b>0;i+=2) { arr[i]=cur++; b--; } for(int i=0;i<n;i++) { if(arr[i]==0) arr[i]=cur++; } } else { cur=n; for(int i=1;a>0;i+=2) { arr[i]=cur--; a--; } cur=1; for(int i=2;b>0;i+=2) { arr[i]=cur++; b--; } for(int i=0;i<n;i++) { if(arr[i]==0) { arr[i]=cur++; } } } for(int ele:arr) out.print(ele+" "); out.println(); } out.close(); } static long pow(long a,long b) { if(b<0) return 1; long res=1; while(b!=0) { if((b&1)!=0) { res*=a; res%=mod; } a*=a; a%=mod; b=b>>1; } return res; } static int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } static long nck(int n,int k) { if(k>n) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { // return fact[(int)n]; long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n); int 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()); } long[] lreadArray(int n) { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } 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
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
3f491846e14e876bd8af1467f9d94de2
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; import java.io.*; public class Practice { static boolean multipleTC = true; FastReader in; PrintWriter out; static int mod = (int) (1e9 + 7); int mod2 = 998244353; int parent[]; int rank[]; public static void main(String[] args) throws Exception { new Practice().run(); } void run() throws Exception { in = new FastReader(); out = new PrintWriter(System.out); int T = (multipleTC) ? ni() : 1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); } void pre() throws Exception { } void solve(int TC) throws Exception { int n = ni(); int a = ni(); int b = ni(); if (Math.abs(a - b) > 1 || a > (n - 1) / 2 || b > (n - 1) / 2 || (a + b) > (n - 2)) { pn("-1"); } else { if (a == 0 && b == 0) { int per[] = new int[n]; for (int i = 1; i <= n; i++) { per[i - 1] = i; } pn(printArr(per)); } else if (a == b) { int per[] = buildEqual(n, a, b); pn(printArr(per)); } else if (a > b) { int per[] = buildBackward(n, a, b); pn(printArr(per)); } else { int per[] = buildForward(n, a, b); pn(printArr(per)); } } } private int[] buildBackward(int n, int a, int b) { int per[] = new int[n]; for (int i = 1; i <= n; i++) { per[i - 1] = i; } int aCreated = 0; int bCreated = 0; for (int i = n - 1; i >= 1; i -= 2) { swap(per, i, i - 1); aCreated++; bCreated = aCreated - 1; if (aCreated == a && bCreated == b) { return per; } } return per; } private int[] buildEqual(int n, int a, int b) { int per[] = new int[n]; for (int i = 1; i <= n; i++) { per[i - 1] = i; } int aCreated = 0; int bCreated = 0; for (int i = 0; i < n - 1; i += 2) { swap(per, i, i + 1); bCreated++; aCreated = bCreated - 1; if (bCreated == b) { break; } } swap(per, n - 1, n - 2); return per; } private int[] buildForward(int n, int a, int b) { int per[] = new int[n]; for (int i = 1; i <= n; i++) { per[i - 1] = i; } int aCreated = 0; int bCreated = 0; for (int i = 0; i < n - 1; i += 2) { swap(per, i, i + 1); bCreated++; aCreated = bCreated - 1; if (aCreated == a && bCreated == b) { return per; } } return per; } void union(int u, int v) { u = findParent(u); v = findParent(v); if (rank[u] > rank[v]) { parent[v] = u; } else if (rank[u] < rank[v]) { parent[u] = v; } else { parent[v] = u; rank[u]++; } } int findParent(int v) { if (v == parent[v]) return v; return parent[v] = findParent(parent[v]); } StringBuilder printArr(int arr[]) { StringBuilder sb = new StringBuilder(); int n = arr.length; for (int i = 0; i < n; i++) { sb.append(arr[i] + " "); } return sb; } StringBuilder printArr(long arr[]) { StringBuilder sb = new StringBuilder(); int n = arr.length; for (int i = 0; i < n; i++) { sb.append(arr[i] + " "); } return sb; } class pair { int x, y; pair(int x, int y) { this.x = x; this.y = y; } @Override public String toString() { return " " + x + ", " + y + "]"; } } class pairL { long x, y; pairL(long x, long y) { this.x = x; this.y = y; } @Override public String toString() { return " " + x + ", " + y + "]"; } } boolean isSorted(int arr[]) { for (int i = 0; i < arr.length - 1; i++) { if (arr[i] > arr[i + 1]) { return false; } } return true; } void swap(int arr[], int x, int y) { int t = arr[x]; arr[x] = arr[y]; arr[y] = t; } static int bitCount(int x) { return x == 0 ? 0 : (1 + bitCount(x & (x - 1))); } void sort(int arr[]) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < arr.length; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < arr.length; i++) arr[i] = list.get(i); } static void dbg(Object... o) { System.err.println(Arrays.deepToString(o)); } ArrayList<Long> primeFactorization(long n) { ArrayList<Long> factorization = new ArrayList<>(); while (n % 2 == 0) { factorization.add(2L); n /= 2; } for (long d = 3; d * d <= n; d += 2) { while (n % d == 0) { factorization.add(d); n /= d; } } if (n > 1) factorization.add(n); return factorization; } private ArrayList<Integer> getPrimes(int n) { boolean sieve[] = new boolean[n + 1]; sieve[0] = sieve[1] = true; for (int i = 2; i * i <= n; i++) { if (!sieve[i]) { for (int j = i * i; j <= n; j += i) { sieve[j] = true; } } } ArrayList<Integer> primes = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (!sieve[i]) { primes.add(i); } } return primes; } public static boolean isPrime(long n) { if (n < 2) return false; if (n == 2 || n == 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; long sqrtN = (long) Math.sqrt(n) + 1; for (long i = 6L; i <= sqrtN; i += 6) { if (n % (i - 1) == 0 || n % (i + 1) == 0) return false; } return true; } public static long gcd(long a, long b) { if (a > b) a = (a + b) - (b = a); if (a == 0L) return b; return gcd(b % a, a); } public long binaryExpo(long base, long exp) { long res = 1; while (exp > 0) { if ((exp & 1) == 1) res = res * base; base = base * base; exp >>= 1; } return res; } public long pow(long base, long exp, long MOD) { base %= MOD; long res = 1; while (exp > 0) { if ((exp & 1) == 1) res = res * base % MOD; base = base * base % MOD; exp >>= 1; } return res; } public static long totient(long n) { long result = n; for (int p = 2; p * p <= n; ++p) if (n % p == 0) { while (n % p == 0) n /= p; result -= result / p; } if (n > 1) result -= result / n; return result; } int bit(long n) { return (n == 0) ? 0 : (1 + bit(n & (n - 1))); } public long max(long... arr) { long max = arr[0]; for (long itr : arr) max = Math.max(max, itr); return max; } public int max(int... arr) { int max = arr[0]; for (int itr : arr) max = Math.max(max, itr); return max; } public long min(long... arr) { long min = arr[0]; for (long itr : arr) min = Math.min(min, itr); return min; } public int min(int... arr) { int min = arr[0]; for (int itr : arr) min = Math.min(min, itr); return min; } public long sum(long... arr) { long sum = 0; for (long itr : arr) sum += itr; return sum; } public long sum(int... arr) { long sum = 0; for (int itr : arr) sum += itr; return sum; } void p(Object o) { out.print(o); } void pn(Object o) { out.println(o); } void pni(Object o) { out.println(o); out.flush(); } String n() throws Exception { return in.next(); } String nln() throws Exception { return in.nextLine(); } int ni() throws Exception { return Integer.parseInt(in.next()); } long nl() throws Exception { return Long.parseLong(in.next()); } long[] readArr(int n) throws Exception { long arr[] = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nl(); } return arr; } double nd() throws Exception { return Double.parseDouble(in.next()); } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception { br = new BufferedReader(new FileReader(s)); } String next() throws Exception { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception { String str = ""; try { str = br.readLine(); } catch (IOException e) { throw new Exception(e.toString()); } return str; } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
ba0d997f5201e3983a518d5a38025cf0
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.util.*; public class Main { static class Pair { int l,r; public Pair(int l,int r) { this.l=l; this.r=r; } } static long mod= (long)1e9+7; public static final double PI = 3.141592653589793d; public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public static void solve(int testNumber, InputReader in, OutputWriter out) { int t=in.readInt(); while(t-->0) { int n=in.readInt(); int a=in.readInt(); int b=in.readInt(); if(a+b>n-2 || Math.abs(a-b)>1) { out.printLine(-1); } else { int arr[]=new int[n]; if(a>=b) { for(int i=0;i<n;i++) { arr[i]=i+1; } } else { for(int i=0;i<n;i++) { arr[i]=n-i; } } for(int i=1;i<n && a!=0 && b!=0;i+=2) { CP.fast_swap(arr,i,i+1); --a; --b; } if(a>0 || b>0) { CP.fast_swap(arr,n-2,n-1); } for(int i=0;i<n;i++) { out.print(arr[i]+" "); } out.printLine(); } } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; 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 int readInt() { 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 int[] nextIntArray(int arraySize) { int[] array = new int[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = readInt(); } return array; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isNewLine(int c) { return c == '\n'; } public String nextLine() { int c = read(); StringBuilder result = new StringBuilder(); do { result.appendCodePoint(c); c = read(); } while (!isNewLine(c)); return result.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sign = 1; if (c == '-') { sign = -1; c = read(); } long result = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } result *= 10; result += c & 15; c = read(); } while (!isSpaceChar(c)); return result * sign; } public long[] nextLongArray(int arraySize) { long array[] = new long[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextLong(); } return array; } public double nextDouble() { double ret = 0, div = 1; byte c = (byte) read(); while (c <= ' ') { c = (byte) read(); } boolean neg = (c == '-'); if (neg) { c = (byte) read(); } do { ret = ret * 10 + c - '0'; } while ((c = (byte) read()) >= '0' && c <= '9'); if (c == '.') { while ((c = (byte) read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class CP { static boolean isPrime(long n) { if (n <= 1) return false; if (n == 2 || n == 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; (long) i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } static String addChar(String s, int n, String ch) { String res =s+String.join("", Collections.nCopies(n, ch)); return res; } static int ifnotPrime(int[] prime, int x) { return (prime[x / 64] & (1 << ((x >> 1) & 31))); } static int log2(long n) { return (int)(Math.log10(n) / Math.log10(2)); } static void makeComposite(int[] prime, int x) { prime[x / 64] |= (1 << ((x >> 1) & 31)); } static ArrayList<Integer> bitWiseSieve(int n) { ArrayList<Integer> al = new ArrayList<>(); int prime[] = new int[n / 64 + 1]; for (int i = 3; i * i <= n; i += 2) { if (ifnotPrime(prime, i) == 0) for (int j = i * i, k = i << 1; j < n; j += k) makeComposite(prime, j); } al.add(2); for (int i = 3; i <= n; i += 2) if (ifnotPrime(prime, i) == 0) al.add(i); return al; } public static long[] sort(long arr[]){ List<Long> list = new ArrayList<>(); for(long n : arr){list.add(n);} Collections.sort(list); for(int i=0;i<arr.length;i++){ arr[i] = list.get(i); } return arr; } static ArrayList<Integer> sieve(long size) { ArrayList<Integer> pr = new ArrayList<Integer>(); boolean prime[] = new boolean[(int) size]; for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i * i < prime.length; i++) { if (prime[i]) { for (int j = i * i; j < prime.length; j += i) { prime[j] = false; } } } for (int i = 2; i < prime.length; i++) if (prime[i]) pr.add(i); return pr; } static ArrayList<Integer> segmented_sieve(int l, int r, ArrayList<Integer> primes) { ArrayList<Integer> al=new ArrayList<>(); if (l == 1) ++l; int max = r - l + 1; int arr[] = new int[max]; for (int p : primes) { if (p * p <= r) { int i = (l / p) * p; if (i < l) i += p; for (; i <= r; i += p) { if (i != p) { arr[i - l] = 1; } } } } for (int i = 0; i < max; ++i) { if (arr[i] == 0) { al.add(l+i); } } return al; } static boolean isfPrime(long n, int iteration) { if (n == 0 || n == 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; Random rand = new Random(); for (int i = 0; i < iteration; i++) { long r = Math.abs(rand.nextLong()); long a = r % (n - 1) + 1; if (modPow(a, n - 1, n) != 1) return false; } return true; } static long modPow(long a, long b, long c) { long res = 1; for (int i = 0; i < b; i++) { res *= a; res %= c; } return res % c; } private static long binPower(long a, long l, long mod) { long res = 0; while (l > 0) { if ((l & 1) == 1) { res = mulmod(res, a, mod); ; l >>= 1; } a = mulmod(a, a, mod); } return res; } private static long mulmod(long a, long b, long c) { long x = 0, y = a % c; while (b > 0) { if (b % 2 == 1) { x = (x + y) % c; } y = (y * 2L) % c; b /= 2; } return x % c; } static long binary_Expo(long a, long b) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res *= a; --b; } a *= a; b /= 2; } return res; } static long Modular_Expo(long a, long b) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res = (res * a) % 1000000007 ; --b; } a = (a * a) % 1000000007; b /= 2; } return res%1000000007; } static int i_gcd(int a, int b) { while (true) { if (b == 0) return a; int c = a; a = b; b = c % b; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long ceil_div(long a, long b) { return (a + b - 1) / b; } static int getIthBitFromInt(int bits, int i) { return (bits >> (i - 1)) & 1; } private static TreeMap<Long, Long> primeFactorize(long n) { TreeMap<Long, Long> pf = new TreeMap<>(Collections.reverseOrder()); long cnt = 0; long total = 1; for (long i = 2; (long) i * i <= n; ++i) { if (n % i == 0) { cnt = 0; while (n % i == 0) { ++cnt; n /= i; } pf.put(cnt, i); //total*=(cnt+1); } } if (n > 1) { pf.put(1L, n); //total*=2; } return pf; } static long upper_Bound(long a[], long x) { long l = -1, r = a.length; while (l + 1 < r) { long m = (l + r) >>> 1; if (a[(int) m] <= x) l = m; else r = m; } return l + 1; } static int lower_Bound(ArrayList<Integer> a,int x) { int l = 0, r = a.size()-1,ans=-1,mid=0; while(l<=r) { mid=(l+r)>>1; if(a.get(mid)<=x) { ans=mid; l=mid+1; } else { r=mid-1; } } return ans; } static long lower_Bound(long a[],long x) { int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[(int) m] >= x) r = m; else l = m; } return (long)r; } static int upperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static int bsh(int a[],int t) { int ans =-1; int i = 0, j = a.length - 1; while (i <= j) { int mid = i + (j - i) / 2; if (a[mid] > t) { ans = mid; //System.out.print("uppr "+a[ans]); j=mid-1; } else{ //System.out.print("less "+a[mid]); i=mid+1; } } return ans; } static int bsl(int a[],int t) { int ans =-1; int i = 0, j = a.length-1; while (i <= j) { int mid = i + (j - i) / 2; if (a[mid] <= t) { ans = mid; i=mid+1; } else if (a[mid] > t) { j = mid - 1; } } return ans; } static int upper_Bound(ArrayList<Integer> a,int x) //closest to the right { int l = 0, r = a.size()-1,ans=-1,mid=0; while(l<=r) { mid=(l+r)>>1; if(a.get(mid)>x) { ans=mid; r=mid-1; } else { l=mid+1; } } return ans; } static boolean isSquarefactor(int x, int factor, int target) { int s = (int) Math.round(Math.sqrt(x)); return factor * s * s == target; } static boolean isSquare(int x) { int s = (int) Math.round(Math.sqrt(x)); return x * x == s; } static void sort(int a[]) // heap sort { PriorityQueue<Integer> q = new PriorityQueue<>(); for (int i = 0; i < a.length; i++) q.add(a[i]); for (int i = 0; i < a.length; i++) a[i] = q.poll(); } static void shuffle(int[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); fast_swap(in, idx, i); } } static boolean isPalindrome(String s) { StringBuilder sb=new StringBuilder(s); sb.reverse(); if(s.equals(sb.toString())) { return true; } return false; } static int[] computeLps(String pat) { int len = 0, i = 1, m = pat.length(); int lps[] = new int[m]; lps[0] = 0; while (i < m) { if (pat.charAt(i) == pat.charAt(len)) { ++len; lps[i] = len; ++i; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = len; ++i; } } } return lps; } static void kmp(String s, String pat,int[] prf,int[] st) { int n = s.length(), m = pat.length(); int lps[] = computeLps(pat); int i = 0, j = 0; while (i < n) { if (s.charAt(i) == pat.charAt(j)) { i++; j++; if (j == m) { ++prf[(i-j)+m]; ++st[(i-j)+1]; j=lps[j - 1]; } } else { if (j != 0) { j = lps[j - 1]; } else { i++; } } } } static void reverse_ruffle_sort(int a[]) { shuffle(a); Arrays.sort(a); for (int l = 0, r = a.length - 1; l < r; ++l, --r) fast_swap(a, l, r); } static void ruffle_sort(int a[]) { shuffle(a); Arrays.sort(a); } static int getMax(int arr[], int n) { int mx = arr[0]; for (int i = 1; i < n; i++) if (arr[i] > mx) mx = arr[i]; return mx; } static ArrayList<Long> primeFactors(long n) { ArrayList<Long> al = new ArrayList<>(); al.add(1L); while (n % 2 == 0) { if(!al.contains(2L)) { al.add(2L); } n /= 2L; } for (int i = 3; (long) i * i <= n; i += 2) { while ((n % i == 0)) { if(!al.contains((long)i)) { al.add((long) i); } n /= i; } } if (n > 2) { if(!al.contains(n)) { al.add(n); } } return al; } static int[] z_function(String s) { int n = s.length(), z[] = new int[n]; for (int i = 1, l = 0, r = 0; i < n; ++i) { if (i <= r) z[i] = Math.min(z[i - l], r - i + 1); while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i])) ++z[i]; if (i + z[i] - 1 > r) { l = i; r = i + z[i] - 1; } } return z; } static void swap(int a[], int idx1, int idx2) { a[idx1] += a[idx2]; a[idx2] = a[idx1] - a[idx2]; a[idx1] -= a[idx2]; } static void fast_swap(int[] a, int idx1, int idx2) { if (a[idx1] == a[idx2]) return; a[idx1] ^= a[idx2]; a[idx2] ^= a[idx1]; a[idx1] ^= a[idx2]; } public static void fast_sort(long[] array) { ArrayList<Long> copy = new ArrayList<>(); for (long i : array) copy.add(i); Collections.sort(copy); for (int i = 0; i < array.length; i++) array[i] = copy.get(i); } static int divCount(int n) { boolean hash[] = new boolean[n + 1]; Arrays.fill(hash, true); for (int p = 2; p * p < n; p++) if (hash[p] == true) for (int i = p * 2; i < n; i += p) hash[i] = false; int total = 1; for (int p = 2; p <= n; p++) { if (hash[p]) { int count = 0; if (n % p == 0) { while (n % p == 0) { n = n / p; count++; } total = total * (count + 1); } } } return total; } static long binomialCoeff(long n,long k) { long res = 1; if (k > n - k) k = n - k; for (int i = 0; i < k; ++i) { res = (res*(n - i)); res /= (i + 1); } return res; } static long c(long fact[],long n,long k) { if(k>n) return 0; long res=fact[(int)n]; res= (int) ((res * Modular_Expo(fact[(int)k], mod - 2))%mod); res= (int) ((res * Modular_Expo(fact[(int)n - (int)k], mod - 2))%mod); return res%mod; } public static ArrayList<Long> getFactors(long x) { ArrayList<Long> facts=new ArrayList<>(); for(long i=2;i*i<=x;++i) { if(x%i==0) { facts.add(i); if(i!=x/i) { facts.add(x/i); } } } return facts; } static void matrix_expo(long n,long[][] A, long[][] I){ while(n>0){ if(n%2==0){ Multiply(A,A); n/=2; } else{ Multiply(I,A); n--; } } } static void Multiply(long[][] A , long[][]B){ int n= A.length , m=A[0].length, p=B[0].length; long [][] C=new long[n][p]; for(int i=0;i<n;i++){ for(int j=0;j<p;j++){ for(int k=0;k<m;k++){ C[i][j]+=((A[i][k]%mod)*(B[k][j]%mod))%mod; C[i][j]=C[i][j]%mod; } } } for(int i=0;i<n;i++){ for(int j=0;j<p;j++){ A[i][j]=C[i][j]; } } } public static HashMap<Integer, Integer> sortMap(HashMap<Integer, Integer> map) { List<Map.Entry<Integer,Integer>> list=new LinkedList<>(map.entrySet()); Collections.sort(list,(o1,o2)->o2.getValue()-o1.getValue()); HashMap<Integer,Integer> temp=new LinkedHashMap<>(); for(Map.Entry<Integer,Integer> i:list) { temp.put(i.getKey(),i.getValue()); } return temp; } public static long lcm(long l,long l2) { long val=CP.gcd(l,l2); return (l*l2)/val; } } static void py() { System.out.println("YES"); } static void pn() { System.out.println("NO"); } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } public void printLine(int i) { writer.println(i); } public void printLine(char c) { writer.println(c); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
2bcd60104d948f2f2a9a6faf4ace71f5
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.math.*; public class Final { static final Reader s = new Reader(); static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int t = s.nextInt(); // int t=1; for(int i=1; i<=t; ++i) { // out.print("Case #"+i+": "); new Solver(); } out.close(); } static class Solver { Solver() { int n = s.nextInt(); int a = s.nextInt(); int b = s.nextInt(); int[] ans = new int[n]; TreeSet<Integer> se = new TreeSet<>(); if(abs(a-b)>1 || a+b+2>n) { System.out.println(-1); return; } Arrays.fill(ans, -1); for(int i=1;i<=n;i++)se.add(i); StringBuilder sb = new StringBuilder(""); if(a>b) { int max = n; int min=1; int cnt=0; for(int i=1;cnt<a;i+=2,cnt++) { ans[i] = max; se.remove(max); max--; } cnt=0; for(int i=2;cnt<b;i+=2,cnt++) { ans[i] = min; se.remove(min); min++; } for(int i=0;i<n;i++) { if(ans[i]==-1) { ans[i] = se.last(); se.remove(ans[i]); } sb.append(ans[i]+" "); } } else if(a<b) { int max = n; int min=1; int cnt=0; for(int i=1;cnt<b;i+=2,cnt++) { ans[i] = min; se.remove(min); min++; } cnt=0; for(int i=2;cnt<a;i+=2,cnt++) { ans[i] = max; se.remove(max); max--; } for(int i=0;i<n;i++) { if(ans[i]==-1) { ans[i] = se.first(); se.remove(ans[i]); } sb.append(ans[i]+" "); } } else { int max = n; int min=1; int cnt=0; for(int i=1;cnt<a;i+=2,cnt++) { ans[i] = max; se.remove(max); max--; } cnt=0; for(int i=2;cnt<b;i+=2,cnt++) { ans[i] = min; se.remove(min); min++; } for(int i=0;i<n;i++) { if(ans[i]==-1) { ans[i] = se.first(); se.remove(ans[i]); } sb.append(ans[i]+" "); } } System.out.println(sb); } } static class Reader { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; String next() { while(st==null||!st.hasMoreTokens()) { try { st=new StringTokenizer(in.readLine()); } catch(Exception e) {} } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
bee44e8aa73cc514ff34417a50601880
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.util.*; public class cp { static int mod=(int)1e9+7; // static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static int[] sp; static int size=(int)1e6; static int[] arInt; static long[] arLong; public static void main(String[] args) throws IOException { long tc=sc.nextLong(); // Scanner sc=new Scanner(System.in); // int tc=1; // primeSet=new HashSet<>(); // sieveOfEratosthenes((int)1e5); while(tc-->0) { int n=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); if(a+b+2>n || Math.abs(a-b)>1) { out.println(-1); continue; } ArrayList<Integer> ans=new ArrayList<>(); if(a==b) { for(int i=0;i<=a;i++) { ans.add(i*2+2); ans.add(i*2+1); } Collections.reverse(ans); int i=a*2+3; while(i<=n) { ans.add(i++); } } else { for(int i=0;i<Math.max(a, b);i++) { ans.add(i*2+2); ans.add(i*2+1); } ans.add(2*Math.max(a, b)+1); int i=Math.max(a, b)*2+2; while(i<=n) { ans.add(i++); } if(a>b) { for(i=0;i<n;i++) { ans.set(i, n+1-ans.get(i)); } } } print_int(ans); } out.flush(); out.close(); System.gc(); } /* ...SOLUTION ENDS HERE...........SOLUTION ENDS HERE... */ static void arrInt(int n) throws IOException { arInt=new int[n]; for (int i = 0; i < arInt.length; i++) { arInt[i]=sc.nextInt(); } } static void arrLong(int n) throws IOException { arLong=new long[n]; for (int i = 0; i < arInt.length; i++) { arLong[i]=sc.nextLong(); } } static boolean util(long n,long k,long d1,long d2) { if(n%3!=0) return false; for(int sign1=-1;sign1<=1;sign1++) { for(int sign2=-1;sign2<=1;sign2++) { if(sign1==0 || sign2==0) continue; long D1=d1*sign1; long D2=d2*sign2; long x2=(k-D1+D2)/3; if((k-D1+D2)%3!=0) continue; if(x2>=0 && x2<=k) { long x1=D1+x2; long x3=x2-D2; if(x1==x2 && x2==x3 && x3%3==0) return true; if (x1 >= 0 && x1 <= k && x3 >= 0 && x3 <= k) { if (x1 <= n / 3 && x2 <= n / 3 && x3 <= n / 3) { long temp=n/3-x1; temp+=n/3-x2; temp+=n/3-x3; return temp==n-k; } } } } } return false; } // function to find last index <= y static int upper(ArrayList<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; } static int lower(ArrayList<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; } static int N = 501; // Array to store inverse of 1 to N static long[] factorialNumInverse = new long[N + 1]; // Array to precompute inverse of 1! to N! static long[] naturalNumInverse = new long[N + 1]; // Array to store factorial of first N numbers static long[] fact = new long[N + 1]; // Function to precompute inverse of numbers public static void InverseofNumber(int p) { naturalNumInverse[0] = naturalNumInverse[1] = 1; for(int i = 2; i <= N; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse[0] = factorialNumInverse[1] = 1; // Precompute inverse of natural numbers for(int i = 2; i <= N; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to N public static void factorial(int p) { fact[0] = 1; // Precompute factorials for(int i = 1; i <= N; i++) { fact[i] = (fact[i - 1] * (long)i) % p; } } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R, int p) { // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p; return ans; } static String tr(String s) { int now = 0; while (now + 1 < s.length() && s.charAt(now)== '0') ++now; return s.substring(now); } static ArrayList<Integer> ans; static void dfs(int node,Graph gg,int cnt,int k,ArrayList<Integer> temp) { if(cnt==k) return; for(Integer each:gg.list[node]) { if(each==0) { temp.add(each); ans=new ArrayList<>(temp); temp.remove(temp.size()-1); continue; } temp.add(each); dfs(each,gg,cnt+1,k,temp); temp.remove(temp.size()-1); } return; } static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static ArrayList<Integer> commDiv(int a, int b) { // find gcd of a, b int n = gcd(a, b); // Count divisors of n. ArrayList<Integer> Div=new ArrayList<>(); for (int i = 1; i <= Math.sqrt(n); i++) { // if 'i' is factor of n if (n % i == 0) { // check if divisors are equal if (n / i == i) Div.add(i); else { Div.add(i); Div.add(n/i); } } } return Div; } static HashSet<Integer> factors(int x) { HashSet<Integer> a=new HashSet<Integer>(); for(int i=2;i*i<=x;i++) { if(x%i==0) { a.add(i); a.add(x/i); } } return a; } static class Node { int vertex; HashSet<Node> adj; boolean rem; Node(int ver) { vertex=ver; rem=false; adj=new HashSet<Node>(); } @Override public String toString() { return vertex+" "; } } static class Tuple{ int a; int b; int c; public Tuple(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } } //function to find prime factors of n static HashMap<Long,Long> findFactors(long n2) { HashMap<Long,Long> ans=new HashMap<>(); if(n2%2==0) { ans.put(2L, 0L); // cnt++; while((n2&1)==0) { n2=n2>>1; ans.put(2L, ans.get(2L)+1); // } } for(long i=3;i*i<=n2;i+=2) { if(n2%i==0) { ans.put((long)i, 0L); // cnt++; while(n2%i==0) { n2=n2/i; ans.put((long)i, ans.get((long)i)+1); } } } if(n2!=1) { ans.put(n2, ans.getOrDefault(n2, (long) 0)+1); } return ans; } //fenwick tree implementaion static class fwt { int n; long BITree[]; fwt(int n) { this.n=n; BITree=new long[n+1]; } fwt(int arr[], int n) { this.n=n; BITree=new long[n+1]; for(int i = 0; i < n; i++) updateBIT(n, i, arr[i]); } long getSum(int index) { long sum = 0; index = index + 1; while(index>0) { sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(int n, int index,int val) { index = index + 1; while(index <= n) { BITree[index] += val; index += index & (-index); } } void print() { for(int i=0;i<n;i++) out.print(getSum(i)+" "); out.println(); } } //Function to find number of set bits static int setBitNumber(long n) { if (n == 0) return 0; int msb = 0; n = n / 2; while (n != 0) { n = n / 2; msb++; } return msb; } static int getFirstSetBitPos(long n) { return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1; } static ArrayList<Integer> primes; static HashSet<Integer> primeSet; static boolean prime[]; static 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 = 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; } } // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) primeSet.add(i); } } static long mod(long a, long b) { long c = a % b; return (c < 0) ? c + b : c; } static void swap(long arr[],int i,int j) { long temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } static boolean util(int a,int b,int c) { if(b>a)util(b, a, c); while(c>=a) { c-=a; if(c%b==0) return true; } return (c%b==0); } static void flag(boolean flag) { out.println(flag ? "YES" : "NO"); out.flush(); } static void print(int a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print(long a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print_int(ArrayList<Integer> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void print_long(ArrayList<Long> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void printYesNo(boolean condition) { if (condition) { out.println("Yes"); } else { out.println("No"); } } static int LowerBound(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; } static 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 static 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; } static int upperIndex(long arr[], int n, long 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; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static int UpperBound(long a[], long x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static class DisjointUnionSets { int[] rank, parent; int n; // Constructor public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } // Creates n sets with single item in each void makeSet() { for (int i = 0; i < n; i++) parent[i] = i; } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } // Unites the set that includes x and the set // that includes x void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else // if ranks are the same { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } // if(xRoot!=yRoot) // parent[y]=x; } int connectedComponents() { int cnt=0; for(int i=0;i<n;i++) { if(parent[i]==i) cnt++; } return cnt; } } static class Graph { int v; ArrayList<Integer> list[]; Graph(int v) { this.v=v; list=new ArrayList[v+1]; for(int i=1;i<=v;i++) list[i]=new ArrayList<Integer>(); } void addEdge(int a, int b) { this.list[a].add(b); } } // static class GraphMap{ // Map<String,ArrayList<String>> graph; // GraphMap() { // // TODO Auto-generated constructor stub // graph=new HashMap<String,ArrayList<String>>(); // // } // void addEdge(String a,String b) // { // if(graph.containsKey(a)) // this.graph.get(a).add(b); // else { // this.graph.put(a, new ArrayList<>()); // this.graph.get(a).add(b); // } // } // } // static void dfsMap(GraphMap g,HashSet<String> vis,String src,int ok) // { // vis.add(src); // // if(g.graph.get(src)!=null) // { // for(String each:g.graph.get(src)) // { // if(!vis.contains(each)) // { // dfsMap(g, vis, each, ok+1); // } // } // } // // cnt=Math.max(cnt, ok); // } static double sum[]; static long cnt; // static void DFS(Graph g, boolean[] visited, int u) // { // visited[u]=true; // // for(int i=0;i<g.list[u].size();i++) // { // int v=g.list[u].get(i); // // if(!visited[v]) // { // cnt1=cnt1*2; // DFS(g, visited, v); // // } // // } // // // } static class Pair implements Comparable<Pair> { int x,y; Pair(int x,int y) { this.x=x; this.y=y; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return this.x-o.x; } } static long sum_array(int a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static long sum_array(long a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void reverse_array(int a[]) { int n=a.length; int i,t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static void reverse_array(long a[]) { int n=a.length; int i; long t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } // static long modInverse(long a, long m) // { // long g = gcd(a, m); // // return power(a, m - 2, m); // // } static long power(long x, long y) { long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static int power(int x, int y) { int res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static long gcd(long a, long b) { if (a == 0) return b; //cnt+=a/b; return gcd(b%a,a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } 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[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; 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 PrintWriter out=new PrintWriter(System.out); static int int_max=Integer.MAX_VALUE; static int int_min=Integer.MIN_VALUE; static long long_max=Long.MAX_VALUE; static long long_min=Long.MIN_VALUE; }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
4ec52778450fbbd0996d1c207d5f58e3
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void reverse(int arr[]) { int l=0,r=arr.length-1; while(l<r) { int t=arr[l]; arr[l++]=arr[r]; arr[r--]=t; } } 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=sc.nextInt(); int b=sc.nextInt(); int arr[]=new int[n]; for(int i=1;i<=n;i++) arr[i-1]=i; if(Math.abs(a-b)>1 || a+b>n-2) { System.out.println(-1); } else { if(a==b) { for(int i=1;a-->0;i+=2) { int temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } else if(a>b) { for(int i=(n-(a*2));i<n-1;i+=2) { int temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } else { reverse(arr); for(int i=(n-(b*2))+1;i<n;i+=2) { int temp=arr[i]; arr[i]=arr[i-1]; arr[i-1]=temp; } } for(int x:arr) System.out.print(x+" "); System.out.println(); } } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
a0823cf84733349919245be19a891c4a
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.util.*; public class Main{ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); if(a+b==0) { for(int i=1;i<=n;i++) { System.out.print(i+" "); } System.out.println(); } else if(a+b>n-2) { System.out.println("-1"); } else if(a>n/2||b>(n-1)/2) { System.out.println("-1"); } else if(Math.abs(a-b)>1) { System.out.println("-1"); } else if(a+b==n-2) { if(a>=b) { for(int i=1,c=n;i<=n/2;i++,c--) { System.out.print(i+" "+c+" "); } if(n%2==1) { System.out.print(n/2+1); } System.out.println(); } else { for(int i=1,c=n;i<=n/2;i++,c--) { System.out.print(c+" "+i+" "); } if(n%2==1) { System.out.print(n/2+1); } System.out.println(); } } else { int ans[]=new int[n]; if(a==b) { int s=1,e=n,idx=0; while(idx<n) { if(a>0) { ans[idx]=s; ans[++idx]=e; s++; e--; a--; idx++; } else { ans[idx]=s; s++; idx++; } } } else if(a>b) { int s=1,e=n,idx=0; while(idx<n) { if(a>0) { ans[idx]=s; ans[++idx]=e; s++; e--; a--; b--; idx++; } else { ans[idx]=e; e--; idx++; } } } else { int s=1,e=n,idx=0; while(idx<n) { if(b>0) { ans[idx]=e; ans[++idx]=s; s++; e--; b--; idx++; } else { ans[idx]=s; s++; idx++; } } } for(int i:ans) { System.out.print(i+" "); } System.out.println(); } } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
43b5bff8cadad718e0d053d49632b1f0
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class test { static StringBuilder sb; static long fact[]; static int mod = (int) (1e9 + 7); static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return (int) (this.y - o.y); } } // static void solve() { // int N = i(); // int[] arr1 = readArray(N); // int[] arr = new int[N + 1]; // for (int i = 1; i <= N; i++) { // arr[i] = arr1[i - 1]; // } // // fenwicktree(arr); // // int Q = i(); // for (int i = 0; i < Q; i++) { // String s = s(); // if (s.equals("q")) { // int l = i(); // int r = i(); // long val = query(l, r); // sb.append(val + "\n"); // } else { // int idx = i(); // int delta = i(); // arr[idx] += delta; // update(idx, delta); // } // } // } static void solve() { int n = i(); int a = i(); int b = i(); if (Math.abs(a-b) > 1 || (a+b) > n-2) { System.out.println(-1); return; } int count = 0 ; if (a > b) { System.out.print(1 + " ") ; int high = n, low = 2 ; for (int i = 0 ; i < b ; i++) { System.out.print(high + " "); System.out.print(low + " "); high--; low++ ; } for (; high >= low; high--) System.out.print(high + " ") ; } else if (a < b) { System.out.print(n + " "); int high = n-1, low = 1 ; for (int i = 0 ; i < a ; i++) { System.out.print(low+" "+ high + " "); low++ ; high-- ; } for (; low <= high; low++) System.out.print(low+ " "); } else { System.out.print(1 + " "); int high = n, low = 2 ; for (int i = 0 ; i < b ; i++) { System.out.print(high + " "); System.out.print(low + " "); high--; low++ ; } for (; low <= high; low++) System.out.print(low+ " "); } System.out.println(); } public static void main(String[] args) { sb = new StringBuilder(); int test = i(); while (test-- > 0) { solve(); } System.out.println(sb); } /* * fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++) * { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; } */ // **************NCR%P****************** static long p(long x, long y)// POWER FXN // { if (y == 0) return 1; long res = 1; while (y > 0) { if (y % 2 == 1) { res = (res * x) % mod; y--; } x = (x * x) % mod; y = y / 2; } return res; } static long ncr(int n, int r) { if (r > n) return (long) 0; long res = fact[n] % mod; // System.out.println(res); res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod; res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod; // System.out.println(res); return res; } // **************END****************** // *************Disjoint set // union*********// // ***************PRIME FACTORIZE // ***********************************// static TreeMap<Integer, Integer> prime(long n) { TreeMap<Integer, Integer> h = new TreeMap<>(); long num = n; for (int i = 2; i <= Math.sqrt(num); i++) { if (n % i == 0) { int nt = 0; while (n % i == 0) { n = n / i; nt++; } h.put(i, nt); } } if (n != 1) h.put((int) n, 1); return h; } // *****CLASS PAIR // ************************************************* // *****CLASS PAIR // *************************************************** static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; 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 int Int() { 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 String String() { 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 String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static InputReader in = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static long[] sort(long[] a2) { int n = a2.length; ArrayList<Long> l = new ArrayList<>(); for (long i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static long pow(long x, long y) { long res = 1; while (y > 0) { if (y % 2 != 0) { res = (res * x);// % modulus; y--; } x = (x * x);// % modulus; y = y / 2; } return res; } // GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public static long gcd(long x, long y) { if (x == 0) return y; else return gcd(y % x, x); } // ******LOWEST COMMON MULTIPLE // ********************************************* public static long lcm(long x, long y) { return (x * (y / gcd(x, y))); } // INPUT // PATTERN******************************************************** public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } public static int[] readArray(int n) { int A[] = new int[n]; for (int i = 0; i < n; i++) { A[i] = i(); } return A; } public static long[] readArray(long n) { long A[] = new long[(int) n]; for (int i = 0; i < n; i++) { A[i] = l(); } return A; } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
ceb52716fd57c5d27b2ee1f452656b3c
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Arrays; import java.util.Random; import java.io.FileWriter; import java.io.PrintWriter; /* Solution Created: 14:27:05 11/12/2021 Custom Competitive programming helper. */ public class Main { public static void solve() { int n = in.nextInt(), a = in.nextInt(), b = in.nextInt(); if((a+b)>(n-2)) { out.println(-1); return; } int[] op = new int[n]; boolean up = a>b; for(int i = 1; i<n-1; i++) { if(up && a>0) { op[i] = 1; a--; up = !up; }else if(!up && b>0) { op[i] = -1; b--; up = !up; } } if(a!=0 || b!=0) { out.println(-1); return; } int l = 1, r = n; boolean downFall = op[1] != 1; if(op[1]==1) { out.print((l++)+" "); }else { out.print((r--)+" "); } for(int i = 1; i<n; i++) { if(op[i]==1) { out.print((r--)+" "); downFall = true; }else if(op[i]==-1) { out.print((l++)+" "); downFall = false; }else { if(downFall) out.print((r--)+" "); else out.print((l++)+" "); } } out.println(); } public static void main(String[] args) { in = new Reader(); out = new Writer(); int t = in.nextInt(); while(t-->0) solve(); out.exit(); } static Reader in; static Writer out; static class Reader { static BufferedReader br; static StringTokenizer st; public Reader() { this.br = new BufferedReader(new InputStreamReader(System.in)); } public Reader(String f){ try { this.br = new BufferedReader(new FileReader(f)); } catch (IOException e) { e.printStackTrace(); } } public int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public double[] nd(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); return a; } public long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[] nca() { return next().toCharArray(); } public String[] ns(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public int nextInt() { ensureNext(); return Integer.parseInt(st.nextToken()); } public double nextDouble() { ensureNext(); return Double.parseDouble(st.nextToken()); } public Long nextLong() { ensureNext(); return Long.parseLong(st.nextToken()); } public String next() { ensureNext(); return st.nextToken(); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); return null; } } private void ensureNext() { if (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } } } static class Util{ private static Random random = new Random(); static long[] fact; public static void initFactorial(int n, long mod) { fact = new long[n+1]; fact[0] = 1; for (int i = 1; i < n+1; i++) fact[i] = (fact[i - 1] * i) % mod; } public static long modInverse(long a, long MOD) { long[] gcdE = gcdExtended(a, MOD); if (gcdE[0] != 1) return -1; // Inverted doesn't exist long x = gcdE[1]; return (x % MOD + MOD) % MOD; } public static long[] gcdExtended(long p, long q) { if (q == 0) return new long[] { p, 1, 0 }; long[] vals = gcdExtended(q, p % q); long tmp = vals[2]; vals[2] = vals[1] - (p / q) * vals[2]; vals[1] = tmp; return vals; } public static long nCr(int n, int r, long MOD) { if (r == 0) return 1; return (fact[n] * modInverse(fact[r], MOD) % MOD * modInverse(fact[n - r], MOD) % MOD) % MOD; } public static long nCr(int n, int r) { return (fact[n]/fact[r])/fact[n-r]; } public static long nPr(int n, int r, long MOD) { if (r == 0) return 1; return (fact[n] * modInverse(fact[n - r], MOD) % MOD) % MOD; } public static long nPr(int n, int r) { return fact[n]/fact[n-r]; } public static boolean isPrime(long v) { if (v <= 1) return false; if (v <= 3) return true; if (v % 2 == 0 || v % 3 == 0) return false; for (long i = 5; i * i <= v; i = i + 6) if (v % i == 0 || v % (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 long pow(long x, long pow, long mod){ long res = 1; x = x % mod; if (x == 0) return 0; while (pow > 0){ if ((pow & 1) != 0) res = (res * x) % mod; pow >>= 1; x = (x * x) % mod; } return res; } public static int gcd(int a, int b) { int tmp = 0; while(b != 0) { tmp = b; b = a%b; a = tmp; } return a; } public static long gcd(long a, long b) { long tmp = 0; while(b != 0) { tmp = b; b = a%b; a = tmp; } return a; } public static int random(int min, int max) { return random.nextInt(max-min+1)+min; } public static void dbg(Object... o) { System.out.println(Arrays.deepToString(o)); } public static void reverse(int[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { int tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(int[] s) { reverse(s, 0, s.length-1); } public static void reverse(long[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { long tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(long[] s) { reverse(s, 0, s.length-1); } public static void reverse(float[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { float tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(float[] s) { reverse(s, 0, s.length-1); } public static void reverse(double[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { double tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(double[] s) { reverse(s, 0, s.length-1); } public static void reverse(char[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { char tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(char[] s) { reverse(s, 0, s.length-1); } public static <T> void reverse(T[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { T tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static <T> void reverse(T[] s) { reverse(s, 0, s.length-1); } public static void shuffle(int[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); int t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(long[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); long t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(float[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); float t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(double[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); double t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(char[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); char t = s[i]; s[i] = s[j]; s[j] = t; } } public static <T> void shuffle(T[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); T t = s[i]; s[i] = s[j]; s[j] = t; } } public static void sortArray(int[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(long[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(float[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(double[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(char[] a) { shuffle(a); Arrays.sort(a); } public static <T extends Comparable<T>> void sortArray(T[] a) { Arrays.sort(a); } public static int[][] rotate90(int[][] a){ int n = a.length, m = a[0].length; int[][] ans = new int[m][n]; for(int i = 0; i<n; i++) for(int j = 0; j<m; j++) ans[m-j-1][i] = a[i][j]; return ans; } public static char[][] rotate90(char[][] a){ int n = a.length, m = a[0].length; char[][] ans = new char[m][n]; for(int i = 0; i<n; i++) for(int j = 0; j<m; j++) ans[m-j-1][i] = a[i][j]; return ans; } } static class Writer { private PrintWriter pw; public Writer(){ pw = new PrintWriter(System.out); } public Writer(String f){ try { pw = new PrintWriter(new FileWriter(f)); } catch (IOException e) { e.printStackTrace(); } } public void yesNo(boolean condition) { println(condition?"YES":"NO"); } public void printArray(int[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); } public void printlnArray(int[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); pw.println(); } public void printArray(long[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); } public void printlnArray(long[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); pw.println(); } public void print(Object o) { pw.print(o.toString()); } public void println(Object o) { pw.println(o.toString()); } public void println() { pw.println(); } public void flush() { pw.flush(); } public void exit() { pw.close(); } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
38edc2a31184476bea86a8fd6c41c2d6
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.Scanner; import javax.swing.text.html.StyleSheet; public class B_Build_the_Permutation{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int testCases = scan.nextInt(); while(testCases-->0){ int n = scan.nextInt(); int maximums = scan.nextInt(); int minimus = scan.nextInt(); int needed = maximums+minimus+2; int difference = Math.abs(maximums-minimus); if(needed<=n && (difference==0||difference==1)){ int[] valsNeeded = new int[needed]; if(maximums>=minimus){ int dif = n-needed; for(int i = 0;i<needed;i++){ valsNeeded[i] =i+1+dif; } for(int i = 1;i<=dif;i++){ System.out.print(i+" "); } }else{ for(int i = n;i>needed;i--){ System.out.print(i+" "); } int counter = n-(n-needed); for(int i = 0;i<needed;i++){ valsNeeded[i] = counter; counter--; } } int startIndex = 0; int endIndex = needed-1; while(startIndex<endIndex){ System.out.print(valsNeeded[startIndex]+" "); System.out.print(valsNeeded[endIndex]+" "); startIndex++; endIndex--; } if(needed%2==1){ System.out.print(valsNeeded[endIndex]+" "); } System.out.println(); }else{ System.out.println(-1); } } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
87b807bd89b2286bbdf4da3a7c49160b
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int caseNo = scanner.nextInt(); int[][] input = new int[caseNo][3]; for (int i = 0; i < caseNo; i++) { for (int j = 0; j < 3; j++) input[i][j] = scanner.nextInt(); } for (int i = 0; i < caseNo; i++) { if (Math.abs(input[i][1]-input[i][2])>1 || input[i][1]+input[i][2]+2>input[i][0]) System.out.println(-1); else { int up = input[i][1]; int down = input[i][2]; boolean upside = up>down ? false : true; int min = 1; int max = input[i][0]; if (upside) { System.out.print(max+" "); max--; } else { System.out.print(min+" "); min++; } for (int j = 1; j < input[i][0]; j++) { if (up > 0 || down > 0) upside = upside ? false : true; if (upside) { System.out.print(max+" "); max--; up--; } else { System.out.print(min+" "); min++; down--; } } System.out.println(""); } } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
21d1c8309fa889f274d3ac0356e5732d
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.OutputStream; import java.io.BufferedOutputStream; import java.util.*; public class CodeForces_758 { public static void main(String[] args)throws Exception{ FastScanner in = new FastScanner();OutputStream out = new BufferedOutputStream ( System.out ); int t=in.nextInt(); while(t-->0){ int n=in.nextInt();int a=in.nextInt();int b=in.nextInt(); if((Math.abs(a-b)>1) ||((a+b+2)>n) ||(n==0)){ out.write(("-1"+"\n").getBytes()); } else{ int[] ans=new int[n+1];int x=1;int y=(n-(a+b+2)); if(a>b){ for(int i=(a+b+3);i<=n;i++){ ans[i]=y;y--; } y=(n-(a+b+2))+1; for(int i=1;i<=(a+b+2);i+=2){ if(i>n) break; ans[i]=y;y++; } for(int i=2;i<=(a+b+2);i+=2){ if(i>n) break; ans[i]=y;y++; } } else if(a<b){ for(int i=2;i<=(a+b+2);i+=2){ if(i>n) break; ans[i]=x;x++; } for(int i=1;i<=(a+b+2);i+=2){ if(i>n) break; ans[i]=x;x++; } for(int i=(a+b+3);i<=n;i++){ ans[i]=x;x++; } } else { for(int i=1;i<=(a+b+2);i+=2){ if(i>n) break; ans[i]=x;x++; } for(int i=2;i<=(a+b+2);i+=2){ if(i>n) break; ans[i]=x;x++; } for(int i=(a+b+3);i<=n;i++){ ans[i]=x;x++; } } for(int i=1;i<=n;i++){ out.write((ans[i]+" ").getBytes()); } out.write("\n".getBytes()); } } out.flush(); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
624e906e9c3c9494338bb41a5726530c
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.util.*; public class b { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int numcases = Integer.parseInt(in.readLine()); StringBuilder str = new StringBuilder(); for(int casenum = 0; casenum < numcases; casenum++){ StringTokenizer tokenizer = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); int a = Integer.parseInt(tokenizer.nextToken()); int b = Integer.parseInt(tokenizer.nextToken()); int max = Math.max(a, b); int min = Math.min(a, b); if(max > (n-1)/2 || min > (n-2)/2 || max - min > 1 || max - min < 0){ str.append(-1 + "\n"); continue; } if(n == 2){ str.append("1 2\n"); continue; } int a2 = a; int b2 = b; min = 1; max = n; int[] arr = new int[n]; boolean[] greater = new boolean[n]; if(a2 >= b2){ greater[0] = false; } else{ greater[0] = true; } for(int i = 1; i < greater.length; i++){ if(a2 > 0 && !greater[i-1]){ greater[i] = true; a2--; } else if(b2 > 0 && greater[i-1]){ greater[i] = false; b2--; } else{ greater[i] = greater[i-1]; } } //System.out.println(Arrays.toString(greater)); for(int i = 0; i < greater.length; i++){ if(greater[i]){ arr[i] = max--; } else{ arr[i] = min++; } } for(int i = 0; i < arr.length; i++){ str.append(arr[i] + (i < n - 1 ? " " : "\n")); } } System.out.print(str); in.close(); out.close(); } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
1fafa9e5a471b60d49252411f13711c9
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; // @author : Dinosparton public class test { static class Pair{ long x; long y; Pair(long x,long y){ this.x = x; this.y = y; } } static class Duo{ int x; String s; Duo(int x,String s){ this.x = x; this.s = s; } } static class Sort implements Comparator<Pair> { @Override public int compare(Pair a, Pair b) { if(a.x!=b.x) { return (int)(a.x - b.x); } else { return (int)(a.y-b.y); } } } static class Compare { void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.x!=p2.x) { return (int)(p1.x - p2.x); } else { return (int)(p1.y - p2.y); } } }); // for (int i = 0; i < n; i++) { // System.out.print(arr[i].x + " " + arr[i].y + " "); // } // System.out.println(); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void recursion(boolean visited[],ArrayList<Integer> list) { if(list.size() == visited.length) { int peak = 0; int valley = 0; for(int i=1;i<visited.length-1;i++) { if(list.get(i)>list.get(i-1) && list.get(i)>list.get(i+1)) { peak++; } if(list.get(i)<list.get(i-1) && list.get(i)<list.get(i+1)) { valley++; } System.out.print(list.get(i)+" "); } System.out.print(list.get(visited.length-1)+" | "); System.out.println(peak+" "+valley); } for(int i=0;i<visited.length;i++) { if(!visited[i]) { visited[i] = true; list.add(i); recursion(visited,list); list.remove(list.size()-1); visited[i] = false; } } } public static void main(String args[]) throws Exception { Scanner sc = new Scanner(); StringBuilder res = new StringBuilder(); int tc = sc.nextInt(); while(tc-->0) { int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int arr[] = new int[n]; for (int i = 1; i <= n; i++) { arr[i-1] = i; } int x = a + b; if (x > n - 2 || Math.abs(a - b) > 1) { res.append(-1+"\n"); continue; } boolean flag = false; if (a > b) { int temp = arr[n-1]; arr[n-1] = arr[n-2]; arr[n-2] = temp; a--; } if (a < b) { int temp = arr[0]; arr[0] = arr[1]; arr[1] = temp; b--; flag = true; } int index = 1; if(flag) { index++; } for (int i = 0; i < a; i++) { int temp = arr[index]; arr[index] = arr[index+1]; arr[index+1] = temp; index += 2; } for (int i : arr) { res.append(i+" "); } res.append("\n"); } System.out.println(res); } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
e7e1e4b1037fb2178be640976d4a668e
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; import java.sql.Array; import java.sql.ResultSet; import java.sql.SQLException; public class Solution { private static class MyScanner { private static final int BUF_SIZE = 2048; BufferedReader br; private MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } private boolean isSpace(char c) { return c == '\n' || c == '\r' || c == ' '; } String next() { try { StringBuilder sb = new StringBuilder(); int r; while ((r = br.read()) != -1 && isSpace((char)r)); if (r == -1) { return null; } sb.append((char) r); while ((r = br.read()) != -1 && !isSpace((char)r)) { sb.append((char)r); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } static class Reader{ BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long mod = (long)(1e9 + 7); static void sort(long[] arr ) { ArrayList<Long> al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr ) { ArrayList<Integer> al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i); } static long mod_mul( long... a) { long ans = a[0]%mod; for(int i = 1 ; i<a.length ; i++) { ans = (ans * (a[i]%mod))%mod; } return ans; } static long mod_sum( long... a) { long ans = 0; for(long e:a) { ans = (ans + e)%mod; } return ans; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static long modInverse(long a, long m) { long g = gcd(a, m); return power(a, m - 2, m); } static long lcm(long a , long b) { return (a*b)/gcd(a, b); } static int lcm(int a , int b) { return (int)((a*b)/gcd(a, b)); } static long power(long x, long y, long m){ if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (int)((p * (long)p) % m); if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); } static class Combinations{ private long[] z; private long[] z1; private long[] z2; private long mod; public Combinations(long N , long mod) { this.mod = mod; z = new long[(int)N+1]; z1 = new long[(int)N+1]; z[0] = 1; for(int i =1 ; i<=N ; i++) z[i] = (z[i-1]*i)%mod; z2 = new long[(int)N+1]; z2[0] = z2[1] = 1; for (int i = 2; i <= N; i++) z2[i] = z2[(int)(mod % i)] * (mod - mod / i) % mod; z1[0] = z1[1] = 1; for (int i = 2; i <= N; i++) z1[i] = (z2[i] * z1[i - 1]) % mod; } long fac(long n) { return z[(int)n]; } long invrsNum(long n) { return z2[(int)n]; } long invrsFac(long n) { return invrsFac((int)n); } long ncr(long N, long R) { if(R<0 || R>N ) return 0; long ans = ((z[(int)N] * z1[(int)R]) % mod * z1[(int)(N - R)]) % mod; return ans; } } static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } void makeSet() { for (int i = 0; i < n; i++) { parent[i] = i; } } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } } } static int max(int... a ) { int max = a[0]; for(int e:a) max = Math.max(max, e); return max; } static long max(long... a ) { long max = a[0]; for(long e:a) max = Math.max(max, e); return max; } static int min(int... a ) { int min = a[0]; for(int e:a) min = Math.min(e, min); return min; } static long min(long... a ) { long min = a[0]; for(long e:a) min = Math.min(e, min); return min; } static int[] KMP(String str) { int n = str.length(); int[] kmp = new int[n]; for(int i = 1 ; i<n ; i++) { int j = kmp[i-1]; while(j>0 && str.charAt(i) != str.charAt(j)) { j = kmp[j-1]; } if(str.charAt(i) == str.charAt(j)) j++; kmp[i] = j; } return kmp; } /************************************************ Query **************************************************************************************/ /***************************************** Sparse Table ********************************************************/ static class SparseTable{ private long[][] st; SparseTable(long[] arr){ int n = arr.length; st = new long[n][25]; log = new int[n+2]; build_log(n+1); build(arr); } private void build(long[] arr) { int n = arr.length; for(int i = n-1 ; i>=0 ; i--) { for(int j = 0 ; j<25 ; j++) { int r = i + (1<<j)-1; if(r>=n) break; if(j == 0 ) st[i][j] = arr[i]; else st[i][j] = Math.min(st[i][j-1] , st[ i + ( 1 << (j-1) ) ][ j-1 ] ); } } } public long gcd(long a ,long b) { if(a == 0) return b; return gcd(b%a , a); } public long query(int l ,int r) { int w = r-l+1; int power = log[w]; return Math.min(st[l][power],st[r - (1<<power) + 1][power]); } private int[] log; void build_log(int n) { log[1] = 0; for(int i = 2 ; i<=n ; i++) { log[i] = 1 + log[i/2]; } } } /******************************************************** Segement Tree *****************************************************/ /** static class SegmentTree{ long[] tree; long[] arr; int n; SegmentTree(long[] arr){ this.n = arr.length; tree = new long[4*n+1]; this.arr = arr; buildTree(0, n-1, 1); } void buildTree(int s ,int e ,int index ) { if(s == e) { tree[index] = arr[s]; return; } int mid = (s+e)/2; buildTree( s, mid, 2*index); buildTree( mid+1, e, 2*index+1); tree[index] = Math.min(tree[2*index] , tree[2*index+1]); } long query(int si ,int ei) { return query(0 ,n-1 , si ,ei , 1 ); } private long query( int ss ,int se ,int qs , int qe,int index) { if(ss>=qs && se<=qe) return tree[index]; if(qe<ss || se<qs) return (long)(1e17); int mid = (ss + se)/2; long left = query( ss , mid , qs ,qe , 2*index); long right= query(mid + 1 , se , qs ,qe , 2*index+1); return Math.min(left, right); } public void update(int index , int val) { arr[index] = val; for(long e:arr) System.out.print(e+" "); update(index , 0 , n-1 , 1); } private void update(int id ,int si , int ei , int index) { if(id < si || id>ei) return; if(si == ei ) { tree[index] = arr[id]; return; } if(si > ei) return; int mid = (ei + si)/2; update( id, si, mid , 2*index); update( id , mid+1, ei , 2*index+1); tree[index] = Math.min(tree[2*index] ,tree[2*index+1]); } } */ /* ***************************************************************************************************************************************************/ // static MyScanner sc = new MyScanner(); // only in case of less memory static Reader sc = new Reader(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) throws IOException { int tc = 1; tc = sc.nextInt(); for(int i = 1 ; i<=tc ; i++) { // sb.append("Case #" + i + ": " ); // During KickStart && HackerCup TEST_CASE(); } System.out.println(sb); } static void TEST_CASE() { int n = sc.nextInt(); int a = sc.nextInt() , b = sc.nextInt() ; if( a + b > n - 2 || a > (n-1)/2 || b > (n-1)/2 || Math.abs(a-b)>1 ) { sb.append("-1\n"); return; } int arr[] = new int[n]; for(int i = 0 ;i<n;i++) { arr[i]=i+1; } int i = 1 ; if(a==b ) { while(a>0 && i < n + 1) { int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; a--; i+=2; } } else if(a>b) { i = n- 1; while(a>0 && i < n + 1) { int temp = arr[i]; arr[i] = arr[i-1]; arr[i-1] = temp; a--; i-=2; } } else if ( b > a) { i = 0 ; while(b>0 && i < n + 1) { int temp = arr[i+1]; arr[i+1] = arr[i]; arr[i] = temp; b--; i+=2; } } for(int e:arr) sb.append(e+" "); sb.append("\n"); } } /*******************************************************************************************************************************************************/ /** 1 */
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
5843d873aa5054e8908b00f9fc37e7b6
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { int T = in.nextInt(); while (T-- > 0) { solveOne(in, out); } } private void solveOne(Scanner in, PrintWriter out) { int N = in.nextInt(); int A = in.nextInt(); int B = in.nextInt(); if (Math.abs(A - B) >= 2) { out.println(-1); return; } if (A == 0 & B == 0) { L.printIntArray(L.identityPerm(N), out); return; } int ans[] = null; if (A > B) { // maxs > mins ans = generate(N, true); } else { ans = generate(N, false); } int cntMin = 0, cntMax = 0; int breakIdx = -1; for (int i = 1; i < N - 1; i++) { if (ans[i - 1] < ans[i] && ans[i] > ans[i + 1]) cntMax++; if (cntMax == A && cntMin == B) { breakIdx = i; break; } if (ans[i - 1] > ans[i] && ans[i] < ans[i + 1]) cntMin++; if (cntMax == A && cntMin == B) { breakIdx = i; break; } } if (cntMax != A || cntMin != B) { out.println(-1); return; } if (breakIdx == -1) { out.println(-1); return; } boolean isMin = false; if (ans[breakIdx] < ans[breakIdx + 1] && ans[breakIdx] < ans[breakIdx - 1]) { isMin = true; } int start = 1; for (int i = 0; i <= breakIdx; i++) { if (ans[i] == start) start++; } if (!isMin) { for (int i = N - 1, cnt = start; i > breakIdx; i--, cnt++) { ans[i] = cnt; } } else { for (int i = breakIdx + 1, cnt = start; i < N; i++, cnt++) { ans[i] = cnt; } } L.printIntArray(ans, out); out.println(); } private int[] generate(int N, boolean flip) { int ans[] = new int[N]; int start = 1; int end = N; for (int i = 0; i < N; i++) { if (flip) ans[i] = start++; if (!flip) ans[i] = end--; flip = !flip; } return ans; } } static class L { public static void printIntArray(int[] array, PrintWriter out) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < array.length; i++) { stringBuilder.append(array[i] + " "); } out.print(stringBuilder.toString()); } public static int[] identityPerm(int N) { int[] p = new int[N]; for (int i = 1; i <= N; i++) { p[i - 1] = i; } return p; } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
bf521872368ebf1ae04f9ee7e7152a1f
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; import java.io.*; public class Main { static StringBuilder sb; static dsu dsu; static long fact[]; static int mod = (int) (1e9 + 7); static void solve() { int n=i(); int a=i(); int b=i(); // if(a==0&&b==0){ // for(int i=1;i<=n;i++)sb.append(i+" "); // return; // } if(Math.abs(a-b)>1){ sb.append(-1+"\n"); return; } int[]arr=new int[n]; if(a>b){ int lo=1; int hi=n; int c=0; for(int i=0;i<n;i++){ if(c>=a){ arr[i]=hi; hi--; continue; } if(i%2==0){ arr[i]=lo; lo++; } else{ arr[i]=hi; hi--; c++; } } } else if(a<b){ int lo=1; int hi=n; int c=0; for(int i=0;i<n;i++){ if(c>=b){ arr[i]=lo; lo++; continue; } if(i%2==0){ arr[i]=hi; hi--; } else{ arr[i]=lo; lo++; c++; } } } else{ int lo=1; int hi=n; int c=0; for(int i=0;i<n;i++){ if(c==a){ arr[i]=lo; lo++; c++; continue; } if(c>a){ arr[i]=lo; lo++; continue; } if(i%2==0){ arr[i]=lo; lo++; } else{ arr[i]=hi; hi--; c++; } } } int ma=0; int mb=0; for(int i=1;i<n-1;i++){ if(arr[i]<arr[i+1]&&arr[i]<arr[i-1]){ mb++; } else if(arr[i]>arr[i+1]&&arr[i]>arr[i-1]){ ma++; } } if(ma!=a||mb!=b){ sb.append(-1+"\n"); return; } for(int i=0;i<n;i++)sb.append(arr[i]+" "); sb.append("\n"); } public static void main(String[] args) { sb = new StringBuilder(); int test = i(); while (test-- > 0) { solve(); } System.out.println(sb); } /* * fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++) * { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; } */ //**************NCR%P****************** static long ncr(int n, int r) { if (r > n) return (long) 0; long res = fact[n] % mod; // System.out.println(res); res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod; res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod; // System.out.println(res); return res; } static long p(long x, long y)// POWER FXN // { if (y == 0) return 1; long res = 1; while (y > 0) { if (y % 2 == 1) { res = (res * x) % mod; y--; } x = (x * x) % mod; y = y / 2; } return res; } //**************END****************** // *************Disjoint set // union*********// static class dsu { int parent[]; dsu(int n) { parent = new int[n]; for (int i = 0; i < n; i++) parent[i] = -1; } int find(int a) { if (parent[a] < 0) return a; else { int x = find(parent[a]); parent[a] = x; return x; } } void merge(int a, int b) { a = find(a); b = find(b); if (a == b) return; parent[b] = a; } } //**************PRIME FACTORIZE **********************************// static TreeMap<Integer, Integer> prime(long n) { TreeMap<Integer, Integer> h = new TreeMap<>(); long num = n; for (int i = 2; i <= Math.sqrt(num); i++) { if (n % i == 0) { int nt = 0; while (n % i == 0) { n = n / i; nt++; } h.put(i, nt); } } if (n != 1) h.put((int) n, 1); return h; } //****CLASS PAIR ************************************************ static class Pair implements Comparable<Pair> { int x; long y; Pair(int x, long y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return (int) (this.y - o.y); } } //****CLASS PAIR ************************************************** static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; 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 int Int() { 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 String String() { 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 String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static InputReader in = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static long[] sort(long[] a2) { int n = a2.length; ArrayList<Long> l = new ArrayList<>(); for (long i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static char[] sort(char[] a2) { int n = a2.length; ArrayList<Character> l = new ArrayList<>(); for (char i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static long pow(long x, long y) { long res = 1; while (y > 0) { if (y % 2 != 0) { res = (res * x);// % modulus; y--; } x = (x * x);// % modulus; y = y / 2; } return res; } //GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public static long gcd(long x, long y) { if (x == 0) return y; else return gcd(y % x, x); } // ******LOWEST COMMON MULTIPLE // ********************************************* public static long lcm(long x, long y) { return (x * (y / gcd(x, y))); } //INPUT PATTERN******************************************************** public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } public static int[] readArrayi(int n) { int A[] = new int[n]; for (int i = 0; i < n; i++) { A[i] = i(); } return A; } public static long[] readArray(long n) { long A[] = new long[(int) n]; for (int i = 0; i < n; i++) { A[i] = l(); } return A; } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
5f30377c6e38fc0f56710126743573c2
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.util.*; public class q2 { public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // public static long mod = 1000000007; public static void solve() throws Exception { String[] parts = br.readLine().split(" "); int n = Integer.parseInt(parts[0]); int a = Integer.parseInt(parts[1]); int b = Integer.parseInt(parts[2]); int val = (n - 1) / 2; if (a > val || b > val || a + b > n - 2 || Math.abs(a - b) > 1) { System.out.println(-1); return; } if (a == 0 && b == 0) { StringBuilder ans = new StringBuilder(); for (int i = 1; i <= n; i++) ans.append(i).append(' '); System.out.println(ans); return; } if (n == 2) { System.out.println(-1); return; } StringBuilder ans = new StringBuilder(); if (a > b) { int sm = n - (2 * a + 1); ans.append(sm + 1).append(' ').append(sm + 3).append(' ').append(sm + 2).append(' '); int tn = sm + 4; for (int i = 1; i < a; i++) { ans.append(tn + 1).append(' ').append(tn).append(' '); tn += 2; } while (sm != 0) ans.append(sm--).append(' '); } else if (a == b) { ans.append(n).append(' ').append(n - 2).append(' ').append(n - 1).append(' '); n -= 3; for (int i = 1; i < b; i++) { ans.append(n - 1).append(' ').append(n).append(' '); n -= 2; } while (n > 0) ans.append(n--).append(' '); } else { int on = n + 1; n = 2 * b + 1; int tn = n + 1; ans.append(n).append(' ').append(n - 2).append(' ').append(n - 1).append(' '); n -= 3; for (int i = 1; i < b; i++) { ans.append(n - 1).append(' ').append(n).append(' '); n -= 2; } while (tn != on) ans.append(tn++).append(' '); } System.out.println(ans); } public static void main(String[] args) throws Exception { int tests = Integer.parseInt(br.readLine()); for (int test = 1; test <= tests; test++) { solve(); } } // public static void sort(int[] arr){ // ArrayList<Integer> temp = new ArrayList<>(); // for(int val : arr) temp.add(val); // // Collections.sort(temp); // // for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i); // } // public static void sort(long[] arr){ // ArrayList<Long> temp = new ArrayList<>(); // for(long val : arr) temp.add(val); // // Collections.sort(temp); // // for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i); // } // // public static long power(long a,long b,long mod){ // if(b == 0) return 1; // // long p = power(a,b / 2,mod); // p = (p * p) % mod; // // if(b % 2 == 1) return (p * a) % mod; // return p; // } // // public static int GCD(int a,int b){ // return b == 0 ? a : GCD(b,a % b); // } // public static long GCD(long a,long b){ // return b == 0 ? a : GCD(b,a % b); // } // // public static int LCM(int a,int b){ // return a * b / GCD(a,b); // } // public static long LCM(long a,long b){ // return a * b / GCD(a,b); // } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
a086a96ddc93f7dc08e34289060140c9
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.time.Year; import java.util.Scanner; import java.util.*; public class test2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 测试次数 int testnum = in.nextInt(); for (int i = 0; i< testnum; i++) { int n = in.nextInt(); int b = in.nextInt(); int a = in.nextInt(); int na = a; int nb = b; int[] ans = new int[n]; if(a>(n-1)/2||b>(n-1)/2||a+b>n-2||Math.abs(a-b)>1){ System.out.println(-1); continue; } int min = 1; int max = n; if(a>b){ int start = 1; while ((a-->0)){ ans[start] = min++; ans[start - 1] = max--; start += 2; } if(na==nb){ for (int j = start-1; j <n ; j++) { ans[j] = max--; } }else{ for (int j = n-1; j >=start-1 ; j--) { ans[j] = max--; } } }else{ int start = 1; while ((b-->0)){ ans[start] = max--; ans[start - 1] = min++; start += 2; } if(na==nb){ for (int j = start-1; j <n ; j++) { ans[j] = min++; } }else{ for (int j = n-1; j >=start-1 ; j--) { ans[j] = min++; } } } for (int j = 0; j < n; j++) { System.out.print(ans[j]+" "); } System.out.println(); } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
9fd7cfa9f188963dc6d998faff2968f7
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
/** * Created by Himanshu **/ import java.util.*; import java.io.*; import java.math.*; public class B1608 { public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); Reader s = new Reader(); int t = s.i(); while (t-- > 0) { int n = s.i() , a = s.i() , b = s.i(); if ((a+b) > (n-2) || Math.abs(a-b) > 1) { out.println(-1); continue; } int start = 1 , end = n; int [] arr = new int[n]; int i = 0; if (b > a) { while (b > 0) { arr[i++] = end; arr[i++] = start; start++; end--; b--; } for (int j=start;j<=end;j++) { arr[i++] = j; } } else { int x = b+1-a; while (a > 0) { arr[i++] = start; arr[i++] = end; start++; end--; a--; } if (x > 0) { for (int j=start;j<=end;j++) { arr[i++] = j; } } else { for (int j=end;j>=start;j--) { arr[i++] = j; } } } for (int x : arr) out.print(x + " "); out.println(); } out.flush(); } public static void shuffle(long[] arr) { int n = arr.length; Random rand = new Random(); for (int i = 0; i < n; i++) { long temp = arr[i]; int randomPos = i + rand.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = temp; } } private static long phi(long n) { long result = n; for (long i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) n /= i; result -= result / i; } } if (n > 1) result -= result / n; return result; } private static int gcd(int a, int b) { if(b == 0) return a; return gcd(b,a%b); } public static long nCr(long[] fact, long[] inv, int n, int r, long mod) { if (n < r) return 0; return ((fact[n] * inv[n - r]) % mod * inv[r]) % mod; } private static void factorials(long[] fact, long[] inv, long mod, int n) { fact[0] = 1; inv[0] = 1; for (int i = 1; i <= n; ++i) { fact[i] = (fact[i - 1] * i) % mod; inv[i] = power(fact[i], mod - 2, mod); } } private static long power(long a, long n, long p) { long result = 1; while (n > 0) { if (n % 2 == 0) { a = (a * a) % p; n /= 2; } else { result = (result * a) % p; n--; } } return result; } private static long power(long a, long n) { long result = 1; while (n > 0) { if (n % 2 == 0) { a = (a * a); n /= 2; } else { result = (result * a); n--; } } return result; } private static long query(long[] tree, int in, int start, int end, int l, int r) { if (start >= l && r >= end) return tree[in]; if (end < l || start > r) return 0; int mid = (start + end) / 2; long x = query(tree, 2 * in, start, mid, l, r); long y = query(tree, 2 * in + 1, mid + 1, end, l, r); return x + y; } private static void update(int[] arr, long[] tree, int in, int start, int end, int idx, int val) { if (start == end) { tree[in] = val; arr[idx] = val; return; } int mid = (start + end) / 2; if (idx > mid) update(arr, tree, 2 * in + 1, mid + 1, end, idx, val); else update(arr, tree, 2 * in, start, mid, idx, val); tree[in] = tree[2 * in] + tree[2 * in + 1]; } private static void build(int[] arr, long[] tree, int in, int start, int end) { if (start == end) { tree[in] = arr[start]; return; } int mid = (start + end) / 2; build(arr, tree, 2 * in, start, mid); build(arr, tree, 2 * in + 1, mid + 1, end); tree[in] = (tree[2 * in + 1] + tree[2 * in]); } static class Reader { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar, numChars; public Reader() { this(System.in); } public Reader(InputStream is) { mIs = is; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = mIs.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String s() { 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 long l() { 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 int i() { 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 double d() throws IOException { return Double.parseDouble(s()); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public int[] arr(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = i(); } return ret; } public long[] arrLong(int n) { long[] ret = new long[n]; for (int i = 0; i < n; i++) { ret[i] = l(); } return ret; } } // static class pairLong implements Comparator<pairLong> { // long first, second; // // pairLong() { // } // // pairLong(long first, long second) { // this.first = first; // this.second = second; // } // // @Override // public int compare(pairLong p1, pairLong p2) { // if (p1.first == p2.first) { // if(p1.second > p2.second) return 1; // else return -1; // } // if(p1.first > p2.first) return 1; // else return -1; // } // } // static class pair implements Comparator<pair> { // int first, second; // // pair() { // } // // pair(int first, int second) { // this.first = first; // this.second = second; // } // // @Override // public int compare(pair p1, pair p2) { // if (p1.first == p2.first) return p1.second - p2.second; // return p1.first - p2.first; // } // } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
87955ed30b03c8d6a77bb3fd720943c9
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
//package currentContest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.Random; import java.util.StringTokenizer; public class P1 { public static void main(String[] args) { // TODO Auto-generated method stub FastReader sc=new FastReader(); int t=sc.nextInt(); StringBuilder st=new StringBuilder(); while(t--!=0){ int n=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); ArrayDeque<Integer> keep=new ArrayDeque<Integer>(); ArrayDeque<Integer> ans=new ArrayDeque<Integer>(); for(int i=1;i<=n;i++) { keep.add(i); } int maxpossible=(n-2)/2+(n-2)%2; if(a==0&&b==0) { for(int x:keep) { st.append(x+" "); } st.append("\n"); }else if(a>maxpossible||b>maxpossible||(a==maxpossible&&b==maxpossible&&(n-2)%2==1)) { st.append("-1\n"); }else if(Math.max(a,b)-Math.min(a,b)>1) { st.append("-1\n"); }else { //ans abb ayega hi har case mein if(a>b) { int flag=0; ans.add(keep.removeFirst()); while(a>0&&b>0) { if(flag==0) { a--; ans.add(keep.removeLast()); flag=1; }else { b--; ans.add(keep.removeFirst()); flag=0; } } while(a>0) { a--; ans.add(keep.removeLast()); flag=1; } while(keep.size()>0) { ans.add(keep.removeLast()); } }else { int flag=1; ans.add(keep.removeLast()); while(a>0&&b>0) { if(flag==0) { a--; ans.add(keep.removeLast()); flag=1; }else { b--; ans.add(keep.removeFirst()); flag=0; } } while(b>0) { b--; ans.add(keep.removeFirst()); flag=1; while(keep.size()>0) { ans.add(keep.removeFirst()); } } while(a>0) { a--; ans.add(keep.removeLast()); flag=1; while(keep.size()>0) { ans.add(keep.removeLast()); } } } for(int x:ans) { st.append(x+" "); } st.append("\n"); } } System.out.println(st); } static class DisjointUnionSets { int[] rank, parent; int n; // Constructor public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } // Creates n sets with single item in each void makeSet() { for (int i = 0; i < n; i++) { // Initially, all elements are in // their own set. parent[i] = i; } } // Returns representative of x's set int find(int x) { // Finds the representative of the set // that x is an element of if (parent[x] != x) { // if x is not the parent of itself // Then x is not the representative of // his set, parent[x] = find(parent[x]); // so we recursively call Find on its parent // and move i's node directly under the // representative of this set } return parent[x]; } // Unites the set that includes x and the set // that includes x void union(int x, int y) { // Find representatives of two sets int xRoot = find(x), yRoot = find(y); // Elements are in the same set, no need // to unite anything. if (xRoot == yRoot) return; // If x's rank is less than y's rank if (rank[xRoot] < rank[yRoot]) // Then move x under y so that depth // of tree remains less parent[xRoot] = yRoot; // Else if y's rank is less than x's rank else if (rank[yRoot] < rank[xRoot]) // Then move y under x so that depth of // tree remains less parent[yRoot] = xRoot; else // if ranks are the same { // Then move y under x (doesn't matter // which one goes where) parent[yRoot] = xRoot; // And increment the result tree's // rank by 1 rank[xRoot] = rank[xRoot] + 1; } } } static FastReader sc = new FastReader(); public static void solvegraph() { int n = sc.nextInt(); int edge[][] = new int[n - 1][2]; for (int i = 0; i < n - 1; i++) { edge[i][0] = sc.nextInt() - 1; edge[i][1] = sc.nextInt() - 1; } ArrayList<ArrayList<Integer>> ad = new ArrayList<>(); for (int i = 0; i < n; i++) { ad.add(new ArrayList<Integer>()); } for (int i = 0; i < n - 1; i++) { ad.get(edge[i][0]).add(edge[i][1]); ad.get(edge[i][1]).add(edge[i][0]); } int parent[] = new int[n]; Arrays.fill(parent, -1); parent[0] = n; ArrayDeque<Integer> queue = new ArrayDeque<>(); queue.add(0); int child[] = new int[n]; Arrays.fill(child, 0); ArrayList<Integer> lv = new ArrayList<Integer>(); while (!queue.isEmpty()) { int toget = queue.getFirst(); queue.removeFirst(); child[toget] = ad.get(toget).size() - 1; for (int i = 0; i < ad.get(toget).size(); i++) { if (parent[ad.get(toget).get(i)] == -1) { parent[ad.get(toget).get(i)] = toget; queue.addLast(ad.get(toget).get(i)); } } lv.add(toget); } child[0]++; } static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[] = new Character[s.length()]; for (int i = 0; i < s.length(); i++) { ch[i] = s.charAt(i); } Arrays.sort(ch); StringBuffer st = new StringBuffer(""); for (int i = 0; i < s.length(); i++) { st.append(ch[i]); } return st.toString(); } public static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } 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
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
b9ab39a3c828178063ca6baa98a0b134
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; import java.io.*; public class Main { static long mod = 1000000007; static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); int t =sc.nextInt(); while( t--> 0) { int n=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); if(a+b+2>n || (Math.abs(a-b)>1)){ out.println("-1"); continue; } if(a==b){ int m=a+b+2; for(int i=n;i>m;i--){ out.print(i+" "); } for(int i=2;i<=m;i++){ out.print(i+" "+(i-1)+" "); i++; } }else if(a>b){ int m=n-(a+b+1); for(int i=1;i<=m;i++){ out.print(i+" "); } for(int i=m+2;i<=n;i++){ out.print(i+" "+(i-1)+" "); i++; } }else if(b>a){ int m=a+b+1; for(int i=2;i<=m;i++){ out.print(i+" "+(i-1)+" "); i++; } for(int i=m+1;i<=n;i++){ out.print(i+" "); } } out.println(); } out.flush(); } public static boolean check(int[]arr,int skip){ int i=0; int j=arr.length-1; while(i<=j){ if(arr[i]==skip){ i++; continue; }else if(arr[j]==skip){ j--; continue; } if(arr[i]==arr[j]){ i++; j--; }else{ return false; } } return true; } public static void print(int[]arr){ for(int i=0;i<arr.length;i++){ out.print(arr[i]+" "); } out.println(); } public static void swap(int i,int j,int[]arr){ int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } // public static void changes(char[][]grid,char[][]ans,int i,int j){ // int k=i+1; // while(k<grid.length && grid[k][j]=='.'){ // k++; // } // swap(i,j,k-1,grid); // } // public static void swap(int r,int c,int nr,char[][]arr){ // char temp=arr[r][c]; // arr[r][c]=arr[nr][c]; // arr[nr][c]=temp; // } /* * WARNING -> DONT EVER USE ARRAYS.SORT() IN ANY WAY. * A B C are easy just dont give up , you can do it! * FIRST AND VERY IMP -> READ AND UNDERSTAND THE QUESTION VERY CAREFULLY. * WARNING -> DONT CODE BULLSHIT , ALWAYS CHECK THE LOGIC ON MULTIPLE TESTCASES AND EDGECASES BEFORE. * SECOND -> TRY TO FIND RELEVENT PATTERN SMARTLY. * WARNING -> IF YOU THINK YOUR SOLUION IS JUST DUMB DONT SUBMIT IT BEFORE RECHECKING ON YOUR END. */ public static boolean ifpowof2(long n ) { return ((n&(n-1)) == 0); } static boolean isprime(long x ) { if( x== 2) { return true; } if( x%2 == 0) { return false; } for( long i = 3 ;i*i <= x ;i+=2) { if( x%i == 0) { return false; } } return true; } static boolean[] sieveOfEratosthenes(long n) { boolean prime[] = new boolean[(int)n + 1]; for (int i = 0; i <= n; i++) { prime[i] = true; } for (long p = 2; p * p <= n; p++) { if (prime[(int)p] == true) { for (long i = p * p; i <= n; i += p) prime[(int)i] = false; } } return prime; } public static int[] nextLargerElement(int[] arr, int n) { Stack<Integer> stack = new Stack<>(); int rtrn[] = new int[n]; rtrn[n-1] = -1; stack.push( n-1); for( int i = n-2 ;i >= 0 ; i--){ int temp = arr[i]; int lol = -1; while( !stack.isEmpty() && arr[stack.peek()] <= temp){ if(arr[stack.peek()] == temp ) { lol = stack.peek(); } stack.pop(); } if( stack.isEmpty()){ if( lol != -1) { rtrn[i] = lol; } else { rtrn[i] = -1; } } else{ rtrn[i] = stack.peek(); } stack.push( i); } return rtrn; } static void mysort(int[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); int loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static void mySort(long[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); long loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static long rightmostsetbit(long n) { return n&-n; } static long leftmostsetbit(long n) { long k = (long)(Math.log(n) / Math.log(2)); return k; } static HashMap<Long,Long> primefactor( long n){ HashMap<Long ,Long> hm = new HashMap<>(); long temp = 0; while( n%2 == 0) { temp++; n/=2; } if( temp!= 0) { hm.put( 2L, temp); } long c = (long)Math.sqrt(n); for( long i = 3 ; i <= c ; i+=2) { temp = 0; while( n% i == 0) { temp++; n/=i; } if( temp!= 0) { hm.put( i, temp); } } if( n!= 1) { hm.put( n , 1L); } return hm; } static ArrayList<Integer> allfactors(int abs) { HashMap<Integer,Integer> hm = new HashMap<>(); ArrayList<Integer> rtrn = new ArrayList<>(); for( int i = 2 ;i*i <= abs; i++) { if( abs% i == 0) { hm.put( i , 0); hm.put(abs/i, 0); } } for( int x : hm.keySet()) { rtrn.add(x); } if( abs != 0) { rtrn.add(abs); } return rtrn; } public static int[][] prefixsum( int n , int m , int arr[][] ){ int prefixsum[][] = new int[n+1][m+1]; for( int i = 1 ;i <= n ;i++) { for( int j = 1 ; j<= m ; j++) { int toadd = 0; if( arr[i-1][j-1] == 1) { toadd = 1; } prefixsum[i][j] = toadd + prefixsum[i][j-1] + prefixsum[i-1][j] - prefixsum[i-1][j-1]; } } return prefixsum; } 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
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
1f68ccce05730fefbdea11a3945e00ca
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
//package prog_temps; import java.util.*; import java.io.*; public class Java_Template { static boolean[] primecheck = new boolean[1000002]; static int M = 1000000007; static int mn = Integer.MIN_VALUE; static int mx = Integer.MAX_VALUE; static int vis[]; static ArrayList<ArrayList<Integer>> list; public static char rev(char c){ int diff = c-'A'; char ans = (char)('Z' - diff); return ans; } public static void swap(int a[], int i,int j){ int temp = a[i]; a[i] = a[j]; a[j] = temp; } public static int getFirstSetBitPos(int n) { return (int)((Math.log10(n & -n)) / Math.log10(2)); } public static void sortbyColumn(int arr[][], int col) { // Using built-in sort function Arrays.sort Arrays.sort(arr, new Comparator<int[]>() { @Override // Compare values according to columns public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; else return -1; } }); // End of function call sort(). } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter w = new PrintWriter(System.out); int t = 1; t = sc.nextInt(); while (t-- > 0) { solve(sc, w); } w.close(); } public static void solve(FastReader sc, PrintWriter w) throws Exception { int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int arr[] = new int[n]; if(a+b+2 >n || Math.abs(a-b) > 1){ System.out.println(-1); return; } // now, the seq exists; we need a+b+2 numbers int l = n-a-b-1; int h = n; int count =0; if(a>b) { for (int i = n - a - b - 2; i < n; i++) { if ((count % 2) == 0) { //use l arr[i] = l; l++; count++; continue; } arr[i] = h; h--; count++; } for (int i = 0; i < n - a - b - 2; i++) { arr[i] = i + 1; } // debug("arr", arr); } else if(b>a){ l = 1; h = a+b+2; for (int i = 0; i < a+b+2; i++) { if ((count % 2) == 0) { //use l arr[i] = h; h--; count++; continue; } arr[i] = l; l++; count++; } for (int i = a+b+2; i < n; i++) { arr[i] = i + 1; } // debug("arr", arr); } else { //a ==b l = 1; h = a+b+2; for (int i = 0; i < a+b+2; i++) { if ((count % 2) == 0) { //use l arr[i] = l; l++; count++; continue; } arr[i] = h; count++; h--; } for (int i = a+b+2; i < n; i++) { arr[i] = i + 1; } // debug("arr", arr); } for (int i = 0; i < n; i++) { System.out.print(arr[i]+ " "); } System.out.println(); } public static void debug(String X , String x){ System.out.println(X+ " : "+x); } public static void debug(String X , int x){ System.out.println(X+ " : "+x); } public static void debug(String X , long x){ System.out.println(X+ " : "+x); } public static void debug(String X , double x){ System.out.println(X+ " : "+x); } public static void debug(String X , boolean x){ System.out.println(X+ " : "+x); } public static void debug(String X , int[] x){ System.out.print(X+ " : "); for (int ele : x) { System.out.print(ele+" "); } System.out.println(); } public static void debug(String X , long[] x){ System.out.print(X+ " : "); for (long ele : x) { System.out.print(ele+" "); } System.out.println(); } public static void debug(String X , boolean[] x){ System.out.print(X+ " : "); for (boolean ele : x) { System.out.print(ele+" "); } System.out.println(); } public static void merge( int[] a, int[] l, int[] r, int left, int right) { int i = 0, j = 0, k = 0; while (i < left && j < right) { if (l[i] <= r[j]) { a[k++] = l[i++]; } else { a[k++] = r[j++]; } } while (i < left) { a[k++] = l[i++]; } while (j < right) { a[k++] = r[j++]; } } public static void mergeSort(int[] a, int n) { if (n < 2) { return; } int mid = n / 2; int[] l = new int[mid]; int[] r = new int[n - mid]; for (int i = 0; i < mid; i++) { l[i] = a[i]; } for (int i = mid; i < n; i++) { r[i - mid] = a[i]; } mergeSort(l, mid); mergeSort(r, n - mid); merge(a, l, r, mid, n - mid); } 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; } void readArr(int[] ar, int n) { for (int i = 0; i < n; i++) { ar[i] = nextInt(); } } } public static boolean perfectSqr(long a) { long sqrt = (long) Math.sqrt(a); if (sqrt * sqrt == a) { return true; } return false; } 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; } } } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long power(long x, long y) { long res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = res * x; // y must be even now y = y >> 1; // y = y/2 x = x * x; // Change x to x^2 } return res; } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
11a72aee2d1c0ed7d58d77c362a22e09
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.util.*; public class Q1608B { static int mod = (int) (1e9 + 7); static void solve() { int n = i(); int a = i(); int b = i(); if (a + b > n - 2) { System.out.println(-1); return; } if (Math.abs(a - b) > 1) { System.out.println(-1); return; } int[] arr = new int[n]; for (int i = 0; i < arr.length; i++) { arr[i] = i + 1; } if(a==b &&a==0){ for(int i=0;i<arr.length;i++){ System.out.print(arr[i]+" "); } System.out.println(); return; } if (a > b) { // last se int i = n - 1; int val = a; while (val-- > 0) { if (i - 1 >= 0) { swap(arr, i, i - 1); } else { System.out.println(-1); return; } i -= 2; } } else if (a < b) { // start se int i = 0; int val = b; while (val-- > 0) { if (i +1 <n) { swap(arr, i, i + 1); } else { System.out.println(-1); return; } i += 2; } } else { int i = 0; int val = a; while (val-- > 0) { if (i +1 <n) { swap(arr, i, i + 1); } else { System.out.println(-1); return; } i += 2; } swap(arr, n-1, n-2); } // System.out.println(Arrays.toString(arr)); for(int val:arr){ System.out.print(val+" "); } System.out.println(); } public static void swap(int[] arr, int v1, int v2) { int val1 = arr[v1]; int val2 = arr[v2]; arr[v1] = val2; arr[v2] = val1; } public static void main(String[] args) { int test = i(); while (test-- > 0) { solve(); } } // -----> POWER ---> long power(long x, long y) <---- power // -----> LCM ---> long lcm(long x, long y) <---- lcm // -----> GCD ---> long gcd(long x, long y) <---- gcd // -----> SIEVE --> ArrayList<Integer> sieve(int N) <-----sieve // -----> NCR ---> long ncr(int n, int r) <---- ncr // -----> BINARY_SEARCH ---> int binary_Search(long[]arr,long x) // <----binary_Search // -----> (SORTING OF LONG, CHAR,INT) -->long[] sortLong(long[] a2)<-- // -----> (INPUT OF INT,LONG ,STRING) -> int i() long l() String s()<- // tempstart static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; 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 int Int() { 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 String String() { 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 String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static InputReader in = new InputReader(System.in); public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
4a11e4d8a30ab435825da16dd61ae461
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class BuildThePermutation { private 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 scanner = new FastReader(); int tests = scanner.nextInt(); while (tests-- > 0) { int numbers = scanner.nextInt(); int highs = scanner.nextInt(); int lows = scanner.nextInt(); if(highs+lows+2 > numbers || Math.abs(highs-lows) > 1) { System.out.println(-1); continue; } int l = 1; int r = numbers; boolean goUp = true; if(highs < lows) { goUp = false; System.out.print(numbers + " "); r--; } else { System.out.print(1 + " "); l++; } for (int i = 1; i < numbers; i++) { if(highs <= 0 && lows <= 0) { if(!goUp) { System.out.print(r + " "); r--; } else { System.out.print(l + " "); l++; } continue; } if(goUp) { System.out.print(r + " "); highs--; r--; goUp = false; } else { System.out.print(l + " "); lows--; l++; goUp = true; } } System.out.println(); } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
b00311c3918e263232cbbc05e4842c81
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.InputMismatchException; /** * Provide prove of correctness before implementation. Implementation can cost a lot of time. * Anti test that prove that it's wrong. * <p> * Do not confuse i j k g indexes, upTo and length. Do extra methods!!! Write more informative names to simulation * <p> * Will program ever exceed limit? * Try all approaches with prove of correctness if task is not obvious. * If you are given formula/rule: Try to play with it. * Analytic solution/Hardcoded solution/Constructive/Greedy/DP/Math/Brute force/Symmetric data * Number theory * Game theory (optimal play) that consider local and global strategy. */ public class CF1608B { private void solveOne() { int n = nextInt(); int a = nextInt(); int b = nextInt(); if (a + b + 2 > n || Math.abs(a - b) > 1) { System.out.println(-1); } else { int[] ans = new int[n]; if (a == b) { int permLen = a + b + 2; int val = 1; for (int i = 0; i < permLen; i+=2) { ans[i] = val++; } for (int i = 1; i < permLen; i+=2) { ans[i] = val++; } for (int i = permLen; i < n; i++) { ans[i] = val++; } } else if(a < b) { int permLen = a + b + 1; int val = (permLen + 1) / 2 + (permLen + 1) % 2; for (int i = 0; i < permLen; i+=2) { ans[i] = val++; } int val0 = 1; for (int i = 1; i < permLen; i+=2) { ans[i] = val0++; } for (int i = permLen; i < n; i++) { ans[i] = val++; } } else if (a > b){ int permLen = a + b + 1; int val = n; for (int i = n - 2; a-- > 0 ; i -= 2 ) { ans[i] = val--; } ans[n - 1] = val--; for (int i = n - 3; b-- > 0 ; i -= 2 ) { ans[i] = val--; } for (int i = 0; i < n - permLen; i++) { ans[i] = i + 1; } } System.out.println(ans); } } private void solve() { int t = System.in.readInt(); for (int tt = 0; tt < t; tt++) { solveOne(); } } class AssertionRuntimeException extends RuntimeException { AssertionRuntimeException(Object expected, Object actual, Object... input) { super("expected = " + expected + ",\n actual = " + actual + ",\n " + Arrays.deepToString(input)); } } private int nextInt() { return System.in.readInt(); } private long nextLong() { return System.in.readLong(); } private String nextString() { return System.in.readString(); } private int[] nextIntArr(int n) { return System.in.readIntArray(n); } private long[] nextLongArr(int n) { return System.in.readLongArray(n); } public static void main(String[] args) { new CF1608B().run(); } static class System { private static FastInputStream in; private static FastPrintStream out; } private void run() { System.in = new FastInputStream(java.lang.System.in); System.out = new FastPrintStream(java.lang.System.out); solve(); System.out.flush(); } private static class FastPrintStream { private static final int BUF_SIZE = 8192; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastPrintStream() { this(java.lang.System.out); } public FastPrintStream(OutputStream os) { this.out = os; } public FastPrintStream(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastPrintStream print(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastPrintStream print(char c) { return print((byte) c); } public FastPrintStream print(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastPrintStream print(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } //can be optimized public FastPrintStream print0(char[] s) { if (ptr + s.length < BUF_SIZE) { for (char c : s) { buf[ptr++] = (byte) c; } } else { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } } return this; } //can be optimized public FastPrintStream print0(String s) { if (ptr + s.length() < BUF_SIZE) { for (int i = 0; i < s.length(); i++) { buf[ptr++] = (byte) s.charAt(i); } } else { for (int i = 0; i < s.length(); i++) { buf[ptr++] = (byte) s.charAt(i); if (ptr == BUF_SIZE) innerflush(); } } return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastPrintStream print(int x) { if (x == Integer.MIN_VALUE) { return print((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { print((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastPrintStream print(long x) { if (x == Long.MIN_VALUE) { return print("" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { print((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastPrintStream print(double x, int precision) { if (x < 0) { print('-'); x = -x; } x += Math.pow(10, -precision) / 2; // if(x < 0){ x = 0; } print((long) x).print("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; print((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastPrintStream println(char c) { return print(c).println(); } public FastPrintStream println(int x) { return print(x).println(); } public FastPrintStream println(long x) { return print(x).println(); } public FastPrintStream println(String x) { return print(x).println(); } public FastPrintStream println(int[] a, int from, int upTo, char separator) { for (int i = from; i < upTo; i++) { print(a[i]); print(separator); } print('\n'); return this; } public FastPrintStream println(int[] a) { return println(a, 0, a.length, ' '); } public FastPrintStream println(double x, int precision) { return print(x, precision).println(); } public FastPrintStream println() { return print((byte) '\n'); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } } private static class FastInputStream { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastInputStream(InputStream stream) { this.stream = stream; } public double[] readDoubleArray(int size) { double[] array = new double[size]; for (int i = 0; i < size; i++) { array[i] = readDouble(); } return array; } public String[] readStringArray(int size) { String[] array = new String[size]; for (int i = 0; i < size; i++) { array[i] = readString(); } return array; } public char[] readCharArray(int size) { char[] array = new char[size]; for (int i = 0; i < size; i++) { array[i] = readCharacter(); } return array; } public void readIntArrays(int[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) { arrays[j][i] = readInt(); } } } public void readLongArrays(long[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) { arrays[j][i] = readLong(); } } } public void readDoubleArrays(double[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) { arrays[j][i] = readDouble(); } } } public char[][] readTable(int rowCount, int columnCount) { char[][] table = new char[rowCount][]; for (int i = 0; i < rowCount; i++) { table[i] = this.readCharArray(columnCount); } return table; } public int[][] readIntTable(int rowCount, int columnCount) { int[][] table = new int[rowCount][]; for (int i = 0; i < rowCount; i++) { table[i] = readIntArray(columnCount); } return table; } public double[][] readDoubleTable(int rowCount, int columnCount) { double[][] table = new double[rowCount][]; for (int i = 0; i < rowCount; i++) { table[i] = this.readDoubleArray(columnCount); } return table; } public long[][] readLongTable(int rowCount, int columnCount) { long[][] table = new long[rowCount][]; for (int i = 0; i < rowCount; i++) { table[i] = readLongArray(columnCount); } return table; } public String[][] readStringTable(int rowCount, int columnCount) { String[][] table = new String[rowCount][]; for (int i = 0; i < rowCount; i++) { table[i] = this.readStringArray(columnCount); } return table; } public String readText() { StringBuilder result = new StringBuilder(); while (true) { int character = read(); if (character == '\r') { continue; } if (character == -1) { break; } result.append((char) character); } return result.toString(); } public void readStringArrays(String[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) { arrays[j][i] = readString(); } } } public long[] readLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) { array[i] = readLong(); } return array; } public int[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = readInt(); } return array; } 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 int peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) { return -1; } } return buf[curChar]; } public int peekNonWhitespace() { while (isWhitespace(peek())) { read(); } return peek(); } public int readInt() { 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 readLong() { 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 String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return readLine(); } else { return readLine0(); } } public char readCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public double readDouble() { 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, readInt()); } 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, readInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) { read(); } return value == -1; } public String next() { return readString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
4da06ba7376ef0533a9e324e432939fb
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
//package com.company; import java.io.*; import java.math.BigInteger; import java.util.*; public class Main{ static boolean[] primecheck = new boolean[1000002]; public static void main(String[] args) { 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 ArrayList<Integer> adj[]; static int[] vis; static long cnt = 0; static long mod = (long)1e9 + 7; static class PROBLEM { static void swap(int[] a, int l, int r){ int temp = a[l]; a[l] = a[r]; a[r] = temp; } public void solve(FastReader in, PrintWriter out) { int n = in.nextInt(), a = in.nextInt(), b = in.nextInt(); if(Math.abs(a-b) < 2 && Math.abs(a-b) > -1 && (a+b) <= n-2){ int[] c = new int[n]; for (int i = 0; i < n; i++) { c[i] = i+1; } if(a>=b) { int i = n - 1; while (a > 0) { swap(c, i, i - 1); a--; if (i != n - 1) b--; i -= 2; } if(b>0){ swap(c, 0, 1); b--; } }else { int i = 0; while (b > 0) { swap(c, i, i + 1); b--; if (i != 0) a--; i += 2; } } for (int j = 0; j < n; j++) { out.print(c[j]+" "); } out.println(); }else{ out.println(-1); } // int n = in.nextInt(), k = in.nextInt(); // // adj = new ArrayList[n]; // for (int i = 0; i < n; i++) { // adj[i] = new ArrayList<>(); // } // // for (int i = 0; i < n - 1; i++) { // int u = in.nextInt()-1; // int v = in.nextInt()-1; // int col = in.nextInt(); // // if(col == 0){ // adj[u].add(v); // adj[v].add(u); // } // } // // long good = fast_pow(n, k); // long bad = 0; // // vis = new int[n]; // for (int i = 0; i < n; i++) { // if(vis[i] == 0){ // cnt = 0; // dfs(i); // bad += fast_pow(cnt, k); // } // } // // bad %= mod; // good = (good-bad+mod)%mod; // // out.println(good); } } static void dfs(int i){ vis[i] = 1; cnt++; for(int j: adj[i]){ if(vis[j] == 0) dfs(j); } } static HashMap<Integer, Integer> initializeMap(int n){ HashMap<Integer,Integer> hm = new HashMap<>(); for (int i = 0; i <= n; i++) { hm.put(i, 0); } return hm; } static boolean isRegular(char[] c){ Stack<Character> s = new Stack<>(); for (char value : c) { if (s.isEmpty() && value == ')') return false; if (value == '(') s.push(value); else s.pop(); } return s.isEmpty(); } static ArrayList<ArrayList<Integer>> createAdj(int n, int e){ FastReader in = new FastReader(); ArrayList<ArrayList<Integer>> adj = new ArrayList<>(); for (int i = 0; i < n + 1; i++) { adj.add(new ArrayList<>()); } for (int i = 0; i < e; i++) { int a = in.nextInt(), b = in.nextInt(); System.out.println(a); adj.get(a).add(b); adj.get(b).add(a); } return adj; } static int binarySearch(int[] a, int l, int r, int x){ if(r>=l){ int mid = l + (r-l)/2; if(a[mid] == x) return mid; if(a[mid] > x) return binarySearch(a, l, mid-1, x); else return binarySearch(a,mid+1, r, x); } return -1; } static boolean isPalindromeI(int n){ int d = 0; int y = n; while(y>0){ d++; y/=10; } int[] a = new int[d]; for (int i = 0; i < d; i++) { a[i] = n%10; n/=10; } System.out.println(Arrays.toString(a)); for (int i = 0; i < d / 2; i++) { if(a[i] != a[d-i-1]) 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 / gcd(a, b)) * 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 long fast_pow(long a, long b) { //Jeel bhai OP if(b == 0) return 1L; long val = fast_pow(a, b / 2); if(b % 2 == 0) return val * val % mod; else return val * val % mod * a % mod; } 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); } static class Pair implements Comparable<Pair>{ char x; int y; Pair(char x, int y){ this.x = x; this.y = y; } public int compareTo(Pair o){ int ans = Integer.compare(x, o.x); if(o.x == x) ans = Integer.compare(y, o.y); return ans; } } static class Tuple implements Comparable<Tuple>{ int x, y, id; Tuple(int x, int y, int id){ this.x = x; this.y = y; this.id = id; } public int compareTo(Tuple o){ int ans = Integer.compare(x, o.x); if(o.x == x) ans = Integer.compare(y, o.y); return ans; } } // 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 int compareToY(Pair<U, V> b) { // int cmpU = y.compareTo(b.y); // return cmpU != 0 ? cmpU : x.compareTo(b.x); // } // // 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) { //array.length replaced with ctr int ctr = array.length; if (ctr <= 1) { return array; } int midpoint = ctr / 2; int[] left = new int[midpoint]; int[] right; if (ctr % 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]; } left = mergeSort(left); right = mergeSort(right); int[] 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
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
9bc06b89ded79759f863b09a9e810b15
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; import java.io.*; public class code6{ static final int M = 1000000007; // Fast input static class Hrittik_FastReader{ StringTokenizer st; BufferedReader br; public int n; public Hrittik_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(); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } double nextDouble(){ return Double.parseDouble(next()); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } } // Fast output static class Hrittik_FastWriter { private final BufferedWriter bw; public Hrittik_FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void print(Object object) throws IOException { bw.append("" + object); } public void close() throws IOException { bw.close(); } } /** * * @param a * @param b * @return gcd of two numbers using recursion */ private static int gcd(int a , int b){ if(b == 0) return a; return gcd(b, a%b); } public static void main(String[] args) { try { Hrittik_FastReader Sc =new Hrittik_FastReader(); Hrittik_FastWriter out = new Hrittik_FastWriter(); int t = Sc.nextInt(); while(t-- > 0){ // write your code here int n = Sc.nextInt(); int a = Sc.nextInt(); int b = Sc.nextInt(); int ans[] = new int[n]; if(Math.abs(a-b) <= 1 && (a+b) <= (n-2)){ if(a > b){ int x = n - a; for(int i = n; i > n - a; i--){ System.out.print(x + " " + i + " "); x--; } for(int i = x; i >= 1; i--){ System.out.print(i + " "); } System.out.println(); } else if(b > a){ int x = b + 1; for(int i = 1; i <= b; i++){ System.out.print(x + " " + i + " "); x++; } for(int i = x; i <= n; i++){ System.out.print(i + " "); } System.out.println(); } else{ int x = a + 2; System.out.print((a+1) + " "); for(int i = 1; i <= a; i++){ System.out.print(x + " " + i + " "); x++; } for(int i = x; i <= n; i++){ System.out.print(i + " "); } System.out.println(); } } else{ System.out.println(-1); } } out.close(); } catch (Exception e) { return; } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
b2dc4f241229f8d767b4d3e7fc19d558
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.io.DataInputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.LinkedList; import java.util.List; public class Main { private static void run() throws IOException { int n, a, b; n = in.nextInt(); a = in.nextInt(); b = in.nextInt(); if (Math.abs(a - b) > 1) { out.println(-1); return; } List<Integer> ans = new LinkedList<>(); if (a >= b) { boolean last = false; if (a == b) { n--; b--; last = true; } int end = n - (a + b); int top = n - a + 1; if (end <= 1) { out.println(-1); return; } for (int i = 1; i < end; i++) { ans.add(i); } for (int i = 0; i < a; i++) { ans.add(top++); ans.add(end++); } if (last) { ans.add(n + 1); } } else { int top = b + 1; for (int i = 0; i < b; i++) { ans.add(top++); ans.add(i + 1); } if (top > n) { out.println(-1); return; } while (top <= n) { ans.add(top++); } } for (int now : ans) { out.print(now); out.print(' '); } out.println(); } public static void main(String[] args) throws IOException { in = new Reader(); out = new PrintWriter(new OutputStreamWriter(System.out)); int t = in.nextInt(); for (int i = 0; i < t; i++) { run(); } out.flush(); in.close(); out.close(); } private static int gcd(int a, int b) { if (a == 0 || b == 0) return 0; while (b != 0) { int tmp; tmp = a % b; a = b; b = tmp; } return a; } static final long mod = 1000000007; static long pow_mod(long a, long b) { long result = 1; while (b != 0) { if ((b & 1) != 0) result = (result * a) % mod; a = (a * a) % mod; b >>= 1; } return result; } private static long add_mod(long... longs) { long ans = 0; for (long now : longs) { ans = (ans + now) % mod; } return ans; } private static long multiplied_mod(long... longs) { long ans = 1; for (long now : longs) { ans = (ans * now) % mod; } return ans; } @SuppressWarnings("FieldCanBeLocal") private static Reader in; private static PrintWriter out; private static int[] read_int_array(int len) throws IOException { int[] a = new int[len]; for (int i = 0; i < len; i++) { a[i] = in.nextInt(); } return a; } private static long[] read_long_array(int len) throws IOException { long[] a = new long[len]; for (int i = 0; i < len; i++) { a[i] = in.nextLong(); } return a; } private static void print_array(int[] array) { for (int now : array) { out.print(now); out.print(' '); } out.println(); } private static void print_array(long[] array) { for (long now : array) { out.print(now); out.print(' '); } out.println(); } static class Reader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { final byte[] buf = new byte[1024]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextSign() throws IOException { byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() throws IOException { int b; // noinspection ALL while ((b = read()) != -1 && isSpaceChar(b)) { ; } return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final 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(); } final 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(); } final 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 { din.close(); } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
7fdf9d56abd9da846786d8b99296b23d
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.Scanner; public class BuildThePermutation { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int i = 0; while (i < t) { int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); if (Math.abs(a - b) > 1) { System.out.println(-1); i++; continue; } if (a + b + 2 > n) { System.out.println(-1); i++; continue; } int[] p = new int[n]; if (a > b) { int cur = n; for (int j = 1,count = 0; count < a;j += 2, count++) { p[j] = cur; cur--; } int r = cur; cur = 1; for (int j = 2,count = 0; count < b; j += 2, count++) { p[j] = cur; cur++; } p[0] = r--; for (int j = a + b + 1; j < n; j++) p[j] = r--; } else if (a < b) { int cur = 1; for (int j = 1, count = 0; count < b; count++, j += 2) { p[j] = cur; cur++; } int l = cur; cur = n; for (int j = 2, count = 0; count < a; count++, j += 2) { p[j] = cur; cur--; } p[0] = l++; for (int j = a + b + 1; j < n; j++) { p[j] = l++; } } else { int cur = n; for (int j = 1, count = 0; count < a; count++, j += 2) { p[j] = cur; cur--; } cur = 1; for (int j = 2, count = 0; count < b; count++, j += 2) { p[j] = cur; cur++; } int left = cur; p[0] = left++; for (int j = a + b + 1; j < n; j++) { p[j] = left++; } } for (int j = 0; j < n; j++) { if (j == n - 1) { System.out.println(p[j]); break; } System.out.print(p[j] + " "); } i++; } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
f7f32a2a428843b0ad7ae50cf045f57b
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; public class Main{ static int [] path; static int tmp; static boolean find; 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 = sc.nextInt(); int b = sc.nextInt(); if((a+b)>(n-2) || Math.abs(a-b)>1) System.out.println(-1); else{ int [] arr = new int [n]; if(a>b){ int last = n; int first = 1; int tmp = a; for (int i = 0; i <n ; i++) { if(tmp!=0) { if (i % 2 != 0) { arr[i] = last--; tmp--; } else arr[i] = first++; }else{ arr[i] = last--; } } }else if(b>a){ int last = 1; int first = n; int tmp = b; for (int i = 0; i <n ; i++) { if(tmp!=0) { if (i % 2 != 0) { arr[i] = last++; tmp--; } else arr[i] = first--; }else{ arr[i] = last++; } } }else{ int last = n; int first = 1; int tmp = a; for (int i = 0; i <n ; i++) { if (i % 2 != 0 && tmp!=0) { arr[i] = last--; tmp--; } else arr[i] = first++; } } for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } System.out.println(); } } } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
a6bdd3b0e00c4f2abe6c088c717a4a57
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import com.sun.security.jgss.GSSUtil; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; public class javacp{ static FastReader fs=new FastReader(); 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[] inputIntArray(int n){ int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i] = fs.nextInt(); return arr; } } static void sort(int[] arr){ ArrayList<Integer> al = new ArrayList<>(); for(int val : arr){ al.add(val); } Collections.sort(al); for(int i=0;i<arr.length;i++){ arr[i] = al.get(i); } } static class Pair implements Comparable<Pair>{ int src; int dest; long wt; Pair(int src,int dest,long wt){ this.src = src; this.dest = dest; this.wt = wt; } public int compareTo(Pair pair){ if(this.wt > pair.wt) return 1; else if(this.wt < pair.wt) return -1; else return 0; } } static int[] par1 , rank1 , size; static int[] par2; static int[] rank2; static boolean union1(int vt1 , int vt2){ int parent1 = findParent1(vt1) , parent2 = findParent1(vt2); int r1 = rank1[parent1] , r2 = rank1[parent2]; if(parent1 == parent2) return false; if(r1 < r2) { par1[parent1] = parent2; size[parent2] += size[parent1]; } else{ par1[parent2] = parent1; size[parent1] += size[parent2]; } if(r1 == r2) rank1[parent1]++; return true; } static void union2(int vt1 , int vt2){ int parent1 = findParent2(vt1) , parent2 = findParent2(vt2); int r1 = rank2[parent1] , r2 = rank2[parent2]; if(parent1 == parent2) return; if(r1 < r2) par2[parent1] = parent2; else par2[parent2] = parent1; if(r1 == r2) rank2[parent1]++; } static int findParent1(int vt){ if(par1[vt] == vt) return vt; par1[vt] = findParent1(par1[vt]); return par1[vt]; } static int findParent2(int vt){ if(par2[vt] == vt) return vt; par2[vt] = findParent2(par2[vt]); return par2[vt]; } static void initialise1(int numOfVtces){ par1 = new int[numOfVtces]; rank1 = new int[numOfVtces]; size = new int[numOfVtces]; for(int i=0;i<numOfVtces;i++){ par1[i] = i; rank1[i] = 1; size[i] = 1; } } static void initialise2(int numOfVtces){ par2 = new int[numOfVtces]; rank2 = new int[numOfVtces]; for(int i=0;i<numOfVtces;i++){ par2[i] = i; rank2[i] = 0; } } public static void main(String[] args) throws IOException{ int t = 1; t = fs.nextInt(); while (t-- > 0) { int n = fs.nextInt() , max = fs.nextInt() , min = fs.nextInt(); if(Math.abs(max-min) > 1 || Math.abs(max+min) > n-2) { System.out.println(-1); continue; } int s = max + min + 2; if(max == min){ int lo = 1 , high = s , prev = 1; while (lo <= high){ if(prev == 1){ System.out.print(lo+" "); prev = 0; lo++; } else{ System.out.print(high +" "); prev = 1; high--; } } s++; while (s <= n){ System.out.print(s+" "); s++; } } else if(max > min){ int lo = n - s + 1 , hi = n , prev = 1; while (lo <= hi) { if (prev == 1) { System.out.print(lo + " "); lo++; prev = 0; } else { System.out.print(hi + " "); hi--; prev = 1; } } int temp = n - s; while (temp > 0) { System.out.print(temp + " "); temp--; } } else{ int lo = 1 , hi = s , prev = 0; while (lo <= hi) { if (prev == 1) { System.out.print(lo + " "); lo++; prev = 0; } else { System.out.print(hi + " "); hi--; prev = 1; } } s++; while (s <= n){ System.out.print(s+" "); s++; } } System.out.println(); } } private static String generateABPattern(int a , int b){ StringBuilder sb = new StringBuilder(); while (a > 0 || b > 0){ if(b-a >= 2){ sb.append("bb"); if(a != 0){ sb.append("a"); a--; } b -= 2; } else if(a-b >= 2){ sb.append("aa"); if(b != 0){ sb.append("b"); b--; } a -= 2; } else if(a >= b){ sb.append("a"); a--; } else{ sb.append("b"); b--; } } return sb.toString(); } private static int binarySearch(int[] arr , int i , int j , int val){ while (i <= j){ int mid = (i+j)/2; if(arr[mid] == val) return mid; else if(arr[mid] > val) j = mid-1; else i = mid+1; } return -1; } private static int lower_bound(int[] arr , int i, int j, int val){ int min = -1; while (i <= j){ int mid = (i+j)/2; if(arr[mid] > val) j = mid-1; else{ min = mid; i = mid+1; } } return min; } private static int upper_bound(int[] arr, int i , int j , int val){ int min = -1; while (i <= j){ int mid = (i+j)/2; if(arr[mid] < val) i = mid+1; else{ min = mid; j = mid-1; } } return min; } static int modInverse(int a, int m) { int m0 = m; int y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient int q = a / m; int t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } static int mod = 1000000007; static long power(long a,long b,long m) { a %= m; long res = 1; while (b > 0) { if ((b & 1) == 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } static class CustomSort implements Comparator<int[]> { public int compare(int[] a, int[] b) { return a[0] - b[0]; } } static void printArr(int[] arr){ for(int i=0;i< arr.length;i++) { System.out.print(arr[i] + " "); } System.out.println(); } private static int lcm(int a, int b) { int gcd = gcd(a,b); return (int)((long)a*(long)b)/gcd ; } private static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b , a % b); } private static void swap(int[] arr, int start, int rand_pivot) { int temp = arr[start]; arr[start] = arr[rand_pivot]; arr[rand_pivot] = temp; } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
f505576077cb60e6030afde0c3689e7c
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
import java.util.*; import java.io.*; public class ja { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int start = 1; boolean can = true; int end = n; if (a + 2 > n || b + 2 > n) pw.println(-1); else if (a == b) { int peaks = 0; int crests = 0; int arr[] = new int[n]; int count = 0; int i = 1; while (count < a && i < n) { arr[i] = end--; i += 2; count++; } for (i = 0; i < n; i++) { if (arr[i] == 0) { if (start > end) can = false; arr[i] = start++; } } for (i = 1; i < n - 1; i++) { if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) peaks++; else if (arr[i] < arr[i + 1] && arr[i] < arr[i - 1]) crests++; } // pw.println(Arrays.toString(arr)); if (peaks != a || crests != b || !can) pw.println(-1); else { for (int x : arr) pw.print(x + " "); pw.println(); } } else if (a > b) { int peaks = 0; int crests = 0; int arr[] = new int[n]; int count = 0; int i = 1; while (count < a && i < n) { arr[i] = end--; i += 2; count++; } for (i = 0; i < n; i++) { if (arr[i] == 0) { if (end < 0) can = false; arr[i] = end--; } } for (i = 1; i < n - 1; i++) { if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) peaks++; else if (arr[i] < arr[i + 1] && arr[i] < arr[i - 1]) crests++; } // pw.println(Arrays.toString(arr)); if (peaks != a || crests != b || !can) pw.println(-1); else { for (int x : arr) pw.print(x + " "); pw.println(); } } else { int peaks = 0; int crests = 0; int arr[] = new int[n]; int count = 0; int i = 1; while (count < b && i < n) { arr[i] = start++; i += 2; count++; } for (i = 0; i < n; i++) { if (arr[i] == 0) { if (start > end) can = false; arr[i] = start++; } } for (i = 1; i < n - 1; i++) { if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) peaks++; else if (arr[i] < arr[i + 1] && arr[i] < arr[i - 1]) crests++; } // pw.println(Arrays.toString(arr)); if (peaks != a || crests != b || !can) pw.println(-1); else { for (int x : arr) pw.print(x + " "); pw.println(); } } } pw.close(); } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
a1be7c3723b7bb6a36032f23dcb8206e
train_109.jsonl
1639217100
You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &lt; p_i &gt; p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} &gt; p_i &lt; p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.
256 megabytes
//package contests; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.* ; import java.math.*; import java.io.*; public class javaTemplate { public static final int M = 1000000007 ; public static BigInteger f1 = new BigInteger("1000000007") ; static FastReader sc = new FastReader(); static int N = 86028121 ; static boolean globB[] = new boolean[N+1] ; static int globA[] = new int[10000000] ; public static void main(String[] args) { int t= sc.nextInt() ; while(t -- != 0) { int n = sc.nextInt() ; int a = sc.nextInt() ; int b = sc.nextInt() ; solve(n,a,b) ; System.out.println(); } } public static void solve(int n, int a, int b) { if(a+b > n-2 || abs(a-b) > 1) { System.out.print("-1"); } else { int x = 0 ; if(a == 0 && b == 1) { System.out.print("2 1" + " "); for(int i = 3 ; i<= n ; i++) { System.out.print(i + " "); } } else if(a == 1 && b == 0) { for(int i = 1 ; i<= n-2 ; i++) { System.out.print(i + " "); } System.out.print(n + " " + (n-1)); } else if(a == 0 && b == 0){ for(int i = 1 ; i<= n ; i++) { System.out.print(i + " "); } } else { if(a > b) { x = a+b+1 ; for(int i = 1 ; i<= n-x ; i++) { System.out.print(i + " "); } for(int i = n-x+1 ; i<= n ; i+=2) { System.out.print((i+1) + " " + i + " "); } } else if(a < b) { x = a+b+1 ; for(int i = 1 ; i<= x ; i+=2) { System.out.print((i+1) + " " + i + " "); } for(int i = x+1 ; i<= n ; i++) { System.out.print(i + " "); } } else { x = a+b ; for(int i = 1 ; i<= x ; i+=2) { System.out.print((i+1) + " " + i + " "); } for(int i = x+1 ; i<= n-2 ; i++) { System.out.print(i + " "); } System.out.print(n + " "+ (n-1)); } } } } // LinkedList class public class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } // Tree class static class Node { int data ; Node left ; Node right ; Node(int data) { this.data = data ; } } /* @ Observation is imp. for problem A and B 1->> Read the problem properly take time to read the problem if it is too lengthy. 2->> Draw the test cases of A and B 3->> Don't over think the problem 4->> Try to take sum variable as long 5->> Avoid Nested if else statement 6->> If it looks like a Greedy problem try to figure out the ans. for the smallest test case. 7->> If it is a binary String check no. of 0's and 1's. If it is a even try to pair it (may be not applicable for every questions). 8->> Always check the constrains and try BruteForce 9->> Sometimes math formulas are also imp. @ Implement it quickly */ //_________________________//Template//_____________________________________________________________________ // FAST I/O 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; } } // Sorting public static void sort(long[] arr) { ArrayList<Long> ls = new ArrayList<Long>(); for(long x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } // Modulo static int mod(int a) { return (a%M+M)%M; } // **Using Comparator for Sorting** // Arrays.sort(a, new Comparator<long[]>() { // @Override // public int compare(long[] o1, long[] o2) { // return (int)(o2[1] - o1[1]); // } // }); // Palindrom or Not static boolean isPalindrome(String str, int low, int high) { while (low < high) { if (str.charAt(low) != str.charAt(high)) return false; low++; high--; } return true; } // check if sorted or not public static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } // GCD public static int GCD(int a , int b) { if(b == 0) return a ; return GCD(b, a%b) ; } // sieve public static void sieve() { Arrays.fill(globB, true); globB[0] = false ; globB[1] = false ; for(int i = 2 ; i * i <= N ; i++) { if(globB[i]) { for(int j = i * i ; j<= N ; j+= i) { globB[j] = false ; } } } } // fastPower public static long fastPowerModulo(long a, long b, long n) { long res = 1 ; while(b > 0) { if((b&1) != 0) { res = (res * a % n) % n ; } a = (a % n * a % n) % n ; b = b >> 1 ; } return res ; } // LCM static int LCM(int a, int b) { return (a / GCD(a, b)) * b; } }
Java
["3\n4 1 1\n6 1 2\n6 4 0"]
1 second
["1 3 2 4\n4 2 3 1 5 6\n-1"]
NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 &lt; p_2 &gt; p_3$$$, and $$$2$$$ is the only such index, and $$$p_2&gt; p_3 &lt; p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2fdbf033e83d7c17841f640fe1fc0e55
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,200
For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
standard output
PASSED
9e1b69c8a6bd85d3942f0e80ea78dbaa
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.Scanner; public class FindArray { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n = scanner.nextInt(); int[][]array=new int[n][1000]; int[]arr=new int[n]; for(int i=0;i<n;i++) arr[i]=scanner.nextInt(); array=findArray(n,arr); for(int i=0;i<n;i++){ for(int j=0;j<arr[i];j++){ System.out.print(array[i][j]+" "); } System.out.println(); } } public static int[][] findArray(int n,int[]arr){ int[][]array=new int[n][1000]; for(int i=0;i<n;i++){ int[]ansArr=new int[arr[i]]; ansArr=firstNPrime(arr[i]); for(int j=0;j<arr[i];j++){ array[i][j]=ansArr[j]; } } return array; } public static int[] firstNPrime(int n){ int i=0,k=2; int[] primeArr=new int[n]; if(n==1){ primeArr[0]=1; return primeArr; } while(i<n){ if(isPrime(k)){ primeArr[i]=k; i++; } k++; } return primeArr; } public static boolean isPrime(int num){ boolean prime = true; for(int i=2;i<=Math.sqrt(num);i++){ if(num%i==0){ prime=false; break; } } return prime; } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
eb84684832628af864db7975d9d0d087
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.Scanner; public class find_array { public static void main(String[] args) { try { Scanner scn=new Scanner(System.in); int t=scn.nextInt(); while(t!=0) { int n=scn.nextInt(); System.out.print(2+" "); int old=2; for(int i=1;i<n;i++) { int next=old+1; while(next %old==0) next++; System.out.print(next+" "); old=next; } System.out.println(); t--; } } catch(Exception e) { } finally { } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
a5088d3ed2b13e796f699c121ef17125
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.*; public class CodeForces { private static boolean isPrime(int num) { if (num <= 1) { return false; } for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) { return false; } } return true; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = Integer.parseInt(scanner.nextLine()); while (t--!=0) { int n = Integer.parseInt(scanner.nextLine()); ArrayList<Integer> ans = new ArrayList<Integer>(); int j = 2; while (true) { if (isPrime(j)) { ans.add(j); } j++; if (ans.size()==n) break; } String fin = ""; for (int i=0; i<n; i++) { fin += ans.get(i) + " "; } System.out.println(fin); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
ddb229611982ea5091a871fa43bf5430
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
//package games; import java.util.ArrayDeque; import java.util.Deque; import java.util.Scanner; /** * @author SHshuo * @data 2021/12/11--18:07 */ public class findArray { public static void main(String[] args) { // 输入的数据 Scanner sc = new Scanner(System.in); int[] array = new int[sc.nextInt()]; findArray findArray = new findArray(); // 实现规则 for(int i = 0; i < array.length; i++){ array[i] = sc.nextInt(); } for(int i = 0; i < array.length; i++){ findArray.rules(array[i]); } } private void rules(int input) { // 开始的暴力求解 int[] array = new int[input]; Deque<Integer> deque = new ArrayDeque<>(); int i = 2, j = 0; while(j < array.length){ if(deque.isEmpty() || i % deque.peek() != 0){ deque.offer(i); array[j++] = i; } i++; } // 打印 for(int num : array){ System.out.print(num); System.out.print("\t"); } System.out.println(); } // 质数 // public static boolean isPrime3(int n) { // if (n <= 3) { // return n > 1; // } // int sqrt = (int) Math.sqrt(n); // for (int i = 3; i <= sqrt; i += 2) { // if (n % 2 == 0 || n % i == 0) { // return false; // } // } // return true; // } // // // public void test4(int n){ // int[] array = new int[n]; // array[0] = 1; // double prescription = Math.sqrt(n); // for (int i = 2; i <= prescription; i++) { // for (int j = i * i; j <= n; j += i) { // array[j - 1] = 1; // } // } // //遍历数组,把值为0的数全部统计出来,得到素数之和 // for (int i = 0; i < array.length; i++) { // if(array[i] == 0) // sum++; // } // System.out.println(n+"以内的素数有"+sum+"个"); // long end = System.currentTimeMillis(); // System.out.println("The time cost is " + (end - start)); // System.out.println(""); // } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
645decb4ec0afde5de111411ba667c05
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
/*package whatever //do not write package name here */ import java.io.*; import java.util.Scanner; public class Array { 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==1){ System.out.println("1"); } else if(n==2){ System.out.println("2 3"); } else { for(int c=1;c<=n;c++) { System.out.println(c+2); } } } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
2832453f846974614a3a0a808ef47239
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class A { 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[64];// line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; 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; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } private 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(); } } public static void main(String[] args) throws IOException { Reader s = new Reader(); try { int T = s.nextInt(); while (T-- > 0) { int n = s.nextInt(); StringBuilder sb = new StringBuilder(); if(n == 1){ sb.append(1); System.out.println(sb.toString()); continue; } List<Integer> li = primeNumbersList((int) Math.pow(n, 2), n); for (int i = 0; i < n-1;i++) { sb.append(li.get(i)); sb.append(" "); } sb.append(li.get(n-1)); System.out.println(sb.toString()); } } catch (Exception e) { s.close(); return; } } private static List<Integer> primeNumbersList(int n, int req) { List<Integer> ans = new ArrayList<>(); boolean primeArr[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) { primeArr[i] = true; } for (int i = 2; i * i <= n; i++) { if (primeArr[i] == true) { for (int j = i * i; j <= n; j += i) primeArr[j] = false; } } for (int i = 2; i <= n && ans.size() <= req; i++) { if (primeArr[i] == true) ans.add(i); } return ans; } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
542cfecbebd72c8b4e3e13c9ef4126b1
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.Scanner; public class FindArray { public static int[] findArr(int n) { int[] arr = new int[n]; arr[0] = 2; for (int i = 1; i < n; i++) { arr[i] = arr[i - 1] + 1; while (arr[i] % arr[i - 1] == 0) { arr[i]++; } } return arr; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int test = scanner.nextInt(); for (int i = 0; i < test; i++) { int n = scanner.nextInt(); if (n == 1) { System.out.println(1); } else { int[] arr = findArr(n); printArr(arr); } } } public static void printArr(int[] arr) { for (int i : arr) { System.out.print(i + " "); } System.out.println(); } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
d8de8bb573ac172586e0c7a48b8b97f9
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class A { static BufferedReader br; static StringTokenizer st; static PrintWriter pw; static String nextToken() { try { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } static int nextInt() { return Integer.parseInt(nextToken()); } static long nextLong() { return Long.parseLong(nextToken()); } static double nextDouble() { return Double.parseDouble(nextToken()); } static String nextLine() { try { return br.readLine(); } catch (IOException e) { throw new IllegalArgumentException(); } } static char nextChar() { try { return (char) br.read(); } catch (IOException e) { throw new IllegalArgumentException(e); } } public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); int t = 1; t = nextInt(); while (t-- > 0) { solve(); } pw.close(); } private static void solve() { int n = nextInt(); for (int i = 0; i < n; i++) { pw.print(i+2 + " "); } pw.println(); } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
1fc7fb42ce575da2dc6a742239a32ccf
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.*; import java.util.*; public class Rough { public static void main(String[] args) throws Exception { Scanner s = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tc = s.nextInt(); for(int t=1;t<=tc;t++) { int n=s.nextInt(); for(int i=2;i<n+2;i++)pw.print(i+" "); pw.println(); } pw.close(); } static final Random ran=new Random(); static void sort(int[] ar) { int n=ar.length; for(int i=0;i<n;i++) { int doer=ran.nextInt(n),temp=ar[doer]; ar[doer]=ar[i];ar[i]=temp; } Arrays.sort(ar); } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
2ae0677428a896f7b35239c77bed48b8
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.*; import java.util.*; public class Rough { static int N=(int)1e4; static boolean[] ar=new boolean[(N+5)]; static ArrayList<Integer> isprime=new ArrayList<>(); static void seive() { ar[0]=ar[1]=false; ar[2]=true; for(int i=4;i<=N;i+=2) { ar[i]=false; } for(int i=3;i*i<=N;i+=2) { if(ar[i]) { for(int j=i*i;j<=N;j+=2*i) { ar[j]=false; } } } isprime.add(2); for(int i=3;i<=N;i+=2) { if(ar[i]) isprime.add(i); } } public static void main(String[] args) throws Exception { Arrays.fill(ar, true); seive(); Scanner s = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tc = s.nextInt(); for(int t=1;t<=tc;t++) { int n=s.nextInt(); for(int i=0;i<n;i++)pw.print(isprime.get(i)+" "); pw.println(); } pw.close(); } static final Random ran=new Random(); static void sort(int[] ar) { int n=ar.length; for(int i=0;i<n;i++) { int doer=ran.nextInt(n),temp=ar[doer]; ar[doer]=ar[i];ar[i]=temp; } Arrays.sort(ar); } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
687aba721b3f97722bcbce8eaf6560f1
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.*; import java.util.*; public class Solution{ static PrintWriter out = new PrintWriter(System.out); static int mod = 1000000009; public static void main(String[] args) throws IOException { FastReader fs = new FastReader(); int t = fs.nextInt(); while(t > 0){ int n = fs.nextInt(); if(n == 1){out.println(1);} else{ int val = 3; for(int i = 0; i < n; i++){ out.print(val + " "); val += 1; } out.println(); } t--; } out.flush(); } static String rvr(String s){ StringBuilder sb = new StringBuilder(s); sb.reverse(); return sb.toString(); } static void op(int[] a, int n){ int max = Integer.MIN_VALUE; for(int i = 0; i < n; i++){ max = Math.max(max, a[i]); } for(int i = 0; i < n; i++){ a[i] = max - a[i]; } } static int isCs(String x, String y, int n, int m, int[][] dp){ if(n == 0 || m == 0){ return 0; } if(dp[n-1][m-1] != 0) {return dp[n-1][m-1];} if(x.charAt(n-1) == y.charAt(m-1)){ return dp[n-1][m-1] = isCs(x, y, n-1, m-1, dp) + 1; }else{ return dp[n-1][m-1] = Math.max(isCs(x, y, n-1, m, dp), isCs(x, y, n, m-1, dp)); } } static int binarySearch(int[] a){ int n = a.length; return 0; } static boolean distinct(String s){ for(int i = 0; i < s.length()-1; i++){ for(int j = i+1; j < s.length(); j++){ if(s.charAt(i) == s.charAt(j)){ return false; } } } return true; } static long sumOfDigit(long n){ long sum = 0; while(n > 0){ long rem = n % 10; sum += rem; n = n / 10; } return sum; } static void swap(int a, int b){ int temp = a; a = b; b = temp; } static int[] numArr(int n){ int len = countDigit(n); int[] a = new int[len]; int i = 0; while(i < len){ a[i] = n % 10; n = n / 10; i++; } return a; } static void lcs(String X, String Y, int m, int n, int temp) { int[][] L = new int[m+1][n+1]; // Following steps build L[m+1][n+1] in bottom up fashion. Note // that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] for (int i=0; i<=m; i++) { for (int j=0; j<=n; j++) { if (i == 0 || j == 0) L[i][j] = 0; else if (X.charAt(i-1) == Y.charAt(j-1)) L[i][j] = L[i-1][j-1] + 1; else L[i][j] = Math.max(L[i-1][j], L[i][j-1]); } } // Following code is used to print LCS int index = L[m][n]; temp = index; // Create a character array to store the lcs string char[] lcs = new char[index+1]; lcs[index] = '\u0000'; // Set the terminating character // Start from the right-most-bottom-most corner and // one by one store characters in lcs[] int i = m; int j = n; while (i > 0 && j > 0) { // If current character in X[] and Y are same, then // current character is part of LCS if (X.charAt(i-1) == Y.charAt(j-1)) { // Put current character in result lcs[index-1] = X.charAt(i-1); // reduce values of i, j and index i--; j--; index--; } // If not same, then find the larger of two and // go in the direction of larger value else if (L[i-1][j] > L[i][j-1]) i--; else j--; } for(int k=0;k<=temp;k++) System.out.print(lcs[k]); } static void sortDe(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); int list = l.size() - 1; for (int i = 0; i < a.length; i++) { a[i] = l.get(list); list--; } } static void bracket(int n){ for(int i = 0; i < n; i++){ out.print("("); } for(int i = 0; i < n; i++){ out.print(")"); } } static int countDigit(int n){ return (int) Math.floor(Math.log10(n)) + 1; } static int countDigit(long n){ return (int) Math.floor(Math.log10(n)) + 1; } static void print(int[] ar){ for(int i = 0; i < ar.length; i++){ out.print(ar[i] + " "); } } static long countEven(long n){ long c = 0; long rem = 0; while(n > 1){ rem = n % 10; if(rem % 2 == 0){ c++; } n = n / 10; } return c; } static boolean divisor(long n){ int count = 2; for(int i = 2; i*i <= n; i++){ if(n % i == 0){ count++; } if(count < 3){ break; } } if(count == 3){return true;} else{return false;} } static String reverseString(String s){ int j = s.length() - 1; String st = new String(); while(j >= 0){ st += s.charAt(j); j--; } return st; } static boolean isPallindrome(String s){ int i = 0, j = s.length() - 1; while(i < j){ if(s.charAt(i) != s.charAt(j)){ return false; } i++; j--; } return true; } static boolean lcsPallindrom(String s){ int i = 0, j = s.length()-1; while(i < j){ if(s.charAt(i) != s.charAt(j)){ return false; } i++; j--; } return true; } static int[] nprimeNumbers(int n){ int[] mark = new int[n+1]; Arrays.fill(mark, 1); mark[0] = 0; mark[1] = 0; for(int i = 2; i < mark.length; i++){ if(mark[i] == 1){ for(int j = i*i; j < mark.length; j += i){ mark[j] = 0; } } } return mark; } static long gcd(long a, long b){ if(b > a){return gcd(b, a);} if(b == 0){return a;} return gcd(b, a % b); } static int gcd(int a, int b){ if(b > a){return gcd(b, a);} if(b == 0){return a;} return gcd(b, a % b); } static class Pair { int lvl; int str; Pair(int lvl, int str){ this.lvl = lvl; this.str = str; } } static class lvlcompare implements Comparator<Pair> { public int compare(Pair p1, Pair p2){ if(p1.lvl == p2.lvl){ return 0; }else if(p1.lvl > p2.lvl){ return 1; }else { return -1; } } } static long count(long n){ long ct = 0; while(n > 0){ n = n / 10; ct++; } return ct; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() {br = new BufferedReader(new InputStreamReader(System.in));} String next() { while (st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();}} return st.nextToken();} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble(){return Double.parseDouble(next());} String nextLine() { String str = ""; try {str = br.readLine();} catch (IOException e) {e.printStackTrace();} return str; } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
2f353bd169c810d6e8695cab70ec3cf0
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.ArrayList; import java.util.List; 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 singh */ public class findArray { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int i=2,s=0; ArrayList<Integer> a=new ArrayList<>(); while(s<n) { a.add(i); i++; s++; } for (Integer name : a) { System.out.print(name+" "); } System.out.println(); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
f76a5d10f6eaff846cb27696a860dcec
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; import java.math.BigInteger; public class Solution { public static void main(String[] args) { FastReader f = new FastReader(); int t = f.nextInt(); while (t-- > 0) { int n = f.nextInt(); BigInteger a = BigInteger.valueOf(1); for (int i = 0; i < n; i++) { a = a.add(BigInteger.valueOf(2)); System.out.print(a + " "); } System.out.println(); } } static int compareStrings(String stra, String strb) { for (int i = 0; i < stra.length(); i++) { if (stra.charAt(i) == strb.charAt(i)) continue; if (stra.charAt(i) > strb.charAt(i)) return 1; if (stra.charAt(i) < strb.charAt(i)) return -1; } return 0; } static int[] sortArray(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); return arr; } 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
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
b7f0a2bd0b700669327a785be21cad80
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.math.*; import java.util.* ; import java.io.* ; public class B { static final int mod = (int)1e9+7 ; static final double pi = 3.1415926536 ; static boolean not_prime[] = new boolean[1000001] ; static void sieve() { for(int i=2 ; i*i<1000001 ; i++) { if(not_prime[i]==false) { for(int j=2*i ; j<1000001 ; j+=i) { not_prime[j]=true ; } } }not_prime[0]=true ; not_prime[1]=true ; } public static long bexp(long base , long power) { long res=1L ; base = base%mod ; while(power>0) { if((power&1)==1) { res=(res*base)%mod ; power-- ; } else { base=(base*base)%mod ; power>>=1 ; } } return res ; } static long modInverse(long n, long p){return power(n, p - 2, p); } static long power(long x, long y, long p) { // Initialize result long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long nCrModPFermat(int n, int r, long p){if(n<r) return 0 ;if (r == 0)return 1; long[] fac = new long[n + 1];fac[0] = 1;for (int i = 1; i <= n; i++)fac[i] = fac[i - 1] * i % p;return (fac[n] * modInverse(fac[r], p) % p* modInverse(fac[n - r], p) % p) % p;} static long mod_add(long a, long b){ return ((a % mod) + (b % mod)) % mod; } static long mod_sub(long a, long b){ return ((a % mod) - (b % mod) + mod) % mod; } static long mod_mult(long a, long b){ return ((a % mod) * (b % mod)) % mod; } static long lcm(int a, int b){ return (a / gcd(a, b)) * b; } static long gcd(long a, long b){if (b == 0) {return a;}return gcd(b, a % b);} public static void main(String[] args)throws IOException//throws FileNotFoundException { FastReader in = new FastReader() ; StringBuilder op = new StringBuilder() ; int T = in.nextInt() ; // int T=1 ; for(int tt=0 ; tt<T ; tt++) { int n = in.nextInt() ; int fill=2 ; int a[] = new int[n] ; for(int i=0 ; i<n ; i++)a[i]=fill++ ; for(int i : a)op.append(i+" ") ; op.append("\n") ; } System.out.println(op.toString()); } static class pair implements Comparable<pair> { int first , second ; pair(int first , int second){ this.first=first ; this.second=second; } public int compareTo(pair other) { return this.first-other.first ; } } 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 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
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
1ee2724c56b4ebdf84e024c3fb21b615
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=1;i<=t;i++) { int n = sc.nextInt(); for(int j=2;j<=(n+1);j++) { System.out.print(j+" "); } System.out.println(" "); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
393ffeb2fbc0b7130578f598df81ae87
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.Stream; public class A_758 { public static final long[] POWER2 = generatePOWER2(); public static final IteratorBuffer<Long> ITERATOR_BUFFER_PRIME = new IteratorBuffer<>(streamPrime(1000000).iterator()); public static long BIG = 1000000000 + 7; private static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer stringTokenizer = null; private static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static class Array<Type> implements Iterable<Type> { private final Object[] array; public Array(int size) { this.array = new Object[size]; } public Array(int size, Type element) { this(size); Arrays.fill(this.array, element); } public Array(Array<Type> array, Type element) { this(array.size() + 1); for (int index = 0; index < array.size(); index++) { set(index, array.get(index)); } set(size() - 1, element); } public Array(List<Type> list) { this(list.size()); int index = 0; for (Type element : list) { set(index, element); index += 1; } } public Type get(int index) { return (Type) this.array[index]; } @Override public Iterator<Type> iterator() { return new Iterator<Type>() { int index = 0; @Override public boolean hasNext() { return this.index < size(); } @Override public Type next() { Type result = Array.this.get(index); index += 1; return result; } }; } public Array set(int index, Type value) { this.array[index] = value; return this; } public int size() { return this.array.length; } public List<Type> toList() { List<Type> result = new ArrayList<>(); for (Type element : this) { result.add(element); } return result; } @Override public String toString() { return "[" + A_758.toString(this, ", ") + "]"; } } static class BIT { private static int lastBit(int index) { return index & -index; } private final long[] tree; public BIT(int size) { this.tree = new long[size]; } public void add(int index, long delta) { index += 1; while (index <= this.tree.length) { tree[index - 1] += delta; index += lastBit(index); } } public long prefix(int end) { long result = 0; while (end > 0) { result += this.tree[end - 1]; end -= lastBit(end); } return result; } public int size() { return this.tree.length; } public long sum(int start, int end) { return prefix(end) - prefix(start); } } static abstract class Edge<TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge>> { public final TypeVertex vertex0; public final TypeVertex vertex1; public final boolean bidirectional; public Edge(TypeVertex vertex0, TypeVertex vertex1, boolean bidirectional) { this.vertex0 = vertex0; this.vertex1 = vertex1; this.bidirectional = bidirectional; this.vertex0.edges.add(getThis()); if (this.bidirectional) { this.vertex1.edges.add(getThis()); } } public abstract TypeEdge getThis(); public TypeVertex other(Vertex<TypeVertex, TypeEdge> vertex) { TypeVertex result; if (vertex0 == vertex) { result = vertex1; } else { result = vertex0; } return result; } public void remove() { this.vertex0.edges.remove(getThis()); if (this.bidirectional) { this.vertex1.edges.remove(getThis()); } } @Override public String toString() { return this.vertex0 + "->" + this.vertex1; } } public static class EdgeDefault<TypeVertex extends Vertex<TypeVertex, EdgeDefault<TypeVertex>>> extends Edge<TypeVertex, EdgeDefault<TypeVertex>> { public EdgeDefault(TypeVertex vertex0, TypeVertex vertex1, boolean bidirectional) { super(vertex0, vertex1, bidirectional); } @Override public EdgeDefault<TypeVertex> getThis() { return this; } } public static class EdgeDefaultDefault extends Edge<VertexDefaultDefault, EdgeDefaultDefault> { public EdgeDefaultDefault(VertexDefaultDefault vertex0, VertexDefaultDefault vertex1, boolean bidirectional) { super(vertex0, vertex1, bidirectional); } @Override public EdgeDefaultDefault getThis() { return this; } } public static class FIFO<Type> { public SingleLinkedList<Type> start; public SingleLinkedList<Type> end; public FIFO() { this.start = null; this.end = null; } public boolean isEmpty() { return this.start == null; } public Type peek() { return this.start.element; } public Type pop() { Type result = this.start.element; this.start = this.start.next; return result; } public void push(Type element) { SingleLinkedList<Type> list = new SingleLinkedList<>(element, null); if (this.start == null) { this.start = list; this.end = list; } else { this.end.next = list; this.end = list; } } } static class Fraction implements Comparable<Fraction> { public static final Fraction ZERO = new Fraction(0, 1); public static Fraction fraction(long whole) { return fraction(whole, 1); } public static Fraction fraction(long numerator, long denominator) { Fraction result; if (denominator == 0) { throw new ArithmeticException(); } if (numerator == 0) { result = Fraction.ZERO; } else { int sign; if (numerator < 0 ^ denominator < 0) { sign = -1; numerator = Math.abs(numerator); denominator = Math.abs(denominator); } else { sign = 1; } long gcd = gcd(numerator, denominator); result = new Fraction(sign * numerator / gcd, denominator / gcd); } return result; } public final long numerator; public final long denominator; private Fraction(long numerator, long denominator) { this.numerator = numerator; this.denominator = denominator; } public Fraction add(Fraction fraction) { return fraction(this.numerator * fraction.denominator + fraction.numerator * this.denominator, this.denominator * fraction.denominator); } @Override public int compareTo(Fraction that) { return Long.compare(this.numerator * that.denominator, that.numerator * this.denominator); } public Fraction divide(Fraction fraction) { return multiply(fraction.inverse()); } public boolean equals(Fraction that) { return this.compareTo(that) == 0; } public boolean equals(Object that) { return this.compareTo((Fraction) that) == 0; } public Fraction getRemainder() { return fraction(this.numerator - getWholePart() * denominator, denominator); } public long getWholePart() { return this.numerator / this.denominator; } public Fraction inverse() { return fraction(this.denominator, this.numerator); } public Fraction multiply(Fraction fraction) { return fraction(this.numerator * fraction.numerator, this.denominator * fraction.denominator); } public Fraction neg() { return fraction(-this.numerator, this.denominator); } public Fraction sub(Fraction fraction) { return add(fraction.neg()); } @Override public String toString() { String result; if (getRemainder().equals(Fraction.ZERO)) { result = "" + this.numerator; } else { result = this.numerator + "/" + this.denominator; } return result; } } static class IteratorBuffer<Type> { private Iterator<Type> iterator; private List<Type> list; public IteratorBuffer(Iterator<Type> iterator) { this.iterator = iterator; this.list = new ArrayList<Type>(); } public Iterator<Type> iterator() { return new Iterator<Type>() { int index = 0; @Override public boolean hasNext() { return this.index < list.size() || IteratorBuffer.this.iterator.hasNext(); } @Override public Type next() { if (list.size() <= this.index) { list.add(iterator.next()); } Type result = list.get(index); index += 1; return result; } }; } } public static class MapCount<Type> extends SortedMapAVL<Type, Long> { private int count; public MapCount(Comparator<? super Type> comparator) { super(comparator); this.count = 0; } public long add(Type key, Long delta) { long result; if (delta > 0) { Long value = get(key); if (value == null) { value = delta; } else { value += delta; } put(key, value); result = delta; } else { result = 0; } this.count += result; return result; } public int count() { return this.count; } public List<Type> flatten() { List<Type> result = new ArrayList<>(); for (Entry<Type, Long> entry : entrySet()) { for (long index = 0; index < entry.getValue(); index++) { result.add(entry.getKey()); } } return result; } @Override public SortedMapAVL<Type, Long> headMap(Type keyEnd) { throw new UnsupportedOperationException(); } @Override public void putAll(Map<? extends Type, ? extends Long> map) { throw new UnsupportedOperationException(); } public long remove(Type key, Long delta) { long result; if (delta > 0) { Long value = get(key) - delta; if (value <= 0) { result = delta + value; remove(key); } else { result = delta; put(key, value); } } else { result = 0; } this.count -= result; return result; } @Override public Long remove(Object key) { Long result = super.remove(key); this.count -= result; return result; } @Override public SortedMapAVL<Type, Long> subMap(Type keyStart, Type keyEnd) { throw new UnsupportedOperationException(); } @Override public SortedMapAVL<Type, Long> tailMap(Type keyStart) { throw new UnsupportedOperationException(); } } public static class MapSet<TypeKey, TypeValue> extends SortedMapAVL<TypeKey, SortedSetAVL<TypeValue>> implements Iterable<TypeValue> { private Comparator<? super TypeValue> comparatorValue; public MapSet(Comparator<? super TypeKey> comparatorKey, Comparator<? super TypeValue> comparatorValue) { super(comparatorKey); this.comparatorValue = comparatorValue; } public MapSet(Comparator<? super TypeKey> comparatorKey, SortedSetAVL<Entry<TypeKey, SortedSetAVL<TypeValue>>> entrySet, Comparator<? super TypeValue> comparatorValue) { super(comparatorKey, entrySet); this.comparatorValue = comparatorValue; } public boolean add(TypeKey key, TypeValue value) { SortedSetAVL<TypeValue> set = computeIfAbsent(key, k -> new SortedSetAVL<>(comparatorValue)); return set.add(value); } public TypeValue firstValue() { TypeValue result; Entry<TypeKey, SortedSetAVL<TypeValue>> firstEntry = firstEntry(); if (firstEntry == null) { result = null; } else { result = firstEntry.getValue().first(); } return result; } @Override public MapSet<TypeKey, TypeValue> headMap(TypeKey keyEnd) { return new MapSet<>(this.comparator, this.entrySet.headSet(new AbstractMap.SimpleEntry<>(keyEnd, null)), this.comparatorValue); } public Iterator<TypeValue> iterator() { return new Iterator<TypeValue>() { Iterator<SortedSetAVL<TypeValue>> iteratorValues = values().iterator(); Iterator<TypeValue> iteratorValue = null; @Override public boolean hasNext() { return iteratorValues.hasNext() || (iteratorValue != null && iteratorValue.hasNext()); } @Override public TypeValue next() { if (iteratorValue == null || !iteratorValue.hasNext()) { iteratorValue = iteratorValues.next().iterator(); } return iteratorValue.next(); } }; } public TypeValue lastValue() { TypeValue result; Entry<TypeKey, SortedSetAVL<TypeValue>> lastEntry = lastEntry(); if (lastEntry == null) { result = null; } else { result = lastEntry.getValue().last(); } return result; } public boolean removeSet(TypeKey key, TypeValue value) { boolean result; SortedSetAVL<TypeValue> set = get(key); if (set == null) { result = false; } else { result = set.remove(value); if (set.size() == 0) { remove(key); } } return result; } @Override public MapSet<TypeKey, TypeValue> tailMap(TypeKey keyStart) { return new MapSet<>(this.comparator, this.entrySet.tailSet(new AbstractMap.SimpleEntry<>(keyStart, null)), this.comparatorValue); } } public static class Matrix { public final int rows; public final int columns; public final Fraction[][] cells; public Matrix(int rows, int columns) { this.rows = rows; this.columns = columns; this.cells = new Fraction[rows][columns]; for (int row = 0; row < rows; row++) { for (int column = 0; column < columns; column++) { set(row, column, Fraction.ZERO); } } } public void add(int rowSource, int rowTarget, Fraction fraction) { for (int column = 0; column < columns; column++) { this.cells[rowTarget][column] = this.cells[rowTarget][column].add(this.cells[rowSource][column].multiply(fraction)); } } private int columnPivot(int row) { int result = this.columns; for (int column = this.columns - 1; 0 <= column; column--) { if (this.cells[row][column].compareTo(Fraction.ZERO) != 0) { result = column; } } return result; } public void reduce() { for (int rowMinimum = 0; rowMinimum < this.rows; rowMinimum++) { int rowPivot = rowPivot(rowMinimum); if (rowPivot != -1) { int columnPivot = columnPivot(rowPivot); Fraction current = this.cells[rowMinimum][columnPivot]; Fraction pivot = this.cells[rowPivot][columnPivot]; Fraction fraction = pivot.inverse().sub(current.divide(pivot)); add(rowPivot, rowMinimum, fraction); for (int row = rowMinimum + 1; row < this.rows; row++) { if (columnPivot(row) == columnPivot) { add(rowMinimum, row, this.cells[row][columnPivot(row)].neg()); } } } } } private int rowPivot(int rowMinimum) { int result = -1; int pivotColumnMinimum = this.columns; for (int row = rowMinimum; row < this.rows; row++) { int pivotColumn = columnPivot(row); if (pivotColumn < pivotColumnMinimum) { result = row; pivotColumnMinimum = pivotColumn; } } return result; } public void set(int row, int column, Fraction value) { this.cells[row][column] = value; } public String toString() { String result = ""; for (int row = 0; row < rows; row++) { for (int column = 0; column < columns; column++) { result += this.cells[row][column] + "\t"; } result += "\n"; } return result; } } public static class Node<Type> { public static <Type> Node<Type> balance(Node<Type> result) { while (result != null && 1 < Math.abs(height(result.left) - height(result.right))) { if (height(result.left) < height(result.right)) { Node<Type> right = result.right; if (height(right.right) < height(right.left)) { result = new Node<>(result.value, result.left, right.rotateRight()); } result = result.rotateLeft(); } else { Node<Type> left = result.left; if (height(left.left) < height(left.right)) { result = new Node<>(result.value, left.rotateLeft(), result.right); } result = result.rotateRight(); } } return result; } public static <Type> Node<Type> clone(Node<Type> result) { if (result != null) { result = new Node<>(result.value, clone(result.left), clone(result.right)); } return result; } public static <Type> Node<Type> delete(Node<Type> node, Type value, Comparator<? super Type> comparator) { Node<Type> result; if (node == null) { result = null; } else { int compare = comparator.compare(value, node.value); if (compare == 0) { if (node.left == null) { result = node.right; } else { if (node.right == null) { result = node.left; } else { Node<Type> first = first(node.right); result = new Node<>(first.value, node.left, delete(node.right, first.value, comparator)); } } } else { if (compare < 0) { result = new Node<>(node.value, delete(node.left, value, comparator), node.right); } else { result = new Node<>(node.value, node.left, delete(node.right, value, comparator)); } } result = balance(result); } return result; } public static <Type> Node<Type> first(Node<Type> result) { while (result.left != null) { result = result.left; } return result; } public static <Type> Node<Type> get(Node<Type> node, Type value, Comparator<? super Type> comparator) { Node<Type> result; if (node == null) { result = null; } else { int compare = comparator.compare(value, node.value); if (compare == 0) { result = node; } else { if (compare < 0) { result = get(node.left, value, comparator); } else { result = get(node.right, value, comparator); } } } return result; } public static <Type> Node<Type> head(Node<Type> node, Type value, Comparator<? super Type> comparator) { Node<Type> result; if (node == null) { result = null; } else { int compare = comparator.compare(value, node.value); if (compare == 0) { result = node.left; } else { if (compare < 0) { result = head(node.left, value, comparator); } else { result = new Node<>(node.value, node.left, head(node.right, value, comparator)); } } result = balance(result); } return result; } public static int height(Node node) { return node == null ? 0 : node.height; } public static <Type> Node<Type> insert(Node<Type> node, Type value, Comparator<? super Type> comparator) { Node<Type> result; if (node == null) { result = new Node<>(value, null, null); } else { int compare = comparator.compare(value, node.value); if (compare == 0) { result = new Node<>(value, node.left, node.right); ; } else { if (compare < 0) { result = new Node<>(node.value, insert(node.left, value, comparator), node.right); } else { result = new Node<>(node.value, node.left, insert(node.right, value, comparator)); } } result = balance(result); } return result; } public static <Type> Node<Type> last(Node<Type> result) { while (result.right != null) { result = result.right; } return result; } public static int size(Node node) { return node == null ? 0 : node.size; } public static <Type> Node<Type> tail(Node<Type> node, Type value, Comparator<? super Type> comparator) { Node<Type> result; if (node == null) { result = null; } else { int compare = comparator.compare(value, node.value); if (compare == 0) { result = new Node<>(node.value, null, node.right); } else { if (compare < 0) { result = new Node<>(node.value, tail(node.left, value, comparator), node.right); } else { result = tail(node.right, value, comparator); } } result = balance(result); } return result; } public static <Type> void traverseOrderIn(Node<Type> node, Consumer<Type> consumer) { if (node != null) { traverseOrderIn(node.left, consumer); consumer.accept(node.value); traverseOrderIn(node.right, consumer); } } public final Type value; public final Node<Type> left; public final Node<Type> right; public final int size; private final int height; public Node(Type value, Node<Type> left, Node<Type> right) { this.value = value; this.left = left; this.right = right; this.size = 1 + size(left) + size(right); this.height = 1 + Math.max(height(left), height(right)); } public Node<Type> rotateLeft() { Node<Type> left = new Node<>(this.value, this.left, this.right.left); return new Node<>(this.right.value, left, this.right.right); } public Node<Type> rotateRight() { Node<Type> right = new Node<>(this.value, this.left.right, this.right); return new Node<>(this.left.value, this.left.left, right); } } public static class SingleLinkedList<Type> { public final Type element; public SingleLinkedList<Type> next; public SingleLinkedList(Type element, SingleLinkedList<Type> next) { this.element = element; this.next = next; } public void toCollection(Collection<Type> collection) { if (this.next != null) { this.next.toCollection(collection); } collection.add(this.element); } } public static class SmallSetIntegers { public static final int SIZE = 20; public static final int[] SET = generateSet(); public static final int[] COUNT = generateCount(); public static final int[] INTEGER = generateInteger(); private static int count(int set) { int result = 0; for (int integer = 0; integer < SIZE; integer++) { if (0 < (set & set(integer))) { result += 1; } } return result; } private static final int[] generateCount() { int[] result = new int[1 << SIZE]; for (int set = 0; set < result.length; set++) { result[set] = count(set); } return result; } private static final int[] generateInteger() { int[] result = new int[1 << SIZE]; Arrays.fill(result, -1); for (int integer = 0; integer < SIZE; integer++) { result[SET[integer]] = integer; } return result; } private static final int[] generateSet() { int[] result = new int[SIZE]; for (int integer = 0; integer < result.length; integer++) { result[integer] = set(integer); } return result; } private static int set(int integer) { return 1 << integer; } } public static class SortedMapAVL<TypeKey, TypeValue> implements SortedMap<TypeKey, TypeValue> { public final Comparator<? super TypeKey> comparator; public final SortedSetAVL<Entry<TypeKey, TypeValue>> entrySet; public SortedMapAVL(Comparator<? super TypeKey> comparator) { this(comparator, new SortedSetAVL<>((entry0, entry1) -> comparator.compare(entry0.getKey(), entry1.getKey()))); } private SortedMapAVL(Comparator<? super TypeKey> comparator, SortedSetAVL<Entry<TypeKey, TypeValue>> entrySet) { this.comparator = comparator; this.entrySet = entrySet; } @Override public void clear() { this.entrySet.clear(); } @Override public Comparator<? super TypeKey> comparator() { return this.comparator; } @Override public boolean containsKey(Object key) { return this.entrySet().contains(new AbstractMap.SimpleEntry<>((TypeKey) key, null)); } @Override public boolean containsValue(Object value) { throw new UnsupportedOperationException(); } @Override public SortedSetAVL<Entry<TypeKey, TypeValue>> entrySet() { return this.entrySet; } public Entry<TypeKey, TypeValue> firstEntry() { return this.entrySet.first(); } @Override public TypeKey firstKey() { return firstEntry().getKey(); } @Override public TypeValue get(Object key) { Entry<TypeKey, TypeValue> entry = new AbstractMap.SimpleEntry<>((TypeKey) key, null); entry = this.entrySet.get(entry); return entry == null ? null : entry.getValue(); } @Override public SortedMapAVL<TypeKey, TypeValue> headMap(TypeKey keyEnd) { return new SortedMapAVL<>(this.comparator, this.entrySet.headSet(new AbstractMap.SimpleEntry<>(keyEnd, null))); } @Override public boolean isEmpty() { return this.entrySet.isEmpty(); } @Override public Set<TypeKey> keySet() { return new SortedSet<TypeKey>() { @Override public boolean add(TypeKey typeKey) { throw new UnsupportedOperationException(); } @Override public boolean addAll(Collection<? extends TypeKey> collection) { throw new UnsupportedOperationException(); } @Override public void clear() { throw new UnsupportedOperationException(); } @Override public Comparator<? super TypeKey> comparator() { throw new UnsupportedOperationException(); } @Override public boolean contains(Object o) { throw new UnsupportedOperationException(); } @Override public boolean containsAll(Collection<?> collection) { throw new UnsupportedOperationException(); } @Override public TypeKey first() { throw new UnsupportedOperationException(); } @Override public SortedSet<TypeKey> headSet(TypeKey typeKey) { throw new UnsupportedOperationException(); } @Override public boolean isEmpty() { return size() == 0; } @Override public Iterator<TypeKey> iterator() { final Iterator<Entry<TypeKey, TypeValue>> iterator = SortedMapAVL.this.entrySet.iterator(); return new Iterator<TypeKey>() { @Override public boolean hasNext() { return iterator.hasNext(); } @Override public TypeKey next() { return iterator.next().getKey(); } }; } @Override public TypeKey last() { throw new UnsupportedOperationException(); } @Override public boolean remove(Object o) { throw new UnsupportedOperationException(); } @Override public boolean removeAll(Collection<?> collection) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(Collection<?> collection) { throw new UnsupportedOperationException(); } @Override public int size() { return SortedMapAVL.this.entrySet.size(); } @Override public SortedSet<TypeKey> subSet(TypeKey typeKey, TypeKey e1) { throw new UnsupportedOperationException(); } @Override public SortedSet<TypeKey> tailSet(TypeKey typeKey) { throw new UnsupportedOperationException(); } @Override public Object[] toArray() { throw new UnsupportedOperationException(); } @Override public <T> T[] toArray(T[] ts) { throw new UnsupportedOperationException(); } }; } public Entry<TypeKey, TypeValue> lastEntry() { return this.entrySet.last(); } @Override public TypeKey lastKey() { return lastEntry().getKey(); } @Override public TypeValue put(TypeKey key, TypeValue value) { TypeValue result = get(key); Entry<TypeKey, TypeValue> entry = new AbstractMap.SimpleEntry<>(key, value); this.entrySet().add(entry); return result; } @Override public void putAll(Map<? extends TypeKey, ? extends TypeValue> map) { map.entrySet() .forEach(entry -> put(entry.getKey(), entry.getValue())); } @Override public TypeValue remove(Object key) { TypeValue result = get(key); Entry<TypeKey, TypeValue> entry = new AbstractMap.SimpleEntry<>((TypeKey) key, null); this.entrySet.remove(entry); return result; } @Override public int size() { return this.entrySet().size(); } @Override public SortedMapAVL<TypeKey, TypeValue> subMap(TypeKey keyStart, TypeKey keyEnd) { return new SortedMapAVL<>(this.comparator, this.entrySet.subSet(new AbstractMap.SimpleEntry<>(keyStart, null), new AbstractMap.SimpleEntry<>(keyEnd, null))); } @Override public SortedMapAVL<TypeKey, TypeValue> tailMap(TypeKey keyStart) { return new SortedMapAVL<>(this.comparator, this.entrySet.tailSet(new AbstractMap.SimpleEntry<>(keyStart, null))); } @Override public String toString() { return this.entrySet().toString(); } @Override public Collection<TypeValue> values() { return new Collection<TypeValue>() { @Override public boolean add(TypeValue typeValue) { throw new UnsupportedOperationException(); } @Override public boolean addAll(Collection<? extends TypeValue> collection) { throw new UnsupportedOperationException(); } @Override public void clear() { throw new UnsupportedOperationException(); } @Override public boolean contains(Object value) { throw new UnsupportedOperationException(); } @Override public boolean containsAll(Collection<?> collection) { throw new UnsupportedOperationException(); } @Override public boolean isEmpty() { return SortedMapAVL.this.entrySet.isEmpty(); } @Override public Iterator<TypeValue> iterator() { return new Iterator<TypeValue>() { Iterator<Entry<TypeKey, TypeValue>> iterator = SortedMapAVL.this.entrySet.iterator(); @Override public boolean hasNext() { return this.iterator.hasNext(); } @Override public TypeValue next() { return this.iterator.next().getValue(); } }; } @Override public boolean remove(Object o) { throw new UnsupportedOperationException(); } @Override public boolean removeAll(Collection<?> collection) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(Collection<?> collection) { throw new UnsupportedOperationException(); } @Override public int size() { return SortedMapAVL.this.entrySet.size(); } @Override public Object[] toArray() { throw new UnsupportedOperationException(); } @Override public <T> T[] toArray(T[] ts) { throw new UnsupportedOperationException(); } }; } } public static class SortedSetAVL<Type> implements SortedSet<Type> { public Comparator<? super Type> comparator; public Node<Type> root; private SortedSetAVL(Comparator<? super Type> comparator, Node<Type> root) { this.comparator = comparator; this.root = root; } public SortedSetAVL(Comparator<? super Type> comparator) { this(comparator, null); } public SortedSetAVL(Collection<? extends Type> collection, Comparator<? super Type> comparator) { this(comparator, null); this.addAll(collection); } public SortedSetAVL(SortedSetAVL<Type> sortedSetAVL) { this(sortedSetAVL.comparator, Node.clone(sortedSetAVL.root)); } @Override public boolean add(Type value) { int sizeBefore = size(); this.root = Node.insert(this.root, value, this.comparator); return sizeBefore != size(); } @Override public boolean addAll(Collection<? extends Type> collection) { return collection.stream() .map(this::add) .reduce(true, (x, y) -> x | y); } @Override public void clear() { this.root = null; } @Override public Comparator<? super Type> comparator() { return this.comparator; } @Override public boolean contains(Object value) { return Node.get(this.root, (Type) value, this.comparator) != null; } @Override public boolean containsAll(Collection<?> collection) { return collection.stream() .allMatch(this::contains); } @Override public Type first() { return Node.first(this.root).value; } public Type get(Type value) { Node<Type> node = Node.get(this.root, value, this.comparator); return node == null ? null : node.value; } @Override public SortedSetAVL<Type> headSet(Type valueEnd) { return new SortedSetAVL<>(this.comparator, Node.head(this.root, valueEnd, this.comparator)); } @Override public boolean isEmpty() { return this.root == null; } @Override public Iterator<Type> iterator() { Stack<Node<Type>> path = new Stack<>(); return new Iterator<Type>() { { push(SortedSetAVL.this.root); } @Override public boolean hasNext() { return !path.isEmpty(); } @Override public Type next() { if (path.isEmpty()) { throw new NoSuchElementException(); } else { Node<Type> node = path.peek(); Type result = node.value; if (node.right != null) { push(node.right); } else { do { node = path.pop(); } while (!path.isEmpty() && path.peek().right == node); } return result; } } public void push(Node<Type> node) { while (node != null) { path.push(node); node = node.left; } } }; } @Override public Type last() { return Node.last(this.root).value; } @Override public boolean remove(Object value) { int sizeBefore = size(); this.root = Node.delete(this.root, (Type) value, this.comparator); return sizeBefore != size(); } @Override public boolean removeAll(Collection<?> collection) { return collection.stream() .map(this::remove) .reduce(true, (x, y) -> x | y); } @Override public boolean retainAll(Collection<?> collection) { SortedSetAVL<Type> set = new SortedSetAVL<>(this.comparator); collection.stream() .map(element -> (Type) element) .filter(this::contains) .forEach(set::add); boolean result = size() != set.size(); this.root = set.root; return result; } @Override public int size() { return this.root == null ? 0 : this.root.size; } @Override public SortedSetAVL<Type> subSet(Type valueStart, Type valueEnd) { return tailSet(valueStart).headSet(valueEnd); } @Override public SortedSetAVL<Type> tailSet(Type valueStart) { return new SortedSetAVL<>(this.comparator, Node.tail(this.root, valueStart, this.comparator)); } @Override public Object[] toArray() { return toArray(new Object[0]); } @Override public <T> T[] toArray(T[] ts) { List<Object> list = new ArrayList<>(); Node.traverseOrderIn(this.root, list::add); return list.toArray(ts); } @Override public String toString() { return "{" + A_758.toString(this, ", ") + "}"; } } public static class Tree2D { public static final int SIZE = 1 << 30; public static final Tree2D[] TREES_NULL = new Tree2D[] { null, null, null, null }; public static boolean contains(int x, int y, int left, int bottom, int size) { return left <= x && x < left + size && bottom <= y && y < bottom + size; } public static int count(Tree2D[] trees) { int result = 0; for (int index = 0; index < 4; index++) { if (trees[index] != null) { result += trees[index].count; } } return result; } public static int count ( int rectangleLeft, int rectangleBottom, int rectangleRight, int rectangleTop, Tree2D tree, int left, int bottom, int size ) { int result; if (tree == null) { result = 0; } else { int right = left + size; int top = bottom + size; int intersectionLeft = Math.max(rectangleLeft, left); int intersectionBottom = Math.max(rectangleBottom, bottom); int intersectionRight = Math.min(rectangleRight, right); int intersectionTop = Math.min(rectangleTop, top); if (intersectionRight <= intersectionLeft || intersectionTop <= intersectionBottom) { result = 0; } else { if (intersectionLeft == left && intersectionBottom == bottom && intersectionRight == right && intersectionTop == top) { result = tree.count; } else { size = size >> 1; result = 0; for (int index = 0; index < 4; index++) { result += count ( rectangleLeft, rectangleBottom, rectangleRight, rectangleTop, tree.trees[index], quadrantLeft(left, size, index), quadrantBottom(bottom, size, index), size ); } } } } return result; } public static int quadrantBottom(int bottom, int size, int index) { return bottom + (index >> 1) * size; } public static int quadrantLeft(int left, int size, int index) { return left + (index & 1) * size; } public final Tree2D[] trees; public final int count; private Tree2D(Tree2D[] trees, int count) { this.trees = trees; this.count = count; } public Tree2D(Tree2D[] trees) { this(trees, count(trees)); } public Tree2D() { this(TREES_NULL); } public int count(int rectangleLeft, int rectangleBottom, int rectangleRight, int rectangleTop) { return count ( rectangleLeft, rectangleBottom, rectangleRight, rectangleTop, this, 0, 0, SIZE ); } public Tree2D setPoint ( int x, int y, Tree2D tree, int left, int bottom, int size ) { Tree2D result; if (contains(x, y, left, bottom, size)) { if (size == 1) { result = new Tree2D(TREES_NULL, 1); } else { size = size >> 1; Tree2D[] trees = new Tree2D[4]; for (int index = 0; index < 4; index++) { trees[index] = setPoint ( x, y, tree == null ? null : tree.trees[index], quadrantLeft(left, size, index), quadrantBottom(bottom, size, index), size ); } result = new Tree2D(trees); } } else { result = tree; } return result; } public Tree2D setPoint(int x, int y) { return setPoint ( x, y, this, 0, 0, SIZE ); } } public static class Tuple2<Type0, Type1> { public final Type0 v0; public final Type1 v1; public Tuple2(Type0 v0, Type1 v1) { this.v0 = v0; this.v1 = v1; } @Override public String toString() { return "(" + this.v0 + ", " + this.v1 + ")"; } } public static class Tuple2Comparable<Type0 extends Comparable<? super Type0>, Type1 extends Comparable<? super Type1>> extends Tuple2<Type0, Type1> implements Comparable<Tuple2Comparable<Type0, Type1>> { public Tuple2Comparable(Type0 v0, Type1 v1) { super(v0, v1); } @Override public int compareTo(Tuple2Comparable<Type0, Type1> that) { int result = this.v0.compareTo(that.v0); if (result == 0) { result = this.v1.compareTo(that.v1); } return result; } } public static class Tuple3<Type0, Type1, Type2> { public final Type0 v0; public final Type1 v1; public final Type2 v2; public Tuple3(Type0 v0, Type1 v1, Type2 v2) { this.v0 = v0; this.v1 = v1; this.v2 = v2; } @Override public String toString() { return "(" + this.v0 + ", " + this.v1 + ", " + this.v2 + ")"; } } public static class Vertex < TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge> > implements Comparable<Vertex<? super TypeVertex, ? super TypeEdge>> { public static < TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge>, TypeResult > TypeResult breadthFirstSearch ( TypeVertex vertex, TypeEdge edge, BiFunctionResult<TypeVertex, TypeEdge, TypeResult> function, Array<Boolean> visited, FIFO<TypeVertex> verticesNext, FIFO<TypeEdge> edgesNext, TypeResult result ) { if (!visited.get(vertex.index)) { visited.set(vertex.index, true); result = function.apply(vertex, edge, result); for (TypeEdge edgeNext : vertex.edges) { TypeVertex vertexNext = edgeNext.other(vertex); if (!visited.get(vertexNext.index)) { verticesNext.push(vertexNext); edgesNext.push(edgeNext); } } } return result; } public static < TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge>, TypeResult > TypeResult breadthFirstSearch ( Array<TypeVertex> vertices, int indexVertexStart, BiFunctionResult<TypeVertex, TypeEdge, TypeResult> function, TypeResult result ) { Array<Boolean> visited = new Array<>(vertices.size(), false); FIFO<TypeVertex> verticesNext = new FIFO<>(); verticesNext.push(vertices.get(indexVertexStart)); FIFO<TypeEdge> edgesNext = new FIFO<>(); edgesNext.push(null); while (!verticesNext.isEmpty()) { result = breadthFirstSearch(verticesNext.pop(), edgesNext.pop(), function, visited, verticesNext, edgesNext, result); } return result; } public static < TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge> > boolean cycle ( TypeVertex start, SortedSet<TypeVertex> result ) { boolean cycle = false; Stack<TypeVertex> stackVertex = new Stack<>(); Stack<TypeEdge> stackEdge = new Stack<>(); stackVertex.push(start); stackEdge.push(null); while (!stackVertex.isEmpty()) { TypeVertex vertex = stackVertex.pop(); TypeEdge edge = stackEdge.pop(); if (!result.contains(vertex)) { result.add(vertex); for (TypeEdge otherEdge : vertex.edges) { if (otherEdge != edge) { TypeVertex otherVertex = otherEdge.other(vertex); if (result.contains(otherVertex)) { cycle = true; } else { stackVertex.push(otherVertex); stackEdge.push(otherEdge); } } } } } return cycle; } public static < TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge> > SortedSet<TypeVertex> depthFirstSearch ( TypeVertex start, BiConsumer<TypeVertex, TypeEdge> functionVisitPre, BiConsumer<TypeVertex, TypeEdge> functionVisitPost ) { SortedSet<TypeVertex> result = new SortedSetAVL<>(Comparator.naturalOrder()); Stack<TypeVertex> stackVertex = new Stack<>(); Stack<TypeEdge> stackEdge = new Stack<>(); stackVertex.push(start); stackEdge.push(null); while (!stackVertex.isEmpty()) { TypeVertex vertex = stackVertex.pop(); TypeEdge edge = stackEdge.pop(); if (result.contains(vertex)) { functionVisitPost.accept(vertex, edge); } else { result.add(vertex); stackVertex.push(vertex); stackEdge.push(edge); functionVisitPre.accept(vertex, edge); for (TypeEdge otherEdge : vertex.edges) { TypeVertex otherVertex = otherEdge.other(vertex); if (!result.contains(otherVertex)) { stackVertex.push(otherVertex); stackEdge.push(otherEdge); } } } } return result; } public static < TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge> > SortedSet<TypeVertex> depthFirstSearch ( TypeVertex start, Consumer<TypeVertex> functionVisitPreVertex, Consumer<TypeVertex> functionVisitPostVertex ) { BiConsumer<TypeVertex, TypeEdge> functionVisitPreVertexEdge = (vertex, edge) -> { functionVisitPreVertex.accept(vertex); }; BiConsumer<TypeVertex, TypeEdge> functionVisitPostVertexEdge = (vertex, edge) -> { functionVisitPostVertex.accept(vertex); }; return depthFirstSearch(start, functionVisitPreVertexEdge, functionVisitPostVertexEdge); } public final int index; public final List<TypeEdge> edges; public Vertex(int index) { this.index = index; this.edges = new ArrayList<>(); } @Override public int compareTo(Vertex<? super TypeVertex, ? super TypeEdge> that) { return Integer.compare(this.index, that.index); } @Override public String toString() { return "" + this.index; } } public static class VertexDefault<TypeEdge extends Edge<VertexDefault<TypeEdge>, TypeEdge>> extends Vertex<VertexDefault<TypeEdge>, TypeEdge> { public VertexDefault(int index) { super(index); } } public static class VertexDefaultDefault extends Vertex<VertexDefaultDefault, EdgeDefaultDefault> { public static Array<VertexDefaultDefault> vertices(int n) { Array<VertexDefaultDefault> result = new Array<>(n); for (int index = 0; index < n; index++) { result.set(index, new VertexDefaultDefault(index)); } return result; } public VertexDefaultDefault(int index) { super(index); } } public static class Wrapper<Type> { public Type value; public Wrapper(Type value) { this.value = value; } public Type get() { return this.value; } public void set(Type value) { this.value = value; } @Override public String toString() { return this.value.toString(); } } public static void add(int delta, int[] result) { for (int index = 0; index < result.length; index++) { result[index] += delta; } } public static void add(int delta, int[]... result) { for (int index = 0; index < result.length; index++) { add(delta, result[index]); } } public static long add(long x, long y) { return (x + y) % BIG; } public static int binarySearchMaximum(Function<Integer, Boolean> filter, int start, int end) { return -binarySearchMinimum(x -> filter.apply(-x), -end, -start); } public static int binarySearchMinimum(Function<Integer, Boolean> filter, int start, int end) { int result; if (start == end) { result = end; } else { int middle = start + (end - start) / 2; if (filter.apply(middle)) { result = binarySearchMinimum(filter, start, middle); } else { result = binarySearchMinimum(filter, middle + 1, end); } } return result; } public static void close() { out.close(); } private static void combinations(int n, int k, int start, SortedSet<Integer> combination, List<SortedSet<Integer>> result) { if (k == 0) { result.add(new SortedSetAVL<>(combination, Comparator.naturalOrder())); } else { for (int index = start; index < n; index++) { if (!combination.contains(index)) { combination.add(index); combinations(n, k - 1, index + 1, combination, result); combination.remove(index); } } } } public static List<SortedSet<Integer>> combinations(int n, int k) { List<SortedSet<Integer>> result = new ArrayList<>(); combinations(n, k, 0, new SortedSetAVL<>(Comparator.naturalOrder()), result); return result; } public static <Type> int compare(Iterator<Type> iterator0, Iterator<Type> iterator1, Comparator<Type> comparator) { int result = 0; while (result == 0 && iterator0.hasNext() && iterator1.hasNext()) { result = comparator.compare(iterator0.next(), iterator1.next()); } if (result == 0) { if (iterator1.hasNext()) { result = -1; } else { if (iterator0.hasNext()) { result = 1; } } } return result; } public static <Type> int compare(Iterable<Type> iterable0, Iterable<Type> iterable1, Comparator<Type> comparator) { return compare(iterable0.iterator(), iterable1.iterator(), comparator); } public static long divideCeil(long x, long y) { return (x + y - 1) / y; } public static Set<Long> divisors(long n) { SortedSetAVL<Long> result = new SortedSetAVL<>(Comparator.naturalOrder()); result.add(1L); for (Long factor : factors(n)) { SortedSetAVL<Long> divisors = new SortedSetAVL<>(result); for (Long divisor : result) { divisors.add(divisor * factor); } result = divisors; } return result; } public static LinkedList<Long> factors(long n) { LinkedList<Long> result = new LinkedList<>(); Iterator<Long> primes = ITERATOR_BUFFER_PRIME.iterator(); Long prime; while (n > 1 && (prime = primes.next()) * prime <= n) { while (n % prime == 0) { result.add(prime); n /= prime; } } if (n > 1) { result.add(n); } return result; } public static long faculty(int n) { long result = 1; for (int index = 2; index <= n; index++) { result *= index; } return result; } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public static long[] generatePOWER2() { long[] result = new long[63]; for (int x = 0; x < result.length; x++) { result[x] = 1L << x; } return result; } public static boolean isPrime(long x) { boolean result = x > 1; Iterator<Long> iterator = ITERATOR_BUFFER_PRIME.iterator(); Long prime; while ((prime = iterator.next()) * prime <= x) { result &= x % prime > 0; } return result; } public static long knapsack(List<Tuple3<Long, Integer, Integer>> itemsValueWeightCount, int weightMaximum) { long[] valuesMaximum = new long[weightMaximum + 1]; for (Tuple3<Long, Integer, Integer> itemValueWeightCount : itemsValueWeightCount) { long itemValue = itemValueWeightCount.v0; int itemWeight = itemValueWeightCount.v1; int itemCount = itemValueWeightCount.v2; for (int weight = weightMaximum; 0 <= weight; weight--) { for (int index = 1; index <= itemCount && 0 <= weight - index * itemWeight; index++) { valuesMaximum[weight] = Math.max(valuesMaximum[weight], valuesMaximum[weight - index * itemWeight] + index * itemValue); } } } long result = 0; for (long valueMaximum : valuesMaximum) { result = Math.max(result, valueMaximum); } return result; } public static boolean knapsackPossible(List<Tuple2<Integer, Integer>> itemsWeightCount, int weightMaximum) { boolean[] weightPossible = new boolean[weightMaximum + 1]; weightPossible[0] = true; int weightLargest = 0; for (Tuple2<Integer, Integer> itemWeightCount : itemsWeightCount) { int itemWeight = itemWeightCount.v0; int itemCount = itemWeightCount.v1; for (int weightStart = 0; weightStart < itemWeight; weightStart++) { int count = 0; for (int weight = weightStart; weight <= weightMaximum && (0 < count || weight <= weightLargest); weight += itemWeight) { if (weightPossible[weight]) { count = itemCount; } else { if (0 < count) { weightPossible[weight] = true; weightLargest = weight; count -= 1; } } } } } return weightPossible[weightMaximum]; } public static long lcm(int a, int b) { return a * b / gcd(a, b); } public static void main(String[] args) { try { solve(); } catch (IOException exception) { exception.printStackTrace(); } close(); } public static long mul(long x, long y) { return (x * y) % BIG; } public static double nextDouble() throws IOException { return Double.parseDouble(nextString()); } public static int nextInt() throws IOException { return Integer.parseInt(nextString()); } public static void nextInts(int n, int[]... result) throws IOException { for (int index = 0; index < n; index++) { for (int value = 0; value < result.length; value++) { result[value][index] = nextInt(); } } } public static int[] nextInts(int n) throws IOException { int[] result = new int[n]; nextInts(n, result); return result; } public static String nextLine() throws IOException { return bufferedReader.readLine(); } public static long nextLong() throws IOException { return Long.parseLong(nextString()); } public static void nextLongs(int n, long[]... result) throws IOException { for (int index = 0; index < n; index++) { for (int value = 0; value < result.length; value++) { result[value][index] = nextLong(); } } } public static long[] nextLongs(int n) throws IOException { long[] result = new long[n]; nextLongs(n, result); return result; } public static String nextString() throws IOException { while ((stringTokenizer == null) || (!stringTokenizer.hasMoreTokens())) { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); } return stringTokenizer.nextToken(); } public static String[] nextStrings(int n) throws IOException { String[] result = new String[n]; { for (int index = 0; index < n; index++) { result[index] = nextString(); } } return result; } public static <T> List<T> permutation(long p, List<T> x) { List<T> copy = new ArrayList<>(); for (int index = 0; index < x.size(); index++) { copy.add(x.get(index)); } List<T> result = new ArrayList<>(); for (int indexTo = 0; indexTo < x.size(); indexTo++) { int indexFrom = (int) p % copy.size(); p = p / copy.size(); result.add(copy.remove(indexFrom)); } return result; } public static <Type> List<List<Type>> permutations(List<Type> list) { List<List<Type>> result = new ArrayList<>(); result.add(new ArrayList<>()); for (Type element : list) { List<List<Type>> permutations = result; result = new ArrayList<>(); for (List<Type> permutation : permutations) { for (int index = 0; index <= permutation.size(); index++) { List<Type> permutationNew = new ArrayList<>(permutation); permutationNew.add(index, element); result.add(permutationNew); } } } return result; } public static long[][] sizeGroup2CombinationsCount(int maximum) { long[][] result = new long[maximum + 1][maximum + 1]; for (int length = 0; length <= maximum; length++) { result[length][0] = 1; for (int group = 1; group <= length; group++) { result[length][group] = add(result[length - 1][group - 1], result[length - 1][group]); } } return result; } public static Stream<BigInteger> streamFibonacci() { return Stream.generate(new Supplier<BigInteger>() { private BigInteger n0 = BigInteger.ZERO; private BigInteger n1 = BigInteger.ONE; @Override public BigInteger get() { BigInteger result = n0; n0 = n1; n1 = result.add(n0); return result; } }); } public static Stream<Long> streamPrime(int sieveSize) { return Stream.generate(new Supplier<Long>() { private boolean[] isPrime = new boolean[sieveSize]; private long sieveOffset = 2; private List<Long> primes = new ArrayList<>(); private int index = 0; public void filter(long prime, boolean[] result) { if (prime * prime < this.sieveOffset + sieveSize) { long remainingStart = this.sieveOffset % prime; long start = remainingStart == 0 ? 0 : prime - remainingStart; for (long index = start; index < sieveSize; index += prime) { result[(int) index] = false; } } } public void generatePrimes() { Arrays.fill(this.isPrime, true); this.primes.forEach(prime -> filter(prime, isPrime)); for (int index = 0; index < sieveSize; index++) { if (isPrime[index]) { this.primes.add(this.sieveOffset + index); filter(this.sieveOffset + index, isPrime); } } this.sieveOffset += sieveSize; } @Override public Long get() { while (this.primes.size() <= this.index) { generatePrimes(); } Long result = this.primes.get(this.index); this.index += 1; return result; } }); } public static <Type> String toString(Iterator<Type> iterator, String separator) { StringBuilder stringBuilder = new StringBuilder(); if (iterator.hasNext()) { stringBuilder.append(iterator.next()); } while (iterator.hasNext()) { stringBuilder.append(separator); stringBuilder.append(iterator.next()); } return stringBuilder.toString(); } public static <Type> String toString(Iterator<Type> iterator) { return toString(iterator, " "); } public static <Type> String toString(Iterable<Type> iterable, String separator) { return toString(iterable.iterator(), separator); } public static <Type> String toString(Iterable<Type> iterable) { return toString(iterable, " "); } public static long totient(long n) { Set<Long> factors = new SortedSetAVL<>(factors(n), Comparator.naturalOrder()); long result = n; for (long p : factors) { result -= result / p; } return result; } interface BiFunctionResult<Type0, Type1, TypeResult> { TypeResult apply(Type0 x0, Type1 x1, TypeResult x2); } public static void solve() throws IOException { List<Long> primes = streamPrime(1000).limit(1000).collect(Collectors.toList()); Long[] as = primes.toArray(new Long[0]); int t = nextInt(); for (int test = 0; test < t; test++) { int n = nextInt(); for (int i = 0; i < n; i++) { out.print(as[i] + " "); } out.println(); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
fe56e8f6c3fc429020e8d3e19ef7abbe
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; // Solution public class G { public static void main(String[] args) { //~ BufferedReader br = new BufferedReader(new FileReader("input.txt")); //~ PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); //~ FastInput sc = new FastInput(); //~ sc.br = br; FastInput sc = new FastInput(); int T = sc.nextInt(); while(T-->0) { int n = sc.nextInt(); for(int i = 2;i<=n+1;i++) System.out.print(i+" "); System.out.println(); } } } class FastInput { BufferedReader br; StringTokenizer st; public FastInput() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while(st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { System.out.println(e.toString()); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { System.out.println(e.toString()); } return str; } public static void sort(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public int [] readArr(int n) { int [] a = new int[n]; for(int i = 0; i<n; i++) a[i] = nextInt(); return a; } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
643814de310809df43caa3d9dea0aac6
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int T = Integer.parseInt(in.readLine()); for (int ts=1; ts<=T; ts++) { int N = Integer.parseInt(in.readLine()); out.write(1000000 + ""); for (int i=1; i<N; i++) { /*if (i % 2 == 0) { out.write(" " + 3); } else { out.write(" " + 2); }*/ out.write(" " + (1000000 + i)); } out.write("\n"); } in.close(); out.close(); } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
19fc530168ac6ad03a754e94c8a317d3
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for (int i = 0; i < t; i++) { int n=sc.nextInt(); int[] arr=new int[1000]; arr[0]=2; arr[1]=3; int x=4; int j=2; while (j<arr.length) { while(!isprime(x)){ x++; } arr[j]=x; j++; x++; } for (int k = 0; k < n; k++) { System.out.print(arr[k]+" "); } System.out.println(); } } public static boolean isprime(int n) { for (int i = 2; i*i <= n; i++) { if(n%i==0){ return false; } } return true; } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
db11dd12812baf63d2dc60af6c448a5a
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int input = in.nextInt(); for (int i=0; i<input; i++) { int len = in.nextInt(); for (int j=2; j<=len+1; j++) { System.out.print(j); System.out.print(" "); } System.out.println(); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
8430ea95fccbdf1be6afb03a8ffe107f
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.*; import java.util.*; public class Test { public static void main(String[] args) throws IOException { Reader rd = new Reader(); int t = rd.nextInt(); while (t-- > 0) { int n = rd.nextInt(); if (n == 1) System.out.println("1"); else { int i = 2; while (n-- > 0) { System.out.print(i + " "); i += 1; } System.out.println(); } } } /** * method to solve current cp problem */ private static void solve() { } /** * you can ignore the below snippet code as it's just for taking faster input in java */ 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[64]; // 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(); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
02392f3ff030a7e4e677be249e95ca23
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.*; import java.util.*; public class Solution { static class DSU { private int[] parent; private int[] size; private int totalGroup; private int maxSize = 1; public DSU(int n) { parent = new int[n]; totalGroup = n; for (int i = 0; i < n; i++) { parent[i] = i; } size = new int[n]; Arrays.fill(size, 1); } public boolean union(int a, int b) { int parentA = findParent(a); int parentB = findParent(b); if (parentA == parentB) { return false; } totalGroup--; if (parentA < parentB) { this.parent[parentB] = parentA; this.size[parentA] += this.size[parentB]; maxSize = Math.max(this.size[parentA], maxSize); } else { this.parent[parentA] = parentB; this.size[parentB] += this.size[parentA]; maxSize = Math.max(this.size[parentB], maxSize); } return true; } public int findParent(int a) { if (parent[a] != a) { parent[a] = findParent(parent[a]); } return parent[a]; } public int getGroupSize(int a) { return this.size[findParent(a)]; } public int getTotalGroup() { return totalGroup; } } public static void main(String[] args) throws Exception { int tc = io.nextInt(); for (int i = 0; i < tc; i++) { solve(); } io.close(); } private static void solve() throws Exception { int n = io.nextInt(); for (int i = 0; i < n; i++) { io.print((i + 2)); io.print(" "); } io.println(); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(a.length); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } //-----------PrintWriter for faster output--------------------------------- public static FastIO io = new FastIO(); //-----------MyScanner class for faster input---------- static class FastIO extends PrintWriter { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar, numChars; // standard input public FastIO() { this(System.in, System.out); } public FastIO(InputStream i, OutputStream o) { super(o); stream = i; } // file input public FastIO(String i, String o) throws IOException { super(new FileWriter(o)); stream = new FileInputStream(i); } // throws InputMismatchException() if previously detected end of file private int nextByte() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars == -1) return -1; // end of file } return buf[curChar++]; } // to read in entire lines, replace c <= ' ' // with a function that checks whether c is a line break public String next() { int c; do { c = nextByte(); } while (c <= ' '); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > ' '); return res.toString(); } public String nextLine() { int c; do { c = nextByte(); } while (c < '\n'); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > '\n'); return res.toString(); } public int nextInt() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public long nextLong() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } int[] nextInts(int n) { int[] data = new int[n]; for (int i = 0; i < n; i++) { data[i] = io.nextInt(); } return data; } public double nextDouble() { return Double.parseDouble(next()); } } //-------------------------------------------------------- }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
377dd3f56351a55354d7987e293dbf2a
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.*; import java.util.*; public class Main { static void daunism1() { } static void dfs(int v, int w) { } static long gcd(long a, long b) { if (b == 0) return Math.abs(a); return gcd(b, a % b); } public static void main(String[] args) throws IOException { // br = new BufferedReader(new FileReader("pencils.in")); // out = new PrintWriter("pencils.out"); int t = nextInt(); for (int i = 0; i < t; i++) { int n=nextInt(); for (int j = 2; j <= n+1; j++) { out.print(j+" "); } out.println(); } out.close(); } static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer in = new StringTokenizer(""); public static boolean hasNext() throws IOException { if (in.hasMoreTokens()) return true; String s; while ((s = br.readLine()) != null) { in = new StringTokenizer(s); if (in.hasMoreTokens()) return true; } return false; } public static String nextToken() throws IOException { while (!in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public static long nextLong() throws IOException { return Long.parseLong(nextToken()); } public static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } static class Maincraft implements Comparable<Maincraft> { int x; int y; public Maincraft(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Maincraft o) { return Integer.compare(x, o.x); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
48da36e78ff2c289633657af9daa4320
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.Scanner; public class Main { public static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int tc = scanner.nextInt(); while (tc-->0){ int size = scanner.nextInt(); int [] array = new int[size]; for (int i =0;i< array.length;i++){ array[i]=i+2; } for (int i =0;i< array.length;i++){ System.out.print(array[i] + " "); } System.out.println(); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
0ec7f79a5f1640f23c39a0d7542c6300
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
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(); for(int i=2;i<=n+1;i++) { System.out.print(i+" "); } System.out.println(); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
bc8663ca87ef3222d29095326d47bd6f
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.*; public class findarray { public static void main(String [] args) { Scanner in=new Scanner (System.in); int n=in.nextInt(); int ar[] =new int [n]; int j; int i; //int prime[]=new int [100000]; for(i=0;i<n;i++) { ar[i]=in.nextInt(); } for(i=0;i<n;i++) { if(ar[i]==1) { System.out.println("1\n"); } else { for(j=2;j<=ar[i]+1;j++) { System.out.printf("%d ",j); } System.out.printf("\n"); } } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
f21519ee2697a65102992e3dca8422dc
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.Scanner; /** * @author Guy_Z * @date 2021-12-15 17:03 */ public class A_FindArray { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //the number of the test cases int t = scanner.nextInt(); int[][] arrays = new int[t][]; for (int i = 0; i < t; i++) { //length of the array int n = scanner.nextInt(); int[] array = new int[n]; for (int i1 = 0; i1 < n; i1++) { array[i1] = i1 + 2; } arrays[i] = array; } for (int i = 0; i < t; i++) { for (int i1 = 0; i1 < arrays[i].length; i1++) { System.out.print(arrays[i][i1] + " "); } System.out.println(); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
af9c1442e8640b21e8490eeafc8a8a05
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.Scanner; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int noOfInputs = sc.nextInt(); for( int i=0; i< noOfInputs; i++) { int n = sc.nextInt(); for( int j=2; j<=n ; j++) { System.out.print(j+" "); }System.out.println(n+1); } sc.close(); } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
ebde88c9904df7206b541a2b6c306af8
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.*; import java.io.InputStreamReader; import java.math.BigInteger; import java.net.Socket; import java.util.*; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { Scanner cin=new Scanner(System.in); int t =cin.nextInt(); while(t-->0) { int n=cin.nextInt(); for(int i=2;i<=n+1;i++) { System.out.print(i+" "); } System.out.println(); } } public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
541b6dc640d9788e9c8f5b0e82b074f6
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.Scanner; /** * * @author Omar Faruk */ public class FindArray { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while(T-- > 0){ int n = sc.nextInt(); //StringBuilder str = new StringBuilder(); for (int i = 2; i <= n+1; i++) { System.out.print(i+" "); } System.out.println(); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
d73ffeacd8d35b4413be2bab5fda280d
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.Scanner; /** * * @author Omar Faruk */ public class FindArray { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while(T-- > 0){ int n = sc.nextInt(); StringBuilder str = new StringBuilder(); for (int i = 2; i <= n+1; i++) { str.append(i+" "); } System.out.println(str.toString()); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
e9d67712efe07ce1c760ab58acd6662f
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.Scanner; public class MyClass { static Scanner in=new Scanner(System.in); public static void main(String args[]) { int testCases=in.nextInt(); for(int t=0;t<testCases;t++){ int n=in.nextInt(); if(n==1){ System.out.println(1); continue; } int i=2; int time=1; for( i=n;i<=n*2 && time<=n;i++){ System.out.print(i+" "); i++; time++; if(time<=n){ System.out.print(i+" "); } ++time; } System.out.println(); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
94618783e5144763e0cc836fc83164fd
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.Scanner; public class SolnA { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t>0) { t--; int n = sc.nextInt(); if(n==0) return; int[] ans = new int[n]; for(int i=0;i<n;i++) { if(i==0) ans[i]=2; else ans[i]=ans[i-1]+1; } for(int i=0;i<n;i++) { System.out.print(ans[i]+" "); } System.out.println(); } sc.close(); } public static void run() { } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
da9aef970a323e8316faf49766a19239
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { // write your code here Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++) { int n=sc.nextInt(); int g=2; while(n!=0) { System.out.println(g); g+=1; n--; } } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
5c3a6c7cabc782bcb7198107b5889a7a
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class SolutionA { static boolean primes[] = new boolean[8000 + 1]; public static void main(String args[]) { Scanner cin = new Scanner(System.in); int t = cin.nextInt(); primeSieveOpt1(8000); while (t-- > 0) { int n = cin.nextInt(); if (n == 1) { System.out.print(1 + " "); n--; } for (int p = 2; p < primes.length && n > 0; p++) { if (primes[p]) { System.out.print(p + " "); n--; } } System.out.println(); } } private static void primeSieveOpt1(int num) { Arrays.fill(primes, true); primes[0] = primes[1] = false; for (int p = 2; p <= num; p++) { if (isPrime(p)) { for (int mult = p * p; mult <= num; mult += p) { primes[mult] = false; } } } } private static boolean isPrime(int num) { if (num <= 1) return false; if (num == 2 || num == 3) return true; if (num % 2 == 0 || num % 3 == 0) return false; for (int n = 5; n * n <= num; n += 6) { if (num % n == 0 || num % (n + 2) == 0) return false; } return true; } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
4bddd461327be1a91bf9bca95f5abafa
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.util.*; public class FindArray { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); for(int i=1; i<=n ;i++) System.out.print(i+1+" "); System.out.println(); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
f86e216780e2fae358f304f692f86b41
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.*; import java.util.StringTokenizer; /** * Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } public static class Task { private boolean[] notPrime; private void init() { notPrime = new boolean[1000002]; for(int i=2;i<=1000001;i++) { for(int j=2;j*i<=1000001;j++) { notPrime[i*j] = true; } } } private boolean isPrime(Integer num) { return !notPrime[num]; } public void solve(int cnt, InputReader in, PrintWriter out) { cnt = in.nextInt(); for (int c = 0; c < cnt; c++) { int n = in.nextInt(); int x = 2; for(int i=0;i<n;i++) { out.print(x+" "); x = x+1; } out.println(); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
a3889e355c43f304241cf0aa83c39256
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
// package CF.R_758; import java.io.*; import java.util.*; import java.math.*; /** * @Author: merickbao * @Created_Time: 2021-07-10 10:43 * @Description: */ public class Main { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); // code here Task solver = new A(); solver.solve(1, in, out); out.close(); } } class A implements Task{ @Override public void solve(int testNumber, InputReader in, OutputWriter out) { int t = in.readInt(); while (t-- > 0){ int n = in.readInt(); if (n == 1) { System.out.println(1); } else { int[] a = new int[8000]; List<Integer> all = new ArrayList<>(); Arrays.fill(a, 1); for (int i = 2; i < 8000; i++) { if (a[i] == 1) { System.out.print(i + " "); all.add(i); n--; if (n == 0) break; } for (int j = 0; j < all.size() && i * all.get(j) < 8000; j++) { a[i * all.get(j)] = 0; if (i % all.get(j) == 0) { break; } } } } } } } interface Task{ public void solve(int testNumber, InputReader in, OutputWriter out); } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; 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 int peek() { if (numChars == -1) return -1; if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) return -1; } return buf[curChar]; } public int readInt() { 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 readLong() { 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 String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) return readLine(); else return readLine0(); } public BigInteger readBigInteger() { try { return new BigInteger(readString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public double readDouble() { 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, readInt()); 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, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) read(); return value == -1; } public String next() { return readString(); } public InputReader.SpaceCharFilter getFilter() { return filter; } public void setFilter(InputReader.SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(char[] array) { writer.print(array); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) writer.print(' '); writer.print(array[i]); } } public void print(long[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) writer.print(' '); writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void printLine(long[] array) { print(array); writer.println(); } public void printLine() { writer.println(); } public void printLine(Object...objects) { print(objects); writer.println(); } public void print(char i) { writer.print(i); } public void printLine(char i) { writer.println(i); } public void printLine(char[] array) { writer.println(array); } public void printFormat(String format, Object...objects) { writer.printf(format, objects); } public void close() { writer.close(); } public void flush() { writer.flush(); } public void print(long i) { writer.print(i); } public void printLine(long i) { writer.println(i); } public void print(int i) { writer.print(i); } public void printLine(int i) { writer.println(i); } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
cec154d3b94bdea6432a0475551dacf9
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.math.BigInteger; import java.util.*; public class Test { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int T=sc.nextInt(); while(T-->0){ int n=sc.nextInt(); int k=2; for(int i=0;i<n;i++) System.out.print(k++ +" "); System.out.println(); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
e2ebff44bb06cc0ca2c0c9689d4ab647
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.time.LocalDate; import java.util.*; //BigInteger x = new BigInteger(a); //Collections.reverseOrder() public class Main { static FastScanner sc; // static PrintWriter syso; static long mod = 1_000_000_007; static int maxi = Integer.MAX_VALUE; //INF static long maxl = Long.MAX_VALUE; //LINF static int [] M = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; static int [] dx = {1,1,0,-1,-1,-1, 0, 1}; static int [] dy = {0,1,1, 1, 0,-1,-1,-1}; static int [] d4x = {1,-1, 0, 0}; static int [] d4y = {0, 0, 1,-1}; public static void main(String[]args) { sc = new FastScanner(); long testCases=1; testCases=sc.nextInt(); while (testCases-->0) solve(); } public static void solve(){ // int a =sc.nextInt(); // String s = "11"; // for(int i = 0; i<a;i++) { // s += "1"; // System.out.print(s + " "); // } // System.out.println(); int n = sc.nextInt(); if (n == 1) { System.out.println(1); } else { int ans = 2; for(int i = 0; i<n;i++) { System.out.print(ans++ + " "); } System.out.println(); } } static class Pair implements Comparable<Pair> { // Pair [] a = new Pair [n]; // for (int i = 0; i<n;i++) // a[i] = new Pair(sc.nextInt(), sc.nextInt()); int a, b; Pair(int a, int b){ this.a=a; this.b=b; } public int compareTo(Pair o) { return a==o.a?b-o.b:a-o.a; } } static int BinarySearch(int[] nums, int target) { int lo =0; int hi = nums.length-1; while (lo<hi) { int mid =lo+(hi-lo)/2; int value = nums[mid]; if (value > target) { hi = mid; } else if (value < target) { lo = mid+1; } else { return mid; } } return lo; } static void removeDuplicates(int arr[], int n) { if (n == 0 || n == 1) return; int j = 0; for (int i = 0; i < n-1; i++) if (arr[i] != arr[i+1]) arr[j++] = arr[i]; arr[j++] = arr[n-1]; return; } static void primeset(HashSet<Integer> set,int n){ if(n%2==0) set.add(2); while (n % 2 == 0) n /= 2; for (int i = 3; i <= (n); i += 2) { while (n % i == 0) { set.add(i); n /= i; } } } static public int missingNumber(int[] nums) { int len=nums.length; int sum = 0; for(int i=0;i<len;i++){ sum+=nums[i]; } return (((len*(len+1))/2)-sum);//Using maths AP Formula we can easily find the missing number. } static boolean isSquare(int n){ return Math.ceil(Math.sqrt(n))==Math.floor(Math.sqrt(n)) ? true:false; } static int getSum(int n){ int res=0; while(n>0){ res+=(n%10); n/=10; } return res; } static int gcd(int a,int b) { if(a==0)return b; else return gcd(b%a,a); } static long Lgcd(long a,long b) { if(a==0)return b; else return Lgcd(b%a,a); } static boolean isPrime(int n) { if (n == 2) return true; else if ((n % 2 == 0 && n > 2) || n < 2) return false; else { for (int i = 3; i <= (int) Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } } // static class Dijkstra { // static long[] get(List<Edge>[] edges, int start) { // long[] ret = new long[edges.length]; // Arrays.fill(ret, Long.MAX_VALUE); // ret[start] = 0; // PriorityQueue<Edge> pq = new PriorityQueue<Edge>((s1, s2)->Long.compare(s1.cost, s2.cost)); // pq.add(new Edge(start, 0)); // // while (!pq.isEmpty()) { // Edge now = pq.poll(); // if (ret[now.to] < now.cost) continue; // for (Edge e : edges[now.to]) { // if (e.cost != Long.MAX_VALUE && ret[e.to] > ret[now.to] + e.cost) { // ret[e.to] = ret[now.to] + e.cost; // pq.add(new Edge(e.to, ret[e.to])); // } // } // } // return ret; // } // } 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 { InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public char[] nextCA() { return next().toCharArray(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIA(int N) { int[] a = new int[N]; for (int i = 0; i < N; i++) a[i] = this.nextInt(); return a; } public int[] nextIA(int N, boolean flag) { int[] a = new int[N]; for (int i = 0; i < N; i++) a[i] = this.nextInt()-1; return a; } public long[] nextLA(int N) { long[] a = new long[N]; for (int i = 0; i < N; i++) a[i] = this.nextLong(); return a; } public double[] nextDA(int N) { double[] a = new double[N]; for (int i = 0; i < N; i++) a[i] = this.nextDouble(); return a; } public boolean[][] bgrid(int H, int W, char c) { boolean[][] a = new boolean[H][W]; for (int i = 0; i < H; i++) { char[] s = this.nextCA(); for (int j = 0; j < W; j++) a[i][j] = s[j] == c; } return a; } public long[][] nextLongMatrix(int height, int width) { long[][] mat = new long[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width) { int[][] mat = new int[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width) { double[][] mat = new double[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width) { char[][] mat = new char[height][width]; for (int h = 0; h < height; h++) { String s = this.next(); for (int w = 0; w < width; w++) { mat[h][w] = s.charAt(w); } } return mat; } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
67ec3932ec60dc9a5ae308db6cf3151e
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.*; import java.util.*; public class A_Find_Array { 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 f=new FastReader(); StringBuilder sb=new StringBuilder(); int t = f.nextInt(); while(t-->0){ int r=f.nextInt(); int h=2; for(int i=0;i<r;i++){ sb.append(h+" "); h++; } sb.append("\n"); } System.out.println(sb); } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output
PASSED
1a7643fa6d0966412a7a31f1c3c2663e
train_109.jsonl
1639217100
Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 &lt; a_2 &lt; \ldots &lt;a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Codeforces { 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()); for (int i = 2; i <= n + 1; i++) { System.out.print(i + " "); } System.out.println(); } } }
Java
["3\n1\n2\n7"]
1 second
["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"]
NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2&lt;3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.
Java 8
standard input
[ "constructive algorithms", "math" ]
76bfced1345f871832957a65e2a660f8
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
800
For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.
standard output