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
dde898d9fcfbea473052389e2304cd32
train_003.jsonl
1563636900
Recall that string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero or all) characters. For example, for the string $$$a$$$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w": "vvvv" "vvvv" "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows: "vvvovvv" "vvvovvv" "vvvovvv" "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing "w" with "vv". For example, $$$s$$$ can be equal to "vov".
256 megabytes
import java.util.*; import java.io.*; public class Main implements Runnable{ public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); Solve solver = new Solve(); solver.solve(1, in, out); out.close(); } public static void main(String[] args) { new Thread(null,new Main(),"Main",1<<24).start(); } static class Solve { public void solve(int testNumber, InputReader in, OutputWriter out) { int[] preCnt = new int[1000000]; int[] postCnt = new int[1000000]; String n = in.next(); for (int i = 1; i < n.length(); i++) { preCnt[i] = preCnt[i - 1]; if (n.charAt(i) == 'v' && n.charAt(i-1) == 'v') { preCnt[i]++; } } for (int i = n.length() - 2; i >= 0; i--) { postCnt[i] = postCnt[i + 1]; if (n.charAt(i) == 'v' && n.charAt(i+1) == 'v') { postCnt[i]++; } } long total = 0; for (int i = 0; i < n.length(); i++) { if (n.charAt(i) == 'o') { total += (long)preCnt[i] * postCnt[i]; } } out.println(total); } } 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 close() { writer.close(); } public void println(String string) { writer.println(string); } public void println(int number) { writer.println(number); } public void println(long number) { writer.println(number); } public void println(double number) { writer.println(number); } public void print(long number) { writer.print(number); } public void print(int number) { writer.print(number); } public void print(String string) { writer.print(string); } public void print(double number) { writer.print(number); } } static class InputReader { private InputStream in; private byte[] buffer = new byte[1024]; private int curbuf; private int lenbuf; public InputReader(InputStream in) { this.in = in; this.curbuf = this.lenbuf = 0; } public String nextLine() { StringBuilder sb = new StringBuilder(); int b = readByte(); while (b!=10) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public boolean hasNextByte() { if (curbuf >= lenbuf) { curbuf = 0; try { lenbuf = in.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return false; } return true; } private int readByte() { if (hasNextByte()) return buffer[curbuf++]; else return -1; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private void skip() { while (hasNextByte() && isSpaceChar(buffer[curbuf])) curbuf++; } public boolean hasNext() { skip(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[][] nextCharMap(int n, int m) { char[][] map = new char[n][m]; for (int i = 0; i < n; i++) map[i] = next().toCharArray(); return map; } } }
Java
["vvvovvv", "vvovooovovvovoovoovvvvovovvvov"]
1 second
["4", "100"]
NoteThe first example is explained in the legend.
Java 8
standard input
[ "dp", "strings" ]
6cc6db6f426bb1bce59f23bfcb762b08
The input contains a single non-empty string $$$s$$$, consisting only of characters "v" and "o". The length of $$$s$$$ is at most $$$10^6$$$.
1,300
Output a single integer, the wow factor of $$$s$$$.
standard output
PASSED
294b139777e8c631b13c4ac8686b47e9
train_003.jsonl
1563636900
Recall that string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero or all) characters. For example, for the string $$$a$$$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w": "vvvv" "vvvv" "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows: "vvvovvv" "vvvovvv" "vvvovvv" "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing "w" with "vv". For example, $$$s$$$ can be equal to "vov".
256 megabytes
import java.io.*; import java.util.*; public class vk18 { public static void main(String[]st) throws Exception { //Scanner scan=new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s; s=br.readLine(); int i=0; long w=0,o=0,wow=0; for(char c:s.toCharArray()) { if(c=='v') { if( i>0 && s.charAt(i-1)=='v' ) {w++; wow+=o;} } else o=o+w; i++; } System.out.println(wow); } }
Java
["vvvovvv", "vvovooovovvovoovoovvvvovovvvov"]
1 second
["4", "100"]
NoteThe first example is explained in the legend.
Java 8
standard input
[ "dp", "strings" ]
6cc6db6f426bb1bce59f23bfcb762b08
The input contains a single non-empty string $$$s$$$, consisting only of characters "v" and "o". The length of $$$s$$$ is at most $$$10^6$$$.
1,300
Output a single integer, the wow factor of $$$s$$$.
standard output
PASSED
dca264a7f49cf539cf4a4d4a322b0076
train_003.jsonl
1563636900
Recall that string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero or all) characters. For example, for the string $$$a$$$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w": "vvvv" "vvvv" "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows: "vvvovvv" "vvvovvv" "vvvovvv" "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing "w" with "vv". For example, $$$s$$$ can be equal to "vov".
256 megabytes
/* Aman Agarwal algo.java */ import java.util.*; import java.io.*; public class B1178 { 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 FastReader sc = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static int[][] nai2(int n,int m)throws IOException{int a[][] = new int[n][m];for(int i=0;i<n;i++)for(int j=0;j<m;j++)a[i][j] = ni();return a;} static int[] nai(int N,int start)throws IOException{int[]A=new int[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;} static Integer[] naI(int N,int start)throws IOException{Integer[]A=new Integer[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;} static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;} static char[] nac()throws IOException{char[]A=sc.next().toCharArray();return A;} static void print(int arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();} static void print(long arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} // return the number of set bits. static boolean isPrime(int number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}int sqrt = (int) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;} static boolean isPrime(long number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}long sqrt = (long) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;} static int power(int n,int p){if(p==0)return 1;int a = power(n,p/2);a = a*a;int b = p & 1;if(b!=0){a = n*a;}return a;} static long power(long n,long p){if(p==0)return 1;long a = power(n,p/2);a = a*a;long b = p & 1;if(b!=0){a = n*a;}return a;} static void reverse(int[] a) {int b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}} static void reverse(long[] a) {long b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}} static void swap(int a[],int i,int j){a[i] = a[i] ^ a[j];a[j] = a[j] ^ a[i];a[i] = a[i] ^ a[j];} static void swap(long a[],int i,int j){a[i] = a[i] ^ a[j];a[j] = a[j] ^ a[i];a[i] = a[i] ^ a[j];} static int count(int n){int c=0;while(n>0){c++;n = n/10;}return c;} static int[] prefix_sum(int a[],int n){int s[] = new int[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;} static long[] prefix_sum_int(int a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;} static long[] prefix_sum_Integer(Integer a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;} static long[] prefix_sum_long(long a[],int n){long s[] = new long[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;} static boolean isPerfectSquare(double x){double sr = Math.sqrt(x);return ((sr - Math.floor(sr)) == 0);} static ArrayList<Integer> sieve(int n) {int k=0; boolean prime[] = new boolean[n+1];ArrayList<Integer> p_arr = new ArrayList<>();for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ k=p;if(prime[p] == true) { p_arr.add(p);for(int i = p*2; i <= n; i += p) prime[i] = false; } }for(int i = k+1;i<=n;i++){if(prime[i]==true)p_arr.add(i);}return p_arr;} static boolean[] seive_check(int n) {boolean prime[] = new boolean[n+1];for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ if(prime[p] == true) { for(int i = p*2; i <= n; i += p) prime[i] = false; } }prime[1]=false;return prime;} static int get_bits(int n){int p=0;while(n>0){p++;n = n>>1;}return p;} static int get_bits(long n){int p=0;while(n>0){p++;n = n>>1;}return p;} static int get_2_power(int n){if((n & (n-1))==0)return get_bits(n)-1;return -1;} static int get_2_power(long n){if((n & (n-1))==0)return get_bits(n)-1;return -1;} //gives the divisors of first n natural numbers in n*sqrt(n) time.. static ArrayList<ArrayList<Integer>> divisors_n(int n){int i,j;ArrayList<ArrayList<Integer>> ans = new ArrayList<>();ArrayList<Integer> arr = new ArrayList<>();arr.add(1);ans.add(arr);for (i = 2; i <= n; i++){arr = new ArrayList<>();for (j = 1; j * j <= i; j++){if (i % j == 0){arr.add(j);if (i / j != j)arr.add(i/j);}}ans.add(arr);} return ans;} //gives divisors of a particular number in sqrt(n) time.. static ArrayList<Integer> divisors_1(int n){ArrayList<Integer> ans = new ArrayList<>();for (int i=1; i<=Math.sqrt(n); i++){if (n%i==0){if (n/i == i)ans.add(i);else{ans.add(i);ans.add(n/i);}}}return ans;} static void close(){out.flush();} /*-------------------------Main Code Starts(algo.java)----------------------------------*/ public static void solve() throws IOException { char s[] = sc.next().toCharArray(); long a=0,b=0,c=0; for(int i=1;i<s.length;i++) { if(s[i]=='o') b = b+a; else if(s[i-1]=='v') { a++; c = c + b; } } out.println(c); } public static void main(String[] args) throws IOException { int test = 1; //test = sc.nextInt(); while(test-- > 0) { solve(); } close(); } }
Java
["vvvovvv", "vvovooovovvovoovoovvvvovovvvov"]
1 second
["4", "100"]
NoteThe first example is explained in the legend.
Java 8
standard input
[ "dp", "strings" ]
6cc6db6f426bb1bce59f23bfcb762b08
The input contains a single non-empty string $$$s$$$, consisting only of characters "v" and "o". The length of $$$s$$$ is at most $$$10^6$$$.
1,300
Output a single integer, the wow factor of $$$s$$$.
standard output
PASSED
85b58c184384835cc4ab6d74f8102f49
train_003.jsonl
1563636900
Recall that string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero or all) characters. For example, for the string $$$a$$$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w": "vvvv" "vvvv" "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows: "vvvovvv" "vvvovvv" "vvvovvv" "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing "w" with "vv". For example, $$$s$$$ can be equal to "vov".
256 megabytes
import java.io.PrintWriter; import java.util.LinkedList; import java.util.List; import java.util.Scanner; /** * */ /** * * https://codeforces.com/contest/1178/problem/B * * @author Maksim * */ public class WOWFactor { /** * @param args */ public static void main(String[] args) { final Scanner in = new Scanner(System.in); final PrintWriter out = new PrintWriter(System.out); final String input = in.nextLine(); final Solution solution = new Solution(); out.println(solution.solution(input.toCharArray())); out.close(); in.close(); } public static class Solution { public Long solution(char chars[]) { long countWoW = 0; int from = 0; final List<CharRange> charRanges = new LinkedList<>(); int prefixWSum = 0; int suffixWSum = 0; while (from < chars.length) { // count w { final int count = countUntilNextChar(from, chars, 'v'); if (count != 0) { final int wCount = count - 1; suffixWSum += wCount; final CharRange range = new CharRange(from, from + count, wCount, 'w'); from += count; charRanges.add(range); } } // count o { final int countO = countUntilNextChar(from, chars, 'o'); final CharRange range = new CharRange(from, from + countO, countO, 'o'); charRanges.add(range); from += countO; } } for (final CharRange charRange : charRanges) { if (charRange.c == 'o') { countWoW += 1L*prefixWSum * charRange.count * suffixWSum; } if (charRange.c == 'w') { prefixWSum += charRange.count; suffixWSum -= charRange.count; } } return countWoW; } private int countUntilNextChar(int from, char[] where, char c) { int index = from; while (where.length > index && where[index] == c) { index++; } return index - from; } } private static class CharRange { final int from; final int to; final char c; final int count; /** * * @param from * @param to * @param c * @param count */ public CharRange(int from, int to, int count, char c) { this.from = from; this.to = to; this.c = c; this.count = count; } @Override public String toString() { return "CharRange [c=" + c + ", count=" + count + "]"; } } }
Java
["vvvovvv", "vvovooovovvovoovoovvvvovovvvov"]
1 second
["4", "100"]
NoteThe first example is explained in the legend.
Java 8
standard input
[ "dp", "strings" ]
6cc6db6f426bb1bce59f23bfcb762b08
The input contains a single non-empty string $$$s$$$, consisting only of characters "v" and "o". The length of $$$s$$$ is at most $$$10^6$$$.
1,300
Output a single integer, the wow factor of $$$s$$$.
standard output
PASSED
60a6f5d80c540cad89b9cf4984700c18
train_003.jsonl
1563636900
Recall that string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero or all) characters. For example, for the string $$$a$$$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w": "vvvv" "vvvv" "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows: "vvvovvv" "vvvovvv" "vvvovvv" "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing "w" with "vv". For example, $$$s$$$ can be equal to "vov".
256 megabytes
import java.util.*; import java.io.*; public class CFQB { public static void main(String args[]) throws Exception { Scan scan = new Scan(); Print print = new Print(); String string=scan.scanString(); int N=string.length(); long arr[] = new long[N]; for(int i=1;i<N;i++) { arr[i]=arr[i-1]; if(string.charAt(i)=='v' && string.charAt(i-1)=='v') { arr[i]++; } } long ans=0; long temp=0; for(int i=2;i<N-2;i++) { if(string.charAt(i)=='o') { temp=(arr[i-1])*(arr[N-1]-arr[i]); ans+=Math.max(temp,0); } } print.println(ans); print.close(); } static class Print { private final BufferedWriter bw; public Print() { 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 Scan { private byte[] buf = new byte[1024 * 1024 * 4]; private int index; private InputStream in; private int total; public Scan() { in = System.in; } public int scan() throws IOException { if (total < 0) throw new InputMismatchException(); if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) return -1; } return buf[index++]; } public int scanInt() throws IOException { int integer = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } else throw new InputMismatchException(); } return neg * integer; } public double scanDouble() throws IOException { double doub = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n) && n != '.') { if (n >= '0' && n <= '9') { doub *= 10; doub += n - '0'; n = scan(); } else throw new InputMismatchException(); } if (n == '.') { n = scan(); double temp = 1; while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { temp /= 10; doub += (n - '0') * temp; n = scan(); } else throw new InputMismatchException(); } } return doub * neg; } public long scanLong() throws IOException { long ret = 0; long c = scan(); while (c <= ' ') { c = scan(); } boolean neg = (c == '-'); if (neg) { c = scan(); } do { ret = ret * 10 + c - '0'; } while ((c = scan()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public String scanString() throws IOException { StringBuilder sb = new StringBuilder(); int n = scan(); while (isWhiteSpace(n)) n = scan(); while (!isWhiteSpace(n) || n == ' ') { sb.append((char) n); n = scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; return false; } } }
Java
["vvvovvv", "vvovooovovvovoovoovvvvovovvvov"]
1 second
["4", "100"]
NoteThe first example is explained in the legend.
Java 8
standard input
[ "dp", "strings" ]
6cc6db6f426bb1bce59f23bfcb762b08
The input contains a single non-empty string $$$s$$$, consisting only of characters "v" and "o". The length of $$$s$$$ is at most $$$10^6$$$.
1,300
Output a single integer, the wow factor of $$$s$$$.
standard output
PASSED
42f334f5780cfd5ae17ed1fca1a0fae2
train_003.jsonl
1563636900
Recall that string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero or all) characters. For example, for the string $$$a$$$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w": "vvvv" "vvvv" "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows: "vvvovvv" "vvvovvv" "vvvovvv" "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing "w" with "vv". For example, $$$s$$$ can be equal to "vov".
256 megabytes
import java.util.*; import java.io.*; public class CFQB { public static void main(String args[]) throws Exception { Scan scan = new Scan(); Print print = new Print(); String string=scan.scanString(); int N=string.length(); long arr[] = new long[N]; for(int i=1;i<N;i++) { arr[i]=arr[i-1]; if(string.charAt(i)=='v' && string.charAt(i-1)=='v') { arr[i]++; } } long ans=0; for(int i=1;i<N-1;i++) { if(string.charAt(i)=='o') { ans+=(arr[i-1])*(arr[N-1]-arr[i]); } } print.println(ans); print.close(); } static class Print { private final BufferedWriter bw; public Print() { 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 Scan { private byte[] buf = new byte[1024 * 1024 * 4]; private int index; private InputStream in; private int total; public Scan() { in = System.in; } public int scan() throws IOException { if (total < 0) throw new InputMismatchException(); if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) return -1; } return buf[index++]; } public int scanInt() throws IOException { int integer = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } else throw new InputMismatchException(); } return neg * integer; } public double scanDouble() throws IOException { double doub = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n) && n != '.') { if (n >= '0' && n <= '9') { doub *= 10; doub += n - '0'; n = scan(); } else throw new InputMismatchException(); } if (n == '.') { n = scan(); double temp = 1; while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { temp /= 10; doub += (n - '0') * temp; n = scan(); } else throw new InputMismatchException(); } } return doub * neg; } public long scanLong() throws IOException { long ret = 0; long c = scan(); while (c <= ' ') { c = scan(); } boolean neg = (c == '-'); if (neg) { c = scan(); } do { ret = ret * 10 + c - '0'; } while ((c = scan()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public String scanString() throws IOException { StringBuilder sb = new StringBuilder(); int n = scan(); while (isWhiteSpace(n)) n = scan(); while (!isWhiteSpace(n) || n == ' ') { sb.append((char) n); n = scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; return false; } } }
Java
["vvvovvv", "vvovooovovvovoovoovvvvovovvvov"]
1 second
["4", "100"]
NoteThe first example is explained in the legend.
Java 8
standard input
[ "dp", "strings" ]
6cc6db6f426bb1bce59f23bfcb762b08
The input contains a single non-empty string $$$s$$$, consisting only of characters "v" and "o". The length of $$$s$$$ is at most $$$10^6$$$.
1,300
Output a single integer, the wow factor of $$$s$$$.
standard output
PASSED
a600f0bfc7e87552a8d665c39a053780
train_003.jsonl
1563636900
Recall that string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero or all) characters. For example, for the string $$$a$$$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w": "vvvv" "vvvv" "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows: "vvvovvv" "vvvovvv" "vvvovvv" "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing "w" with "vv". For example, $$$s$$$ can be equal to "vov".
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.*; public class GUESSPRM { 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 sc = new FastReader(); String a = sc.nextLine(); if (a.length() <= 2) { System.out.println(0); return; } long leftCount = 0; long rightCount = 0; long totalCount = 0; for (int i = a.length()-1; i > 0 ; i--) { if (a.charAt(i) == 'v' && a.charAt(i-1) == 'v') { rightCount++; } } for (int i = 0; i < a.length()-1; i++) { if (a.charAt(i) == 'v' && a.charAt(i+1) == 'v') { leftCount++; rightCount--; } else if(a.charAt(i) == 'o'){ totalCount += (rightCount * leftCount); } } System.out.println(totalCount); } }
Java
["vvvovvv", "vvovooovovvovoovoovvvvovovvvov"]
1 second
["4", "100"]
NoteThe first example is explained in the legend.
Java 8
standard input
[ "dp", "strings" ]
6cc6db6f426bb1bce59f23bfcb762b08
The input contains a single non-empty string $$$s$$$, consisting only of characters "v" and "o". The length of $$$s$$$ is at most $$$10^6$$$.
1,300
Output a single integer, the wow factor of $$$s$$$.
standard output
PASSED
aab42d414d900a71e61c727dabbb96fe
train_003.jsonl
1563636900
Recall that string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero or all) characters. For example, for the string $$$a$$$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w": "vvvv" "vvvv" "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows: "vvvovvv" "vvvovvv" "vvvovvv" "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing "w" with "vv". For example, $$$s$$$ can be equal to "vov".
256 megabytes
import java.util.Scanner; public class wowFactor { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); String in = scan.next(); long totalWs=0; for(int i=0;i<in.length()-1;i++){ if(in.charAt(i)=='v'&&in.charAt(i+1)=='v') totalWs++; } long curWs=0; long prod=0; for(int i=0;i<in.length()-1;i++){ if(in.charAt(i)=='v'&&in.charAt(i+1)=='v'){ totalWs--; curWs++; } if(in.charAt(i)=='o'){ prod+=curWs*totalWs; } } System.out.println(prod); } }
Java
["vvvovvv", "vvovooovovvovoovoovvvvovovvvov"]
1 second
["4", "100"]
NoteThe first example is explained in the legend.
Java 8
standard input
[ "dp", "strings" ]
6cc6db6f426bb1bce59f23bfcb762b08
The input contains a single non-empty string $$$s$$$, consisting only of characters "v" and "o". The length of $$$s$$$ is at most $$$10^6$$$.
1,300
Output a single integer, the wow factor of $$$s$$$.
standard output
PASSED
ad04e4ac4d1b539b3ecf9c6412c64185
train_003.jsonl
1563636900
Recall that string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero or all) characters. For example, for the string $$$a$$$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w": "vvvv" "vvvv" "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows: "vvvovvv" "vvvovvv" "vvvovvv" "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing "w" with "vv". For example, $$$s$$$ can be equal to "vov".
256 megabytes
import java.util.Scanner; public class WOWfactor { public static void main(String args[]) { Scanner in = new Scanner(System.in); String s =in.nextLine(); int n = s.length(); long a=0,b=0,c = 0 ; for (int i = 1; i < s.length(); ++i) { if (s.charAt(i) == 'o') { b += a; } else if (i > 0 && s.charAt(i-1) == 'v') { a +=1; c += b; } } System.out.println(c); } }
Java
["vvvovvv", "vvovooovovvovoovoovvvvovovvvov"]
1 second
["4", "100"]
NoteThe first example is explained in the legend.
Java 8
standard input
[ "dp", "strings" ]
6cc6db6f426bb1bce59f23bfcb762b08
The input contains a single non-empty string $$$s$$$, consisting only of characters "v" and "o". The length of $$$s$$$ is at most $$$10^6$$$.
1,300
Output a single integer, the wow factor of $$$s$$$.
standard output
PASSED
38bef5e42b5a214587337816c5f37317
train_003.jsonl
1563636900
Recall that string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero or all) characters. For example, for the string $$$a$$$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w": "vvvv" "vvvv" "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows: "vvvovvv" "vvvovvv" "vvvovvv" "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing "w" with "vv". For example, $$$s$$$ can be equal to "vov".
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Tarek */ 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); BWOWFactor solver = new BWOWFactor(); solver.solve(1, in, out); out.close(); } static class BWOWFactor { public void solve(int testNumber, InputReader in, PrintWriter out) { String k = in.next(); char[] s = k.toCharArray(); long a = 0, b = 0, c = 0; for (int i = 0; i < s.length; i++) { if (s[i] == 'o') { b += a; } else if (i > 0 && s[i - 1] == 'v') { a++; c += b; } } out.println(c); } } 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(); } } }
Java
["vvvovvv", "vvovooovovvovoovoovvvvovovvvov"]
1 second
["4", "100"]
NoteThe first example is explained in the legend.
Java 8
standard input
[ "dp", "strings" ]
6cc6db6f426bb1bce59f23bfcb762b08
The input contains a single non-empty string $$$s$$$, consisting only of characters "v" and "o". The length of $$$s$$$ is at most $$$10^6$$$.
1,300
Output a single integer, the wow factor of $$$s$$$.
standard output
PASSED
5bd45a178a57c7645d39b52f212bf944
train_003.jsonl
1563636900
Recall that string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero or all) characters. For example, for the string $$$a$$$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w": "vvvv" "vvvv" "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows: "vvvovvv" "vvvovvv" "vvvovvv" "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing "w" with "vv". For example, $$$s$$$ can be equal to "vov".
256 megabytes
import java.util.*; public class div573{ public static void main(String sp[]){ Scanner sc = new Scanner(System.in); String st = sc.next(); int n = st.length(); ArrayList<Long> left = new ArrayList<>(); ArrayList<Long> right = new ArrayList<>(); for(int i=0;i<n;i++){ left.add(0l); right.add(0l); } for(int i=1;i<n;i++){ if(st.charAt(i)==st.charAt(i-1) && st.charAt(i)=='v') left.set(i,left.get(i)+1l); } for(int i=1;i<n;i++){ left.set(i,left.get(i)+left.get(i-1)); } for(int i=n-2;i>=0;i--){ if(st.charAt(i)==st.charAt(i+1) && st.charAt(i)=='v') right.set(i,right.get(i)+1l); } for(int i=n-2;i>=0;i--){ right.set(i,right.get(i)+right.get(i+1)); } long total=0; for(int i=0;i<n;i++){ if(st.charAt(i)=='o'){ total+=(left.get(i)*right.get(i)); } }System.out.println(total); } }
Java
["vvvovvv", "vvovooovovvovoovoovvvvovovvvov"]
1 second
["4", "100"]
NoteThe first example is explained in the legend.
Java 8
standard input
[ "dp", "strings" ]
6cc6db6f426bb1bce59f23bfcb762b08
The input contains a single non-empty string $$$s$$$, consisting only of characters "v" and "o". The length of $$$s$$$ is at most $$$10^6$$$.
1,300
Output a single integer, the wow factor of $$$s$$$.
standard output
PASSED
58d7efd4f164fb5c2186b9ee2e88e296
train_003.jsonl
1563636900
Recall that string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero or all) characters. For example, for the string $$$a$$$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w": "vvvv" "vvvv" "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows: "vvvovvv" "vvvovvv" "vvvovvv" "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing "w" with "vv". For example, $$$s$$$ can be equal to "vov".
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); ArrayList<Long> arr = new ArrayList<Long>(); long ans=0,total=0, cont=0, atual=0; char last=' '; for(char c:s.toCharArray()) { if(c=='v') { if(last=='o') { atual=1; }else atual++; last='v'; }else { if(last=='o') { arr.add(cont); }else { cont+=Math.max(0, atual-1); arr.add(cont); total+=Math.max(0, atual-1); last='o'; } } } if(s.charAt(s.length()-1)=='v') total+=atual-1; for(Long l:arr) { ans+=l*(total-l); } System.out.println(ans); } }
Java
["vvvovvv", "vvovooovovvovoovoovvvvovovvvov"]
1 second
["4", "100"]
NoteThe first example is explained in the legend.
Java 8
standard input
[ "dp", "strings" ]
6cc6db6f426bb1bce59f23bfcb762b08
The input contains a single non-empty string $$$s$$$, consisting only of characters "v" and "o". The length of $$$s$$$ is at most $$$10^6$$$.
1,300
Output a single integer, the wow factor of $$$s$$$.
standard output
PASSED
ae3c301d706ea8c1e6bb2f6b695dbd6d
train_003.jsonl
1556289300
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence $$$a$$$ consisting of $$$n$$$ integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence $$$[1, 2, 4, 3, 2]$$$ the answer is $$$4$$$ (you take $$$1$$$ and the sequence becomes $$$[2, 4, 3, 2]$$$, then you take the rightmost element $$$2$$$ and the sequence becomes $$$[2, 4, 3]$$$, then you take $$$3$$$ and the sequence becomes $$$[2, 4]$$$ and then you take $$$4$$$ and the sequence becomes $$$[2]$$$, the obtained increasing sequence is $$$[1, 2, 3, 4]$$$).
256 megabytes
import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class p1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = in.nextInt(); } int cur = 0; int l = 0; int r = n-1; int count = 0; boolean lflag = false; boolean rflag = false; StringBuilder path = new StringBuilder(); while ((cur < array[l] || cur < array[r]) && r >= l) { int checkval = check(cur, l, r, array); if (l == r) { count++; path.append('L'); break; } if (lflag || rflag) { break; } if (checkval == 0) { cur = array[l]; l++; path.append('L'); } else if (checkval == 1) { cur = array[r]; r--; path.append('R'); } else { int choose = runSim(cur, l, r, array); if (choose == 0) { cur = array[l]; l++; path.append('L'); lflag = true; } else { cur = array[r]; r--; path.append('R'); rflag = true; } } count++; } if (lflag) { while (cur < array[l] && l < r) { cur = array[l]; l++; path.append('L'); count++; } } else if (rflag) { while (cur < array[r] && r > l) { cur = array[r]; r--; path.append('R'); count++; } } System.out.println(count); System.out.println(path); } public static int check(int cur, int l, int r, int[] array) { if (cur < array[l] && cur < array[r]) { if (array[l] < array[r]) { return 0; } else if (array[r] < array[l]) { return 1; } } else if (cur < array[l]) { return 0; } else if (cur < array[r]) { return 1; } return 2; } public static int runSim(int cur, int l, int r, int[] array) { int tl = l, tr = r, tcur = cur; int lcount = 0, rcount = 0; while (tcur < array[tl] && tl < tr) { tcur = array[tl]; tl++; lcount++; } tcur = cur; tl = l; tr = r; while (tcur < array[tr] && tr > tl) { tcur = array[tr]; tr--; rcount++; } return lcount > rcount ? 0 : 1; } }
Java
["5\n1 2 4 3 2", "7\n1 3 5 6 5 4 2", "3\n2 2 2", "4\n1 2 4 3"]
2 seconds
["4\nLRRR", "6\nLRLRRR", "1\nR", "4\nLLRR"]
NoteThe first example is described in the problem statement.
Java 8
standard input
[ "greedy" ]
1ae1e051b6c8240eb27c64ae05c342cf
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.
1,700
In the first line of the output print $$$k$$$ — the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.
standard output
PASSED
3dc3e723105af598b4315cb7aa5ff9e5
train_003.jsonl
1556289300
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence $$$a$$$ consisting of $$$n$$$ integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence $$$[1, 2, 4, 3, 2]$$$ the answer is $$$4$$$ (you take $$$1$$$ and the sequence becomes $$$[2, 4, 3, 2]$$$, then you take the rightmost element $$$2$$$ and the sequence becomes $$$[2, 4, 3]$$$, then you take $$$3$$$ and the sequence becomes $$$[2, 4]$$$ and then you take $$$4$$$ and the sequence becomes $$$[2]$$$, the obtained increasing sequence is $$$[1, 2, 3, 4]$$$).
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Solution{ public static void incSub(int[] a, int n){ OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); StringBuffer sb = new StringBuffer(); int low = 0, high = n-1; int max = 0; while(low < high){ if( (a[low] < a[high] && a[low] > max) || (a[low] > max && a[high] <= max)){ sb.append("L"); max = a[low++]; } else if( (a[high] < a[low] && a[high] > max) || (a[high] > max && a[low] <= max)){ sb.append("R"); max = a[high--]; } else break; } if(a[low] == a[high] && a[low]> max && high-low > 0){ find(sb,a,low,high,max); } if(high-low == 0 && max < a[low]) sb.append("R"); out.println(sb.length()); out.println(new String(sb)); out.close(); } static void find(StringBuffer sb, int[] a, int low, int high,int max) { int l = low; if(a[l] > max){ while(a[l] < a[l+1]) l++; } int r = high; if(a[r] > max){ while(a[r] < a[r-1]) r--; } if(l - low >= high - r){ sb.append("L"); int t = l - low; while(t > 0){ sb.append("L"); t--; } } else{ sb.append("R"); int t = high - r; while(t > 0){ sb.append("R"); t--; } } } public static void main (String[] args) throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int[] a = new int[n]; st = new StringTokenizer(br.readLine()); for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken()); incSub(a,n); } }
Java
["5\n1 2 4 3 2", "7\n1 3 5 6 5 4 2", "3\n2 2 2", "4\n1 2 4 3"]
2 seconds
["4\nLRRR", "6\nLRLRRR", "1\nR", "4\nLLRR"]
NoteThe first example is described in the problem statement.
Java 8
standard input
[ "greedy" ]
1ae1e051b6c8240eb27c64ae05c342cf
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.
1,700
In the first line of the output print $$$k$$$ — the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.
standard output
PASSED
b0d176e3c9c525435de08c649bd62821
train_003.jsonl
1556289300
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence $$$a$$$ consisting of $$$n$$$ integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence $$$[1, 2, 4, 3, 2]$$$ the answer is $$$4$$$ (you take $$$1$$$ and the sequence becomes $$$[2, 4, 3, 2]$$$, then you take the rightmost element $$$2$$$ and the sequence becomes $$$[2, 4, 3]$$$, then you take $$$3$$$ and the sequence becomes $$$[2, 4]$$$ and then you take $$$4$$$ and the sequence becomes $$$[2]$$$, the obtained increasing sequence is $$$[1, 2, 3, 4]$$$).
256 megabytes
import java.util.*; public class C2 { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int l=0; int r=n-1; int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } ArrayList<Integer> al=new ArrayList<Integer>(); int c=0; StringBuilder sb=new StringBuilder(); while(l<=r) { int f1=0; int f2=0; //System.out.println(f1+" "+f2); if(al.size()==0) { f1=1; f2=1; } if(al.size()!=0 && al.get(al.size()-1) < arr[l]) { f1 = 1; } if(al.size()!=0 && al.get(al.size()-1) < arr[r]) { f2 = 1; } if((al.size() != 0 && al.get(al.size()-1) < arr[l] && al.get(al.size()-1) < arr[r] && arr[l]==arr[r])||al.size() == 0 && arr[l]==arr[r]) { int w1=1; int w2=1; for(int i=l+1;i<=r;i++) { if(arr[i]>arr[i-1]) { //System.out.println(i); w1++; } else break; } for(int i = r - 1; i >= l; i--) { if(arr[i] > arr[i+1]) { w2++; } else break; } //System.out.println(w1+" "+w2); if(w1 > w2) { System.out.println((sb.length() + w1)); System.out.print(sb); for(int i = 0;i < w1; i++) System.out.print("L"); } else { System.out.println((sb.length()+w2)); System.out.print(sb); for(int i = 0; i < w2; i++) System.out.print("R"); } return; } if(f1==0&&f2==0) { break; } if(f1==1&&f2==0) { // System.out.print("L"); sb.append("L"); al.add(arr[l]); l++; c++; } else if(f1==0&&f2==1) { // System.out.print("R"); sb.append("R"); al.add(arr[r]); r--; c++; } else { if(arr[l]<arr[r]) { // System.out.print("L"); sb.append("L"); al.add(arr[l]); l++; c++; } else { //System.out.print("R"); sb.append("R"); al.add(arr[r]); r--; c++; } } } System.out.println(sb.length()); System.out.println(sb); } }
Java
["5\n1 2 4 3 2", "7\n1 3 5 6 5 4 2", "3\n2 2 2", "4\n1 2 4 3"]
2 seconds
["4\nLRRR", "6\nLRLRRR", "1\nR", "4\nLLRR"]
NoteThe first example is described in the problem statement.
Java 8
standard input
[ "greedy" ]
1ae1e051b6c8240eb27c64ae05c342cf
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.
1,700
In the first line of the output print $$$k$$$ — the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.
standard output
PASSED
00d14f8876ecf3e3d9b99752165c6748
train_003.jsonl
1556289300
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence $$$a$$$ consisting of $$$n$$$ integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence $$$[1, 2, 4, 3, 2]$$$ the answer is $$$4$$$ (you take $$$1$$$ and the sequence becomes $$$[2, 4, 3, 2]$$$, then you take the rightmost element $$$2$$$ and the sequence becomes $$$[2, 4, 3]$$$, then you take $$$3$$$ and the sequence becomes $$$[2, 4]$$$ and then you take $$$4$$$ and the sequence becomes $$$[2]$$$, the obtained increasing sequence is $$$[1, 2, 3, 4]$$$).
256 megabytes
/* * 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. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Deque; import java.util.LinkedList; import java.util.StringTokenizer; /** * * @author User */ public class CF_555_DIV3_D { 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 class Store { int last,len; String s=""; Store(int a,char ch) { last=a; s=s+ch+""; len=1; } } class Com implements Comparator<Store> { public int compare(Store s1,Store s2) { if(s1.len<s2.len) return 1; else return -1; } } public static void main(String[] args) { FastReader sc = new FastReader(); int n=sc.nextInt(); ArrayList<Integer> al=new ArrayList(); for(int i=0;i<n;i++) al.add(sc.nextInt()); Deque<Integer> q=new LinkedList(al); int x=0; String s="",w=""; int last=0,len=0; ArrayList<Store> a2=new ArrayList(); char cc[]=new char[1000000]; int pp=0; int flag=0; while(!q.isEmpty()) { int f=q.getFirst(); int l=q.getLast(); char ch; if(last>=f && last>=l) break; if(l>last && f>last) { if(l<f) { cc[pp++]='R'; last=l; q.removeLast(); } else if(f<l) { cc[pp++]='L'; last=f; q.removeFirst(); } else { int a=0,b=0; char ad1[]=new char[1000000]; char ad2[]=new char[1000000]; int carry=last; Deque<Integer> dq=new LinkedList(q); while(!q.isEmpty() && f>last) { ad1[a++]='L'; last=q.removeFirst(); if(!q.isEmpty()) f=q.getFirst(); } last=carry; //System.out.println("last="+last); while(!dq.isEmpty() && l>last) { ad2[b++]='R'; last=dq.removeLast(); if(!dq.isEmpty()) l=dq.getLast(); } //System.out.println("a="+a+" b="+b); /*System.out.println(Arrays.toString(ad1)); System.out.println(Arrays.toString(ad2));*/ int mm=Math.max(a,b); System.out.println(pp+mm); for(int i=0;i<pp;i++) System.out.print(cc[i]); if(a>b) { for(int i=0;i<a;i++) System.out.print(ad1[i]); } else { for(int i=0;i<b;i++) System.out.print(ad2[i]); } flag=1; break; } } else if(l>last) { cc[pp++]='R'; last=l; q.removeLast(); } else { cc[pp++]='L'; last=f; q.removeFirst(); } } if(flag==1)return; System.out.println(pp); for(int i=0;i<pp;i++) System.out.print(cc[i]); //System.out.println(w); } }
Java
["5\n1 2 4 3 2", "7\n1 3 5 6 5 4 2", "3\n2 2 2", "4\n1 2 4 3"]
2 seconds
["4\nLRRR", "6\nLRLRRR", "1\nR", "4\nLLRR"]
NoteThe first example is described in the problem statement.
Java 8
standard input
[ "greedy" ]
1ae1e051b6c8240eb27c64ae05c342cf
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.
1,700
In the first line of the output print $$$k$$$ — the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.
standard output
PASSED
21559bde4291effa36326c54c9a32ce9
train_003.jsonl
1556289300
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence $$$a$$$ consisting of $$$n$$$ integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence $$$[1, 2, 4, 3, 2]$$$ the answer is $$$4$$$ (you take $$$1$$$ and the sequence becomes $$$[2, 4, 3, 2]$$$, then you take the rightmost element $$$2$$$ and the sequence becomes $$$[2, 4, 3]$$$, then you take $$$3$$$ and the sequence becomes $$$[2, 4]$$$ and then you take $$$4$$$ and the sequence becomes $$$[2]$$$, the obtained increasing sequence is $$$[1, 2, 3, 4]$$$).
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class D3 { public static void main(String[] Args){ Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int[] arr=new int[n]; for (int i = 0; i <n ; i++) { arr[i]=scan.nextInt(); } StringBuilder s=new StringBuilder(); int l=0; int r=n-1; int last=0; while(l<=r){ int lDiff=arr[l]-last; int rDiff=arr[r]-last; if(lDiff>0&&rDiff>0){ if(lDiff==rDiff&&r-l>2){ int lcount=1; for(int tl=l+1;tl<=r;tl++){ if(arr[tl]>arr[tl-1]){ lcount++; } else{ break; } } int rcount=1; for(int tr=r-1;tr>=l;tr--){ if(arr[tr]>arr[tr+1]){ rcount++; } else{ break; } } if(lcount>rcount){ s.append('L'); last=arr[l]; l++; } else{ s.append('R'); last=arr[r]; r--; } } else if(lDiff<rDiff){ s.append('L'); last=arr[l]; l++; } else{ s.append('R'); last=arr[r]; r--; } } else if(lDiff<=0&&rDiff<=0){ break; } else{ if(lDiff>0){ s.append('L'); last=arr[l]; l++; } else{ s.append('R'); last=arr[r]; r--; } } } System.out.println(s.length()); System.out.println(s); } }
Java
["5\n1 2 4 3 2", "7\n1 3 5 6 5 4 2", "3\n2 2 2", "4\n1 2 4 3"]
2 seconds
["4\nLRRR", "6\nLRLRRR", "1\nR", "4\nLLRR"]
NoteThe first example is described in the problem statement.
Java 8
standard input
[ "greedy" ]
1ae1e051b6c8240eb27c64ae05c342cf
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.
1,700
In the first line of the output print $$$k$$$ — the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.
standard output
PASSED
1b64da1a69c8d2cbe022e0f3a6eeaf7f
train_003.jsonl
1556289300
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence $$$a$$$ consisting of $$$n$$$ integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence $$$[1, 2, 4, 3, 2]$$$ the answer is $$$4$$$ (you take $$$1$$$ and the sequence becomes $$$[2, 4, 3, 2]$$$, then you take the rightmost element $$$2$$$ and the sequence becomes $$$[2, 4, 3]$$$, then you take $$$3$$$ and the sequence becomes $$$[2, 4]$$$ and then you take $$$4$$$ and the sequence becomes $$$[2]$$$, the obtained increasing sequence is $$$[1, 2, 3, 4]$$$).
256 megabytes
import java.util.*; import jdk.nashorn.internal.ir.BreakableNode; public class Main { public static void main(String[] args) { Scanner a = new Scanner(System.in); int n = a.nextInt(); int[] data = new int[n]; for(int i=0; i<n; i++){ data[i] = a.nextInt(); } int r = n-1; int l = 0; StringBuilder sb = new StringBuilder(); int currlast = 0; int maxnum =0; while(maxnum<n){ if(data[l]<=currlast){ while(data[r]>currlast){ currlast = data[r]; sb.append('R'); maxnum++; r--; } break; }else if(data[r]<=currlast){ while(data[l]>currlast){ currlast = data[l]; sb.append('L'); maxnum++; l++; } break; }else{ if(data[l]<data[r]){ currlast = data[l]; sb.append('L'); l++; }else if(data[l]>data[r]){ currlast = data[r]; sb.append('R'); r--; }else if(l==r){ sb.append('L'); maxnum++; break; }else{ int leftmax = 0; int rightmax = 0; int csave = currlast; while(data[r]>currlast){ currlast = data[r]; rightmax++; r--; } currlast = csave; while(data[l]>currlast){ currlast = data[l]; leftmax++; l++; } if(leftmax>rightmax){ maxnum+=leftmax; for(int i=0; i<leftmax; i++){ sb.append("L"); } }else{ maxnum+=rightmax; for(int i=0; i<rightmax; i++){ sb.append("R"); } } break; } maxnum++; } } String res = sb.toString(); System.out.println(maxnum); System.out.println(res); } }
Java
["5\n1 2 4 3 2", "7\n1 3 5 6 5 4 2", "3\n2 2 2", "4\n1 2 4 3"]
2 seconds
["4\nLRRR", "6\nLRLRRR", "1\nR", "4\nLLRR"]
NoteThe first example is described in the problem statement.
Java 8
standard input
[ "greedy" ]
1ae1e051b6c8240eb27c64ae05c342cf
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.
1,700
In the first line of the output print $$$k$$$ — the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.
standard output
PASSED
4edb6e6f29bceeed1e5e024bf2e3bfa4
train_003.jsonl
1556289300
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence $$$a$$$ consisting of $$$n$$$ integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence $$$[1, 2, 4, 3, 2]$$$ the answer is $$$4$$$ (you take $$$1$$$ and the sequence becomes $$$[2, 4, 3, 2]$$$, then you take the rightmost element $$$2$$$ and the sequence becomes $$$[2, 4, 3]$$$, then you take $$$3$$$ and the sequence becomes $$$[2, 4]$$$ and then you take $$$4$$$ and the sequence becomes $$$[2]$$$, the obtained increasing sequence is $$$[1, 2, 3, 4]$$$).
256 megabytes
import java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; import static java.lang.Math.abs; import static java.lang.Math.min; import static java.lang.Math.max; import static java.lang.Math.sqrt; import static java.lang.Integer.signum; @SuppressWarnings("unchecked") public class P1157C2 { public void run() throws Exception { int n = nextInt(), a [] = readInt(n), li = 0, ri = n - 1, la = 0; char lr; StringBuilder ans = new StringBuilder(n); while (li <= ri) { if ((li != ri) && (a[li] != a[ri])) { lr = ((a[li] > la) && (a[ri] > la)) ? ((a[li] < a[ri]) ? 'L' : 'R') : ((a[li] > la) ? 'L' : ((a[ri] > la) ? 'R' : '\0')); if (lr != '\0') { la = (lr == 'L') ? a[li++] : a[ri--]; ans.append(lr); } else { break; } } else { // equals StringBuilder _ans = new StringBuilder(ans); for (int i = li, lla = la; i <= ri; i++) { if (a[i] > lla) { lla = a[i]; ans.append('L'); } else { break; } } for (int i = ri; i >= li; i--) { if (a[i] > la) { la = a[i]; _ans.append('R'); } else { break; } } ans = (ans.length() > _ans.length()) ? ans : _ans; break; } } println(ans.length() + "\n" + ans); } public static void main(String... args) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedOutputStream(System.out)); new P1157C2().run(); br.close(); pw.close(); System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]"); } static long startTime = System.currentTimeMillis(); static BufferedReader br; static PrintWriter pw; StringTokenizer stok; String nextToken() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } void print(byte b) { print("" + b); } void print(int i) { print("" + i); } void print(long l) { print("" + l); } void print(double d) { print("" + d); } void print(char c) { print("" + c); } void print(Object o) { if (o instanceof int[]) { print(Arrays.toString((int [])o)); } else if (o instanceof long[]) { print(Arrays.toString((long [])o)); } else if (o instanceof char[]) { print(Arrays.toString((char [])o)); } else if (o instanceof byte[]) { print(Arrays.toString((byte [])o)); } else if (o instanceof short[]) { print(Arrays.toString((short [])o)); } else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o)); } else if (o instanceof float[]) { print(Arrays.toString((float [])o)); } else if (o instanceof double[]) { print(Arrays.toString((double [])o)); } else if (o instanceof Object[]) { print(Arrays.toString((Object [])o)); } else { print("" + o); } } void print(String s) { pw.print(s); } void println() { println(""); } void println(byte b) { println("" + b); } void println(int i) { println("" + i); } void println(long l) { println("" + l); } void println(double d) { println("" + d); } void println(char c) { println("" + c); } void println(Object o) { print(o); println(); } void println(String s) { pw.println(s); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } char nextChar() throws IOException { return (char) (br.read()); } String next() throws IOException { return nextToken(); } String nextLine() throws IOException { return br.readLine(); } int [] readInt(int size) throws IOException { int [] array = new int [size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } long [] readLong(int size) throws IOException { long [] array = new long [size]; for (int i = 0; i < size; i++) { array[i] = nextLong(); } return array; } double [] readDouble(int size) throws IOException { double [] array = new double [size]; for (int i = 0; i < size; i++) { array[i] = nextDouble(); } return array; } String [] readLines(int size) throws IOException { String [] array = new String [size]; for (int i = 0; i < size; i++) { array[i] = nextLine(); } return array; } int gcd(int a, int b) { if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a); a = Math.abs(a); b = Math.abs(b); int az = Integer.numberOfTrailingZeros(a), bz = Integer.numberOfTrailingZeros(b); a >>>= az; b >>>= bz; while (a != b) { if (a > b) { a -= b; a >>>= Integer.numberOfTrailingZeros(a); } else { b -= a; b >>>= Integer.numberOfTrailingZeros(b); } } return (a << Math.min(az, bz)); } long gcd(long a, long b) { if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a); a = Math.abs(a); b = Math.abs(b); int az = Long.numberOfTrailingZeros(a), bz = Long.numberOfTrailingZeros(b); a >>>= az; b >>>= bz; while (a != b) { if (a > b) { a -= b; a >>>= Long.numberOfTrailingZeros(a); } else { b -= a; b >>>= Long.numberOfTrailingZeros(b); } } return (a << Math.min(az, bz)); } void shuffle(int [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); int t = a[i]; a[i] = a[j]; a[j] = t; } } void shuffle(long [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); long t = a[i]; a[i] = a[j]; a[j] = t; } } void shuffle(Object [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); Object t = a[i]; a[i] = a[j]; a[j] = t; } } void flush() { pw.flush(); } void pause() { flush(); System.console().readLine(); } }
Java
["5\n1 2 4 3 2", "7\n1 3 5 6 5 4 2", "3\n2 2 2", "4\n1 2 4 3"]
2 seconds
["4\nLRRR", "6\nLRLRRR", "1\nR", "4\nLLRR"]
NoteThe first example is described in the problem statement.
Java 8
standard input
[ "greedy" ]
1ae1e051b6c8240eb27c64ae05c342cf
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.
1,700
In the first line of the output print $$$k$$$ — the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.
standard output
PASSED
7ee7a4f584ecfbb388c86d498e2e67bb
train_003.jsonl
1556289300
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence $$$a$$$ consisting of $$$n$$$ integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence $$$[1, 2, 4, 3, 2]$$$ the answer is $$$4$$$ (you take $$$1$$$ and the sequence becomes $$$[2, 4, 3, 2]$$$, then you take the rightmost element $$$2$$$ and the sequence becomes $$$[2, 4, 3]$$$, then you take $$$3$$$ and the sequence becomes $$$[2, 4]$$$ and then you take $$$4$$$ and the sequence becomes $$$[2]$$$, the obtained increasing sequence is $$$[1, 2, 3, 4]$$$).
256 megabytes
import java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; import static java.lang.Math.abs; import static java.lang.Math.min; import static java.lang.Math.max; import static java.lang.Math.sqrt; import static java.lang.Integer.signum; @SuppressWarnings("unchecked") public class P1157C { public void run() throws Exception { int n = nextInt(), a [] = readInt(n), li = 0, ri = n - 1, la = 0; char lr; StringBuilder ans = new StringBuilder(n); while (li <= ri) { if ((li != ri) && (a[li] != a[ri])) { lr = ((a[li] > la) && (a[ri] > la)) ? ((a[li] < a[ri]) ? 'L' : 'R') : ((a[li] > la) ? 'L' : ((a[ri] > la) ? 'R' : '\0')); if (lr != '\0') { la = (lr == 'L') ? a[li++] : a[ri--]; ans.append(lr); } else { break; } } else { // equals StringBuilder _ans = new StringBuilder(ans); for (int i = li, lla = la; i <= ri; i++) { if (a[i] > lla) { lla = a[i]; ans.append('L'); } else { break; } } for (int i = ri; i >= li; i--) { if (a[i] > la) { la = a[i]; _ans.append('R'); } else { break; } } ans = (ans.length() > _ans.length()) ? ans : _ans; break; } } println(ans.length() + "\n" + ans); } public static void main(String... args) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedOutputStream(System.out)); new P1157C().run(); br.close(); pw.close(); System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]"); } static long startTime = System.currentTimeMillis(); static BufferedReader br; static PrintWriter pw; StringTokenizer stok; String nextToken() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } void print(byte b) { print("" + b); } void print(int i) { print("" + i); } void print(long l) { print("" + l); } void print(double d) { print("" + d); } void print(char c) { print("" + c); } void print(Object o) { if (o instanceof int[]) { print(Arrays.toString((int [])o)); } else if (o instanceof long[]) { print(Arrays.toString((long [])o)); } else if (o instanceof char[]) { print(Arrays.toString((char [])o)); } else if (o instanceof byte[]) { print(Arrays.toString((byte [])o)); } else if (o instanceof short[]) { print(Arrays.toString((short [])o)); } else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o)); } else if (o instanceof float[]) { print(Arrays.toString((float [])o)); } else if (o instanceof double[]) { print(Arrays.toString((double [])o)); } else if (o instanceof Object[]) { print(Arrays.toString((Object [])o)); } else { print("" + o); } } void print(String s) { pw.print(s); } void println() { println(""); } void println(byte b) { println("" + b); } void println(int i) { println("" + i); } void println(long l) { println("" + l); } void println(double d) { println("" + d); } void println(char c) { println("" + c); } void println(Object o) { print(o); println(); } void println(String s) { pw.println(s); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } char nextChar() throws IOException { return (char) (br.read()); } String next() throws IOException { return nextToken(); } String nextLine() throws IOException { return br.readLine(); } int [] readInt(int size) throws IOException { int [] array = new int [size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } long [] readLong(int size) throws IOException { long [] array = new long [size]; for (int i = 0; i < size; i++) { array[i] = nextLong(); } return array; } double [] readDouble(int size) throws IOException { double [] array = new double [size]; for (int i = 0; i < size; i++) { array[i] = nextDouble(); } return array; } String [] readLines(int size) throws IOException { String [] array = new String [size]; for (int i = 0; i < size; i++) { array[i] = nextLine(); } return array; } int gcd(int a, int b) { if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a); a = Math.abs(a); b = Math.abs(b); int az = Integer.numberOfTrailingZeros(a), bz = Integer.numberOfTrailingZeros(b); a >>>= az; b >>>= bz; while (a != b) { if (a > b) { a -= b; a >>>= Integer.numberOfTrailingZeros(a); } else { b -= a; b >>>= Integer.numberOfTrailingZeros(b); } } return (a << Math.min(az, bz)); } long gcd(long a, long b) { if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a); a = Math.abs(a); b = Math.abs(b); int az = Long.numberOfTrailingZeros(a), bz = Long.numberOfTrailingZeros(b); a >>>= az; b >>>= bz; while (a != b) { if (a > b) { a -= b; a >>>= Long.numberOfTrailingZeros(a); } else { b -= a; b >>>= Long.numberOfTrailingZeros(b); } } return (a << Math.min(az, bz)); } void shuffle(int [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); int t = a[i]; a[i] = a[j]; a[j] = t; } } void shuffle(long [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); long t = a[i]; a[i] = a[j]; a[j] = t; } } void shuffle(Object [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); Object t = a[i]; a[i] = a[j]; a[j] = t; } } void flush() { pw.flush(); } void pause() { flush(); System.console().readLine(); } }
Java
["5\n1 2 4 3 2", "7\n1 3 5 6 5 4 2", "3\n2 2 2", "4\n1 2 4 3"]
2 seconds
["4\nLRRR", "6\nLRLRRR", "1\nR", "4\nLLRR"]
NoteThe first example is described in the problem statement.
Java 8
standard input
[ "greedy" ]
1ae1e051b6c8240eb27c64ae05c342cf
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.
1,700
In the first line of the output print $$$k$$$ — the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.
standard output
PASSED
4242286202ee4e43d60cc5d7f268b227
train_003.jsonl
1556289300
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence $$$a$$$ consisting of $$$n$$$ integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence $$$[1, 2, 4, 3, 2]$$$ the answer is $$$4$$$ (you take $$$1$$$ and the sequence becomes $$$[2, 4, 3, 2]$$$, then you take the rightmost element $$$2$$$ and the sequence becomes $$$[2, 4, 3]$$$, then you take $$$3$$$ and the sequence becomes $$$[2, 4]$$$ and then you take $$$4$$$ and the sequence becomes $$$[2]$$$, the obtained increasing sequence is $$$[1, 2, 3, 4]$$$).
256 megabytes
import java.io.*; import java.util.*; public class Main { static InputReader in = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); static int MOD = 1000000007; static int M = 505; static int oo = (int)1e9; public static void main(String[] args) throws IOException { int n = in.nextInt(); int[] arr = new int[n]; for(int i=0; i<n; ++i) { arr[i] = in.nextInt(); } int[] left = new int[n]; int[] right = new int[n]; left[0] = 1; for(int i=1; i < n; ++i) { if(arr[i-1] > arr[i]) left[i] = left[i-1] + 1; else left[i] = 1; } right[n-1] = 1; for(int i = n-2; i >= 0; --i) { if(arr[i] < arr[i+1]) right[i] = right[i+1] + 1; else right[i] = 1; } int x = 0; ArrayList<Character> ans = new ArrayList<>(); int i = 0, j = n-1; while(i <= j) { int l = arr[i]; int r = arr[j]; if(l > x && r > x) { if(l > r) { j--; ans.add('R'); x = r; } else if(l < r) { i++; ans.add('L'); x = l; } else { if(right[i] > left[j]) { i++; ans.add('L'); x = l; } else { j--; ans.add('R'); x = r; } } } else if(l > x) { i++; ans.add('L'); x = l; } else if(r > x) { j--; ans.add('R'); x = r; } else break; // if(l > r) { // if(x >= l) // break; // if(r > x) { // j--; // ans.add('R'); // x = r; // } // else { // i++; // ans.add('L'); // x = l; // } // } // else if(l < r) { // if(x >= r) // break; // if(l > x) { // i++; // ans.add('L'); // x = l; // } // else { // j--; // ans.add('R'); // x = r; // } // } // else { // if(x >= l) // break; // if(right[i] > left[j]) { // i++; // ans.add('L'); // x = l; // } // else { // j--; // ans.add('R'); // x = r; // } // } } System.out.println(ans.size()); for(char c : ans) System.out.print(c); out.close(); } static void shuffle(int[] a) { Random r = new Random(); for(int i = a.length - 1; i > 0; --i) { int si = r.nextInt(i); int t = a[si]; a[si] = a[i]; a[i] = t; } } static int lower_bound(int[] a, int n, int k) { int s = 0; int e = n; int m; while (e - s > 0) { m = (s + e) / 2; if (a[m] < k) s = m + 1; else e = m; } return e; } static int lower_bound(long[] a, int n, long k) { int s = 0; int e = n; int m; while (e - s > 0) { m = (s + e) / 2; if (a[m] < k) s = m + 1; else e = m; } return e; } 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 class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { super(); this.first = first; this.second = second; } @Override public int compareTo(Pair o) { return this.first != o.first ? this.first - o.first : this.second - o.second; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + first; result = prime * result + second; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Pair other = (Pair) obj; if (first != other.first) return false; if (second != other.second) return false; return true; } } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 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 { 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 { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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 boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } }
Java
["5\n1 2 4 3 2", "7\n1 3 5 6 5 4 2", "3\n2 2 2", "4\n1 2 4 3"]
2 seconds
["4\nLRRR", "6\nLRLRRR", "1\nR", "4\nLLRR"]
NoteThe first example is described in the problem statement.
Java 8
standard input
[ "greedy" ]
1ae1e051b6c8240eb27c64ae05c342cf
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.
1,700
In the first line of the output print $$$k$$$ — the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.
standard output
PASSED
3da3c51bb60274fabf6e751bab1bd073
train_003.jsonl
1556289300
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence $$$a$$$ consisting of $$$n$$$ integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence $$$[1, 2, 4, 3, 2]$$$ the answer is $$$4$$$ (you take $$$1$$$ and the sequence becomes $$$[2, 4, 3, 2]$$$, then you take the rightmost element $$$2$$$ and the sequence becomes $$$[2, 4, 3]$$$, then you take $$$3$$$ and the sequence becomes $$$[2, 4]$$$ and then you take $$$4$$$ and the sequence becomes $$$[2]$$$, the obtained increasing sequence is $$$[1, 2, 3, 4]$$$).
256 megabytes
import java.io.*; import java.util.*; public class Main { static InputReader in = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); static int MOD = 1000000007; static int M = 505; static int oo = (int)1e9; public static void main(String[] args) throws IOException { int n = in.nextInt(); int[] arr = new int[n]; for(int i=0; i<n; ++i) { arr[i] = in.nextInt(); } int[] left = new int[n]; int[] right = new int[n]; left[0] = 1; for(int i=1; i < n; ++i) { if(arr[i-1] > arr[i]) left[i] = left[i-1] + 1; else left[i] = 1; } right[n-1] = 1; for(int i = n-2; i >= 0; --i) { if(arr[i] < arr[i+1]) right[i] = right[i+1] + 1; else right[i] = 1; } int x = 0; ArrayList<Character> ans = new ArrayList<>(); int i = 0, j = n-1; while(i <= j) { int l = arr[i]; int r = arr[j]; if(l > r) { if(x >= l) break; if(r > x) { j--; ans.add('R'); x = r; } else { i++; ans.add('L'); x = l; } } else if(l < r) { if(x >= r) break; if(l > x) { i++; ans.add('L'); x = l; } else { j--; ans.add('R'); x = r; } } else { if(x >= l) break; if(right[i] > left[j]) { i++; ans.add('L'); x = l; } else { j--; ans.add('R'); x = r; } } // if(l == r) { // if(x < l) { // a.pollFirst(); // ans.add('L'); // x = l; // } // else // break; // } // // if(x < l && l < r || r < x && x < l) { // a.pollFirst(); // ans.add('L'); // x = l; // } // else if(x < r && r < l || l < x && x < r) { // a.pollLast(); // ans.add('R'); // x = r; // } // else // break; } System.out.println(ans.size()); for(char c : ans) System.out.print(c); out.close(); } static void shuffle(int[] a) { Random r = new Random(); for(int i = a.length - 1; i > 0; --i) { int si = r.nextInt(i); int t = a[si]; a[si] = a[i]; a[i] = t; } } static int lower_bound(int[] a, int n, int k) { int s = 0; int e = n; int m; while (e - s > 0) { m = (s + e) / 2; if (a[m] < k) s = m + 1; else e = m; } return e; } static int lower_bound(long[] a, int n, long k) { int s = 0; int e = n; int m; while (e - s > 0) { m = (s + e) / 2; if (a[m] < k) s = m + 1; else e = m; } return e; } 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 class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { super(); this.first = first; this.second = second; } @Override public int compareTo(Pair o) { return this.first != o.first ? this.first - o.first : this.second - o.second; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + first; result = prime * result + second; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Pair other = (Pair) obj; if (first != other.first) return false; if (second != other.second) return false; return true; } } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 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 { 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 { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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 boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } }
Java
["5\n1 2 4 3 2", "7\n1 3 5 6 5 4 2", "3\n2 2 2", "4\n1 2 4 3"]
2 seconds
["4\nLRRR", "6\nLRLRRR", "1\nR", "4\nLLRR"]
NoteThe first example is described in the problem statement.
Java 8
standard input
[ "greedy" ]
1ae1e051b6c8240eb27c64ae05c342cf
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.
1,700
In the first line of the output print $$$k$$$ — the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.
standard output
PASSED
18a6bb055a96b2ee39a2cdab60f50792
train_003.jsonl
1556289300
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence $$$a$$$ consisting of $$$n$$$ integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence $$$[1, 2, 4, 3, 2]$$$ the answer is $$$4$$$ (you take $$$1$$$ and the sequence becomes $$$[2, 4, 3, 2]$$$, then you take the rightmost element $$$2$$$ and the sequence becomes $$$[2, 4, 3]$$$, then you take $$$3$$$ and the sequence becomes $$$[2, 4]$$$ and then you take $$$4$$$ and the sequence becomes $$$[2]$$$, the obtained increasing sequence is $$$[1, 2, 3, 4]$$$).
256 megabytes
import java.io.*; import java.util.*; public class Main { static InputReader in = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); static int MOD = 1000000007; static int M = 505; static int oo = (int)1e9; public static void main(String[] args) throws IOException { int n = in.nextInt(); int[] arr = new int[n]; for(int i=0; i<n; ++i) { arr[i] = in.nextInt(); } int[] left = new int[n]; int[] right = new int[n]; left[0] = 1; for(int i=1; i < n; ++i) { if(arr[i-1] > arr[i]) left[i] = left[i-1] + 1; else left[i] = 1; } right[n-1] = 1; for(int i = n-2; i >= 0; --i) { if(arr[i] < arr[i+1]) right[i] = right[i+1] + 1; else right[i] = 1; } int x = 0; // ArrayList<Character> ans = new ArrayList<>(); StringBuilder sb = new StringBuilder(); int i = 0, j = n-1; while(i <= j) { int l = arr[i]; int r = arr[j]; if(l > x && r > x) { if(l > r) { j--; // ans.add('R'); sb.append('R'); x = r; } else if(l < r) { i++; // ans.add('L'); sb.append('L'); x = l; } else { if(right[i] > left[j]) { i++; // ans.add('L'); sb.append('L'); x = l; } else { j--; // ans.add('R'); sb.append('R'); x = r; } } } else if(l > x) { i++; // ans.add('L'); sb.append('L'); x = l; } else if(r > x) { j--; // ans.add('R'); sb.append('R'); x = r; } else break; // if(l > r) { // if(x >= l) // break; // if(r > x) { // j--; // ans.add('R'); // x = r; // } // else { // i++; // ans.add('L'); // x = l; // } // } // else if(l < r) { // if(x >= r) // break; // if(l > x) { // i++; // ans.add('L'); // x = l; // } // else { // j--; // ans.add('R'); // x = r; // } // } // else { // if(x >= l) // break; // if(right[i] > left[j]) { // i++; // ans.add('L'); // x = l; // } // else { // j--; // ans.add('R'); // x = r; // } // } } System.out.println(sb.length()); System.out.println(sb); // System.out.println(ans.size()); // for(char c : ans) // System.out.print(c); out.close(); } static void shuffle(int[] a) { Random r = new Random(); for(int i = a.length - 1; i > 0; --i) { int si = r.nextInt(i); int t = a[si]; a[si] = a[i]; a[i] = t; } } static int lower_bound(int[] a, int n, int k) { int s = 0; int e = n; int m; while (e - s > 0) { m = (s + e) / 2; if (a[m] < k) s = m + 1; else e = m; } return e; } static int lower_bound(long[] a, int n, long k) { int s = 0; int e = n; int m; while (e - s > 0) { m = (s + e) / 2; if (a[m] < k) s = m + 1; else e = m; } return e; } 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 class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { super(); this.first = first; this.second = second; } @Override public int compareTo(Pair o) { return this.first != o.first ? this.first - o.first : this.second - o.second; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + first; result = prime * result + second; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Pair other = (Pair) obj; if (first != other.first) return false; if (second != other.second) return false; return true; } } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 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 { 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 { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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 boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } }
Java
["5\n1 2 4 3 2", "7\n1 3 5 6 5 4 2", "3\n2 2 2", "4\n1 2 4 3"]
2 seconds
["4\nLRRR", "6\nLRLRRR", "1\nR", "4\nLLRR"]
NoteThe first example is described in the problem statement.
Java 8
standard input
[ "greedy" ]
1ae1e051b6c8240eb27c64ae05c342cf
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.
1,700
In the first line of the output print $$$k$$$ — the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.
standard output
PASSED
ccfee592ebd0a4831b909e993b6162b5
train_003.jsonl
1556289300
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence $$$a$$$ consisting of $$$n$$$ integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence $$$[1, 2, 4, 3, 2]$$$ the answer is $$$4$$$ (you take $$$1$$$ and the sequence becomes $$$[2, 4, 3, 2]$$$, then you take the rightmost element $$$2$$$ and the sequence becomes $$$[2, 4, 3]$$$, then you take $$$3$$$ and the sequence becomes $$$[2, 4]$$$ and then you take $$$4$$$ and the sequence becomes $$$[2]$$$, the obtained increasing sequence is $$$[1, 2, 3, 4]$$$).
256 megabytes
import java.util.Scanner; public class C{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a= new int[n+1]; for(int i = 0;i<n;++i) a[i] = in.nextInt(); int l = 0; int r = n-1; int last = -1; int count = 0; StringBuilder str = new StringBuilder(); while(l<=r){ if(a[l] <= last && a[r] <= last) break; if(a[l] == a[r]){ int tl = l+1; while(tl<=r && a[tl]>a[tl-1]) tl++; int tr = r-1; while(tr>=l && a[tr]> a[tr+1]) tr--; if(r - tr > tl - l) for(int i = tr+1;i<=r;++i){ str.append("R"); count++; } else for(int i = l;i<tl;++i){ str.append("L"); count++; } break; } if(a[l] > last && a[l] < a[r] || a[r]<=last){ str.append("L"); last = a[l]; l++; } else{ str.append("R"); last = a[r]; r--; } count++; } System.out.println(count + "\n" + str); } }
Java
["5\n1 2 4 3 2", "7\n1 3 5 6 5 4 2", "3\n2 2 2", "4\n1 2 4 3"]
2 seconds
["4\nLRRR", "6\nLRLRRR", "1\nR", "4\nLLRR"]
NoteThe first example is described in the problem statement.
Java 8
standard input
[ "greedy" ]
1ae1e051b6c8240eb27c64ae05c342cf
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.
1,700
In the first line of the output print $$$k$$$ — the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.
standard output
PASSED
eae41ce2f564ed8c01a314bac5015fd1
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import static java.lang.Math.*; import static java.util.Arrays.*; public class Main { public static void main(String[] args) throws IOException { new Main().run(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); private void run() throws IOException { if (new File("input.txt").exists()) in = new BufferedReader(new FileReader("input.txt")); else in = new BufferedReader(new InputStreamReader(System.in)); if (new File("output.txt").exists()) out = new PrintWriter("output.txt"); else out = new PrintWriter(System.out); solve(); in.close(); out.close(); } void solve() throws IOException { int n = nextInt(); int k = nextInt(); int l = 1, r = n; if (k % 2 == 0) { out.print(r + " "); k--; r--; } while (l <= r) { if (k % 2 == 1 || k <= 0) { out.print(l + " "); l++; } else { out.print(r + " "); r--; } k--; } } String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } String nextToken() throws IOException { while (!st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } boolean EOF() throws IOException { while (!st.hasMoreTokens()) { String str = in.readLine(); if (str == null) return true; st = new StringTokenizer(str); } return false; } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
3cd2bd4c13f6dec1b8d70f7ae658e6dc
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.Scanner; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author bitver */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); boolean[] used = new boolean[n+1]; int current = 1; out.print(current); used[current] = true; while (k > 0){ if (current - k > 0 && !used[current-k]){ current = current-k; } else { current = current+k; } used[current] = true; out.print(" " + current); k--; } for (int i = 1; i < used.length; i++){ if (!used[i]){ out.print(" " + i); } } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
23e6fa34693899242b8a14a5df9cd97a
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.math.BigInteger; import java.util.Scanner; import java.io.PrintWriter; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int k = in.nextInt(); Boolean[] used = new Boolean[n]; for (int i = 0; i < n; ++i) used[i] = false; int l = 0, r = n - 1; for (int i = 0; i < k; ++i) { used[(i & 1) == 0 ? l : r] = true; if ((i & 1) == 0) out.print((1 + (l++))); else out.print((1 + (r--))); out.print(' '); } if ((k & 1) == 1) { for (int i = 0; i < n; ++i) { if (!used[i]) { out.print(i + 1); out.print(' '); } } } else { for (int i = n - 1; i >= 0; --i) { if (!used[i]) { out.print(i + 1); out.print(' '); } } } out.close(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
92346ebefeb892acc138389e225d51db
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.*; import java.util.HashSet; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(), k = sc.nextInt(); int[] ans = new int[n]; ans[0] = 1; HashSet<Integer> taken = new HashSet<>(); int last = n; taken.add(1); for (int i = 1; i < n; i++) { if (k == 1) { int put = ans[i - 1] + 1; if (put>n || taken.contains(put)) put = ans[i - 1] - 1; ans[i] = put; } else { k--; last--; int put = ans[i - 1] + last; if (put>n || taken.contains(put)) put = ans[i - 1] - last; ans[i] = put; } taken.add(ans[i]); } for (int x : ans) out.print(x +" "); out.flush(); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } 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 long[] nextLongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); 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 double[] nextDoubleArray(int n) throws IOException { double[] ans = new double[n]; for (int i = 0; i < n; i++) ans[i] = nextDouble(); return ans; } public short nextShort() throws IOException { return Short.parseShort(next()); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
2f1810851d0b51bb999017ce6dc7f289
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /** * @author Don Li */ public class DiversePermutation { void solve() { int n = in.nextInt(), k = in.nextInt(); int[] res = new int[n]; int p = 1, q = n; for (int i = 0; i < k; i++) { if (i % 2 == 0) res[i] = p++; else res[i] = q--; } if (k % 2 == 0) { for (int i = k; i < n; i++) res[i] = q--; } else { for (int i = k; i < n; i++) res[i] = p++; } for (int i = 0; i < n; i++) { if (i > 0) out.print(' '); out.print(res[i]); } out.println(); } public static void main(String[] args) { in = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); new DiversePermutation().solve(); out.close(); } static FastScanner in; static PrintWriter out; static class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
a13c3b283fa6366a27864f84dae973af
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public static InputReader in; public static PrintWriter pw; public static void main(String args[]) { new Thread(null, new Runnable() { public void run() { try{ solve(); } catch(Exception e){ e.printStackTrace(); } } }, "1", 1 << 26).start(); } static TreeSet<Integer> set; static ArrayList<Pair> g[]; static boolean visited[]; static long edje=0; static boolean vis[]; static int parent[]; static int col[]; static boolean[] vis1; static int Ans=0; static int min=Integer.MAX_VALUE; static int val[]; static int degree[]; static int a[]; static int f[]; static long total=0; static HashSet<String> s; static int distance[]; public static void solve(){ in = new InputReader(System.in); pw = new PrintWriter(System.out); int n=in.nextInt(); int k=in.nextInt(); if(k==1){ for(int i=1;i<=n;i++) System.out.print(i+" "); System.exit(0); } if(k==2) { System.out.print(1+" "+3+" "); System.out.print(2+" "); for(int i=4;i<=n;i++) System.out.print(i+" "); System.exit(0); } int i=1; int j=0; int p=k; System.out.print(1+" "); boolean b=true; while(k>0){ if(b){ System.out.print((i+k)+" "); j = (i+k); b = false; } else{ b = true; System.out.print((j-k)+" "); i++; } k--; } for(int h=(p+2);h<=n;h++) System.out.print(h+" "); } /*public static void dfs(int curr) { vis[curr]=true; int trees=0; for(int x:g[curr]) { if(!vis[x]&&x!=1) { trees++; if(!s.contains(curr+" "+x)) total+=cost[curr][x]; dfs(x); } } if(trees==0) { if(!s.contains(curr+" "+1)) total+=cost[1][curr]; } } public static void dfs1(int curr,int parent) { val[curr]=a[curr]; for(int x:g[curr]) { if(x!=parent) { dfs1(x,curr); val[curr]+=val[x]; } } } public static void dfs2(int curr,int parent) { f[curr]=val[curr]; for(int x:g[curr]) { if(x!=parent) { dfs2(x,curr); f[curr]+=f[x]; } } } /* public static void dfs2(int curr) { if(st[col[curr]].isEmpty()) { ans[curr]=-1; } else { ans[curr]=st[col[curr]].peek(); } st[col[curr]].push(curr); for(int x:g[curr]) { dfs2(x); } st[col[curr]].pop(); } public static void dfs1(int curr) { visited[curr]=true; for(int next:g[curr]) { edje++; if(!visited[next]) dfs1(next); } } public static void dfs(int curr,int prev) { val[curr]=1; for(int x:g[curr]) { if(x!=prev) { dfs(x,curr); val[curr]+=val[x]; } } }*/ public static long power(long a,long b) { long result=1; while(b>0) { if(b%2==1) result*=a; a=a*a; b/=2; } return result; } public static long pow(long n,long p,long m) { long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; if(result>=m) result%=m; p >>=1; n*=n; if(n>=m) n%=m; } return result; } static class Pair implements Comparable<Pair>{ int fir; int sec; Pair(int mr,int i){ fir=mr; sec=i; } @Override public int compareTo(Pair o) { // if(o.val<this.val) return 1; //else //return -1; } } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a%b); } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
6d3438a32d1647eaf6bfcaa329049445
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.*; import java.io.*; /** * * @author umang */ public class C483 { public static int mod = 1000000007; public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n = in.nextInt(); int k = in.nextInt(); HashSet<Integer> set = new HashSet<>(); ArrayList<Integer> list = new ArrayList<>(); for(int i=1;i<=n;i++) set.add(i); list.add(1); int dif=k; int ele = 1; set.remove(1); int f=0; for(int i=k;i>0;i--,dif--){ if(f==0){ if(!set.contains(ele+dif)){ ele-=dif; } else{ ele +=dif; f=1; } } else{ if(!set.contains(ele-dif)){ ele+=dif; } else{ ele-=dif; f=0; } } list.add(ele); set.remove(ele); } for(int i=0;i<list.size();i++) w.print(list.get(i)+" "); for(int i=k+2;i<=n;i++) w.print(i+" "); w.close(); } static class Pair{ int c; int x; public Pair(int x,int c){ this.x=x; this.c=c; } } static class CompareCost implements Comparator{ @Override public int compare(Object t, Object t1) { Pair p1 = (Pair)t; Pair p2 = (Pair)t1; if(p1.c<p2.c){ return -1; } else if(p1.c>p2.c){ return 1; } else return 0; } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
5c3e43fb04c839d1221271eef306bc7a
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.math.BigInteger; import java.util.Scanner; import java.io.PrintWriter; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int k = in.nextInt(); Boolean[] used = new Boolean[n]; for (int i = 0; i < n; ++i) used[i] = false; int l = 0, r = n - 1; for (int i = 0; i < k; ++i) { used[(i & 1) == 0 ? l : r] = true; if ((i & 1) == 0) out.print((1 + (l++))); else out.print((1 + (r--))); out.print(' '); } if ((k & 1) == 1) { for (int i = 0; i < n; ++i) { if (!used[i]) { out.print(i + 1); out.print(' '); } } } else { for (int i = n - 1; i >= 0; --i) { if (!used[i]) { out.print(i + 1); out.print(' '); } } } out.close(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
a10d28e3325e61bb58e31de3eb1e5c9a
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.*; import java.util.*; public class CF483C { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); int[] aa = new int[n]; if (k % 2 == 0) { for (int i = 1; i <= k / 2; i++) { aa[i + i - 1] = i; aa[i + i] = -i; } for (int i = k + 1, j = -k / 2 - 1; i < n; i++, j--) aa[i] = j; } else { for (int i = 1; i <= k / 2; i++) { aa[i + i - 1] = i; aa[i + i] = -i; } for (int i = k, j = k / 2 + 1; i < n; i++, j++) aa[i] = j; } int min = 0; for (int i = 1; i < n; i++) if (min > aa[i]) min = aa[i]; for (int i = 0; i < n; i++) aa[i] -= min - 1; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) sb.append(aa[i] + " "); System.out.println(sb); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
11c3023b75b45f1c828bec84fae569fa
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.awt.Point; import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; import javax.lang.model.element.NestingKind; import javax.management.IntrospectionException; import javax.swing.colorchooser.ColorSelectionModel; import javax.swing.text.AbstractDocument.LeafElement; import javax.xml.soap.MimeHeader; import javax.xml.transform.Templates; import org.xml.sax.HandlerBase; import static java.lang.Math.*; public class Main { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException { if (ONLINE_JUDGE) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } public static void main(String[] args) { new Main().run(); // Sworn to fight and die } public static void mergeSort(int[] a) { mergeSort(a, 0, a.length - 1); } static int curr = 0; static long ans = 0; private static void mergeSort(int[] a, int levtIndex, int rightIndex) { final int MAGIC_VALUE = 2; if (levtIndex < rightIndex) { if (rightIndex - levtIndex <= MAGIC_VALUE) { insertionSort(a, levtIndex, rightIndex); } else { int middleIndex = (levtIndex + rightIndex) / 2; mergeSort(a, levtIndex, middleIndex); ans += curr; mergeSort(a, middleIndex + 1, rightIndex); ans += curr; merge(a, levtIndex, middleIndex, rightIndex); ans += curr; } } } private static void merge(int[] a, int levtIndex, int middleIndex, int rightIndex) { curr = 0; int length1 = middleIndex - levtIndex + 1; int length2 = rightIndex - middleIndex; int[] levtArray = new int[length1]; int[] rightArray = new int[length2]; System.arraycopy(a, levtIndex, levtArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = levtIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = levtArray[i++]; } else { if (levtArray[i] > rightArray[j]) curr += middleIndex - i + 1; a[k] = levtArray[i] <= rightArray[j] ? levtArray[i++] : rightArray[j++]; } } } private static void insertionSort(int[] a, int levtIndex, int rightIndex) { for (int i = levtIndex + 1; i <= rightIndex; i++) { int current = a[i]; int j = i - 1; while (j >= levtIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } public void run() { try { long t1 = System.currentTimeMillis(); init(); solve(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = " + (t2 - t1)); } catch (Exception e) { e.printStackTrace(System.err); System.exit(-1); } } class LOL implements Comparable<LOL> { int x; int y; public LOL(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object object) { boolean sameSame = false; if (object != null && object instanceof LOL) { sameSame = ((this.x == ((LOL) object).x) && (this.y == ((LOL) object).y)); } return sameSame; } @Override public int compareTo(LOL o) { return (x - o.x); // ----> // return o.x * o.y - x * y; // <---- } } class test implements Comparable<test> { int x; int y; public test(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(test o) { return (x - o.x); // ----> // return o.x * o.y - x * y; // <---- } } public long gcd(long a, long b) { if (a % b == 0) return b; else return gcd(b, a % b); } public int gcd(int a, int b) { if (a % b == 0) return b; else return gcd(b, a % b); } boolean prime(int n) { for (int i = 2; i <= sqrt(n); i++) if (n % i == 0) return false; return true; } Map<Integer, List<Integer>> g, gf; boolean[] color; HashMap<Integer, Boolean> compColor; ArrayList<Integer> order, component; List<Integer> answer; ArrayList<ArrayList<Integer>> graph; public void dfs1(Integer v) { color[v] = true; if (!g.containsKey(v)) { order.add(v); return; } for (int i = 0; i < g.get(v).size(); i++) { if (!color[(g.get(v).get(i))]) { dfs1(g.get(v).get(i)); } } order.add(v); } public void dfs2(Integer v) { color[v] = true; if (!gf.containsKey(v)) { component.add(v); return; } component.add(v); for (int i = 0; i < gf.get(v).size(); i++) { if (!color[(gf.get(v).get(i))]) { dfs2(gf.get(v).get(i)); } } } int goal; boolean isFind = false; public void dfs(Integer v) { color[v] = true; if (v == goal) { isFind = true; answer.add(v); return; } for (int i = 0; i < g.get(v).size(); i++) { if (!color[(g.get(v).get(i))]) { dfs(g.get(v).get(i)); if (isFind) { answer.add(v); return; } } } } public void solve() throws IOException { int n = readInt(); int k = readInt(); ArrayList<Integer> ints = new ArrayList<Integer>(); boolean prints[] = new boolean[n + 1]; for (int i = 1; i <= n; i++) { ints.add(i); } int pr = 1; for (int i = 0; k > 0; i++) { out.print(pr + " "); prints[pr] = true; if (i % 2 == 0) { pr += k; } else { pr -= k; } k--; } for (Integer t: ints) { if (!prints[t]) { out.print(t + " "); } } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
08406306b8f157de18955c7452821ec5
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception{ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String[]input = bufferedReader.readLine().split(" "); int n = Integer.parseInt(input[0]); int anInt = Integer.parseInt(input[1]); int[] arr = new int[n]; for (int i=0; i< n; i++) { arr[i] = i+1; } int i1; if (anInt != 1) { i1 = 1; while (anInt>0) { if (i1%2 == 0) { arr[i1] = arr[i1-1] - anInt; } else { arr[i1] = arr[i1-1] + anInt; } anInt--; i1++; } } for (int i = 0; i < arr.length; i++) { System.out.println(arr[i] + " "); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
8b5529a39ed4e2f0a10bf3473cd0881a
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.Scanner; public class Diverse_Permutation { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int n = sc.nextInt(); int k = sc.nextInt(); int i = 1; int j = n; int count = k - 1; int turn = 0; while(count >= 0) { if(turn == 0) { sb.append(i + " "); turn = 1; i++; } else { turn = 0; sb.append(j + " "); j--; } count--; } if(turn == 1) for (int l = i; l <= j; l++) { sb.append(l + " "); } else for (int l = i; l <= j; l++) { sb.append((n-l + 1) + " "); } System.out.println(sb); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
3f9aaf51c4287643b32051f85ddee6dd
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.*; import java.util.*; public class div275C { BufferedReader in; PrintWriter ob; StringTokenizer st; public static void main(String[] args) throws IOException { new div275C().run(); } void run() throws IOException { //in=new BufferedReader(new FileReader("input.txt")); in=new BufferedReader(new InputStreamReader(System.in)); ob=new PrintWriter(System.out); solve(); ob.flush(); } void solve() throws IOException { int n=ni(); int k=ni(); boolean vis[]=new boolean[n+1]; int start=1,j=k; List<Integer> list=new ArrayList<Integer>(); for(int i=1;;i++) { //System.out.println(" lol "+start); vis[start]=true; list.add(start); if(i%2==0) { start=start-j; } else { start=start+j; } --j; if(j==0) {list.add(start);break;} } //System.out.println(start); for(int i=1;i<=n;i++) { if(!vis[i]) { int x=Math.abs(i-start); if(x>=1 && x<=k) { list.add(i); start=i; } } } for(int i : list) ob.print(i+" "); } String ns() throws IOException { return nextToken(); } long nl() throws IOException { return Long.parseLong(nextToken()); } int ni() throws IOException { return Integer.parseInt(nextToken()); } double nd() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { if(st==null || !st.hasMoreTokens()) st=new StringTokenizer(in.readLine()); return st.nextToken(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
03ff1ec116c8ef668a13ad6fa5309dd5
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); int count=1; System.out.print(1); int temp=1; for(int i=k;i>0;i--){ temp +=i*count; System.out.print(" "+temp); count*=-1; } for(int i=2+k;i<=n;i++){ System.out.print(" "+i); } System.out.println(""); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
1322621c13dcad635015e03ed8414e79
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; public class Main extends PrintWriter { BufferedReader in; StringTokenizer stok; final Random rand = new Random(31); final int inf = (int) 1e9; final long linf = (long) 1e18; public void solve() throws IOException { int n = nextInt(); int k = nextInt(); print(1); int x = 1; for (int i = 1; i < k; i++) { if (i % 2 == 1) { x += (n - i); } else { x -= (n - i); } print(" "); print(x); } if (k % 2 == 1) { for (int i = k + 1; i <= n; i++) { print(" "); print(++x); } } else { for (int i = k + 1; i <= n; i++) { print(" "); print(--x); } } } public void run() { try { solve(); close(); } catch (Exception e) { e.printStackTrace(); System.exit(abs(-1)); } } Main() throws IOException { super(System.out); in = new BufferedReader(new InputStreamReader(System.in)); } Main(String s) throws IOException { super("".equals(s) ? "output.txt" : (s + ".out")); in = new BufferedReader(new FileReader("".equals(s) ? "input.txt" : (s + ".in"))); } public static void main(String[] args) throws IOException { try { Locale.setDefault(Locale.US); } catch (Exception ignored) { } new Main().run(); } String next() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = in.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int[] nextIntArray(int len) throws IOException { int[] a = new int[len]; for (int i = 0; i < len; i++) { a[i] = nextInt(); } return a; } void shuffle(int[] a) { for (int i = 1; i < a.length; i++) { int x = rand.nextInt(i + 1); int t = a[i]; a[i] = a[x]; a[x] = t; } } boolean nextPerm(int[] p) { for (int a = p.length - 2; a >= 0; --a) if (p[a] < p[a + 1]) for (int b = p.length - 1; ; --b) if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } return false; } <T> List<T>[] createAdjacencyList(int countVertex) { List<T>[] res = new List[countVertex]; for (int i = 0; i < countVertex; i++) { res[i] = new ArrayList<T>(); } return res; } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
a2758c197bc9297e8394aea2eb5590ac
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.StringTokenizer; import java.util.concurrent.LinkedBlockingDeque; import javax.swing.border.Border; public class a { public static long mod = (long) Math.pow(10, 9) + 7; private static class node implements Comparable<node> { int x; int y; node(int x, int c) { this.x = x; this.y = c; } @Override public int compareTo(node o) { if (o.x < x) return 1; else if (o.x > x) return -1; else if (o.y < y) return 1; else if (o.y > y) return -1; else return 0; } } public static long gcd(long a, long b) { if (b == 0) return a; else return gcd(b, a % b); } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringBuilder qq = new StringBuilder(); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); // int n = Integer.parseInt(in.readLine()); String y[] = in.readLine().split(" "); int n = Integer.parseInt(y[0]); int m = Integer.parseInt(y[1]); int step = m; qq.append(1 + " "); boolean arr[] = new boolean[n + 1]; int t = 1; arr[1] = true; int c = 0; while (step > 0) { if (c % 2 == 0) { t += step; qq.append(t + " "); arr[t] = true; } else { t -= step; qq.append(t + " "); arr[t] = true; } step--; c++; } for (int i = 1; i <= n; i++) { if (!arr[i]) qq.append(i + " "); } System.out.println(qq); // int x = Integer.parseInt(y[2]); // int x2 = Integer.parseInt(y[3]); // // long k1 = (long) Math.ceil((double) n / (double) ((double) x - 1)); // long k2 = (long) Math.ceil((double) m / (double) ((double) x2 - 1)); // // // // long f1 = (k1 - 1) * x - (k1 - 1); // long f2 = (k2 - 1) * x2 - (k2 - 1); // long t = n - f1; // long t2 = m - f2; // // long ans1 = (k1 - 1) * x + t; // long ans2 = (k2 - 1) * x2 + t2; // System.out.println(ans1 + " " + ans2); // if (gcd(ans1, ans2) == 1) { // // } else { // // } out.close(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
30cefb022ef01b4d9ac372a1ed99669e
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.*; import java.util.*; public class DiversePerm{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); long n,k; n=sc.nextLong(); k=sc.nextLong(); ArrayList<Long> al = new ArrayList<Long>(); long j=1; if(k%2==1){ for(long i=k+1;j<=(k+1)/2;i--,j++){ al.add(j); al.add(i); } } else{ long i=k+1; for(;j<=(k/2);j++,i--){ al.add(j); al.add(i); } al.add(i); } for(long i=k+2;i<=n;i++){ al.add(i); } for(long a : al){ System.out.print(a+" "); } System.out.println(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
10999fd891e37fd41db019c1787c7bc4
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.*; import java.util.*; public class c { private static StringTokenizer st; private static BufferedReader br; public static long MOD = 1000000007; public static void print(Object x) { System.out.println(x + ""); } public static String join(List<?> x, String space) { StringBuilder sb = new StringBuilder(); for (Object elt : x) { sb.append(elt); sb.append(space); } return sb.toString(); } public static String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine().trim()); } return st.nextToken(); } public static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public static long nextLong() throws IOException { return Long.parseLong(nextToken()); } public static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public static List<Double> nextDoubles(int n) throws IOException{ List<Double> lst = new ArrayList<Double>(); for (int i = 0; i < n; i++) { lst.add(nextDouble()); } return lst; } public static List<Integer> nextInts(int n) throws IOException{ List<Integer> lst = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { lst.add(nextInt()); } return lst; } public static List<Long> nextLongs(int n) throws IOException{ List<Long> lst = new ArrayList<Long>(); for (int i = 0; i < n; i++) { lst.add(nextLong()); } return lst; } public static List<Integer> emptyList(int n, int val) { List<Integer> lst = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { lst.add(val); } return lst; } public static void main(String[] args) throws Exception { boolean debug = false; InputStream in = System.in; if (debug) { in = new FileInputStream("input.in"); } br = new BufferedReader(new InputStreamReader(in)); print(solve(nextInt(), nextInt())); } public static String solve(int n, int k) { int[] nums = new int[n]; for (int i = 0; i <= k; i+=2) { nums[i] = (i / 2) + 1; } for (int i = 1; i <= k; i+= 2) { nums[i] = k - (i / 2) + 1; } for (int i = k+1; i < n; i++) { nums[i] = i + 1; } StringBuilder st = new StringBuilder(); for (int x: nums) { st.append(x + " "); } st.deleteCharAt(st.length()-1); return st.toString(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
91fb277dbaf10d1ea50430c1ebf696b3
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.Scanner; /** * Created by Константин on 24.10.2014. */ public class C { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int first = scanner.nextInt(); int k = scanner.nextInt(); int[] array = new int[first]; int i; for (i = 0; i < first; i++) { array[i] = i+1; } if(k!= 1){ i = 1; while (k > 0){ if(i%2 == 0){ array[i] = array[i - 1] - k; }else { array[i] = array[i - 1] + k; } k--; i++; } } for (int j = 0; j < array.length; j++) { System.out.print(array[j] + " "); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
c7fb3b2a12416a1554188b723032afd6
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class CF_275C { public static void main(String[] args) { FastScanner sc = new FastScanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int[] res = new int[n]; for(int i=0; i<=n-k; i++){ res[i] = i+1; } int low = n-k+1; int high = n; boolean flip = true; for(int i = low-1; i < n; i++){ if (flip){ res[i] = high; high--; } else { res[i] = low; low++; } flip = !flip; } PrintWriter out = new PrintWriter(System.out); out.print(res[0]); for(int i=1; i<n; i++){ out.print(" "+res[i]); } out.println(); out.flush(); out.close(); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
a2e72c213f4ab78d726d0dc8d567b7e3
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.*; import java.util.*; public class Diverse_Permutation { public static void main(String args[]) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.nextInt(); int k = sc.nextInt(); int[] a = new int[n]; boolean[] used = new boolean[n+1]; for(int i=0;i<n+1;i++) used[i]=false; used[0]=true; int seed=1; for(int i=0;i<n;i+=2){ a[i]=seed; seed++; } seed = n ; for(int i=1;i<n;i+=2){ a[i]=seed; seed--; } for(int i=0;i<k;i++) { out.print(a[i] + " "); used[a[i]]=true; seed=a[i]; } for(int i=0;i<n-k;i++){ // out.print("tejas\n"); if(!used[seed-1]) { out.print(seed-1 + " "); used[seed-1]=true; seed=seed-1; } else if(!used[seed+1]){ out.print(seed+1 + " "); used[seed+1]=true; seed=seed+1; } } out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //-------------------------------------------------------- }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
ea2292fdbd3c740819f67b5370b9ae70
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.*; import java.util.*; public class C { FastScanner in; PrintWriter out; int i = 0, j = 0; void solve() { /**************START**************/ int n = in.nextInt(), k = in.nextInt(); if (k == 1) { for (i = 1; i <= n; i++) { out.print(i + " "); } out.println(); return; } else if (k == 2) { out.print("1 3 2 "); for (i = 4; i <= n; i++) { out.print(i + " "); } out.println(); return; } else { int lim; if (k % 2 == 0) { lim = k/2; } else { lim = (k+1)/2; } for (i = 1; i <= lim; i++) { out.print(i + " " + (k+2-i) + " "); } if (k % 2 == 0) { out.print((k/2 + 1) + " "); } for (i = k + 2; i <= n; i++) { out.print(i + " "); } out.println(); } /***************END***************/ } public static void main(String[] args) { new C().runIO(); } void runIO() { in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } //This will empty out the current line (if non-empty) and return the next line down. If the next line is empty, will return the empty string. String nextLine() { st = null; String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
eb2234019581f9528129ca9abb85be03
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Collections.*; import static java.util.Arrays.*; public class CodeforcesRound275Div2ProblemC { static class Problem { Scanner reader; PrintWriter writer; Problem() { reader = new Scanner(System.in); writer = new PrintWriter(System.out); } Problem(File inputFile, File outputFile) throws IOException { reader = new Scanner(inputFile); writer = new PrintWriter(outputFile); } void runSolution() { solution(); writer.close(); } void solution() { int n = reader.nextInt(), k = reader.nextInt(); --k; int[] p = new int[n + 1]; if (k == 0) { for (int i = 0; i < n; i++) writer.print(i + 1 + " "); return; } if (k % 2 == 0) { k /= 2; //writer.println(k); int t = 1, added = 0, cur = n; for (int i = n; i > 0; i--) if (t == -1 && added < k) { added++; p[i] = added; t = -t; } else { p[i] = cur; cur--; t = -t; } for (int i = 1; i <= n; i++) writer.print(p[i] + " "); } else { k--; k /= 2; int t = 1, added = 0, cur = n; for (int i = n; i > 0; i--) if (t == -1 && added < k) { added++; p[i] = added; t = -t; } else { p[i] = cur; cur--; t = -t; } int x = p[1]; p[1] = p[2]; p[2] = x; for (int i = 1; i <= n; i++) writer.print(p[i] + " "); } } } public static void main(String args[]) throws IOException { Problem C = new Problem(); //Problem C = new Problem(new File("input.txt"), new File("output.txt")); C.runSolution(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
ec8ef71902a3425149c0c7187d7c3424
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.Scanner; public class C275 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); int l=1; int a=1; boolean[] b=new boolean[n+1]; StringBuilder ans=new StringBuilder(); ans.append(1); b[1]=true; while(k>0){ l+=k*a; ans.append(" ").append(l); b[l]=true; k--; a*=-1; } for(int i=1;i<n+1;i++){ if(!b[i]) ans.append(" ").append(i); } System.out.println(ans); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
52abbc2748df19476bdc7eddb148236d
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.OutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.File; import java.io.FileNotFoundException; import java.util.StringTokenizer; import java.io.Writer; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author zodiacLeo */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, FastScanner in, FastPrinter out) { int n = in.nextInt(); int k = in.nextInt(); if (k > n - 1) { out.println(-1); return; } int num = n; int[] a = new int[n]; for (int i = n - k; i < n; i += 2, num--) { a[i] = num; } for (int i = n - 1; i >= 0; i--) { if (a[i] == 0) { a[i] = num; num--; } } for (int i = 0; i < n; i++) { out.print(a[i] + " "); } out.println(); // n = 8; // a = new int[n]; // for (int i = 0; i < n; i++) // { // a[i] = i + 1; // } // do // { // TreeSet<Integer> set = new TreeSet<>(); // for (int i = 1; i < n; i++) // { // set.add(Math.abs(a[i] - a[i - 1])); // } // System.out.println(Arrays.toString(a) + " " + set.size()); // } while (nextPermutation()); } } static class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } } static class FastScanner { public BufferedReader br; public StringTokenizer st; public FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public String next() { while (st == null || !st.hasMoreElements()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
dce212b5d9514f82fc6a753df36aa526
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.OutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.File; import java.io.FileNotFoundException; import java.util.StringTokenizer; import java.io.Writer; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author zodiacLeo */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { private int n; private int[] a; public void solve(int testNumber, FastScanner in, FastPrinter out) { n = in.nextInt(); int k = in.nextInt(); a = new int[n]; int last = n; for (int i = n - k; i < n; i += 2) { a[i] = last; last--; } last = 1; for (int i = 0; i < n; i++) { if (a[i] == 0) { a[i] = last; last++; } } StringBuilder result = new StringBuilder(); for (int i = 0; i < n; i++) { result.append(a[i] + " "); } out.println(result); } } static class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } } static class FastScanner { public BufferedReader br; public StringTokenizer st; public FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public String next() { while (st == null || !st.hasMoreElements()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) { return null; } st = new StringTokenizer(s); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
5f781b487ff41c6483ac9c1c298c4033
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; /** * Created by Egor on 24/10/14. */ public class TaskC { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); String[] input = reader.readLine().split(" "); int n = Integer.parseInt(input[0]); int k = Integer.parseInt(input[1]); StringBuilder builder = new StringBuilder(); if (k != 1) { int lastNumber = n; int prevNumber = 0; for (int i = 0; i < k; i++) { prevNumber = lastNumber; if (i % 2 == 0) { lastNumber = i / 2 + 1; } else { lastNumber = n - (i - 1) / 2; } builder.append(lastNumber).append(" "); } int startIndex = lastNumber; int endIndex = prevNumber; if ((double) startIndex == (double) n / 2.0) { builder.append(startIndex + 1); } else { if ((double) startIndex < (double) n / 2.0) { startIndex++; endIndex--; } else { startIndex--; endIndex++; } if (startIndex < endIndex) { for (int i = startIndex; i <= endIndex; i++) { builder.append(i).append(" "); } } else { for (int i = startIndex; i >= endIndex; i--) { builder.append(i).append(" "); } } } } else { for (int i = 0; i < n; i++) { builder.append(i + 1).append(" "); } } writer.println(builder.toString()); writer.close(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
f4acf47dcae626c04eab2f56e9867b87
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.Scanner; /** * Created by hp on 7/29/2017. */ public class toto { static long []fib=new long[80]; static StringBuilder fib_s ; static boolean []valid=new boolean[700]; public static void main(String[] args) { InputReader in=new InputReader(System.in); PrintWriter out=new PrintWriter(System.out); int n=in.nextInt(); int k=in.nextInt(); int t=k; int sele=1; int c=1; while (t>=0) { out.print(c+" "); c+=sele*t; t--; sele*=-1; } for (int i=k+2;i<=n;i++) { out.print(i+" "); } out.close(); } public static boolean bs(long cnt1,long cnt2,long x,long y,long mid) { long cntxy = mid/ (x * y); long cntx = mid / x - cntxy; long cnty = mid / y - cntxy; long common = mid - cntx - cnty - cntxy; if (common + cnty < cnt1) return false; common -= Math.max(0, cnt1 - cnty); return common + cntx >= cnt2; } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
3aeca8177c79b469b84c807ef486fa91
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; import java.text.*; public class Main { public static Scanner in = new Scanner(System.in); public static PrintStream out = new PrintStream(System.out); public static BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String args[]) throws IOException{ int n=in.nextInt(),k=in.nextInt(); for(int i=1;i<(n-k);i++){ wr.write(i+" "); } int i=n-k; int j=n; while(i<j){ wr.write(i+" "+j+" "); i++;j--; } if (i==j){ wr.write(j+""); } wr.flush(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
6dda775c179e391d803fabaf66fb311e
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.Scanner; import java.io.PrintWriter; public class DiversePermutation { public static void main(String[] args) { Scanner scan = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = scan.nextInt(); int k = scan.nextInt(); int start = 1; int end = n; int last = 1; for(int i = 0; i < k; i++){ if(i % 2 == 0){ out.print(start+" "); last = start; start++; } else{ out.print(end+" "); last = end; end--; } } boolean check = k % 2 == 0; for(int i = k; i < n; i++){ if(check){ last--; } else{ last++; } out.print(last+" "); } out.flush(); out.close(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
2235517e42e0ccaf4acdde8bb8950b55
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.*; import java.io.*; //267630EY public class Main483C { static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) throws IOException { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); int[] a=new int[n]; int x=1; boolean flag=false; for(int i=0;i<n;i++) { a[i]=x; if(k==1&&flag) { x=i+1+1; } else if(i%2==0) { x=k+x; if(k==1) { flag=true; } else k-=1; } else if(i%2!=0) { x=x-k; if(k==1) flag=true; else k--; } } for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.flush(); } static class Scanner { BufferedReader br; StringTokenizer tk=new StringTokenizer(""); public Scanner(InputStream is) { br=new BufferedReader(new InputStreamReader(is)); } public int nextInt() throws IOException { if(tk.hasMoreTokens()) return Integer.parseInt(tk.nextToken()); tk=new StringTokenizer(br.readLine()); return nextInt(); } public long nextLong() throws IOException { if(tk.hasMoreTokens()) return Long.parseLong(tk.nextToken()); tk=new StringTokenizer(br.readLine()); return nextLong(); } public String next() throws IOException { if(tk.hasMoreTokens()) return (tk.nextToken()); tk=new StringTokenizer(br.readLine()); return next(); } public String nextLine() throws IOException { tk=new StringTokenizer(""); return br.readLine(); } public double nextDouble() throws IOException { if(tk.hasMoreTokens()) return Double.parseDouble(tk.nextToken()); tk=new StringTokenizer(br.readLine()); return nextDouble(); } public char nextChar() throws IOException { if(tk.hasMoreTokens()) return (tk.nextToken().charAt(0)); tk=new StringTokenizer(br.readLine()); return nextChar(); } 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 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[] nextIntArrayOneBased(int n) throws IOException { int a[]=new int[n+1]; for(int i=1;i<=n;i++) a[i]=nextInt(); return a; } public long[] nextLongArrayOneBased(int n) throws IOException { long a[]=new long[n+1]; for(int i=1;i<=n;i++) a[i]=nextLong(); return a; } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
b5d5241c0147bd36c6550b1f4e04abe4
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; public class Friends { private static Scanner input = new Scanner(System.in); public static void main(String[] args) { int n = input.nextInt(); int k = input.nextInt(); int[] a = new int[n]; a[0] = 1; int index = 1; int saveK= k; while (k != 0) { if(index % 2 != 0) { a[index] = a[index - 1] + k; } else{ a[index] = a[index - 1] - k; } index++; k--; } for(int i= 0; i < a.length; i++){ if(i < saveK+1) { System.out.print(a[i] + " "); } else{ System.out.print((i+1) + " "); } } System.out.println(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
b2db29b71c5084d3f284b77c6110c215
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.Scanner; public final class DiversePermutationCF { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); int ans[]=new int[n]; int left=1,right=1+k; int start=0; boolean ok=true; while(left<=right){ if(ok){ ans[start]=left++; ok=false; }else{ ans[start]=right--; ok=true; } start++; } for(int i=k+2;i<=n;i++){ ans[start]=i; start++; } for(int ele:ans) System.out.print(ele+" "); System.out.println(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
bca16928bbdd1cb3e5a01c0fb4be20b4
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.ObjectInputStream.GetField; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; public class Q2 { static long MOD = 1000000007; static boolean b[]; static ArrayList<Integer>[] amp; static int sum[],dist[]; static long ans = 0; static int p = 0; static FasterScanner sc = new FasterScanner(); static Queue<Integer> q = new LinkedList<>(); static ArrayList<String>[] arr; static ArrayList<Integer> parent = new ArrayList<>(); static BufferedWriter log; static HashMap<Long,Long> hm; static HashSet<String> hs = new HashSet<>(); static Stack<Integer> s = new Stack<>(); static Pair prr[]; public static void main(String[] args) throws IOException { log = new BufferedWriter(new OutputStreamWriter(System.out)); int n = sc.nextInt(), k = sc.nextInt(); int m = k; log.write(1+" "); boolean b = true; int i = 1, j = 0; while(k>0){ if(b){ log.write(i+k+" "); j = (i+k); b = false; } else{ b = true; log.write(j-k+" "); i++; } k--; } for(int z = (m+2);z<=n;z++){ log.write(z+" "); } log.close(); } private static void permutation(String prefix, String str) { int n = str.length(); if (n == 0) hs.add(prefix); else { for (int i = 0; i < n; i++) permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n)); } } public static char give(char c1,char c2){ if(c1!='a' && c2!='a') return 'a'; if(c1!='b' && c2!='b') return 'b'; return 'c'; } static class Graph{ int vertex; long weight; } public static void buildTree(int n){ int arr[] = sc.nextIntArray(n); for(int i = 0;i<n;i++){ int x = arr[i]-1; amp[i+1].add(x); amp[x].add(i+1); } } public static void bfs(int x,long arr[]){ b[x] = true; q.add(x); while(!q.isEmpty()){ int y = q.poll(); for(int i = 0;i<amp[y].size();i++) { if(!b[amp[y].get(i)]){ q.add(amp[y].get(i)); b[amp[y].get(i)] = true; } } } } public static void buildGraph(int n){ for(int i =0;i<n;i++){ int x = sc.nextInt(), y = sc.nextInt(); amp[--x].add(--y); amp[y].add(x); } } public static void dfs(int x){ b[x] = true; for(int i =0 ;i<amp[x].size();i++){ if(!b[amp[x].get(i)]){ dfs(amp[x].get(i)); } } } static class Pair implements Comparable<Pair> { int u; int v; int z; public Pair(){ } public Pair(int u, int v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31*hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return (u == other.u && v == other.v); } public int compareTo(Pair other) { return Integer.compare(Math.abs(u), Math.abs(other.u)) != 0 ? (Integer.compare(Math.abs(u), Math.abs(other.u))) : (Integer.compare(Math.abs(v), Math.abs(other.v))); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } static class SegmentTree{ int st[]; SegmentTree(int arr[], int n){ int size = 4*n+1; st = new int[size]; build(arr,0,n-1,0); } int build(int arr[], int ss, int se, int si){ if(ss==se){ st[si] = arr[ss]; return st[si]; } int mid = (ss+se)/2; st[si] = build(arr,ss,mid,si*2+1)^build(arr,mid+1,se,si*2+2); return st[si]; } int getXor(int qs, int qe, int ss, int se, int si){ if(qe<ss || qs>se){ return 0; } if(qs<=ss && qe>=se) return st[si]; int mid = (ss+se)/2; return getXor(qs,qe,ss,mid,2*si+1)^getXor(qs,qe, mid+1, se, 2*si+2); } void print(){ for(int i = 0;i<st.length;i++){ System.out.print(st[i]+" "); System.out.println("----------------------"); } } } static int convert(int x){ int cnt = 0; String str = Integer.toBinaryString(x); //System.out.println(str); for(int i = 0;i<str.length();i++){ if(str.charAt(i)=='1'){ cnt++; } } int ans = (int) Math.pow(3, 6-cnt); return ans; } static class Node2{ Node2 left = null; Node2 right = null; Node2 parent = null; int data; } static class BinarySearchTree{ Node2 root = null; int height = 0; int max = 0; int cnt = 1; ArrayList<Integer> parent = new ArrayList<>(); HashMap<Integer, Integer> hm = new HashMap<>(); public void insert(int x){ Node2 n = new Node2(); n.data = x; if(root==null){ root = n; } else{ Node2 temp = root,temp2 = null; while(temp!=null){ temp2 = temp; if(x>temp.data) temp = temp.right; else temp = temp.left; } if(x>temp2.data) temp2.right = n; else temp2.left = n; n.parent = temp2; parent.add(temp2.data); } } public Node2 getSomething(int x, int y, Node2 n){ if(n.data==x || n.data==y) return n; else if(n.data>x && n.data<y) return n; else if(n.data<x && n.data<y) return getSomething(x,y,n.right); else return getSomething(x,y,n.left); } public Node2 search(int x,Node2 n){ if(x==n.data){ max = Math.max(max, n.data); return n; } if(x>n.data){ max = Math.max(max, n.data); return search(x,n.right); } else{ max = Math.max(max, n.data); return search(x,n.left); } } public int getHeight(Node2 n){ if(n==null) return 0; height = 1+ Math.max(getHeight(n.left), getHeight(n.right)); return height; } } static long findDiff(long[] arr, long[] brr, int m){ int i = 0, j = 0; long fa = 1000000000000L; while(i<m && j<m){ long x = arr[i]-brr[j]; if(x>=0){ if(x<fa) fa = x; j++; } else{ if((-x)<fa) fa = -x; i++; } } return fa; } public static long max(long x, long y, long z){ if(x>=y && x>=z) return x; if(y>=x && y>=z) return y; return z; } public static void seive(long n){ b = new boolean[(int) (n+1)]; Arrays.fill(b, true); for(int i = 2;i*i<=n;i++){ if(b[i]){ for(int p = 2*i;p<=n;p+=i){ b[p] = false; } } } } static long modInverse(long a, long mOD2){ return power(a, mOD2-2, mOD2); } static long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y/2, m) % m; p = (p * p) % m; return (y%2 == 0)? p : (x * p) % m; } static long power2(long x,BigInteger y,long m){ long ans = 1; BigInteger two = new BigInteger("2"); while(y.compareTo(BigInteger.ZERO)>0){ if(y.getLowestSetBit()==y.bitCount()){ x = (x*x)%MOD; y = y.divide(two); } else{ ans = (ans*x)%MOD; y = y.subtract(BigInteger.ONE); } } return ans; } static BigInteger power2(BigInteger x, BigInteger y, BigInteger m){ BigInteger ans = new BigInteger("1"); BigInteger one = new BigInteger("1"); BigInteger two = new BigInteger("2"); BigInteger zero = new BigInteger("0"); while(y.compareTo(zero)>0){ //System.out.println(y); if(y.mod(two).equals(one)){ ans = ans.multiply(x).mod(m); y = y.subtract(one); } else{ x = x.multiply(x).mod(m); y = y.divide(two); } } return ans; } static BigInteger power(BigInteger x, BigInteger y, BigInteger m) { if (y.equals(0)) return (new BigInteger("1")); BigInteger p = power(x, y.divide(new BigInteger("2")), m).mod(m); p = (p.multiply(p)).mod(m); return (y.mod(new BigInteger("2")).equals(0))? p : (p.multiply(x)).mod(m); } static long d,x,y; public static void extendedEuclidian(long a, long b){ if(b == 0) { d = a; x = 1; y = 0; } else { extendedEuclidian(b, a%b); int temp = (int) x; x = y; y = temp - (a/b)*y; } } public static long gcd(long n, long m){ if(m!=0) return gcd(m,n%m); else return n; } public static class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = System.in.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 nextString() { 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 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 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 int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
2f35cbd5a652621f2051b2b8c0c831a4
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception{ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String[]input = bufferedReader.readLine().split(" "); int n = Integer.parseInt(input[0]); int anInt = Integer.parseInt(input[1]); int[] arr = new int[n]; for (int i=0; i< n; i++) { arr[i] = i+1; } int i1; if (anInt != 1) { i1 = 1; while (anInt>0) { if (i1%2 == 0) { arr[i1] = arr[i1-1] - anInt; } else { arr[i1] = arr[i1-1] + anInt; } anInt--; i1++; } } for (int i = 0; i < arr.length; i++) { if (arr[i] == 0) break; System.out.println(arr[i] + " "); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
9f3aaefab5e0848cf9c039fe30753976
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.*; import java.math.*; import java.io.PrintStream; import static java.lang.Math.*; public class Task275C { public static Scanner in = new Scanner(System.in); public static PrintStream out = System.out; public static void main(String[] args) { int n = in.nextInt(); int k = in.nextInt(); int prev = 1; int curr = prev; while (prev <= n) { k = min(k, n - prev); int sign = 1; for (int i = k; i >= 0; i--) { out.print(curr + " "); curr += i * sign; sign *= -1; } prev += k + 1; curr = prev; } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
072bcef0b9f57cb016d23601512daf77
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.*; public class GoodWork { static Scanner in =new Scanner(System.in); public static void main(String[] args) { int n=in.nextInt(); int k=in.nextInt(); for(int i=1;i<=(n+1)/2;i++){ System.out.print(i+" "); k--; if(k==0){ for(int u=i+1;u<=n-i+1;u++){System.out.print(u+" ");} break;} System.out.print(n-i+1+" "); k--; if(k==0){ for(int u=n-i;u>=i+1;u--){System.out.print(u+" ");} break;} } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
bf002447988238e1dd718a3a1b96c10a
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.*; import java.util.*; public class DiversePermutation { public static InputReader in; public static PrintWriter out; public static final int MOD = (int) (1e9 + 7); public static void main(String[] args) { in = new InputReader(System.in); out = new PrintWriter(System.out); int n = in.nextInt(), k = in.nextInt(); ArrayList<Integer> a = new ArrayList<Integer>(); if((k&1) == 0) { for(int i = 1; i <= (n-1)/2; i++) { a.add(i); a.add(n + 1 - i); } a.add((n+1)/2); if((n&1) == 0) a.add(n/2 + 1); int i = n - 1; for (; i > 0; i--) { if(Math.abs(a.get(i) - a.get(i-1)) > k) break; } i--; for (; i >= 0; i -= 2) { a.add(a.get(i)); a.set(i, -1); } } else { for(int i = 1; i <= (n-1)/2; i++) { a.add(n + 1 - i); a.add(i); } if((n&1) == 0) a.add(n/2 + 1); a.add((n+1)/2); int i = n - 1; for (; i > 0; i--) { if(Math.abs(a.get(i) - a.get(i-1)) > k) break; } i--; for (; i >= 0; i -= 2) { a.add(a.get(i)); a.set(i, -1); } } //int[] result = new int[n]; for (Integer ii : a) { if(ii == -1) continue; out.print(ii + " "); } out.close(); } static class Node implements Comparable<Node>{ int next; long dist; public Node (int u, int v) { this.next = u; this.dist = v; } public void print() { out.println(next + " " + dist + " "); } public int compareTo(Node n) { return Integer.compare(-this.next, -n.next); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
0378b75aafe382268f490a2d995516ac
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.*;import java.io.*;import java.math.*; public class Main { public static void process()throws IOException { int n=ni(); int k=ni(); int l=1,r=n; for(int i=0;i<k;i++) { if(i%2==0) { p(l+" "); l++; } else { p(r+" "); r--; } } if(k%2==1) for(int i=l;i<=r;i++) p(i+" "); else for(int i=r;i>=l;i--) p(i+" "); pn(""); } static AnotherReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { boolean oj = System.getProperty("ONLINE_JUDGE") != null; if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);} else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");} int t=1; // t=ni(); while(t-->0) {process();} out.flush();out.close(); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static void pni(Object o){out.println(o);out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;} static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
e0e1f9689f1c0edc2b40ad09ea48794f
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class c { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int k = in.nextInt(); int t = n; int s = -1; for (int i = 0; i < k; i++){ out.print(t); out.write(' '); t += (n - 1 - i) * s; s = -s; } t += (n - 1 - (k - 1)) * s; for (int i = k; i < n; i++){ t = t - s; out.print(t); out.write(' '); } in.close(); out.close(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
8778cbe2ce7902169e182749c097ab31
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; public class C483 { public static BufferedReader in; public static PrintWriter out; public static StringTokenizer tokenizer; public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; in = new BufferedReader(new InputStreamReader(inputStream), 32768); out = new PrintWriter(outputStream); solve(); out.close(); } public static void solve() { int n = nextInt(); int k = nextInt(); int am = 1; out.print(1 + " "); int l = 2; int r = n; while (am < k) { if (am % 2 == 1) { out.print(r-- + " "); } else { out.print(l++ + " "); } am++; } if (am % 2 == 1) { while (l <= r) { out.print(l++ + " "); } } else { while (l <= r) { out.print(r-- + " "); } } } public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(in.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public static int nextInt() { return Integer.parseInt(next()); } public static long nextLong() { return Long.parseLong(next()); } public static double nextDouble() { return Double.parseDouble(next()); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
3641ec4a56fc12e2bc92132f66b9870f
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.Scanner; public class C275 { public static void main(String argsp[]) { Scanner s=new Scanner(System.in); int n=s.nextInt();int k=s.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i+=2) { a[i]=n-i/2; } for(int i=1;i<n;i+=2) { a[i]=(int)Math.ceil(i/2.0); } if((k-1)%2==0) for(int i=k;i<n;i++) a[i]=a[i-1]-1; else for(int i=k;i<n;i++) a[i]=a[i-1]+1; StringBuilder ans=new StringBuilder(); for(int i=0;i<n;i++) ans.append(a[i]+" "); System.out.println(ans); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
b85c0b4fd06aad3333b58f878f4055e5
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { 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; } } /////////////////////////////////////////////////////////////////////////////////////////// // RRRRRRRRR AAA HHH HHH IIIIIIIIIIIII LLL // // RR RRR AAAAA HHH HHH IIIIIIIIIII LLL // // RR RRR AAAAAAA HHH HHH III LLL // // RR RRR AAA AAA HHHHHHHHHHH III LLL // // RRRRRR AAA AAA HHHHHHHHHHH III LLL // // RR RRR AAAAAAAAAAAAA HHH HHH III LLL // // RR RRR AAA AAA HHH HHH IIIIIIIIIII LLLLLLLLLLLL // // RR RRR AAA AAA HHH HHH IIIIIIIIIIIII LLLLLLLLLLLL // /////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args)throws IOException { PrintWriter out= new PrintWriter(System.out); Reader sc=new Reader(); int n=sc.i(); int k=sc.i(); int ans[]=new int[n]; if(k==1) { for(int i=1;i<=n;i++) out.print(i+" "); out.flush(); System.exit(0); } int start=1; int end=n; ans[0]=start; ans[1]=end; int value=1; int counter=2; int last=1; k--; int occu[]=new int[n+1]; occu[1]=1; occu[n]=1; while(k!=1) { if(counter%2==0) { ans[counter++]=1+value; occu[1+value]=1; } else { ans[counter++]=n-value; occu[n-value]=1; value++; } k--; } if(counter%2==1) for(int i=1;i<=n;i++) { if(occu[i]==0) ans[counter++]=i; if(counter==n) break; } else for(int i=n;i>=1;i--) { if(occu[i]==0) ans[counter++]=i; if(counter==n) break; } for(int i =0;i<n;i++)out.print(ans[i]+" "); out.flush(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
a60ff361997eac8c76355aa653819248
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); int n =in.nextInt(); int k = in.nextInt(); int[] a = new int[n]; for (int i=0;i<k/2;i++) { out.print(i + 1 + " "); out.print(n - i + " "); a[i]++; a[n - i - 1]++; } if (k%2==0) { for (int i=n-1;i>=0;i--) if (a[i]==0) out.print(i+1+" "); } else for (int i =0;i<n;i++) if (a[i]==0) out.print(i+1+" "); out.flush(); } } class Graph { int n; LinkedList<Integer>[] adjList; public Graph(int n) { this.n = n; adjList = new LinkedList[n]; for (int i = 0; i < n; i++) adjList[i] = new LinkedList<>(); } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 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 { 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 { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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 boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } 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(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
77d7e65f884140872d4251df45c4510d
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Arrays; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.PriorityQueue; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class C { static StringTokenizer st; static BufferedReader br; static PrintWriter pw; public static void main(String[] args) throws IOException { Locale.setDefault(Locale.US); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter( System.out))); int n = nextInt(), k = nextInt(), t = 0, l = 1, r = n; int a[] = new int[n + 1]; while (++t <= k) { if (t % 2 == 1) { a[t] = r--; } else { a[t] = l++; } } if (k % 2 == 0) { l = a[k] + 1; for (int i = k + 1; i <= n; i++) { a[i] = l++; } } else { r = a[k] - 1; for (int i = k + 1; i <= n; i++) { a[i] = r--; } } for (int i = 1; i <= n; i++) { pw.print(a[i] + " "); } pw.close(); /* * 6 2 6 1 2 3 4 5 6 3 6 1 5 4 3 2 6 4 6 1 5 2 3 4 6 5 6 1 5 2 4 3 */ } private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static double nextDouble() throws IOException { return Double.parseDouble(next()); } private static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
d71787aba620267970373e715910f93c
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.Scanner; public class DiversePermutation { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int largest = n; int smallest = 1; boolean altr = true; StringBuffer ans = new StringBuffer(); for (int i = 0; i < k ; i++) { if (altr) ans.append((largest--) + " "); else ans.append((smallest++) + " "); altr = !altr; } if (altr) { for (int i = smallest; i <= largest; i++) { ans.append(i + " "); } } else { for (int i = largest; i >= smallest; i--) { ans.append(i + " "); } } System.out.println(ans); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
b1ec1520a6c10f0f566014187000db4d
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
/* @@@@@@@@@@@@@@@@@@@@@@@ * @@@ Doston Akhmedov @@@ * @@@@@@@@@@@@@@@@@@@@@@@ */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Locale; import java.util.StringTokenizer; public class r_275_c { static StringTokenizer st; static BufferedReader br; static PrintWriter pw; static class sort implements Comparable<sort> { int a, b; public sort(int a, int b) { this.a = a; this.b = b; } public int compareTo(sort arg0) { if (this.a == arg0.a) return (this.b - arg0.b); return -(this.a - arg0.a); } } public static void main(String[] args) throws IOException { Locale.setDefault(Locale.US); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter( System.out))); // gfile(); int n = nextInt(), k = nextInt(),t=k; int l = 1, r = n, p = 0; int a[] = new int[n + 1]; for (int i = 1; i <= n; i++) { if (i % 2 == 1 && k > 1) { a[++p] = r; r--; k--; } else if (i % 2 == 0 && k > 1) { a[++p] = l; l++; k--; } } for (int i = l; i <= r; i++) { a[++p] = i; } int cnt = 0; k=t; boolean used[] = new boolean[n + 1]; for (int i = 1; i <= p - 1; i++) { if (!used[Math.abs(a[i + 1] - a[i])]) { cnt++; used[Math.abs(a[i + 1] - a[i])] = true; } } for (int i = 1; i <= p; i++) { if (cnt != k) { if (a[i] == a[i - 1]+1) { int q = a[i + 1]; a[i + 1] = a[i]; a[i] = q; cnt++; } } pw.print(a[i] + " "); } pw.close(); } private static void gfile() throws IOException { br = new BufferedReader(new FileReader(new File("input.txt"))); pw = new PrintWriter(new BufferedWriter(new FileWriter(new File( "output.txt")))); } private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static double nextDouble() throws IOException { return Double.parseDouble(next()); } private static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
8943aaa7866b58df5033e37f02b89205
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer st1 = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st1.nextToken()); int k = Integer.parseInt(st1.nextToken()); TreeSet<Integer> hs = new TreeSet<>(); for(int i = 1 ; i <= n ; i++) { hs.add(i); } StringBuilder sb = new StringBuilder(); sb.append(hs.pollLast() + " "); int curr = n; boolean flag = true; while(k > 0) { if(flag) { sb.append((curr = curr-k) +" "); hs.remove(new Integer(curr)); flag = false; } else { sb.append((curr = curr+k) +" "); hs.remove(new Integer(curr)); flag = true; } k--; } while(!hs.isEmpty()) sb.append(hs.pollLast() + " "); out.println(sb); out.flush(); out.close(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
743443d61a9d94efe83996b43dbd8fe2
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.StringTokenizer; public class ProblemC { static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int n = sc.nextInt(), k = sc.nextInt(); if ((k - 1) % 2 == 0) { int last = n; for (int i = 0, j = 1, x = (k - 1) / 2; i < n; j++, x--) { if (x <= 0) { pw.print(j + " "); i++; } else { pw.print(j + " " + last-- + " "); i += 2; } } } else { int last = n - 1; pw.print(n + " "); for (int i = 0, j = 1, x = (k - 1) / 2; i < n - 1; j++, x--) { if (x <= 0) { pw.print(j + " "); i++; } else { pw.print(j + " " + last-- + " "); i += 2; } } } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
0b93cac2b78db2f3bcfc618086a3e3ef
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.StringTokenizer; /** * Created by yujiahao on 7/22/16. */ public class cf_275_c { private FastScanner in; private PrintWriter out; public void solve() throws IOException { int n = in.nextInt(); int k = in.nextInt(); int[] res = new int[n]; int l = 1; int r = n; for (int i=0; i<res.length; i++){ if (i<k-1){ if (i%2==0) res[i] = l++; else res[i] = r--; }else{ if (k%2==1) res[i] = l++; else res[i] = r--; } } for (int i=0; i<res.length; i++){ out.print(res[i]+" "); } } public void run() { try { in = new FastScanner(); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } private class FastScanner { private BufferedReader br; private StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next());} } public static void main(String[] arg) { new cf_275_c().run(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
035e843692263e9871067aeb3eea59d2
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.PrintWriter; import java.util.Scanner; public class CF_5 { public static void main(String[] args){ PrintWriter pw = new PrintWriter(System.out, true); Scanner sc = new Scanner(System.in); int n, k, a ,c,d; n = sc.nextInt(); k = sc.nextInt(); int[] ans = new int[n+1]; boolean[] v = new boolean[n+1]; a = n-k; for(int i=1;i<=a;i++){ ans[i] = i; v[i] = true; } for(int i=a+1, b = k;i<=n;i++, b--){ c = ans[i-1]+b; d = ans[i-1]-b; if(c <= n && !v[c]){ v[c] = true; ans[i] = c; }else if(d > 0){ v[d] = true; ans[i] = d; } } for(int i=1;i<=n-1;i++){ pw.print(ans[i]+" "); } pw.println(ans[n]); pw.close(); sc.close(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
0b8379e74b3c86e4cb3d789ef7914082
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.*; public class ProC { static int n,k; static int[] aa=new int[100005]; public static void main(String[] args) { Scanner in=new Scanner(System.in); n=in.nextInt();k=in.nextInt(); for(int i=1;i<=k+1;i++) aa[i]=(i%2==1)?(i+1)/2:k-i/2+2; for(int i=k+2;i<=n;i++) aa[i]=i; for(int i=1;i<=n;i++) System.out.print(aa[i]+" "); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
d09180f786e3fba3b38defdfe5c745df
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Diverse_Permutation { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int n=in.nextInt(); int k=in.nextInt(); int []permutation =new int[n]; boolean []permutationVisited =new boolean[n]; for (int i = 0; i < n; i++) { permutation[i]=i+1; } for (int i = 0; i < permutation.length; i++) { if(i+k<permutation.length&&permutationVisited[i]==false &&i+k>=0){ System.out.print(permutation[i]+" "); permutationVisited[i]=true; if(permutationVisited[i+k]==false){ System.out.print(permutation[i+k]+" "); permutationVisited[i+k]=true; } k-=2; if(k<0) k=0; } } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
e99d5475310788a29dc2d6e69070b6e2
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class MainC { MyScanner sc = new MyScanner(); Scanner sc2 = new Scanner(System.in); final int MOD = 1000000007; int[] dx = { 1, 0, 0, -1 }; int[] dy = { 0, 1, -1, 0 }; void run() { int n = sc.nextInt(); int k = sc.nextInt(); int[] p = new int[n]; ArrayList<Integer> li = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { p[i] = i + 1; } int l = 0; int r = n - 1; int cnt = 0; boolean turn = true; while (cnt != k) { if (turn) { li.add(p[l]); l++; } else { li.add(p[r]); r--; } turn = !turn; cnt++; } if (!turn) { while (li.size() != n) { li.add(p[l++]); } } else { while (li.size() != n) { li.add(p[r--]); } } StringBuilder sb= new StringBuilder(); for(int i=0;i<li.size();i++) { if(i !=0) { sb.append(" " + li.get(i)); } else { sb.append(li.get(i)); } } System.out.println(sb); } public static void main(String[] args) { new MainC().run(); } void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } void debug2(int[][] array) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]); } System.out.println(); } } class MyScanner { int nextInt() { try { int c = System.in.read(); while (c != '-' && (c < '0' || '9' < c)) c = System.in.read(); if (c == '-') return -nextInt(); int res = 0; do { res *= 10; res += c - '0'; c = System.in.read(); } while ('0' <= c && c <= '9'); return res; } catch (Exception e) { return -1; } } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String next() { try { StringBuilder res = new StringBuilder(""); int c = System.in.read(); while (Character.isWhitespace(c)) c = System.in.read(); do { res.append((char) c); } while (!Character.isWhitespace(c = System.in.read())); return res.toString(); } catch (Exception e) { return null; } } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
36d3bf2377d02fb32f2166060d72454f
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Stack; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); String[] values = line.split(" "); int n = Integer.parseInt(values[0]); int k = Integer.parseInt(values[1]); int a = 1; int left = 1+k+1; int last = 1; for (int i = 0; i < n; i++) { System.out.print(last + " "); if (k == 0) { last = left; left++; } else { last = last + a*k; k--; a *= -1; } } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
67fe1355c19a200977d3028812c0bf36
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
//################################################################################################################ import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.math.BigInteger; import java.util.InputMismatchException; //################################################################################################################ public class R275D2C { public static void main(String[] args) { R275D2C c = new R275D2C(); InputReader1 reader = c.new InputReader1(System.in); OutputWriter writer = c.new OutputWriter(System.out); // put your code here int n = reader.readInt(); int k = reader.readInt(); StringBuilder sb = new StringBuilder(); int v1 = 1; int v2 = n; for (int i = 0; i < (k - 1); i++) { if (i % 2 == 0) { sb.append(v1 + " "); v1++; } else { sb.append(v2 + " "); v2--; } } if (k % 2 == 0) { // decreasing int s = (k / 2) + 1; int e = n - ((k / 2) - 1); for (int i = e; i >= s; i--) { sb.append(i + " "); } } else { // increasing int s = (k / 2) + 1; int e = n - (k / 2); for (int i = s; i <= e; i++) { sb.append(i + " "); } } sb.deleteCharAt(sb.length() - 1); writer.println(sb.toString()); reader.close(); writer.close(); } class InputReader1 { // private final boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader1(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 length = readInt(); if (length < 0) return null; byte[] bytes = new byte[length]; for (int i = 0; i < length; i++) bytes[i] = (byte) read(); try { return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { return new String(bytes); } } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuffer buf = new StringBuffer(); 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 boolean readBoolean() { return readInt() == 1; } public void close() { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream stream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter( stream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void println(String s) { writer.println(s); } public void println(int x) { writer.println(x); } public void print(int x) { writer.print(x); } public void println(long x) { writer.println(x); } public void printSpace() { writer.print(" "); } public void close() { writer.close(); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
b2746870b9282aaccbc770c20e415753
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); PrintWriter out = new PrintWriter(System.out); String[] t = in.readLine().split(" "); int n = Integer.parseInt(t[0]); int k = Integer.parseInt(t[1]); out.print("1 "); int jump = n-1; int dir = 1; int pi = 1; int left = n-1; for (int i=1;i<k;i++) { pi = pi + (dir)*jump; dir = dir*-1; jump--; left--; out.print(pi + " "); } while (left>0) { pi = pi + dir; out.print(pi+ " "); left--; } out.close(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
f665ba5498d75cbe391a08fd0b9543a3
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.Scanner; public class CF275C { public static void main(final String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int m = k / 2; StringBuilder sb = new StringBuilder(); int curr = 1; int last = n + 1; while (m > 0) { if (sb.length() > 0) { sb.append(" "); } sb.append(curr); last = n - curr + 1; sb.append(" "); sb.append(last); curr++; m--; } if (k % 2 == 1) { for (int i = curr; i < last; i++) { if (sb.length() > 0) { sb.append(" "); } sb.append(i); } } else { for (int i = last - 1; i >= curr; i--) { if (sb.length() > 0) { sb.append(" "); } sb.append(i); } } System.out.println(sb.toString()); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
b6b16bd58deea77e34475908142bff62
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.Scanner; /** * Created by sgeisenh on 10/24/14. */ public class Perm { public static void main(String args[]) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); int[] A = new int[n]; boolean up = false; for(int i = 0; i < n; i++) { if(i == 0) A[i] = 1; else if (k == 1) { if(up) A[i] = A[i - 1] - 1; else A[i] = A[i - 1] + 1; } else { if(i == 1) { A[i] = n; k--; if(k == 1) up = true; } else if (A[i - 2] > n / 2) { A[i] = A[i - 2] - 1; k--; if(k == 1) up = true; } else { A[i] = A[i - 2] + 1; k--; } } } for(int i = 0; i < n; i++) { System.out.print(A[i] + " "); } System.out.println(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
33a15d48b508c141dde6b99ea2d36ce6
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /** * * @author Aditya Joshi * SPOJ / CodeChef / Codeforces / HackerRank / Timus / UVA */ public class Main { private static MyScanner sc; private static PrintWriter out; public static void main(String[] args) { sc = new MyScanner(); out = new PrintWriter(System.out); int n = sc.nextInt(); int k = sc.nextInt(); StringBuilder answer = new StringBuilder(); if(k == 1) { for(int i = 1; i <= n; i++) answer.append(i).append(" "); out.println(answer.toString()); } else { boolean upper = true; int diff = n - 1; int curr = n; answer.append(n).append(" "); int init_k = k; int cntr = 1; while(k-- > 1) { if(upper) { answer.append((curr - diff)).append(" "); curr = (curr - diff--); upper = false; } else { answer.append((curr + diff)).append(" "); curr = (curr + diff--); upper = true; } cntr++; } if(init_k % 2 == 0) { while(cntr++ < n) answer.append((++curr)).append(" "); } else { while(cntr++ < n) answer.append((--curr)).append(" "); } out.println(answer.toString()); } out.close(); } private static int max(int a, int b) { if(a > b) return a; else return b; } private static int min(int a, int b) { if(a < b) return a; else return b; } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
424a93675f387319bef52911e9db942e
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); StringBuilder res = new StringBuilder(); int cnt = 1; int fi = 1; res.append(fi); int max = fi + k + 1; for (int i = k; i >= 1; i--) { if (((k-i) & 1) == 0) { res.append(" " + (fi + i)); fi += i; } else { res.append(" " + (fi - i)); fi -= i; } } for (int i = max; i <= n; i++) { res.append(" " + i); } out.println(res); } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; 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 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
0a4843065b8b3e1ee3b4241f599b984b
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; /** * Created on 10/24/14. */ public class CF275C { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(br.readLine()); int n = Integer.parseInt(tok.nextToken()); int k = Integer.parseInt(tok.nextToken()); if(k == 1) { for(int i = 1; i <= n; i++) { System.out.print(i + " "); } System.out.println(); System.exit(0); } for(int i = 1; i < n - k; i++) { System.out.print(i + " "); } int c = n - k; int sign = 1; for(int i = k; i >= 1; i--) { System.out.print(c + " "); c += sign * i; sign = -sign; } System.out.println(c); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
c2b4d52f36f615e267fe267e5e6bf815
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Main { public static void main(String srgs[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str[]=br.readLine().split(" "); int n=Integer.parseInt(str[0]); int mark[]=new int[n+1]; int k=Integer.parseInt(str[1]); int pos[]=new int[n+1]; for(int i=1;i<=k;i++) pos[i]=i; System.out.print("1 "); mark[1]=1; int num=0,last=1,val=0,val2=0,got=0; for(int i=1;i<=k;i++) { num=pos[k-i+1]; val=last-num; val2=last+num; got=0; if(val>0) { if(mark[val]==0) { System.out.print(val+" "); mark[val]=1; last=val; got=1; } } if(got==0) { if(val2<=n) { if(mark[val2]==0) { System.out.print(val2+" "); last=val2; mark[val2]=1; } } } } for(int i=1;i<=n;i++) { if(mark[i]==0) System.out.print(i+" "); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
603b966841decb11c092193ddb567583
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.Scanner; public class B { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int k = s.nextInt(); int b = 1; while (b <= n) { int f = b; int l = b + k <= n ? b + k : n; while (f < l) { System.out.print(f++ + " " + l-- + " "); } if (f == l) { System.out.print(f + " "); } b += k + 1; } System.out.println(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
1cac02f9f8fbf070938c58fb1e34ee07
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); StringBuilder res = new StringBuilder(); int cnt = 1; int fi = 1; res.append(fi); int max = fi + k + 1; for (int i = k; i >= 1; i--) { if (((k-i) & 1) == 0) { res.append(" " + (fi + i)); fi += i; } else { res.append(" " + (fi - i)); fi -= i; } } for (int i = max; i <= n; i++) { res.append(" " + i); } out.println(res); } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; 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 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
99390201f35e3085da8b249c9e32d16c
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.PrintWriter; import java.util.Scanner; public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int k = in.nextInt(); int[] A = new int[n]; A[0] = 1; int f = 1; int i; for (i = 1; i <= k; i++) { A[i] = A[i - 1] + f * (k - i + 1); f *= -1; } int current = k + 2; while (i < n) A[i++] = current++; for (int j = 0; j < n; j++) out.print(A[j] + " "); out.println(); out.flush(); out.close(); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
f454182796ddb94376fa1aadfaed822c
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; import java.util.StringTokenizer; /* public class _483C{ } */ public class _483C { private static PrintWriter printWriter; public void solve() throws FileNotFoundException { InputStream inputStream = System.in; InputHelper inputHelper = new InputHelper(inputStream); PrintStream out = System.out; // actual solution int n = inputHelper.readInteger(); int k = inputHelper.readInteger(); int diff = n - k - 1; String ans = ""; for (int i = 0, j = n; i < diff; i++, j--) { // ans += (j + " "); out.print(j + " "); } int an = k + 1; for (int i = 1, j = an; i <= j; i++, j--) { if (i == j) { // ans += (i + " "); out.print(i + " "); break; } // ans += (j + " "); out.print(j + " "); // ans += (i + " "); out.print(i + " "); } out.println(); // out.println(ans.substring(0, ans.length() - 1)); // end here } public static void main(String[] args) throws FileNotFoundException { (new _483C()).solve(); } class InputHelper { StringTokenizer tokenizer = null; private BufferedReader bufferedReader; public InputHelper(InputStream inputStream) { InputStreamReader inputStreamReader = new InputStreamReader( inputStream); bufferedReader = new BufferedReader(inputStreamReader, 16384); } public String read() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { String line = bufferedReader.readLine(); if (line == null) { return null; } tokenizer = new StringTokenizer(line); } catch (IOException e) { e.printStackTrace(); } } return tokenizer.nextToken(); } public Integer readInteger() { return Integer.parseInt(read()); } public Long readLong() { return Long.parseLong(read()); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
dd9a9c4b8db5e64c45df84023f1f7c7f
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; /* * 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 bavudaia */ public class Oct24_2 { public static int max(int a,int b) { return a>b?a:b; } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String S[] = br.readLine().split(" "); int N = Integer.parseInt(S[0]); int K = Integer.parseInt(S[1]); int start = 1,diff = K; int out = 0,val = 0; System.out.print(start); for(int i=1;i<=K;i++) { val = start+diff; System.out.print(" "+val); diff = (diff>0)?diff-1:diff+1; diff = -diff; start = val; } for(int i=K+2;i<=N;i++) { System.out.print(" "+i); } System.out.println(""); } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
cd6f56b12c8301c484becb2e869eaba7
train_003.jsonl
1414170000
Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
256 megabytes
import java.util.Scanner; public class CF275Cd2 { public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int k = scan.nextInt(); int[] result = new int[n]; int first = 1; int last = n; int i=0; if(k%2 == 0 && k != n-1){ result[0] = last; result[1] = first; k--; first++; last--; i=2; } for(; i < n; i++){ if(i%2 == 1 && k > 1){ result[i] = last--; k -= 2; } else { result[i] = first++; } } boolean firstline = true; for(Integer elem : result){ if(firstline){ firstline = false; } else { System.out.print(" "); } System.out.print("" + elem); } } }
Java
["3 2", "3 1", "5 2"]
1 second
["1 3 2", "1 2 3", "1 3 2 4 5"]
NoteBy |x| we denote the absolute value of number x.
Java 6
standard input
[ "constructive algorithms", "implementation" ]
18b3d8f57ecc2b392c7b1708f75a477e
The single line of the input contains two space-separated positive integers n, k (1 ≤ k &lt; n ≤ 105).
1,200
Print n integers forming the permutation. If there are multiple answers, print any of them.
standard output
PASSED
5b86c5aa490123f0d9c24f93ac107a5f
train_003.jsonl
1470323700
Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers a1, a2, ..., an of n elements!Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because of that she invited you to visit her and asked you to process m queries.Each query is processed in the following way: Two integers l and r (1 ≤ l ≤ r ≤ n) are specified — bounds of query segment. Integers, presented in array segment [l,  r] (in sequence of integers al, al + 1, ..., ar) even number of times, are written down. XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 are x1, x2, ..., xk, then Mishka wants to know the value , where  — operator of exclusive bitwise OR. Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Map; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); MishkaAndInterestingSum solver = new MishkaAndInterestingSum(); solver.solve(1, in, out); out.close(); } static class MishkaAndInterestingSum { Map<Integer, Integer> lastIdx; Query[] queries; int[] cnt; int[] ans; public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); int A[] = new int[N]; int prefixXor[] = new int[N + 1]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } int M = in.nextInt(); queries = new Query[M]; for (int i = 0; i < M; i++) { int left = in.nextInt() - 1; int right = in.nextInt() - 1; queries[i] = new Query(left, right, i); } Arrays.sort(queries); lastIdx = new HashMap<>(); cnt = new int[4 * N]; ans = new int[M]; int p = 0; for (int i = 0; i < N; i++) { prefixXor[i + 1] = prefixXor[i] ^ A[i]; } for (int i = 0; i < N; i++) { Integer idx = lastIdx.get(A[i]); if (idx != null) { update(idx, 0, 0, 0, N); } update(i, A[i], 0, 0, N); lastIdx.put(A[i], i); while (p < M && queries[p].right <= i) { Query q = queries[p]; ans[q.idx] = prefixXor[q.right + 1] ^ prefixXor[q.left] ^ get(q.left, q.right + 1, 0, 0, N); p++; } } for (int i = 0; i < M; i++) { out.println(ans[i]); } } private int get(int queryLeft, int queryRight, int id, int left, int right) { if (queryLeft >= right || queryRight <= left) return 0; if (left >= queryLeft && right <= queryRight) return cnt[id]; int mid = (left + right) / 2; int x1 = get(queryLeft, queryRight, 2 * id + 1, left, mid); int x2 = get(queryLeft, queryRight, 2 * id + 2, mid, right); return x1 ^ x2; } private void update(int idx, int val, int id, int left, int right) { if (idx < left || idx >= right) return; if (right - left == 1 && idx == left) { cnt[id] = val; return; } int mid = (left + right) / 2; if (idx < mid) { update(idx, val, 2 * id + 1, left, mid); } else { update(idx, val, 2 * id + 2, mid, right); } cnt[id] = cnt[2 * id + 1] ^ cnt[2 * id + 2]; } private class Query implements Comparable<Query> { private int left; private int right; private int idx; public Query(int left, int right, int idx) { this.left = left; this.right = right; this.idx = idx; } public int compareTo(Query o) { if (this.right == o.right) { return this.left - o.left; } return this.right - o.right; } } } 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\n3 7 8\n1\n1 3", "7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5"]
3.5 seconds
["0", "0\n3\n1\n3\n2"]
NoteIn the second sample:There is no integers in the segment of the first query, presented even number of times in the segment — the answer is 0.In the second query there is only integer 3 is presented even number of times — the answer is 3.In the third query only integer 1 is written down — the answer is 1.In the fourth query all array elements are considered. Only 1 and 2 are presented there even number of times. The answer is .In the fifth query 1 and 3 are written down. The answer is .
Java 8
standard input
[ "data structures" ]
6fe09ae0980bbc59230290c7ac0bc7c3
The first line of the input contains single integer n (1 ≤ n ≤ 1 000 000) — the number of elements in the array. The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — array elements. The third line of the input contains single integer m (1 ≤ m ≤ 1 000 000) — the number of queries. Each of the next m lines describes corresponding query by a pair of integers l and r (1 ≤ l ≤ r ≤ n) — the bounds of query segment.
2,100
Print m non-negative integers — the answers for the queries in the order they appear in the input.
standard output
PASSED
362a39e290f1d084c625c46a5e2baf62
train_003.jsonl
1470323700
Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers a1, a2, ..., an of n elements!Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because of that she invited you to visit her and asked you to process m queries.Each query is processed in the following way: Two integers l and r (1 ≤ l ≤ r ≤ n) are specified — bounds of query segment. Integers, presented in array segment [l,  r] (in sequence of integers al, al + 1, ..., ar) even number of times, are written down. XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 are x1, x2, ..., xk, then Mishka wants to know the value , where  — operator of exclusive bitwise OR. Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class q1 { public static void main(String[] args) throws Exception { InputReader in=new InputReader(System.in); PrintWriter pw=new PrintWriter(System.out); int n=in.nextInt(); int[] a=new int[n]; int[] prefix=new int[n]; for(int i=0;i<n;i++) { a[i]=in.nextInt(); } prefix[0]=a[0]; for(int i=1;i<n;i++) { prefix[i]=prefix[i-1]^a[i]; } int m=in.nextInt(); query[] qr=new query[m]; for(int i=0;i<m;i++) { qr[i]=new query(in.nextInt()-1,in.nextInt()-1,i); } HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>(); segmenttree st=new segmenttree(n); Arrays.sort(qr); int j=0; int[] aa=new int[m]; for(int i=0;i<n;i++) { if(hm.containsKey(a[i])) { int idx=hm.get(a[i]); st.update(0, 0, n-1, idx, 0); } st.update(0, 0,n-1, i,a[i]); hm.put(a[i], i); //System.out.println("for "+i+" j is "+j); while(j<m && qr[j].r==i) { int temp=st.query(0, 0, n-1, qr[j].l, qr[j].r); //System.out.println(temp+" for "+qr[j].l+" "+qr[j].r); temp^=prefix[qr[j].r]; if(qr[j].l>0) { temp^=prefix[qr[j].l-1]; } aa[qr[j].i]=temp; j++; } } for(int i=0;i<m;i++) { pw.println(aa[i]); } pw.close(); } static class query implements Comparable<query> { int l,r,i; public query(int l,int r,int i) { this.l=l; this.r=r; this.i=i; } @Override public int compareTo(query q1) { return Integer.compare(this.r, q1.r); } } static class segmenttree { int sz; int[] segment; int[] a; public segmenttree(int[] a) { this.sz=a.length; this.a=a; this.segment=new int[4*sz]; } public segmenttree(int sz) { this.sz=sz; segment=new int[4*sz]; } public void initialize(int c,int l,int r) { if(l>r) { return; } if(l==r) { segment[c]=a[l]; return; } else { int mid=(l+r)>>1; initialize(2*c+1,l,mid); initialize(2*c+2,mid+1,r); } } public void update(int c,int start,int end,int idx,int val) { if(start==end) { segment[c]=val; } else { int mid=(start+end)/2; if(idx<=mid) { update(2*c+1,start,mid,idx,val); } else { update(2*c+2,mid+1,end,idx,val); } segment[c]=segment[2*c+1]^segment[2*c+2]; } } public int query(int c,int start,int end,int ql,int qr) { if(start>qr || end<ql) { return 0; } if(start>=ql && end<=qr) { return segment[c]; } int mid=(start+end)/2; return query(2*c+1,start,mid,ql,qr)^query(2*c+2,mid+1,end,ql,qr); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3\n3 7 8\n1\n1 3", "7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5"]
3.5 seconds
["0", "0\n3\n1\n3\n2"]
NoteIn the second sample:There is no integers in the segment of the first query, presented even number of times in the segment — the answer is 0.In the second query there is only integer 3 is presented even number of times — the answer is 3.In the third query only integer 1 is written down — the answer is 1.In the fourth query all array elements are considered. Only 1 and 2 are presented there even number of times. The answer is .In the fifth query 1 and 3 are written down. The answer is .
Java 8
standard input
[ "data structures" ]
6fe09ae0980bbc59230290c7ac0bc7c3
The first line of the input contains single integer n (1 ≤ n ≤ 1 000 000) — the number of elements in the array. The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — array elements. The third line of the input contains single integer m (1 ≤ m ≤ 1 000 000) — the number of queries. Each of the next m lines describes corresponding query by a pair of integers l and r (1 ≤ l ≤ r ≤ n) — the bounds of query segment.
2,100
Print m non-negative integers — the answers for the queries in the order they appear in the input.
standard output
PASSED
fc8dd473de035e8145f90f8e38fbb1d3
train_003.jsonl
1470323700
Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers a1, a2, ..., an of n elements!Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because of that she invited you to visit her and asked you to process m queries.Each query is processed in the following way: Two integers l and r (1 ≤ l ≤ r ≤ n) are specified — bounds of query segment. Integers, presented in array segment [l,  r] (in sequence of integers al, al + 1, ..., ar) even number of times, are written down. XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 are x1, x2, ..., xk, then Mishka wants to know the value , where  — operator of exclusive bitwise OR. Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class Mishkaandinterestingsum implements Runnable{ BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); int seg[]; void init() throws FileNotFoundException { try { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } catch (Exception e) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine(), " :"); } catch (Exception e) { return null; } } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } int[] readIntArray(int size) throws IOException { int[] res = new int[size]; for (int i = 0; i < size; i++) { res[i] = readInt(); } return res; } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } public static void main(String[] args) { new Thread(null, new Mishkaandinterestingsum(), "", 1l * 200 * 1024 * 1024).start(); } long timeBegin, timeEnd; void time() { timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } long memoryTotal, memoryFree; void memory() { memoryFree = Runtime.getRuntime().freeMemory(); System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB"); } public void run() { try { timeBegin = System.currentTimeMillis(); memoryTotal = Runtime.getRuntime().freeMemory(); init(); solve(); out.close(); if (System.getProperty("ONLINE_JUDGE") == null) { time(); memory(); } } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } void solve() throws IOException { int n = readInt(); int a[]=new int[n]; seg=new int[4*n]; int i,j=0; int dp[]=new int[n]; for(i=0;i<n;i++) { a[i]=readInt(); if(i!=0) dp[i]=dp[i-1]^a[i]; else dp[i]=a[i]; } int m=readInt(); int ans[]=new int[m]; Query q[]=new Query[m]; for(i=0;i<m;i++) { int l=readInt()-1; int r=readInt()-1; q[i]=new Query(l,r,i); } Arrays.sort(q); // for(int t=0;t<q.length;t++) // { //System.out.println("l="+q[t].l+" r="+q[t].r); // } Map<Integer,Integer> h=new HashMap<Integer,Integer>(); for(i=0;i<n;i++) { if(h.containsKey(a[i])) { int index=h.get(a[i]); //out.println("value "+a[i]+"at index="+index); update(0,n-1,index,index,0,0); } h.put(a[i],i); int index=h.get(a[i]); //out.println("value "+a[i]+"at index="+index); update(0,n-1,i,i,0,a[i]); for(;j<m &&q[j].r==i;j++) { int k=find(0,n-1,q[j].l,q[j].r,0); k^=dp[q[j].r]; if(q[j].l!=0) k^=dp[q[j].l-1]; ans[q[j].i]=k; //out.println("Seqment tree"); // for(int t=0;t<seg.length;t++) // { //out.print(seg[t]+" "); //} } } for(i=0;i<m;i++) { out.println(ans[i]); } out.close(); } public void update(int a,int b,int s,int e,int root,int v) { if(a>e || b<s) return; else if(a==b) seg[root]=v; else { int mid=(a+b)/2; update(a,mid,s,e,2*root+1,v); update(mid+1,b,s,e,2*root+2,v); seg[root]=seg[2*root+1]^seg[2*root+2]; } } public int find(int a,int b,int s,int e,int root) { if(s<=a && b<=e) return seg[root]; int mid=(a+b)>>1; if(mid>=e) return find(a,mid,s,e,2*root+1); if(mid<s) return find(mid+1,b,s,e,2*root+2); return find(a,mid,s,e,2*root+1)^find(mid+1,b,s,e,2*root+2); } class Query implements Comparable<Query> { int l; int r; int i; Query(int l,int r,int i) { this.l=l; this.r=r; this.i=i; } public int compareTo(Query o) { if(this.r!=o.r) return this.r-o.r; else return this.l-o.l; } } }
Java
["3\n3 7 8\n1\n1 3", "7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5"]
3.5 seconds
["0", "0\n3\n1\n3\n2"]
NoteIn the second sample:There is no integers in the segment of the first query, presented even number of times in the segment — the answer is 0.In the second query there is only integer 3 is presented even number of times — the answer is 3.In the third query only integer 1 is written down — the answer is 1.In the fourth query all array elements are considered. Only 1 and 2 are presented there even number of times. The answer is .In the fifth query 1 and 3 are written down. The answer is .
Java 8
standard input
[ "data structures" ]
6fe09ae0980bbc59230290c7ac0bc7c3
The first line of the input contains single integer n (1 ≤ n ≤ 1 000 000) — the number of elements in the array. The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — array elements. The third line of the input contains single integer m (1 ≤ m ≤ 1 000 000) — the number of queries. Each of the next m lines describes corresponding query by a pair of integers l and r (1 ≤ l ≤ r ≤ n) — the bounds of query segment.
2,100
Print m non-negative integers — the answers for the queries in the order they appear in the input.
standard output
PASSED
7476d18cd1bd3ffa8584c46480c9c933
train_003.jsonl
1470323700
Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers a1, a2, ..., an of n elements!Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because of that she invited you to visit her and asked you to process m queries.Each query is processed in the following way: Two integers l and r (1 ≤ l ≤ r ≤ n) are specified — bounds of query segment. Integers, presented in array segment [l,  r] (in sequence of integers al, al + 1, ..., ar) even number of times, are written down. XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 are x1, x2, ..., xk, then Mishka wants to know the value , where  — operator of exclusive bitwise OR. Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class Mishkaandinterestingsum implements Runnable{ BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); int seg[]; void init() throws FileNotFoundException { try { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } catch (Exception e) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine(), " :"); } catch (Exception e) { return null; } } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } int[] readIntArray(int size) throws IOException { int[] res = new int[size]; for (int i = 0; i < size; i++) { res[i] = readInt(); } return res; } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } public static void main(String[] args) { new Thread(null, new Mishkaandinterestingsum(), "", 1l * 200 * 1024 * 1024).start(); } long timeBegin, timeEnd; void time() { timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } long memoryTotal, memoryFree; void memory() { memoryFree = Runtime.getRuntime().freeMemory(); System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB"); } public void run() { try { timeBegin = System.currentTimeMillis(); memoryTotal = Runtime.getRuntime().freeMemory(); init(); solve(); out.close(); if (System.getProperty("ONLINE_JUDGE") == null) { time(); memory(); } } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } void solve() throws IOException { int n = readInt(); int a[]=new int[n]; seg=new int[4*n]; int i,j=0; int dp[]=new int[n]; for(i=0;i<n;i++) { a[i]=readInt(); if(i!=0) dp[i]=dp[i-1]^a[i]; else dp[i]=a[i]; } int m=readInt(); int ans[]=new int[m]; Query q[]=new Query[m]; for(i=0;i<m;i++) { int l=readInt()-1; int r=readInt()-1; q[i]=new Query(l,r,i); } Arrays.sort(q); // for(int t=0;t<q.length;t++) // { //System.out.println("l="+q[t].l+" r="+q[t].r); // } Map<Integer,Integer> h=new HashMap<Integer,Integer>(); for(i=0;i<n;i++) { if(h.containsKey(a[i])) { int index=h.get(a[i]); //out.println("value "+a[i]+"at index="+index); update(0,n-1,index,0,0); } h.put(a[i],i); //out.println("value "+a[i]+"at index="+index); update(0,n-1,i,0,a[i]); for(;j<m &&q[j].r==i;j++) { int k=find(0,n-1,q[j].l,q[j].r,0); k^=dp[q[j].r]; if(q[j].l!=0) k^=dp[q[j].l-1]; ans[q[j].i]=k; //out.println("Seqment tree"); // for(int t=0;t<seg.length;t++) // { //out.print(seg[t]+" "); //} } } for(i=0;i<m;i++) { out.println(ans[i]); } out.close(); } public void update(int a,int b,int loc,int root,int v) { if(a==b){ seg[root]=v; return; } int mid=(a+b)>>1; if(loc<=mid) update(a,mid,loc,2*root+1,v); else update(mid+1,b,loc,2*root+2,v); seg[root]=seg[2*root+1]^seg[2*root+2]; } public int find(int a,int b,int s,int e,int root) { if(s<=a && b<=e) return seg[root]; int mid=(a+b)>>1; if(mid>=e) return find(a,mid,s,e,2*root+1); if(mid<s) return find(mid+1,b,s,e,2*root+2); return find(a,mid,s,e,2*root+1)^find(mid+1,b,s,e,2*root+2); } class Query implements Comparable<Query> { int l; int r; int i; Query(int l,int r,int i) { this.l=l; this.r=r; this.i=i; } public int compareTo(Query o) { if(this.r!=o.r) return this.r-o.r; else return this.l-o.l; } } }
Java
["3\n3 7 8\n1\n1 3", "7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5"]
3.5 seconds
["0", "0\n3\n1\n3\n2"]
NoteIn the second sample:There is no integers in the segment of the first query, presented even number of times in the segment — the answer is 0.In the second query there is only integer 3 is presented even number of times — the answer is 3.In the third query only integer 1 is written down — the answer is 1.In the fourth query all array elements are considered. Only 1 and 2 are presented there even number of times. The answer is .In the fifth query 1 and 3 are written down. The answer is .
Java 8
standard input
[ "data structures" ]
6fe09ae0980bbc59230290c7ac0bc7c3
The first line of the input contains single integer n (1 ≤ n ≤ 1 000 000) — the number of elements in the array. The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — array elements. The third line of the input contains single integer m (1 ≤ m ≤ 1 000 000) — the number of queries. Each of the next m lines describes corresponding query by a pair of integers l and r (1 ≤ l ≤ r ≤ n) — the bounds of query segment.
2,100
Print m non-negative integers — the answers for the queries in the order they appear in the input.
standard output
PASSED
194f585f19938a6e9d7fc8d3050961cb
train_003.jsonl
1470323700
Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers a1, a2, ..., an of n elements!Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because of that she invited you to visit her and asked you to process m queries.Each query is processed in the following way: Two integers l and r (1 ≤ l ≤ r ≤ n) are specified — bounds of query segment. Integers, presented in array segment [l,  r] (in sequence of integers al, al + 1, ..., ar) even number of times, are written down. XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 are x1, x2, ..., xk, then Mishka wants to know the value , where  — operator of exclusive bitwise OR. Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class Mishkaandinterestingsum implements Runnable{ BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); int seg[]; void init() throws FileNotFoundException { try { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } catch (Exception e) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine(), " :"); } catch (Exception e) { return null; } } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } int[] readIntArray(int size) throws IOException { int[] res = new int[size]; for (int i = 0; i < size; i++) { res[i] = readInt(); } return res; } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } public static void main(String[] args) { new Thread(null, new Mishkaandinterestingsum(), "", 1l * 200 * 1024 * 1024).start(); } long timeBegin, timeEnd; void time() { timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } long memoryTotal, memoryFree; void memory() { memoryFree = Runtime.getRuntime().freeMemory(); System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB"); } public void run() { try { timeBegin = System.currentTimeMillis(); memoryTotal = Runtime.getRuntime().freeMemory(); init(); solve(); out.close(); if (System.getProperty("ONLINE_JUDGE") == null) { time(); memory(); } } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } void solve() throws IOException { int n = readInt(); int a[]=new int[n]; seg=new int[4*n]; int i,j=0; int dp[]=new int[n]; for(i=0;i<n;i++) { a[i]=readInt(); if(i!=0) dp[i]=dp[i-1]^a[i]; else dp[i]=a[i]; } int m=readInt(); int ans[]=new int[m]; Query q[]=new Query[m]; for(i=0;i<m;i++) { int l=readInt()-1; int r=readInt()-1; q[i]=new Query(l,r,i); } Arrays.sort(q); // for(int t=0;t<q.length;t++) // { //System.out.println("l="+q[t].l+" r="+q[t].r); // } Map<Integer,Integer> h=new HashMap<Integer,Integer>(); for(i=0;i<n;i++) { if(h.containsKey(a[i])) { int index=h.get(a[i]); //out.println("value "+a[i]+"at index="+index); update(0,n-1,index,index,0,0); } h.put(a[i],i); int index=h.get(a[i]); //out.println("value "+a[i]+"at index="+index); update(0,n-1,i,i,0,a[i]); for(;j<m &&q[j].r==i;j++) { int k=find(0,n-1,q[j].l,q[j].r,0); k^=dp[q[j].r]; if(q[j].l!=0) k^=dp[q[j].l-1]; ans[q[j].i]=k; //out.println("Seqment tree"); // for(int t=0;t<seg.length;t++) // { //out.print(seg[t]+" "); //} } } for(i=0;i<m;i++) { out.println(ans[i]); } out.close(); } public void update(int a,int b,int s,int e,int root,int v) { if(a>e || b<s) return; else if(a==b) seg[root]=v; else { int mid=(a+b)/2; update(a,mid,s,e,2*root+1,v); update(mid+1,b,s,e,2*root+2,v); seg[root]=seg[2*root+1]^seg[2*root+2]; } } public int find(int a,int b,int s,int e,int root) { if(a>e || b<s) return 0; else if(s<=a && b<=e) return seg[root]; else { int mid=(a+b)/2; return (find(a,mid,s,e,2*root+1)^find(mid+1,b,s,e,2*root+2)); } } class Query implements Comparable<Query> { int l; int r; int i; Query(int l,int r,int i) { this.l=l; this.r=r; this.i=i; } public int compareTo(Query o) { if(this.r!=o.r) return this.r-o.r; else return this.l-o.l; } } }
Java
["3\n3 7 8\n1\n1 3", "7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5"]
3.5 seconds
["0", "0\n3\n1\n3\n2"]
NoteIn the second sample:There is no integers in the segment of the first query, presented even number of times in the segment — the answer is 0.In the second query there is only integer 3 is presented even number of times — the answer is 3.In the third query only integer 1 is written down — the answer is 1.In the fourth query all array elements are considered. Only 1 and 2 are presented there even number of times. The answer is .In the fifth query 1 and 3 are written down. The answer is .
Java 8
standard input
[ "data structures" ]
6fe09ae0980bbc59230290c7ac0bc7c3
The first line of the input contains single integer n (1 ≤ n ≤ 1 000 000) — the number of elements in the array. The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — array elements. The third line of the input contains single integer m (1 ≤ m ≤ 1 000 000) — the number of queries. Each of the next m lines describes corresponding query by a pair of integers l and r (1 ≤ l ≤ r ≤ n) — the bounds of query segment.
2,100
Print m non-negative integers — the answers for the queries in the order they appear in the input.
standard output
PASSED
06c16b52e605e5c54ba0aec5e71cfe62
train_003.jsonl
1470323700
Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers a1, a2, ..., an of n elements!Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because of that she invited you to visit her and asked you to process m queries.Each query is processed in the following way: Two integers l and r (1 ≤ l ≤ r ≤ n) are specified — bounds of query segment. Integers, presented in array segment [l,  r] (in sequence of integers al, al + 1, ..., ar) even number of times, are written down. XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 are x1, x2, ..., xk, then Mishka wants to know the value , where  — operator of exclusive bitwise OR. Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented.
256 megabytes
import java.io.*; import java.util.*; public class Program { static BufferedReader in; static PrintWriter out; static StringTokenizer tok = new StringTokenizer(""); public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } private static String readString() { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return tok.nextToken(); } private static int readInt() { return Integer.parseInt(readString()); } private static double readDouble() { return Double.parseDouble(readString()); } private static long readLong() { return Long.parseLong(readString()); } static long MAX = Long.MAX_VALUE; private static void solve() throws IOException { int n = readInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = readInt(); } Fenwik tree = new Fenwik(n + 1); for (int i = 0; i < n; i++) { tree.setValueOdd(i, a[i]); } int[] lastTimeSeen = new int[n]; Map<Integer, Integer> map = new HashMap<>(n); boolean[] isFirst = new boolean[n + 1]; for (int i = n - 1; i >= 0; i--) { int lastTime = map.getOrDefault(a[i], n); isFirst[lastTime] = true; lastTimeSeen[i] = lastTime; map.put(a[i], i); } for (int i = 0; i < n; i++) { if (!isFirst[i]) { tree.setValueUnique(i, a[i]); } } int m = readInt(); Request[] requests = new Request[m]; for (int i = 0; i < m; i++) { requests[i] = new Request(readInt() - 1, readInt() - 1, i); } Arrays.sort(requests); int pointer = 0; int[] ans = new int[m]; for (int left = 0; left < n; left++) { while (pointer < m && requests[pointer].left == left) { int xor = tree.getSum(requests[pointer].left - 1) ^ tree.getSum(requests[pointer].right); ans[requests[pointer].index] = xor; pointer++; } tree.setValueUnique(lastTimeSeen[left], a[left]); } for (int i = 0; i < m; i++) { out.println(ans[i]); } } static class Request implements Comparable<Request> { int left, right, index; public Request(int left, int right, int index) { this.left = left; this.right = right; this.index = index; } @Override public int compareTo(Request o) { int t = Integer.compare(this.left, o.left); return t == 0 ? Integer.compare(this.right, o.right) : t; } } static class Fenwik { int[] t_odd; int[] t_unique; Fenwik(int n) { t_odd = new int[n]; t_unique = new int[n]; } void setValueOdd(int i, int val) { while (i < t_odd.length) { t_odd[i] ^= val; i |= i + 1; } } void setValueUnique(int i, int val) { while (i < t_odd.length) { t_odd[i] ^= val; i |= i + 1; } } int getSum(int r) { int xor = 0; while (r >= 0) { xor ^= t_odd[r] ^ t_unique[r]; r = (r & (r + 1)) - 1; } return xor; } } static class SegmentTree { int n; int[] t_odd; int[] t_unique; public SegmentTree(int n) { this.n = n; this.t_odd = new int[n * 4]; this.t_unique = new int[n * 4]; } int getSum(int idx, int l, int r, int L, int R) { if (l == L && r == R) { return t_odd[idx] ^ t_unique[idx]; } int mid = (l + r) / 2; int res = 0; if (L < mid) { res ^= getSum(idx * 2 + 1, l, mid, L, Math.min(mid, R)); } if (R > mid) { res ^= getSum(idx * 2 + 2, mid, r, Math.max(mid, L), R); } return res; } void setValueOdd(int idx, int l, int r, int index, int value) { if (r - l == 1) { t_odd[idx] = value; return; } int mid = (l + r) / 2; if (index < mid) { setValueOdd(idx * 2 + 1, l, mid, index, value); } else { setValueOdd(idx * 2 + 2, mid, r, index, value); } t_odd[idx] = t_odd[idx * 2 + 1] ^ t_odd[idx * 2 + 2]; } void setValueUnique(int idx, int l, int r, int index, int value) { if (r - l == 1) { t_unique[idx] = value; return; } int mid = (l + r) / 2; if (index < mid) { setValueUnique(idx * 2 + 1, l, mid, index, value); } else { setValueUnique(idx * 2 + 2, mid, r, index, value); } t_unique[idx] = t_unique[idx * 2 + 1] ^ t_unique[idx * 2 + 2]; } } }
Java
["3\n3 7 8\n1\n1 3", "7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5"]
3.5 seconds
["0", "0\n3\n1\n3\n2"]
NoteIn the second sample:There is no integers in the segment of the first query, presented even number of times in the segment — the answer is 0.In the second query there is only integer 3 is presented even number of times — the answer is 3.In the third query only integer 1 is written down — the answer is 1.In the fourth query all array elements are considered. Only 1 and 2 are presented there even number of times. The answer is .In the fifth query 1 and 3 are written down. The answer is .
Java 8
standard input
[ "data structures" ]
6fe09ae0980bbc59230290c7ac0bc7c3
The first line of the input contains single integer n (1 ≤ n ≤ 1 000 000) — the number of elements in the array. The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — array elements. The third line of the input contains single integer m (1 ≤ m ≤ 1 000 000) — the number of queries. Each of the next m lines describes corresponding query by a pair of integers l and r (1 ≤ l ≤ r ≤ n) — the bounds of query segment.
2,100
Print m non-negative integers — the answers for the queries in the order they appear in the input.
standard output
PASSED
c67889eaccbabeb4a71c5a746b59841b
train_003.jsonl
1470323700
Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers a1, a2, ..., an of n elements!Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because of that she invited you to visit her and asked you to process m queries.Each query is processed in the following way: Two integers l and r (1 ≤ l ≤ r ≤ n) are specified — bounds of query segment. Integers, presented in array segment [l,  r] (in sequence of integers al, al + 1, ..., ar) even number of times, are written down. XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 are x1, x2, ..., xk, then Mishka wants to know the value , where  — operator of exclusive bitwise OR. Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * @author pttrung */ public class D_Round_365_Div2 { public static long MOD = 1000000007; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int n = in.nextInt(); int[] data = new int[n]; int[] list = new int[n]; int[] count = new int[n]; HashMap<Integer, Integer> map = new HashMap(); int index = 0; for (int i = 0; i < n; i++) { data[i] = in.nextInt(); if (!map.containsKey(data[i])) { map.put(data[i], index); list[index] = data[i]; index++; } data[i] = map.get(data[i]); } int m = in.nextInt(); int[][] q = new int[m][3]; for (int i = 0; i < m; i++) { q[i] = new int[]{in.nextInt(), in.nextInt() , i}; } Arrays.sort(q, (a, b)->Integer.compare(a[1], b[1])); long[] result = new long[m]; int[]last = new int[n]; Arrays.fill(last,-1); FT tree = new FT(n + 1); index = 0; int[]sum = new int[n]; for(int i = 0 ; i < n; i++){ sum[i] ^= list[data[i]]; if(i > 0){ sum[i] ^= sum[i - 1]; } if(last[data[i]] != -1){ tree.update(last[data[i]] + 1, list[data[i]]); } tree.update(i + 1, list[data[i]]); last[data[i]] = i; while(index < m && q[index][1] == i + 1){ long v = sum[i] ^ tree.get(q[index][1]); if(q[index][0] > 1){ v ^= sum[q[index][0] - 2] ^tree.get(q[index][0] - 1); } result[q[index][2]] = v; index++; } } for (long v : result) { out.println(v); } out.close(); } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int hashCode() { int hash = 5; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Point other = (Point) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(Point o) { return Integer.compare(x, o.x); } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] ^= value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result ^= data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, long b, long MOD) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2, MOD); if (b % 2 == 0) { return val * val % MOD; } else { return val * (val * a % MOD) % MOD; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
Java
["3\n3 7 8\n1\n1 3", "7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5"]
3.5 seconds
["0", "0\n3\n1\n3\n2"]
NoteIn the second sample:There is no integers in the segment of the first query, presented even number of times in the segment — the answer is 0.In the second query there is only integer 3 is presented even number of times — the answer is 3.In the third query only integer 1 is written down — the answer is 1.In the fourth query all array elements are considered. Only 1 and 2 are presented there even number of times. The answer is .In the fifth query 1 and 3 are written down. The answer is .
Java 8
standard input
[ "data structures" ]
6fe09ae0980bbc59230290c7ac0bc7c3
The first line of the input contains single integer n (1 ≤ n ≤ 1 000 000) — the number of elements in the array. The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — array elements. The third line of the input contains single integer m (1 ≤ m ≤ 1 000 000) — the number of queries. Each of the next m lines describes corresponding query by a pair of integers l and r (1 ≤ l ≤ r ≤ n) — the bounds of query segment.
2,100
Print m non-negative integers — the answers for the queries in the order they appear in the input.
standard output