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
4109eb35b9d91cc04c5bdd70b511a35f
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class main{ private static int minimum(int [] input){ int result = 0; int flag = 0; for (int i = 0; i < input.length; i++) { if(input[i] == 1){ result++; if(i != input.length-1) if (input[i+1] == 0) { if(i+1 != input.length-1){ if (input[i+2] == 0) { flag = 1; result++; i++; int j=i; while(j<input.length) { if(input[i] != 0){ i--; break; } if(input.length-1 == j) result--; i++; j++; } } if(flag == 0 && input[i+2] == 1 ){ result++; } flag = 0; } } } } return result; } public static void main(String [] args){ Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int input [] = new int[n]; for (int i = 0; i < input.length; i++) { input[i] = scan.nextInt(); } System.out.println(minimum(input)); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 8
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≀ n ≀ 1000) β€” the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) β€” the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number β€” the minimum number of operations needed to make all the letters read.
standard output
PASSED
2e84cebfea0d87c3b09f84b352bff144
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class main{ private static int minimum(int [] input){ int result = 0; int flag = 0; for (int i = 0; i < input.length; i++) { if(input[i] == 1){ result++; if(i != input.length-1) if (input[i+1] == 0) { if(i+1 != input.length-1){ if (input[i+2] == 0) { flag = 1; result++; i++; int j=i; while(j<input.length) { if(input[i] != 0){ i--; break; } if(input.length-1 == j) result--; i++; j++; } } if(flag == 0 && input[i+2] == 1 ){ result++; } flag = 0; } } } } return result; } public static void main(String [] args){ Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int input [] = new int[n]; for (int i = 0; i < input.length; i++) { input[i] = scan.nextInt(); } System.out.println(minimum(input)); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 8
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≀ n ≀ 1000) β€” the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) β€” the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number β€” the minimum number of operations needed to make all the letters read.
standard output
PASSED
becfdd6c989b85ee514c9dd217b62c4e
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.*; import java.io.*; import java.math.*; //String.format("%.10f", new BigDecimal(ans)) public class Main { InputReader ir = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); void solve() { int n = ir.readInt(); LinkedList <Integer> q = new LinkedList(); int ans = 0; boolean flag = true; for(int i = 0; i < n; i++) { int num = ir.readInt(); q.add(num); } while(q.isEmpty() == false && q.peek() != 1) { q.remove(); } while(q.isEmpty() == false) { int pop = q.remove(); if(pop == 0) { flag = false; continue; } if(flag == true) ans += 1; else ans += 2; flag = true; } out.println(ans); out.close(); } public static void main(String[] args) { new Main().solve(); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public final int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public double readDouble() { return Double.parseDouble(readString()); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 8
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≀ n ≀ 1000) β€” the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) β€” the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number β€” the minimum number of operations needed to make all the letters read.
standard output
PASSED
fd3d7ffe47bfde426af7f7fa42b2ace2
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.*; import java.io.*; public class test{ public static void main(String[] args) { int x,y; FastReader scan=new FastReader(); OutputStream output=System.out; PrintWriter out=new PrintWriter(output); int n=scan.nextInt(); int[] arr=new int[n]; int lastindex=0; for(x=0;x<n;x++) { arr[x]=scan.nextInt(); if(arr[x]==1) lastindex=x; } int count=0; for(x=0;x<n;x++) { if(arr[x]==1) count++; if(x>0 && arr[x-1]==1 && arr[x]==0) count++; } System.out.println(count==0 || lastindex==n-1?count:count-1); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 8
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≀ n ≀ 1000) β€” the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) β€” the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number β€” the minimum number of operations needed to make all the letters read.
standard output
PASSED
f600da2684dfef4f176d122900495579
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.*; public class sudoko { public static void main(String[] args){ Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(),ans=0,prev=0; for (int i=1;i<=n;i++){ int temp=scanner.nextInt(); if (temp==1){ if (i-prev==i) { ans += 1; prev=i; } else if (i-prev==1) { ans += 1; prev = i; } else if (i-prev>1) { ans += 2; prev=i; } } } System.out.println(ans); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 8
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≀ n ≀ 1000) β€” the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) β€” the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number β€” the minimum number of operations needed to make all the letters read.
standard output
PASSED
ab91143767c7787505af29e2e736e6cd
train_004.jsonl
1549208100
We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.
256 megabytes
import java.io.IOException; import java.util.NoSuchElementException; public class Main{ public static void main(String args[])throws Exception{ FastScanner sc= new FastScanner(); String S = sc.next(); String T = sc.next(); int L = S.length(); int l = T.length(); char[] s = {'a','i','u','e','o'}; if(L!=l){ System.out.println("No"); return; }else{ for(int i=0;i<L;i++){ char C = S.charAt(i); char c = T.charAt(i); int V = 0; int v = 0; for(int j=0;j<5;j++){ if(C==s[j]){ V = 1; } if(c==s[j]){ v = 1; } } if(V!=v){ System.out.println("No"); return; } } } System.out.println("Yes"); } } class FastScanner { private final java.io.InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;} public boolean hasNext() { skipUnprintable(); return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return (minus ? -n : n); }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return (int) (minus ? -n : n); }else{ throw new NumberFormatException(); } b = readByte(); } } }
Java
["a\nu", "abc\nukm", "akm\nua"]
1 second
["Yes", "Yes", "No"]
NoteIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $$$s$$$ to $$$t$$$.In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $$$s$$$ to $$$t$$$.
Java 8
standard input
[ "implementation", "strings" ]
2b346d5a578031de4d19edb4f8f2626c
The first line contains the string $$$s$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. The second line contains the string $$$t$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. Both strings $$$s$$$ and $$$t$$$ are guaranteed to be different and consist of lowercase English letters only.
1,000
Output "Yes" (without quotes) if the superhero with name $$$s$$$ can be transformed to the superhero with name $$$t$$$ and "No" (without quotes) otherwise. You can print each letter in any case (upper or lower).
standard output
PASSED
d401bc5f7df93304e94ff9b445d19d36
train_004.jsonl
1549208100
We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class SuperheroTransformation { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String fW; String sW; while((fW=in.readLine()) != null) { sW = in.readLine(); String [] fWSep = fW.split(""); String [] sWSep = sW.split(""); String vowels = "aeiou"; boolean cond = true; if(fW.length() == sW.length()) { for (int i = 0; i < sWSep.length; i++) { if(vowels.contains(sWSep[i]) && !vowels.contains(fWSep[i])) { cond=false; break; } if(!vowels.contains(sWSep[i]) && vowels.contains(fWSep[i])) { cond=false; break; } } }else { cond=false; } if(cond) { System.out.println("Yes"); }else { System.out.println("No"); } } } }
Java
["a\nu", "abc\nukm", "akm\nua"]
1 second
["Yes", "Yes", "No"]
NoteIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $$$s$$$ to $$$t$$$.In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $$$s$$$ to $$$t$$$.
Java 8
standard input
[ "implementation", "strings" ]
2b346d5a578031de4d19edb4f8f2626c
The first line contains the string $$$s$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. The second line contains the string $$$t$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. Both strings $$$s$$$ and $$$t$$$ are guaranteed to be different and consist of lowercase English letters only.
1,000
Output "Yes" (without quotes) if the superhero with name $$$s$$$ can be transformed to the superhero with name $$$t$$$ and "No" (without quotes) otherwise. You can print each letter in any case (upper or lower).
standard output
PASSED
78987e95b0d63dcbdd06686f4c9b641d
train_004.jsonl
1549208100
We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Superhero { public static void main (String[] args) throws java.lang.Exception { try { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); char []ch=br.readLine().toCharArray(); char []ab=br.readLine().toCharArray(); if(ch.length!=ab.length) { System.out.println("No"); }else { boolean t=true,f=false; for(int i=0;i<ch.length;i++) { if((checkVowel(ch[i]) != checkVowel(ab[i]))) t=false; } if(t) System.out.println("Yes"); else System.out.println("No"); } } catch(Exception e) { return;} } static boolean checkVowel(char a) { if(a=='a' || a=='e' || a=='i' || a=='o' || a=='u') return true; return false; } }
Java
["a\nu", "abc\nukm", "akm\nua"]
1 second
["Yes", "Yes", "No"]
NoteIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $$$s$$$ to $$$t$$$.In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $$$s$$$ to $$$t$$$.
Java 8
standard input
[ "implementation", "strings" ]
2b346d5a578031de4d19edb4f8f2626c
The first line contains the string $$$s$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. The second line contains the string $$$t$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. Both strings $$$s$$$ and $$$t$$$ are guaranteed to be different and consist of lowercase English letters only.
1,000
Output "Yes" (without quotes) if the superhero with name $$$s$$$ can be transformed to the superhero with name $$$t$$$ and "No" (without quotes) otherwise. You can print each letter in any case (upper or lower).
standard output
PASSED
98fe6290d9c6e617e4868bdcdf1703df
train_004.jsonl
1549208100
We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.
256 megabytes
import java.util.Scanner; public class baba { public static void main(String[] args) throws Exception { String a,b; Scanner sc = new Scanner(System.in); String s = "aeiou"; a = sc.next(); b = sc.next(); int i; if(a.length() != b.length()) { System.out.println("No"); return; } for(i=0;i<a.length();i++) { int p,q; p=q=0; for(int k=0;k<5;k++) { if(a.charAt(i) == s.charAt(k)) { p++; break; } } for(int k=0;k<5;k++) { if(b.charAt(i) == s.charAt(k)) { p--; break; } } if(p!=0) { System.out.println("No"); return; } } System.out.println("Yes"); } }
Java
["a\nu", "abc\nukm", "akm\nua"]
1 second
["Yes", "Yes", "No"]
NoteIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $$$s$$$ to $$$t$$$.In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $$$s$$$ to $$$t$$$.
Java 8
standard input
[ "implementation", "strings" ]
2b346d5a578031de4d19edb4f8f2626c
The first line contains the string $$$s$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. The second line contains the string $$$t$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. Both strings $$$s$$$ and $$$t$$$ are guaranteed to be different and consist of lowercase English letters only.
1,000
Output "Yes" (without quotes) if the superhero with name $$$s$$$ can be transformed to the superhero with name $$$t$$$ and "No" (without quotes) otherwise. You can print each letter in any case (upper or lower).
standard output
PASSED
ed63f0a632aed5b2d130bc143043aa08
train_004.jsonl
1549208100
We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.
256 megabytes
import java.util.Scanner; import java.util.Arrays; public class SuperHeroTransformation{ public static void main(String[] args){ char[] vowels={'a','e','i','o','u'}; Scanner scan=new Scanner(System.in); char[] super1=scan.nextLine().toString().toCharArray(); char[] super2=scan.nextLine().toString().toCharArray(); boolean canChange=true; if(super1.length==super2.length){ for(int i=0;i<super1.length;i++){ if(isVowel(super1[i])==isVowel(super2[i])){ continue; } else{ canChange=false; break; } } if(canChange){ System.out.println("Yes"); } else{ System.out.println("No"); } } else{ System.out.println("No"); } } public static boolean isVowel(char superchar){ switch(superchar){ case 'a': return true; case 'e': return true; case 'i': return true; case 'o': return true; case 'u': return true; default: return false; } } public static char[] reverseArray(char[] char1){ for(int i=0;i<char1.length/2;i++){ char temp=char1[i]; char1[i]=char1[char1.length-i-1]; char1[char1.length-i-1]=temp; } return char1; } }
Java
["a\nu", "abc\nukm", "akm\nua"]
1 second
["Yes", "Yes", "No"]
NoteIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $$$s$$$ to $$$t$$$.In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $$$s$$$ to $$$t$$$.
Java 8
standard input
[ "implementation", "strings" ]
2b346d5a578031de4d19edb4f8f2626c
The first line contains the string $$$s$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. The second line contains the string $$$t$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. Both strings $$$s$$$ and $$$t$$$ are guaranteed to be different and consist of lowercase English letters only.
1,000
Output "Yes" (without quotes) if the superhero with name $$$s$$$ can be transformed to the superhero with name $$$t$$$ and "No" (without quotes) otherwise. You can print each letter in any case (upper or lower).
standard output
PASSED
e0a7d820b4ace4edd3309a86637dbcc8
train_004.jsonl
1549208100
We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.
256 megabytes
import java.util.Scanner; import java.util.*; public class test1 { public static void main(String[] args) { Scanner s=new Scanner(System.in); String hero_s=s.nextLine(); String hero_t=s.nextLine(); int f=1; char c,d; int sflag,tflag; if(hero_s.length()!=hero_t.length()) f=0; for(int i=0;(i<hero_s.length())&&f!=0;i++) { c=hero_s.charAt(i); switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': sflag=1; break; default: sflag=0;} d=hero_t.charAt(i); switch (d) { case 'a': case 'e': case 'i': case 'o': case 'u': tflag=1; break; default: tflag=0; } if(sflag!=tflag) {f=0; break;} } if(f==0) System.out.println("NO"); if(f==1) System.out.println("Yes"); }}
Java
["a\nu", "abc\nukm", "akm\nua"]
1 second
["Yes", "Yes", "No"]
NoteIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $$$s$$$ to $$$t$$$.In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $$$s$$$ to $$$t$$$.
Java 8
standard input
[ "implementation", "strings" ]
2b346d5a578031de4d19edb4f8f2626c
The first line contains the string $$$s$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. The second line contains the string $$$t$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. Both strings $$$s$$$ and $$$t$$$ are guaranteed to be different and consist of lowercase English letters only.
1,000
Output "Yes" (without quotes) if the superhero with name $$$s$$$ can be transformed to the superhero with name $$$t$$$ and "No" (without quotes) otherwise. You can print each letter in any case (upper or lower).
standard output
PASSED
b65bd89db8748b7165e1cd6d85e2ba93
train_004.jsonl
1549208100
We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.
256 megabytes
import java.util.*; public class Main{ public static void main(String args[]){ int n,k; Scanner cin = new Scanner(System.in); String[] s = new String[2]; HashMap<Character,Integer>Ref = new HashMap<Character,Integer>(); int []Cnt = new int[2]; for(int i = 0 ; i < 26 ; ++i){ Ref.put((char)('a'+i),0); } Ref.put('a',1); Ref.put('e',1); Ref.put('i',1); Ref.put('o',1); Ref.put('u',1); Cnt[0] = 0; Cnt[1] = 0; for(int i = 0 ; i < 2 ; ++i){ s[i] = cin.nextLine(); } if(s[0].length()!=s[1].length()){ System.out.println("No"); return; } for(int i = 0 ; i < s[0].length() ; ++i){ int t = 0; for(int j = 0 ; j < 2 ; ++j){ t+=Ref.get(s[j].charAt(i)); } if(t==1){ System.out.println("No"); return; } } System.out.println("Yes"); } }
Java
["a\nu", "abc\nukm", "akm\nua"]
1 second
["Yes", "Yes", "No"]
NoteIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $$$s$$$ to $$$t$$$.In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $$$s$$$ to $$$t$$$.
Java 8
standard input
[ "implementation", "strings" ]
2b346d5a578031de4d19edb4f8f2626c
The first line contains the string $$$s$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. The second line contains the string $$$t$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. Both strings $$$s$$$ and $$$t$$$ are guaranteed to be different and consist of lowercase English letters only.
1,000
Output "Yes" (without quotes) if the superhero with name $$$s$$$ can be transformed to the superhero with name $$$t$$$ and "No" (without quotes) otherwise. You can print each letter in any case (upper or lower).
standard output
PASSED
16c51ecd60768d63956f3eda83eb6e31
train_004.jsonl
1549208100
We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.
256 megabytes
import java.io.OutputStream; import java.io.PrintWriter; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); Superhero solution = new Superhero(); solution.solution(sc,out); out.close(); } } class Superhero { String vowels = "aeiou"; public void solution(Scanner in, PrintWriter out) { String s = in.next(); String t = in.next(); boolean canTrans = true; if (s.length() != t.length()) { out.print("No"); return; } for (int i = 0; i<s.length(); i++) { if ((isVowel(s.charAt(i)) && isVowel(t.charAt(i))) || (!isVowel(s.charAt(i)) && !isVowel(t.charAt(i)))) { continue; } canTrans = false; break; } if (canTrans) { out.print("Yes"); } else { out.print("No"); } } public boolean isVowel(char c) { return ((c == 'a') || (c == 'e') || (c == 'i') || (c == 'o') || (c == 'u')); } }
Java
["a\nu", "abc\nukm", "akm\nua"]
1 second
["Yes", "Yes", "No"]
NoteIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $$$s$$$ to $$$t$$$.In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $$$s$$$ to $$$t$$$.
Java 8
standard input
[ "implementation", "strings" ]
2b346d5a578031de4d19edb4f8f2626c
The first line contains the string $$$s$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. The second line contains the string $$$t$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. Both strings $$$s$$$ and $$$t$$$ are guaranteed to be different and consist of lowercase English letters only.
1,000
Output "Yes" (without quotes) if the superhero with name $$$s$$$ can be transformed to the superhero with name $$$t$$$ and "No" (without quotes) otherwise. You can print each letter in any case (upper or lower).
standard output
PASSED
068495aa75e53ae80cc01a06edf88e08
train_004.jsonl
1549208100
We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.
256 megabytes
/*package whatever //do not write package name here */ import java.io.*; import java.util.*; public class GFG { public static void main (String[] args)throws IOException { // System.out.println("GfG!"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); String s1 = br.readLine(); // int a[] = new int[] HashSet<Character> hs = new HashSet<Character>(); hs.add('a'); hs.add('e'); hs.add('i'); hs.add('o'); hs.add('u'); boolean b1, b2; if(s.length() != s1.length()) { System.out.println("No"); System.exit(0); } for(int i=0;i<s.length();++i) { b1 = hs.contains(s.charAt(i)); b2 = hs.contains(s1.charAt(i)); if(!((b1&&b2)||(!b1&&!b2))) { System.out.println("No"); System.exit(0); } } System.out.println("Yes"); } }
Java
["a\nu", "abc\nukm", "akm\nua"]
1 second
["Yes", "Yes", "No"]
NoteIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $$$s$$$ to $$$t$$$.In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $$$s$$$ to $$$t$$$.
Java 8
standard input
[ "implementation", "strings" ]
2b346d5a578031de4d19edb4f8f2626c
The first line contains the string $$$s$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. The second line contains the string $$$t$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. Both strings $$$s$$$ and $$$t$$$ are guaranteed to be different and consist of lowercase English letters only.
1,000
Output "Yes" (without quotes) if the superhero with name $$$s$$$ can be transformed to the superhero with name $$$t$$$ and "No" (without quotes) otherwise. You can print each letter in any case (upper or lower).
standard output
PASSED
443aa1f9845e14db9e071456fd5ec9d2
train_004.jsonl
1549208100
We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.
256 megabytes
import java.util.Scanner; public class lab5 { static int flag=0; public static void main(String[] args) { Scanner input = new Scanner(System.in); String s = input.nextLine(); String p = input.nextLine(); funct(s,p); if ((flag == s.length())&(flag==p.length())) { System.out.print("Yes"); }else { System.out.print("No"); } } public static void funct(String a, String b) { if((a.length()==0)||(b.length()==0)) { return; } char p = a.charAt(0); char q = b.charAt(0); if ((p=='a'||p=='e'||p=='i'||p=='o'||p=='u')&(q=='a'||q=='e'||q=='i'||q=='o'||q=='u')){ flag++; funct(a.substring(1),b.substring(1)); return; }else if((p!='a' & p!='e'& p!='i'& p!='o'& p!='u')&(q!='a' & q!='e' & q!='i'& q!='o'& q!='u')) { flag++; funct(a.substring(1),b.substring(1)); return; }else { return; } } }
Java
["a\nu", "abc\nukm", "akm\nua"]
1 second
["Yes", "Yes", "No"]
NoteIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $$$s$$$ to $$$t$$$.In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $$$s$$$ to $$$t$$$.
Java 8
standard input
[ "implementation", "strings" ]
2b346d5a578031de4d19edb4f8f2626c
The first line contains the string $$$s$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. The second line contains the string $$$t$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. Both strings $$$s$$$ and $$$t$$$ are guaranteed to be different and consist of lowercase English letters only.
1,000
Output "Yes" (without quotes) if the superhero with name $$$s$$$ can be transformed to the superhero with name $$$t$$$ and "No" (without quotes) otherwise. You can print each letter in any case (upper or lower).
standard output
PASSED
df803fdba833dba37ed312fba18cc405
train_004.jsonl
1549208100
We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.
256 megabytes
import java.io.PrintWriter; import java.util.*; import java.util.Arrays ; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; public class Test{ static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static int find(String s){ for (int i = 0; i < s.length(); i++) { if(s.charAt(i)=='^'){ return i ; } } return -1 ; } static String ch(long[]x,long b){ for (int i = 0; i < x.length; i++) { if(x[i]==b)return "YES" ; } return "NO" ; } public static void main(String[] args) throws IOException { PrintWriter pw = new PrintWriter(System.out); // Reader in =new Reader (); Scanner in =new Scanner (System.in); String s =in.next() ; String t= in.next() ; if(s.length() <t.length()||s.length()>t.length()){ System.out.println("No"); return ; } LinkedList l1 =new LinkedList() ; l1.add('a') ; l1.add('e') ; l1.add('u') ; l1.add('o') ; l1.add('i') ; boolean c =false ; for (int i = 0; i < Math.min(s.length(), t.length()); i++) { if(l1.contains(s.charAt(i))&&l1.contains(t.charAt(i))){ continue; } if(!l1.contains(s.charAt(i))&&!l1.contains(t.charAt(i))){ continue; } c=true ; break ; } if(c){ System.out.println("No"); }else System.out.println("Yes"); pw.close(); }}
Java
["a\nu", "abc\nukm", "akm\nua"]
1 second
["Yes", "Yes", "No"]
NoteIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $$$s$$$ to $$$t$$$.In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $$$s$$$ to $$$t$$$.
Java 8
standard input
[ "implementation", "strings" ]
2b346d5a578031de4d19edb4f8f2626c
The first line contains the string $$$s$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. The second line contains the string $$$t$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. Both strings $$$s$$$ and $$$t$$$ are guaranteed to be different and consist of lowercase English letters only.
1,000
Output "Yes" (without quotes) if the superhero with name $$$s$$$ can be transformed to the superhero with name $$$t$$$ and "No" (without quotes) otherwise. You can print each letter in any case (upper or lower).
standard output
PASSED
6aebd8aadacd42710015875718013665
train_004.jsonl
1549208100
We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.
256 megabytes
import java.util.Scanner; public class Bear { public static void main(String[] args) { Scanner scanner = new Scanner (System.in); String a= scanner.next(),b=scanner.next();int n = Math.min(a.length(), b.length()); if (a.length()!=b.length()) System.out.println("No"); else { int [] x;x=new int [a.length()]; int [] y;y=new int [b.length()]; for (int i=0;i<n;i++) { if (a.charAt(i)=='a'||a.charAt(i)=='i'||a.charAt(i)=='o'||a.charAt(i)=='u'||a.charAt(i)=='e') x[i]=1; } for (int i=0;i<n;i++) { char s = b.charAt(i); if (s=='a'||s=='i'||s=='o'||s=='u'||s=='e') y[i]=1; } int s=0; for (int i=0;i<n;i++) { if (x[i]!=y[i]) { System.out.println("No");s=1; break; } } if (s==0) System.out.println("Yes"); } scanner.close(); }}
Java
["a\nu", "abc\nukm", "akm\nua"]
1 second
["Yes", "Yes", "No"]
NoteIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $$$s$$$ to $$$t$$$.In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $$$s$$$ to $$$t$$$.
Java 8
standard input
[ "implementation", "strings" ]
2b346d5a578031de4d19edb4f8f2626c
The first line contains the string $$$s$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. The second line contains the string $$$t$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. Both strings $$$s$$$ and $$$t$$$ are guaranteed to be different and consist of lowercase English letters only.
1,000
Output "Yes" (without quotes) if the superhero with name $$$s$$$ can be transformed to the superhero with name $$$t$$$ and "No" (without quotes) otherwise. You can print each letter in any case (upper or lower).
standard output
PASSED
21a04b771f977cd6f3255691b6b90f57
train_004.jsonl
1558105500
Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length $$$k$$$ is vowelly if there are positive integers $$$n$$$ and $$$m$$$ such that $$$n\cdot m = k$$$ and when the word is written by using $$$n$$$ rows and $$$m$$$ columns (the first row is filled first, then the second and so on, with each row filled from left to right), every vowel of the English alphabet appears at least once in every row and every column.You are given an integer $$$k$$$ and you must either print a vowelly word of length $$$k$$$ or print $$$-1$$$ if no such word exists.In this problem the vowels of the English alphabet are 'a', 'e', 'i', 'o' ,'u'.
256 megabytes
import java.io.*; import java.util.*; public class Main { static int getFactor(int num) { for(int i=5; i<num; i++) { if(num % i == 0) { if(num / i >= 5) return i; } } return 1; } 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 fac = getFactor(n); if(fac == 1) System.out.println(-1); else { n /= fac; String str = "aeiou"; String row = String.join("", Collections.nCopies(fac/str.length(), str)) + str.substring(0,fac%str.length()); for(int i=0; i<n; i++) System.out.print(row.substring(i%fac,fac) + row.substring(0,i%fac)); } } }
Java
["7", "36"]
1 second
["-1", "agoeuioaeiruuimaeoieauoweouoiaouimae"]
NoteIn the second example, the word "agoeuioaeiruuimaeoieauoweouoiaouimae" can be arranged into the following $$$6 \times 6$$$ grid: It is easy to verify that every row and every column contain all the vowels.
Java 8
standard input
[ "constructive algorithms", "number theory", "math" ]
933167c31c0f53a43e840cc6cf153f8d
Input consists of a single line containing the integer $$$k$$$ ($$$1\leq k \leq 10^4$$$)Β β€” the required length.
1,100
The output must consist of a single line, consisting of a vowelly word of length $$$k$$$ consisting of lowercase English letters if it exists or $$$-1$$$ if it does not. If there are multiple possible words, you may output any of them.
standard output
PASSED
e7deaa9592a569e696860c7bd6be04fb
train_004.jsonl
1558105500
Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length $$$k$$$ is vowelly if there are positive integers $$$n$$$ and $$$m$$$ such that $$$n\cdot m = k$$$ and when the word is written by using $$$n$$$ rows and $$$m$$$ columns (the first row is filled first, then the second and so on, with each row filled from left to right), every vowel of the English alphabet appears at least once in every row and every column.You are given an integer $$$k$$$ and you must either print a vowelly word of length $$$k$$$ or print $$$-1$$$ if no such word exists.In this problem the vowels of the English alphabet are 'a', 'e', 'i', 'o' ,'u'.
256 megabytes
import java.util.*; public class vowelly { public static void main(String []args) { Scanner in=new Scanner(System.in); //int t=in.nextInt(); //while(t-->0) //{ int k=in.nextInt(); int i,n=-1,m=-1,x=0,j; String s="",p=""; for(i=0;i<20005;i+=5) p=p+"aeiou"; for(i=5;i<=k/5;i++) { if(k%i==0) { n=i; m=k/i; } } if(n<5 || m<5) System.out.println(-1); else { //System.out.println(n+" "+m); for(i=0;i<n;i++) { for(j=i;j<i+m;j++) s=s+p.charAt(j); } /*s=s+"aeiou"; x=m-5; for(i=0;i<x;i++) s=s+"a"; s=s+"eioua"; x=m-5; for(i=0;i<x;i++) s=s+"a"; s=s+"iouae"; x=m-5; for(i=0;i<x;i++) s=s+"a"; s=s+"ouaei"; x=m-5; for(i=0;i<x;i++) s=s+"a"; s=s+"uaeio"; x=m-5; for(i=0;i<x;i++) s=s+"a"; for(i=0;i<n-5;i++) { s=s+"aeiou"; for(j=0;j<m-5;j++) s+="a"; } for(i=0;i<k;i++) { System.out.print(s.charAt(i)); if((i+1)%m==0) System.out.println(); }*/ System.out.println(s); } } }
Java
["7", "36"]
1 second
["-1", "agoeuioaeiruuimaeoieauoweouoiaouimae"]
NoteIn the second example, the word "agoeuioaeiruuimaeoieauoweouoiaouimae" can be arranged into the following $$$6 \times 6$$$ grid: It is easy to verify that every row and every column contain all the vowels.
Java 8
standard input
[ "constructive algorithms", "number theory", "math" ]
933167c31c0f53a43e840cc6cf153f8d
Input consists of a single line containing the integer $$$k$$$ ($$$1\leq k \leq 10^4$$$)Β β€” the required length.
1,100
The output must consist of a single line, consisting of a vowelly word of length $$$k$$$ consisting of lowercase English letters if it exists or $$$-1$$$ if it does not. If there are multiple possible words, you may output any of them.
standard output
PASSED
f285fa075788294ce904824fcaeb9137
train_004.jsonl
1558105500
Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length $$$k$$$ is vowelly if there are positive integers $$$n$$$ and $$$m$$$ such that $$$n\cdot m = k$$$ and when the word is written by using $$$n$$$ rows and $$$m$$$ columns (the first row is filled first, then the second and so on, with each row filled from left to right), every vowel of the English alphabet appears at least once in every row and every column.You are given an integer $$$k$$$ and you must either print a vowelly word of length $$$k$$$ or print $$$-1$$$ if no such word exists.In this problem the vowels of the English alphabet are 'a', 'e', 'i', 'o' ,'u'.
256 megabytes
import java.util.*; public class fivesixoneb { public static void main(String[] args) { Scanner input = new Scanner(System.in); int k = input.nextInt(); int n = 0; int m = 0; for(int i=5;i<k;i++) { if(k%i==0) { if(k/i>=5) { n = i; m = k/i; break; } } } char[][] matrix = new char[n][m]; for(int i=0;i<n+m-1;i++) { for(int j=0;j<=i;j++) { if(j<n & (i-j)<m) { if(i%5==0) { matrix[j][i-j] = 'a'; } if(i%5==1) { matrix[j][i-j] = 'e'; } if(i%5==2) { matrix[j][i-j] = 'i'; } if(i%5==3) { matrix[j][i-j] = 'o'; } if(i%5==4) { matrix[j][i-j] = 'u'; } } } } if(n==0 || m==0) { System.out.println(-1); } else { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(matrix[i][j]); } } } } }
Java
["7", "36"]
1 second
["-1", "agoeuioaeiruuimaeoieauoweouoiaouimae"]
NoteIn the second example, the word "agoeuioaeiruuimaeoieauoweouoiaouimae" can be arranged into the following $$$6 \times 6$$$ grid: It is easy to verify that every row and every column contain all the vowels.
Java 8
standard input
[ "constructive algorithms", "number theory", "math" ]
933167c31c0f53a43e840cc6cf153f8d
Input consists of a single line containing the integer $$$k$$$ ($$$1\leq k \leq 10^4$$$)Β β€” the required length.
1,100
The output must consist of a single line, consisting of a vowelly word of length $$$k$$$ consisting of lowercase English letters if it exists or $$$-1$$$ if it does not. If there are multiple possible words, you may output any of them.
standard output
PASSED
86a59910a2a444b76792880e34b05450
train_004.jsonl
1558105500
Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length $$$k$$$ is vowelly if there are positive integers $$$n$$$ and $$$m$$$ such that $$$n\cdot m = k$$$ and when the word is written by using $$$n$$$ rows and $$$m$$$ columns (the first row is filled first, then the second and so on, with each row filled from left to right), every vowel of the English alphabet appears at least once in every row and every column.You are given an integer $$$k$$$ and you must either print a vowelly word of length $$$k$$$ or print $$$-1$$$ if no such word exists.In this problem the vowels of the English alphabet are 'a', 'e', 'i', 'o' ,'u'.
256 megabytes
import java.util.*; public class fivesixoneb { public static void main(String[] args) { // TODO Auto-generated method stub //System.out.println(26/4); Scanner input = new Scanner(System.in); int k = input.nextInt(); int n = 0; int m = 0; for(int i=5;i<k;i++) { if(k%i==0) { if(k/i>=5) { n = i; m = k/i; break; } } } //System.out.println(n); //System.out.println(m); char[][] matrix = new char[n][m]; for(int i=0;i<n+m-1;i++) { for(int j=0;j<=i;j++) { //System.out.println(i); //System.out.println(j); if(j<n & (i-j)<m) { if(i%5==0) { matrix[j][i-j] = 'a'; } if(i%5==1) { matrix[j][i-j] = 'e'; } if(i%5==2) { matrix[j][i-j] = 'i'; } if(i%5==3) { matrix[j][i-j] = 'o'; } if(i%5==4) { matrix[j][i-j] = 'u'; } } } } if(n==0 || m==0) { System.out.println(-1); } else { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(matrix[i][j]); } } } } }
Java
["7", "36"]
1 second
["-1", "agoeuioaeiruuimaeoieauoweouoiaouimae"]
NoteIn the second example, the word "agoeuioaeiruuimaeoieauoweouoiaouimae" can be arranged into the following $$$6 \times 6$$$ grid: It is easy to verify that every row and every column contain all the vowels.
Java 8
standard input
[ "constructive algorithms", "number theory", "math" ]
933167c31c0f53a43e840cc6cf153f8d
Input consists of a single line containing the integer $$$k$$$ ($$$1\leq k \leq 10^4$$$)Β β€” the required length.
1,100
The output must consist of a single line, consisting of a vowelly word of length $$$k$$$ consisting of lowercase English letters if it exists or $$$-1$$$ if it does not. If there are multiple possible words, you may output any of them.
standard output
PASSED
e28afd6f3334a4f5ebd784fa4e6d3e3e
train_004.jsonl
1558105500
Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length $$$k$$$ is vowelly if there are positive integers $$$n$$$ and $$$m$$$ such that $$$n\cdot m = k$$$ and when the word is written by using $$$n$$$ rows and $$$m$$$ columns (the first row is filled first, then the second and so on, with each row filled from left to right), every vowel of the English alphabet appears at least once in every row and every column.You are given an integer $$$k$$$ and you must either print a vowelly word of length $$$k$$$ or print $$$-1$$$ if no such word exists.In this problem the vowels of the English alphabet are 'a', 'e', 'i', 'o' ,'u'.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; public class abc { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); OutputStream out = new BufferedOutputStream(System.out); int n=Integer.parseInt(br.readLine()); StringBuilder s=new StringBuilder(""); String s1[]={"a","e","i","o","u"}; int flag=0; for(int i=5;i<=(Math.sqrt(n));i++){ if(n%i==0){ flag=1; int q=n/i; for(int j=0;j<i;j++){ for(int k=0;k<q;k++){ s.append(s1[((j%5)+k)%5]); } } break; } } if(flag==0) out.write("-1".getBytes()); else out.write(s.toString().getBytes()); out.flush(); } }
Java
["7", "36"]
1 second
["-1", "agoeuioaeiruuimaeoieauoweouoiaouimae"]
NoteIn the second example, the word "agoeuioaeiruuimaeoieauoweouoiaouimae" can be arranged into the following $$$6 \times 6$$$ grid: It is easy to verify that every row and every column contain all the vowels.
Java 8
standard input
[ "constructive algorithms", "number theory", "math" ]
933167c31c0f53a43e840cc6cf153f8d
Input consists of a single line containing the integer $$$k$$$ ($$$1\leq k \leq 10^4$$$)Β β€” the required length.
1,100
The output must consist of a single line, consisting of a vowelly word of length $$$k$$$ consisting of lowercase English letters if it exists or $$$-1$$$ if it does not. If there are multiple possible words, you may output any of them.
standard output
PASSED
052343fda167123e4b661773b43018bd
train_004.jsonl
1558105500
Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length $$$k$$$ is vowelly if there are positive integers $$$n$$$ and $$$m$$$ such that $$$n\cdot m = k$$$ and when the word is written by using $$$n$$$ rows and $$$m$$$ columns (the first row is filled first, then the second and so on, with each row filled from left to right), every vowel of the English alphabet appears at least once in every row and every column.You are given an integer $$$k$$$ and you must either print a vowelly word of length $$$k$$$ or print $$$-1$$$ if no such word exists.In this problem the vowels of the English alphabet are 'a', 'e', 'i', 'o' ,'u'.
256 megabytes
import java.io.*; import java.nio.CharBuffer; import java.util.NoSuchElementException; public class P1166B { public static void main(String[] args) { SimpleScanner scanner = new SimpleScanner(System.in); PrintWriter writer = new PrintWriter(System.out); int n = scanner.nextInt(); boolean valid = false; char[] vowels = new char[]{'a', 'e', 'i', 'o', 'u'}; for (int m = 5; m * m <= n; ++m) { if (n % m == 0) { valid = true; for (int i = 0; i < m; ++i) { for (int j = 0; j < n / m; ++j) { writer.print(vowels[((j - i) % 5 + 5) % 5]); } // writer.println(); } break; } } if (!valid) writer.println(-1); writer.close(); } private static class SimpleScanner { private static final int BUFFER_SIZE = 10240; private Readable in; private CharBuffer buffer; private boolean eof; SimpleScanner(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); buffer = CharBuffer.allocate(BUFFER_SIZE); buffer.limit(0); eof = false; } private char read() { if (!buffer.hasRemaining()) { buffer.clear(); int n; try { n = in.read(buffer); } catch (IOException e) { n = -1; } if (n <= 0) { eof = true; return '\0'; } buffer.flip(); } return buffer.get(); } void checkEof() { if (eof) throw new NoSuchElementException(); } char nextChar() { checkEof(); char b = read(); checkEof(); return b; } String next() { char b; do { b = read(); checkEof(); } while (Character.isWhitespace(b)); StringBuilder sb = new StringBuilder(); do { sb.append(b); b = read(); } while (!eof && !Character.isWhitespace(b)); return sb.toString(); } int nextInt() { return Integer.valueOf(next()); } long nextLong() { return Long.valueOf(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["7", "36"]
1 second
["-1", "agoeuioaeiruuimaeoieauoweouoiaouimae"]
NoteIn the second example, the word "agoeuioaeiruuimaeoieauoweouoiaouimae" can be arranged into the following $$$6 \times 6$$$ grid: It is easy to verify that every row and every column contain all the vowels.
Java 8
standard input
[ "constructive algorithms", "number theory", "math" ]
933167c31c0f53a43e840cc6cf153f8d
Input consists of a single line containing the integer $$$k$$$ ($$$1\leq k \leq 10^4$$$)Β β€” the required length.
1,100
The output must consist of a single line, consisting of a vowelly word of length $$$k$$$ consisting of lowercase English letters if it exists or $$$-1$$$ if it does not. If there are multiple possible words, you may output any of them.
standard output
PASSED
4b24a05ba854425958c708298aa8aaf0
train_004.jsonl
1558105500
Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length $$$k$$$ is vowelly if there are positive integers $$$n$$$ and $$$m$$$ such that $$$n\cdot m = k$$$ and when the word is written by using $$$n$$$ rows and $$$m$$$ columns (the first row is filled first, then the second and so on, with each row filled from left to right), every vowel of the English alphabet appears at least once in every row and every column.You are given an integer $$$k$$$ and you must either print a vowelly word of length $$$k$$$ or print $$$-1$$$ if no such word exists.In this problem the vowels of the English alphabet are 'a', 'e', 'i', 'o' ,'u'.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class MAIN { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b , a%b); } // Function to find gcd of array of // numbers static long findGCD(long arr[], int l,int r) { long result = arr[r]; for (int i = l; i <r; i++) result = gcd(arr[i], result); return result; } public static void main(String[] args){ // TODO Auto-generated method OutputStream outputStream = System.out; FastReader scn = new FastReader(); PrintWriter out = new PrintWriter(outputStream); int n=scn.nextInt(); int row = 0 , column = 0; if(n>=25) for(int i =5;i<n/2;i++) { if((n/i)*i==n) { row=n/i; column=i; break; } } else n=1; if(row==0||column==0) n=1; if(row<5) n=1; if(column<5) n=1; //out.println(row+" "+column); String vowels = "aeiou"; String result = ""; int t = column/5; for(int i=1;i<=t;i++) { result+=vowels; } int len = column-result.length(); result+=vowels.substring(0,len); //out.println(result); if(n>=25) { StringBuilder s = new StringBuilder(); for(int i=0;i<row;i++) { s.append(result); result=result.substring(column-1)+result.substring(0, column-1); } out.println(s);//" "+s.length()); } else { out.println("-1"); } out.close(); } /*static class comp implements Comparator<Pair> { @Override public int compare(Pair arg0, Pair arg1) { // TODO Auto-generated method stub if(arg0.b<arg1.b) return 1; if(arg0.b>arg1.b) return -1; if(arg0.b==arg1.b) if(arg0.a<arg1.a) return 1; else return -1; return 0; } }*/ static class Pair { int a; int b; } static class Compare { static void compare(Pair ar[]) { Arrays.sort(ar, new Comparator<Pair>(){ @Override public int compare(Pair arg0, Pair arg1) { // TODO Auto-generated method stub if(arg0.b<arg1.b) return 1; if(arg0.b>arg1.b) return -1; if(arg0.b==arg1.b) if(arg0.a<arg1.a) return -1; else return 1; return 0; } }); } } public static int[] radixSort(int[] f){ return radixSort(f, f.length); } public static int[] radixSort(int[] f, int n) { int[] to = new int[n]; { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[f[i]&0xffff]++] = f[i]; int[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(f[i]>>>16)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[f[i]>>>16]++] = f[i]; int[] d = f; f = to;to = d; } return f; } }
Java
["7", "36"]
1 second
["-1", "agoeuioaeiruuimaeoieauoweouoiaouimae"]
NoteIn the second example, the word "agoeuioaeiruuimaeoieauoweouoiaouimae" can be arranged into the following $$$6 \times 6$$$ grid: It is easy to verify that every row and every column contain all the vowels.
Java 8
standard input
[ "constructive algorithms", "number theory", "math" ]
933167c31c0f53a43e840cc6cf153f8d
Input consists of a single line containing the integer $$$k$$$ ($$$1\leq k \leq 10^4$$$)Β β€” the required length.
1,100
The output must consist of a single line, consisting of a vowelly word of length $$$k$$$ consisting of lowercase English letters if it exists or $$$-1$$$ if it does not. If there are multiple possible words, you may output any of them.
standard output
PASSED
608435224aca5493ab1a0a118455b410
train_004.jsonl
1558105500
Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length $$$k$$$ is vowelly if there are positive integers $$$n$$$ and $$$m$$$ such that $$$n\cdot m = k$$$ and when the word is written by using $$$n$$$ rows and $$$m$$$ columns (the first row is filled first, then the second and so on, with each row filled from left to right), every vowel of the English alphabet appears at least once in every row and every column.You are given an integer $$$k$$$ and you must either print a vowelly word of length $$$k$$$ or print $$$-1$$$ if no such word exists.In this problem the vowels of the English alphabet are 'a', 'e', 'i', 'o' ,'u'.
256 megabytes
import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.Stack; @SuppressWarnings("ALL") public class Main { static class gr { int id; public gr(int id) { this.id = id; } } static class pp { int l, r; public pp(int l, int r) { this.l = l; this.r = r; } @Override public String toString() { return "{" + l + ", " + r + "}"; } } private static final long VAL = 1_000_000_000; static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } public static void plusPut(HashMap<Character, Integer> mp, char c) { if (mp.containsKey(c)) { mp.put(c, mp.get(c) + 1); } else { mp.put(c, 1); } } public static void minusDel(HashMap<Character, Integer> mp, char key) { int vv = mp.get(key) - 1; if (vv == 0) { mp.remove(key); } else { mp.put(key, vv); } } public static void minusDelWithCheck(HashMap<Character, Integer> mp, char key) { if (mp.containsKey(key)) { minusDel(mp, key); } } static int calc(int q) { if (q < 2) { return 0; } if (q == 2) { return 1; } int n = q - 1; return ((1 + n) * n / 2); } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a = -1, b = -1; char[] arr = new char[]{'a', 'e', 'i', 'o', 'u'}; for (int i = 5; i <= n / 5; i++) { if (n % i == 0) { a = i; b = n / i; break; } } if (a==-1){ System.out.println(-1); return; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { sb.append(arr[(j + i) % arr.length]); } } System.out.println(sb); } }
Java
["7", "36"]
1 second
["-1", "agoeuioaeiruuimaeoieauoweouoiaouimae"]
NoteIn the second example, the word "agoeuioaeiruuimaeoieauoweouoiaouimae" can be arranged into the following $$$6 \times 6$$$ grid: It is easy to verify that every row and every column contain all the vowels.
Java 8
standard input
[ "constructive algorithms", "number theory", "math" ]
933167c31c0f53a43e840cc6cf153f8d
Input consists of a single line containing the integer $$$k$$$ ($$$1\leq k \leq 10^4$$$)Β β€” the required length.
1,100
The output must consist of a single line, consisting of a vowelly word of length $$$k$$$ consisting of lowercase English letters if it exists or $$$-1$$$ if it does not. If there are multiple possible words, you may output any of them.
standard output
PASSED
8e4d0d9e0c38388861f9ea1c070b54b7
train_004.jsonl
1558105500
Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length $$$k$$$ is vowelly if there are positive integers $$$n$$$ and $$$m$$$ such that $$$n\cdot m = k$$$ and when the word is written by using $$$n$$$ rows and $$$m$$$ columns (the first row is filled first, then the second and so on, with each row filled from left to right), every vowel of the English alphabet appears at least once in every row and every column.You are given an integer $$$k$$$ and you must either print a vowelly word of length $$$k$$$ or print $$$-1$$$ if no such word exists.In this problem the vowels of the English alphabet are 'a', 'e', 'i', 'o' ,'u'.
256 megabytes
import java.util.*; import java.io.*; public class Param { public static void main( String[]args) { Scanner param = new Scanner(System.in); int n=param.nextInt(); ArrayList<Character>l1=new ArrayList<Character>(); char []arr=new char[5]; arr[0]='a'; arr[1]='e'; arr[2]='i'; arr[3]='o'; arr[4]='u'; int k=0; int m=0; if(n>=25){ int y=0; int x=5; while(n%x!=0){ x++; } y=n/x; if(y<5){ System.out.println(-1); } else{ for(int i=0;i<x;i++){ for(int j=0;j<y;j++){ l1.add(arr[(i+j)%5]); } } } for(int i=0;i<l1.size();i++){ System.out.print(l1.get(i)); } } else{ System.out.println(-1); } } }
Java
["7", "36"]
1 second
["-1", "agoeuioaeiruuimaeoieauoweouoiaouimae"]
NoteIn the second example, the word "agoeuioaeiruuimaeoieauoweouoiaouimae" can be arranged into the following $$$6 \times 6$$$ grid: It is easy to verify that every row and every column contain all the vowels.
Java 8
standard input
[ "constructive algorithms", "number theory", "math" ]
933167c31c0f53a43e840cc6cf153f8d
Input consists of a single line containing the integer $$$k$$$ ($$$1\leq k \leq 10^4$$$)Β β€” the required length.
1,100
The output must consist of a single line, consisting of a vowelly word of length $$$k$$$ consisting of lowercase English letters if it exists or $$$-1$$$ if it does not. If there are multiple possible words, you may output any of them.
standard output
PASSED
1e517676766f1b2b0c25472ad2dd42a1
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; /** * https://codeforces.com/contest/1418/problem/B */ public class B { public static void main(String[] args) { Scanner in = new Scanner(System.in); long t = Long.parseLong(in.nextLine()); for (int i = 0; i < t; i++) { int n = Integer.parseInt(in.nextLine()); String[] data1 = in.nextLine().split(" "); String[] data2 = in.nextLine().split(" "); List<Element> elements = new ArrayList<>(n); for (int j = 0; j < n; j++) { elements.add(new Element(Long.parseLong(data1[j]), data2[j].equals("1"))); } System.out.println(getAnswer(elements)); } } private static String getAnswer(List<Element> elements) { List<Element> unblockedElements = elements.stream() .filter(element -> !element.blocked) .sorted(Comparator.comparing(element -> -element.value)) .collect(Collectors.toList()); int j = 0; for (int i = 0; i < elements.size(); i++) { if (elements.get(i).blocked) { continue; } elements.set(i, unblockedElements.get(j++)); } return elements.stream().map(element -> String.valueOf(element.value)).collect(Collectors.joining(" ")); } static class Element { long value; boolean blocked; public Element(long value, boolean blocked) { this.value = value; this.blocked = blocked; } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
982ab354b8875a4565f66fba9f6a907a
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; /** * https://codeforces.com/contest/1418/problem/B */ public class B { public static void main(String[] args) { Scanner in = new Scanner(System.in); long t = Long.parseLong(in.nextLine()); for (int i = 0; i < t; i++) { int n = Integer.parseInt(in.nextLine()); String[] data1 = in.nextLine().split(" "); String[] data2 = in.nextLine().split(" "); List<Element> elements = new ArrayList<>(n); for (int j = 0; j < n; j++) { elements.add(new Element(Long.parseLong(data1[j]), data2[j].equals("1"), j)); } System.out.println(getAnswer(elements)); } } private static String getAnswer(List<Element> elements) { List<Element> unblockedElements = elements.stream() .filter(element -> !element.blocked) .sorted(Comparator.comparing(element -> element.value)) .collect(Collectors.toList()); if (unblockedElements.size() <= 1) { return toString(elements); } for (int i = elements.size() - 1; i >= 0; i--) { if (unblockedElements.size() == 1) { return toString(elements); } if (elements.get(i).blocked) { continue; } if (elements.get(i).blocked && elements.get(i).value < 0 && sum(elements, i) < 0) { return toString(elements); } Element element = elements.get(i); for (int j = 0; j < unblockedElements.size(); j++) { Element unblockedElement = unblockedElements.get(j); elements.set(i, unblockedElement); elements.set(unblockedElement.index, element); if (sum(elements, i) >= 0) { int tempIndex = element.index; element.index = unblockedElement.index; unblockedElement.index = tempIndex; unblockedElements.remove(j); break; } else { elements.set(i, element); elements.set(unblockedElement.index, unblockedElement); } } } return toString(elements); } private static long sum(List<Element> elements, int n) { long sum = 0; for (int i = 0; i <= n; i++) { sum += elements.get(i).value; } return sum; } private static String toString(List<Element> elements) { return elements.stream().map(element -> String.valueOf(element.value)).collect(Collectors.joining(" ")); } static class Element { long value; boolean blocked; int index; public Element(long value, boolean blocked, int index) { this.value = value; this.blocked = blocked; this.index = index; } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
33a1009b35ea74419d71ced5d8b508bc
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int []a=new int[n]; for (int i=0;i<n;i++)a[i]=sc.nextInt(); int []locked=new int[n]; for (int i=0;i<n;i++)locked[i]=sc.nextInt(); Vector<Integer> v = new Vector<>(); for (int i=0;i<n;i++){ if(locked[i]==0){ v.add(a[i]); } } Collections.sort(v); Collections.reverse(v); for (int i=0,j=0;i<n;i++){ if(locked[i]==0){ System.out.print(v.elementAt(j++)+" "); }else{ System.out.print(a[i]+" "); } } System.out.println(); } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
1f7162002837fd493f488276b5b6048b
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.io.*; import java.util.*; public class Coding { static long MOD = 1000000007; static long[] fact = new long[100002]; static long[] pow2 = new long[100002]; static int MAXVAL = 10000000; public static void main(String[] args) { BufferedReader bi = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); //precompute(); try { int t = Integer.valueOf(bi.readLine()); while (t > 0) { int n = Integer.valueOf(bi.readLine()); String[] inputs = bi.readLine().split(" "); int[] ar = new int[n]; for(int i=0;i<n;++i) { ar[i] = Integer.parseInt(inputs[i].trim()); } int[] locked = new int[n]; inputs = bi.readLine().split(" "); List<Integer> ele = new ArrayList<>(); for(int i=0;i<n;++i) { locked[i] = Integer.parseInt(inputs[i].trim()); if (locked[i] == 0) { ele.add(ar[i]); } } //System.out.println(negSum + " " + posSum); Collections.sort(ele); int st = 0; int en = ele.size()-1; int curIndex = 0; while(en>=st) { while (locked[curIndex]==1) { curIndex++; } ar[curIndex] = ele.get(en); curIndex++; en--; } for(int i=0;i<n;++i) { writer.append(String.valueOf(ar[i])); if (i!=n-1) { writer.append(" "); } } writer.append("\n"); writer.flush(); t--; } writer.flush(); } catch (Exception e) { e.printStackTrace(); } } private static int gcd(int a, int b) { if (a<b) { return gcd(b, a); } if (a%b==0) { return b; } return gcd(b, a%b); } private static void dfs(int cur, List<Integer>[] adj, int[] nodeCount, int[] maxChild, int parent, int[] par) { par[cur] = parent; for(int i=0;i<adj[cur].size();++i) { int nxt = adj[cur].get(i); if (nxt!=parent) { dfs(nxt, adj, nodeCount, maxChild, cur, par); nodeCount[cur]+=nodeCount[nxt]; maxChild[cur] = Math.max(maxChild[cur], nodeCount[nxt]); } } } private static int dfs2(int cur, List<Integer>[] adj, int parent) { for(int i=0;i<adj[cur].size();++i) { int nxt = adj[cur].get(i); if (nxt!=parent) { return dfs2(nxt, adj, cur); } } return cur; } private static int findDist(int a, int b, int[] par) { Map<Integer, Integer> dist = new HashMap<>(); int count = 0; dist.put(a, 0); while (par[a]!=-1) { a = par[a]; count++; dist.put(a, count); } count=0; while(!dist.containsKey(b)) { b = par[b]; count++; } return count+dist.get(b); } private static int findLongestPath(int cur, int prev, List<Integer>[] adj, int[] dep) { int diameter = 0; int highest = -1, second=-1; for(int i=0;i<adj[cur].size();++i) { int nxt = adj[cur].get(i); if (nxt!=prev) { diameter = Math.max(diameter, findLongestPath(nxt, cur, adj, dep)); if (dep[nxt]>highest) { second = highest; highest = dep[nxt]; } else if (dep[nxt]>second) { second = dep[nxt]; } } } diameter = Math.max(diameter, highest + second + 2); return diameter; } static long ncr(int n, int r) { long num = fact[n]; long denom = (modExp(fact[r], MOD-2)*modExp(fact[n-r], MOD-2))%MOD; //System.out.println("num is " + num + " denom is" + denom); return (num*denom)%MOD; } static long modExp(long a, long b) { //System.out.println("a is " + a + " and b is " + b); if (a==1) return 1; long ans = 1; while (b!=0) { if (b%2==1) { ans = (ans*a)%MOD; } a = (a*a)%MOD; b/=2; } return ans; } private static void precompute() { fact[0] = 1; pow2[0] = 1; for(int i=1;i<=100000;++i) { fact[i] = (fact[i-1]*i)%MOD; pow2[i] = (pow2[i-1]*2)%MOD; } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
d85124b6f281e32e520c88a51ff3164c
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
// Utilities import java.io.*; import java.util.*; public class Main { static int T; static int N; static int[] a, l; static ArrayList<Pair> arr; public static void main(String[] args) throws IOException { T = in.iscan(); while (T-- > 0) { N = in.iscan(); a = new int[N]; l = new int[N]; for (int i = 0; i < N; i++) a[i] = in.iscan(); arr = new ArrayList<Pair>(); for (int i = 0; i < N; i++) { l[i] = in.iscan(); if (l[i] == 0) { arr.add(new Pair(i, a[i])); } } Collections.sort(arr); int ii = 0; for (int i = 0; i < N; i++) { if (l[i] == 0) { a[i] = arr.get(ii).v; ii++; } out.print(a[i] + " "); } out.println(); } out.close(); } static class Pair implements Comparable<Pair>{ int idx, v; Pair(int idx, int v){ this.idx = idx; this.v = v; } public int compareTo(Pair p) { return -(v - p.v); } } static INPUT in = new INPUT(System.in); static PrintWriter out = new PrintWriter(System.out); private static class INPUT { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public INPUT (InputStream stream) { this.stream = stream; } public INPUT (String file) throws IOException { this.stream = new FileInputStream (file); } public int cscan () throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read (buf); } if (numChars == -1) return numChars; return buf[curChar++]; } public int iscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } int res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public String sscan () throws IOException { int c = cscan (); while (space (c)) c = cscan (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = cscan (); } while (!space (c)); return res.toString (); } public double dscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } double res = 0; while (!space (c) && c != '.') { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); res *= 10; res += c - '0'; c = cscan (); } if (c == '.') { c = cscan (); double m = 1; while (!space (c)) { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); m /= 10; res += (c - '0') * m; c = cscan (); } } return res * sgn; } public long lscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } long res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public boolean space (int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } public static class UTILITIES { static final double EPS = 10e-6; public static int lower_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } public static int gcd (int a, int b) { return b == 0 ? a : gcd (b, a % b); } public static int lcm (int a, int b) { return a * b / gcd (a, b); } public static long fast_pow_mod (long b, long x, int mod) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod; return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod; } public static int fast_pow (int b, int x) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow (b * b, x / 2); return b * fast_pow (b * b, x / 2); } public static long choose (long n, long k) { k = Math.min (k, n - k); long val = 1; for (int i = 0; i < k; ++i) val = val * (n - i) / (i + 1); return val; } public static long permute (int n, int k) { if (n < k) return 0; long val = 1; for (int i = 0; i < k; ++i) val = (val * (n - i)); return val; } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
7caca11d2a0580468013740e43c49e9f
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; /** * * @author Shivam Patel */ public class NegetivePrefix { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); for (int i = 0; i < test; i++) { int arrSize = sc.nextInt(); int[] arr = new int[arrSize]; for (int j = 0; j < arr.length; j++) { arr[j] = sc.nextInt(); } int checkLock[] = new int[arrSize]; for (int k = 0; k < checkLock.length; k++) { checkLock[k] = sc.nextInt(); } ArrayList<Integer> l = new ArrayList(); int[] ans = new int[arr.length]; Arrays.fill(ans, 0); for (int k = 0; k < checkLock.length; k++) { if (checkLock[k] == 1) { ans[k] = arr[k]; } else { l.add(arr[k]); } } Collections.sort(l, Collections.reverseOrder()); rearrange(arr, checkLock, ans, l); } } private static void rearrange(int[] arr, int[] checkLock, int[] ans, ArrayList<Integer> l) { int c = 0; for (int k = 0; k < ans.length; k++) { if (ans[k] == 0 && checkLock[k] == 0) { int x = l.get(c); ans[k] = x; c = c + 1; } } if (ans.length == arr.length) { for (int i = 0; i < ans.length; i++) { System.out.print(" " + ans[i]); } } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
d4eea5729b5582a34b6867c10315cb21
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class BEdu95{ public static void main(String args[]){ FastReader sc = new FastReader(); StringBuilder sb=new StringBuilder(); int t,n,i; t=sc.nextInt(); while(t-->0){ n=sc.nextInt(); int a[]=new int[n]; int p[]=new int[n]; ArrayList<Integer> arr= new ArrayList<>(); for(i=0;i<n;i++) a[i]=sc.nextInt(); int sumpos=0,sumneg=0; for(i=0;i<n;i++){ p[i]=sc.nextInt(); if(p[i]==0){ arr.add(a[i]); } } Collections.sort(arr,Collections.reverseOrder()); int l=0; int sum=0; for(i=0;i<n;i++){ if(p[i]==0){ a[i]=arr.get(l); l++; } } for(i=0;i<n;i++) sb.append(a[i]).append(i==n-1?'\n':' '); } sb.deleteCharAt(sb.length()-1); out.println(sb); } static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(System.out,true); } 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 boolean isPrime(int n) { if(n<2) return false; for(int i=2;i<=(int)Math.sqrt(n);i++) { if(n%i==0) return false; } return true; } public static void print(int a[],int l,int r){ int i; for(i=l;i<=r;i++) out.print(a[i]+" "); out.println(); } public static long fastexpo(long x, long y, long p){ long res=1; while(y > 0){ if((y & 1)==1) res= ((res%p)*(x%p))%p; y= y >> 1; x = ((x%p)*(x%p))%p; } return res; } public static boolean[] sieve (int n) { boolean primes[]=new boolean[n+1]; Arrays.fill(primes,true); primes[0]=primes[1]=false; for(int i=2;i*i<=n;i++){ if(primes[i]){ for(int j=i*i;j<=n;j+=i) primes[j]=false; } } return primes; } public static long gcd(long a,long b){ return (BigInteger.valueOf(a).gcd(BigInteger.valueOf(b))).longValue(); } public static void merge(long a[],int l,int m,int r){ int n1,n2,i,j,k; n1=m-l+1; n2=r-m; long L[]=new long[n1]; long R[]=new long[n2]; for(i=0;i<n1;i++) L[i]=a[l+i]; for(j=0;j<n2;j++) R[j]=a[m+1+j]; i=0;j=0; k=l; while(i<n1&&j<n2){ if(L[i]<=R[j]){ a[k]=L[i]; i++; } else{ a[k]=R[j]; j++; } k++; } while(i<n1){ a[k]=L[i]; i++; k++; } while(j<n2){ a[k]=R[j]; j++; k++; } } public static void sort(long a[],int l,int r){ int m; if(l<r){ m=(l+r)/2; sort(a,l,m); sort(a,m+1,r); merge(a,l,m,r); } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
74322ada842d833aad836eefe6122c28
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.util.*; import java.io.*; public class _1418B { public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] arr = new int[n]; int[] locked = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { locked[i] = sc.nextInt(); if (locked[i] == 0) { list.add(arr[i]); } } Collections.sort(list); Collections.reverse(list); int index = 0; for (int i = 0; i < n; i++) { if (locked[i] == 0) { arr[i] = list.get(index); index++; } } for (int i = 0; i < n; i++) { out.print(arr[i]+" "); } out.println(); } out.close(); } public static PrintWriter out; 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
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
10e75c2d2aaf2806bea80fc86c545a0d
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { static int MOD = 1000000007; // After writing solution, quick scan for: // array out of bounds // special cases e.g. n=1? // // Big numbers arithmetic bugs: // int overflow // sorting, or taking max, after MOD void solve() throws IOException { int T = ri(); for (int Ti = 0; Ti < T; Ti++) { int n = ri(); int[] a = ril(n); int[] l = ril(n); List<Integer> unlocked = new ArrayList<>(); List<Integer> unlockedIdx = new ArrayList<>(); for (int i = 0; i < l.length; i++) { if (l[i] == 1) continue; unlocked.add(a[i]); unlockedIdx.add(i); } Collections.sort(unlocked, Collections.reverseOrder()); for (int i = 0; i < unlocked.size(); i++) { a[unlockedIdx.get(i)] = unlocked.get(i); } for (int ai : a) pw.print(ai + " "); pw.println(); } } // Template code below BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException { Main m = new Main(); m.solve(); m.close(); } void close() throws IOException { pw.flush(); pw.close(); br.close(); } int ri() throws IOException { return Integer.parseInt(br.readLine()); } long rl() throws IOException { return Long.parseLong(br.readLine()); } int[] ril(int n) throws IOException { int[] nums = new int[n]; int c = 0; for (int i = 0; i < n; i++) { int sign = 1; c = br.read(); int x = 0; if (c == '-') { sign = -1; c = br.read(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = br.read(); } nums[i] = x * sign; } while (c != '\n' && c != -1) c = br.read(); return nums; } long[] rll(int n) throws IOException { long[] nums = new long[n]; int c = 0; for (int i = 0; i < n; i++) { int sign = 1; c = br.read(); long x = 0; if (c == '-') { sign = -1; c = br.read(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = br.read(); } nums[i] = x * sign; } while (c != '\n' && c != -1) c = br.read(); return nums; } int[] rkil() throws IOException { int sign = 1; int c = br.read(); int x = 0; if (c == '-') { sign = -1; c = br.read(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = br.read(); } return ril(x); } long[] rkll() throws IOException { int sign = 1; int c = br.read(); int x = 0; if (c == '-') { sign = -1; c = br.read(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = br.read(); } return rll(x); } char[] rs() throws IOException { return br.readLine().toCharArray(); } void sort(int[] A) { Random r = new Random(); for (int i = A.length-1; i > 0; i--) { int j = r.nextInt(i+1); int temp = A[i]; A[i] = A[j]; A[j] = temp; } Arrays.sort(A); } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
e924f07970d0d421a9e2860cb9b341ec
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class Mamo { static long mod=1000000007; static Reader in=new Reader(); static List<Integer >G[]; static long a[],p[],xp[],xv[]; static StringBuilder Sd=new StringBuilder(),Sl=new StringBuilder(); public static void main(String [] args) { //Dir by MohammedElkady int t=in.nextInt(); while(t-->0) { int n=in.nextInt(); long a[]=new long[n]; long c[]; long b[]=new long[n]; ArrayList<Long> A=new ArrayList(); for(int i=0;i<n;i++)a[i]=in.l(); for(int i=0;i<n;i++) {b[i]=in.l(); if(b[i]==0) {b[i]=0;A.add(a[i]);} } c=new long[A.size()]; for(int i=0;i<c.length;i++)c[i]=A.get(i); if(c.length>0) {Sorting.bucketSort(c, c.length);} for(int i=0,u=c.length-1;i<n;i++) { if(b[i]==0) {out.append(c[u]+" ");u--;} else out.append(a[i]+" "); } out.append("\n"); } out.close(); } static long ans=0L; static boolean v[]; static ArrayList<Integer>res; static Queue <Integer> pop; static Stack <Integer>rem; public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static long gcd(long g,long x){if(x<1)return g;else return gcd(x,g%x);} 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 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 double d() throws IOException {return Double.parseDouble(s()) ;} public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public int[] arr(int n){int[] ret = new int[n];for (int i = 0; i < n; i++) {ret[i] = nextInt();}return ret;} } } class stree{ public stree(int n,long a[]) { this.a=a; seg=new long[n*4]; lazy=new long[n*4]; build(1,0,n-1); } long seg[], a[],lazy[]; void check(int p,int s,int e) { if(lazy[p]!=0) { seg[p] += lazy[p]; if(s!=e) { lazy[2*p] += lazy[p]; lazy[2*p+1] += lazy[p]; } lazy[p] = 0; } } void build(int p,int s,int e) { check(p,s,e); if(s==e) { seg[p] = a[s]; return; } build(2*p,s,(s+e)/2); build(2*p+1,(s+e)/2+1,e); seg[p] = Math.max(seg[2*p], seg[2*p+1]); } void update(int p,int s,int e,int i,int v) { check(p,s,e); if(s==e) { seg[p] = v; return; } if(i<=(s+e)/2) update(2*p,s,(s+e)/2,i,v); else update(2*p+1,(s+e)/2+1,e,i,v); seg[p] = Math.max(seg[2*p],seg[2*p+1]); } void update(int p,int s,int e,int a,int b,int v) { check(p,s,e); if(s>=a && e<=b) { seg[p] += v; if(s!=e) { lazy[2*p] += v; lazy[2*p+1] += v; } return; } if(s>b || e<a) return; update(2*p,s,(s+e)/2,a,b,v); update(2*p+1,(s+e)/2+1,e,a,b,v); seg[p] = Math.max(seg[2*p],seg[2*p+1]); } long get(int p,int s,int e,int a,int b) { if(s>=a && e<=b) return seg[p]; if(s>b || e<a) return Long.MIN_VALUE; return Math.max(get(2*p,s,(s+e)/2,a,b), get(2*p+1,(s+e)/2+1,e,a,b)); } } class node implements Comparable<node>{ int a, b; node(int tt,int ll){ a=tt;b=ll; } @Override public int compareTo(node o) { return b-o.b; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } class Sorting{ public static long[] bucketSort(long[] array, int bucketCount) { if (bucketCount <= 0) throw new IllegalArgumentException("Invalid bucket count"); if (array.length <= 1) return array; //trivially sorted long high = array[0]; long low = array[0]; for (int i = 1; i < array.length; i++) { //find the range of input elements if (array[i] > high) high = array[i]; if (array[i] < low) low = array[i]; } double interval = ((double)(high - low + 1))/bucketCount; //range of one bucket ArrayList<Long> buckets[] = new ArrayList[bucketCount]; for (int i = 0; i < bucketCount; i++) { //initialize buckets buckets[i] = new ArrayList(); } for (int i = 0; i < array.length; i++) { //partition the input array buckets[(int)((array[i] - low)/interval)].add(array[i]); } int pointer = 0; for (int i = 0; i < buckets.length; i++) { Collections.sort(buckets[i]); //mergeSort for (int j = 0; j < buckets[i].size(); j++) { //merge the buckets array[pointer] = buckets[i].get(j); pointer++; } } return array; } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
8ca3870aaea31829d0ba3889625fa464
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.io.*; import java.util.*; public class E95B { public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while(t-->0) { int n = in.nextInt(); long a[] = in.readArray(n,1L); int lock[] = in.readArray(n); ArrayList<Long> arr = new ArrayList<>(); long sum = 0; for(int i=0;i<n;i++) { if(lock[i]==0) { sum += a[i]; arr.add(a[i]); } } Collections.sort(arr,Collections.reverseOrder()); int ind = 0; for(int i=0;i<n;i++) { if(lock[i]==0) { out.print(arr.get(ind)+" ");ind++; } else out.print(a[i]+" "); } out.println(); } out.flush(); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while(!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); }catch(IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } Integer[] readArray(long n) { Integer a[] = new Integer[(int)n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } long[] readArray(int n,long x) { long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = nextLong(); return a; } } static class Pair implements Comparable<Pair> { int x, y; Pair(int a, int b){ x = a; y = b; } int getKey() { return x; } int getVal() { return y; } @Override public int compareTo(Pair o) { if(o.getVal() - this.getVal()>0) return -1; else if(o.getVal() - this.getVal()<0) return 1; else return 0; } } static boolean arrayEquals(char a[], char b[]) { int n = a.length; boolean verdict = true; for(int i=0;i<n;i++) { if(a[i]!=b[i]) { verdict = false;break; } } return verdict; } static long lcm(long a, long b) { return (a*b)/gcd(a,b); } static long gcd(long a, long b) { if(b==0) return a; else return gcd(b,a%b); } static long hashInt(int x,int y) { return x*(1_000_000_000L)+y; } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
2176d5d78e1a278ac1839246904cceae
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.util.ArrayList; import java.io.UncheckedIOException; import java.util.List; import java.io.Closeable; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Collections; import java.io.InputStream; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "khokharnikunj8", 1 << 29); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); BNegativePrefixes solver = new BNegativePrefixes(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } } static class BNegativePrefixes { public void solve(int testNumber, FastInput in, FastOutput out) { int n = in.readInt(); int[] ar = new int[n]; for (int i = 0; i < n; i++) { ar[i] = in.readInt(); } int[] br = new int[n]; List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { br[i] = in.readInt(); if (br[i] == 0) list.add(ar[i]); } Collections.sort(list); int pointer = list.size() - 1; for (int i = 0; i < n; i++) { if (br[i] == 0) { ar[i] = list.get(pointer--); } } for (int i = 0; i < n; i++) { out.append(ar[i] + " "); } out.println(); } } static class FastInput { private final InputStream is; private final StringBuilder defaultStringBuf = new StringBuilder(1 << 13); private final byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public String next() { return readString(); } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public String readString(StringBuilder builder) { skipBlank(); while (next > 32) { builder.append((char) next); next = read(); } return builder.toString(); } public String readString() { defaultStringBuf.setLength(0); return readString(defaultStringBuf); } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private final Writer os; private final StringBuilder cache = new StringBuilder(5 << 20); public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } public FastOutput append(char c) { cache.append(c); return this; } public FastOutput append(String c) { cache.append(c); return this; } public FastOutput println() { cache.append(System.lineSeparator()); return this; } public FastOutput flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
4864ef6184f9b8af0a751c07dbb404b5
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; public final class B { public static void main(String args[]) { StringBuilder ans=new StringBuilder();; FastReader in=new FastReader(); int T=in.nextInt(); while(T-->0) { int N=in.nextInt(); int A[]=new int[N]; //int B[]=new int[N]; boolean c[]=new boolean[N]; for(int i=0; i<N; i++)A[i]=in.nextInt(); int fixed=0; for(int i=0; i<N; i++) { if(in.nextInt()==1) { c[i]=true; fixed++; } } int B[]=new int[N-fixed]; int k=0; for(int i=0; i<N; i++)if(!c[i])B[k++]=A[i]; Arrays.sort(B); k=N-fixed-1; for(int i=0; i<N; i++) { if(c[i])ans.append(A[i]+" "); else ans.append(B[k--]+" "); } ans.append("\n"); }System.out.println(ans); } //fucntions //fucntions //fucntions //fucntions static int[] input(int A[]) //input of Int Array { FastReader in=new FastReader(); int N=A.length; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] input(long A[]) //Input of long Array { FastReader in=new FastReader(); for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } static int GCD(int a,int b) //wrong output if a ||b are intially zero { if(b==0) { return a; } else return GCD(b,a%b ); } static boolean isPrime(int N) { for(int i=2; i*i<N; i++) if(N%i==0)return false; return true; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
5f271aad96f08e8ccc7c7fb8874cf43d
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class negativeprefixes { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = Integer.parseInt(in.readLine()); for (int a = 0; a < t; a++) { int n = Integer.parseInt(in.readLine()); int[] initial = new int[n]; StringTokenizer st1 = new StringTokenizer(in.readLine()); for (int i = 0; i < n; i++) { int num = Integer.parseInt(st1.nextToken()); initial[i] = num; } boolean[] locked = new boolean[n]; StringTokenizer st2 = new StringTokenizer(in.readLine()); for (int i = 0; i < n; i++) { int num = Integer.parseInt(st2.nextToken()); locked[i] = num == 1; } ArrayList<Integer> sort = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { if(!locked[i]) { sort.add(initial[i]); } } Collections.sort(sort); int ind = sort.size()-1; for (int i = 0; i < n; i++) { if(locked[i]) { out.print(initial[i] + " "); } else { out.print(sort.get(ind--) + " "); } } out.println(); } out.close(); } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
3c484baed768661b4b4eafed5bc1fe2a
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { PrintWriter out = new PrintWriter(System.out); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(""); String next() throws IOException { if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } long nl() throws IOException { return Long.parseLong(next()); } void solve() throws IOException { for (int tc=ni();tc>0;tc--) { int n=ni(); int[]A=new int[n+1]; for (int i=1;i<=n;i++) A[i]=ni(); boolean[]L=new boolean[n+1]; for (int i=1;i<=n;i++) if (ni()==1) L[i]=true; int[]B=new int[n+1]; boolean[]F=new boolean[n+1]; for (int i=1;i<=n;i++) { if (L[i]) B[i]=A[i]; else { int max=-1000000; int mp=0; for (int j=1;j<=n;j++) { if (L[j] || F[j]) continue; if (A[j]>max) { max=A[j]; mp=j; } } B[i]=A[mp]; F[mp]=true; } } for (int i=1;i<=n;i++) out.print(B[i]+" "); out.println(); } out.flush(); } public static void main(String[] args) throws IOException { new Main().solve(); } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
6cfc738ea44ff0b9b7af49065cd00420
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
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 mod = 1000000007; for (int ks = 0; ks < n; ks++) { int k = sc.nextInt(); int arr[] = new int[k]; int lock[] = new int[k]; ArrayList<Integer> l = new ArrayList<>(); for (int i = 0; i < k; i++) { arr[i] = sc.nextInt(); } for (int i = 0; i < k; i++) { int temp = sc.nextInt(); lock[i] = temp; if (temp == 0) { l.add(arr[i]); } } Collections.sort(l, Collections.reverseOrder()); int currIdx = 0; for (int i = 0; i < k; i++) { if (lock[i] == 1) { } else { arr[i] = l.get(currIdx); currIdx++; } } System.out.print(arr[0]); for (int i = 1; i < k;i++) { System.out.print(" " + arr[i]); } System.out.println(""); } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
795c50924a1bb4952180db4a91a4d9c6
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.util.*; import java.io.*; public class A { public static void main(String args[]) { FastScanner in=new FastScanner(); int t=in.nextInt(); L: while(--t>=0) { int n=in.nextInt(); int a[]=in.nextArray(n); int l[]=in.nextArray(n); ArrayList<Integer> arr=new ArrayList<>(); for(int i=0;i<n;i++) { if(l[i]==0) arr.add(a[i]); } Collections.sort(arr, (x, y)->y-x); int j=0; for(int i=0;i<n;i++) { if(l[i]==0) { a[i]=arr.get(j); j++; } } for(int i=0;i<n;i++) System.out.print(a[i]+" "); System.out.println(); } } /////////////////////////// static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
e30944b910c9ba8c5704985b86ccbdd8
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Comparator; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author fakhoury */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); BNegativePrefixes solver = new BNegativePrefixes(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class BNegativePrefixes { int[] a; boolean[] locked; public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); a = new int[n]; locked = new boolean[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); List<Integer> positions = new ArrayList<>(); for (int i = 0; i < n; i++) { locked[i] = in.nextInt() == 1; if (!locked[i]) positions.add(i); } int[][] b = new int[2][n]; Collections.sort(positions, new Comparator<Integer>() { public int compare(Integer integer, Integer t1) { return a[integer] - a[t1]; } }); loadFromPosition(b[0], positions); Collections.sort(positions, new Comparator<Integer>() { public int compare(Integer integer, Integer t1) { return a[t1] - a[integer]; } }); loadFromPosition(b[1], positions); out.println(getK(b[0]) < getK(b[1]) ? b[0] : b[1]); } void loadFromPosition(int[] b, List<Integer> positions) { int n = b.length; for (int i = 0, j = 0; i < n; i++) { if (locked[i]) b[i] = a[i]; else b[i] = a[positions.get(j++)]; } } long getK(int[] b) { int n = b.length; long[] p = new long[n]; for (int i = 0; i < n; i++) { p[i] = b[i]; p[i] += i > 0 ? p[i - 1] : 0; } for (int i = n - 1; i >= 0; i--) if (p[i] < 0) { return i + 1; } return 0; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void println(int[] array) { print(array); writer.println(); } public void close() { writer.close(); } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
7194b9c9e16f8df278ce3c74d464c34e
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++) { int n = sc.nextInt(); int[] arr = new int[n]; int[] crr = new int[n]; for(int j=0;j<n;j++) { arr[j] = sc.nextInt(); } int count=0; for(int j=0;j<n;j++) { crr[j] = sc.nextInt(); if(crr[j]==1) count++; } if(count<n) findans(arr,crr,n); else { print(arr); } System.out.println(); } sc.close(); } public static void findans(int[] arr,int[] crr, int n) { ArrayList<Integer> plist = new ArrayList<Integer>(); ArrayList<Integer> nlist = new ArrayList<Integer>(); for(int i=0;i<n;i++) { if(crr[i]==0) { if(arr[i]>0) plist.add(arr[i]); else { nlist.add(arr[i]); } } } if(plist.size()!=0) { Collections.sort(plist, new Comparator<Integer>() { public int compare(Integer a, Integer b) { return -1*Integer.compare(a, b); } }); } if(nlist.size()!=0) { Collections.sort(nlist,new Comparator<Integer>() { public int compare(Integer a,Integer b) { return -1*Integer.compare(a, b); } }); } int[] a = new int[nlist.size()+plist.size()]; int i=0; for(;i<plist.size();i++) { a[i] = plist.get(i); } for(;i<a.length;i++) { a[i] = nlist.get(i-plist.size()); } int k=0,j=0; while(k<a.length && j<arr.length) { if(crr[j]==0) { arr[j] = a[k]; k++; } j++; } print(arr); } public static void print(int[] arr) { for(int i=0;i<arr.length;i++){ System.out.print(arr[i]+" "); } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
e277e74072e9d03ce1776954f1a055f6
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class C { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for(int tt=0;tt<T;tt++) { int n = in.nextInt(); int[] a = in.nextArray(n); int[] lock = in.nextArray(n); ArrayList<Integer> sort = new ArrayList<Integer>(); for(int i=0;i<n;i++) { if(lock[i]==0) sort.add(a[i]); } Collections.sort(sort); int index = sort.size()-1; for(int i=0;i<n;i++) { if(lock[i]==0) a[i] = sort.get(index--); } for(int i:a) out.print(i+" "); out.println(); } out.close(); } public static long mul(long a , long b) { int MOD = (int)(1e9+7); return ((a%MOD)*(b%MOD))%MOD; } static final Random random=new Random(); // static void ruffleSort(Pair[] a) { // int n=a.length;//shuffle, then sort // for (int i=0; i<n; i++) { // int oi=random.nextInt(n); // Pair temp=a[oi]; // a[oi]=a[i]; a[i]=temp; // } // Arrays.sort(a); // } static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void fastSort(char[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); char temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } } //class Pair implements Comparable<Pair>{ // int a; // int b; // public Pair(int a, int b) { // this.a = a; // this.b = b; // } // public int compareTo(Pair o) { // if(this.a==o.a) // return this.b - o.b; // return this.a - o.a; // } //}
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
9fa32497f6ce0eb31a67056f3df6e016
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- != 0) { int n = sc.nextInt(); int input = 0; ArrayList<Integer> array = new ArrayList<Integer>(); ArrayList<Integer> lock = new ArrayList<Integer>(); for(int i = 0;i<n;i++) { input = sc.nextInt(); array.add(input); } input = 0; for(int i = 0;i<n;i++) { input = sc.nextInt(); lock.add(input); } ArrayList<Integer> change = new ArrayList<Integer>(); for(int i = 0;i<n;i++) { if(lock.get(i) == 0) { change.add(array.get(i)); } } // if we sort them in decreasing order then we get the maximum negative value in the latest position Collections.sort(change, Collections.reverseOrder()); int j = 0; for(int i = 0;i<n;i++) { if(lock.get(i) == 0) { array.set(i, change.get(j++)); } } for(int i = 0;i<n;i++) { System.out.print(array.get(i) + " " ); } System.out.println(); } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
78e79488c51efa17b92fd1f910f2fa1e
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.io.*; import java.util.*; public class B { static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U,V>>{ public final U a; public final V b; private Pair(U a, V b) { this.a = a; this.b = b; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; if (!a.equals(pair.a)) return false; return b.equals(pair.b); } @Override public int hashCode() { return 31 * a.hashCode() + b.hashCode(); } @Override public String toString() { return "(" + a + ", " + b + ")"; } @Override public int compareTo(Pair<U, V> o) { if(this.a.equals(o.a)){ return getV().compareTo(o.getV()); } return getU().compareTo(o.getU()); } private U getU() { return a; } private V getV() { return b; } static void print(Pair[] pairs){ for(int i=0;i<pairs.length;i++){ System.out.print(pairs[i]+" "); } System.out.println(); } static void print(Pair[][] pairs){ for(int i=0;i<pairs.length;i++){ for(int j=0;j<pairs[0].length;j++) { System.out.print(pairs[i] + " "); } System.out.println(); } } } static BufferedReader inp = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException { int t = Integer.parseInt(inp.readLine()); while (t-->0){ int size = Integer.parseInt(inp.readLine()); int[] given = new int[size]; int[] after = new int[size]; ArrayList<Integer> list = new ArrayList<>(); int[] status = new int[size]; String[] s1 = inp.readLine().split(" "); String[] s2 = inp.readLine().split(" "); for(int i=0;i<size;i++){ int a = Integer.parseInt(s1[i]); int b = Integer.parseInt(s2[i]); given[i] = a; status[i] = b; if(b==0){ list.add(a); } else{ after[i] = a; } } Collections.sort(list,Collections.reverseOrder()); int l = list.size(); for(int i=0;i<l;i++){ list.add(list.get(i)); } int ans = Integer.MAX_VALUE; int sum = 0; for(int i=0;i<size;i++){ sum+=given[i]; if(sum<0){ ans = i+1; } } for(int i=0;i<list.size()/2;i++){ //System.out.println(i); int[] check = after.clone(); int neg = 0; sum = 0; int a = i; int b = 0; while (b<size){ if(status[b]==0){ check[b] = list.get(a); a++; } sum+=check[b]; if(sum<0){ neg = b+1; } b++; } ans = Math.min(neg,ans); if(ans==neg){ given = check.clone(); } } print(given); //out.write(ans+"\n"); } out.flush(); } static boolean check(int mid,int[] given,int size){ Set<Integer> set = new HashSet<>(); Map<Integer,Integer> map = new HashMap(); for(int i=mid;i<size;i++){ if(set.contains(given[i])){ map.put(given[i],map.get(given[i])+1); } else{ map.put(given[i],1); set.add(given[i]); } } boolean ans = false; boolean one = true; Iterator<Integer> iterator = set.iterator(); while (iterator.hasNext()){ if(map.get(iterator.next())>1){ one = false; } } if(one){ ans = true; } for(int i=0;i<size-mid;i++){ int a = given[i+mid]; if(set.contains(a)){ if(map.get(a)>1){ map.put(a,map.get(a)-1); } else{ map.put(a,0); set.remove(a); } } a = given[i]; if(set.contains(a)){ map.put(a,map.get(a)+1); } else{ set.add(a); map.put(a,1); } iterator = set.iterator(); one = true; while (iterator.hasNext()){ if(map.get(iterator.next())>1){ one = false; } } if(one){ ans = true; } } return ans; } static void print(int[] array){ for(int j=0;j<array.length;j++){ System.out.print(array[j]+" "); } System.out.println(); } static void print(int[][] array){ for(int i=0;i< array.length;i++) { for (int j = 0; j < array[0].length; j++) { System.out.print(array[i][j] + " "); } System.out.println(); } } static void print(boolean[] array){ for(int j=0;j<array.length;j++){ System.out.print(array[j]+" "); } System.out.println(); } static void print(boolean[][] array){ for(int i=0;i< array.length;i++) { for (int j = 0; j < array[0].length; j++) { System.out.print(array[i][j] + " "); } System.out.println(); } } static void print(long[] array){ for(int j=0;j<array.length;j++){ System.out.print(array[j]+" "); } System.out.println(); } static void print(long[][] array){ for(int i=0;i< array.length;i++) { for (int j = 0; j < array[0].length; j++) { System.out.print(array[i][j] + " "); } System.out.println(); } } static void print(String[] array){ for(int j=0;j<array.length;j++){ System.out.print(array[j]+" "); } System.out.println(); } static void print(String[][] array){ for(int i=0;i< array.length;i++) { for (int j = 0; j < array[0].length; j++) { System.out.print(array[i][j] + " "); } System.out.println(); } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
470a9266ffadbb2535573d820513f434
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class problemB { public static void main(String[] args) { int T = sc.nextInt(); for (int t = 0; t < T; t++) { solve(); } out.close(); } private static void solve() { int n = sc.nextInt(); int[] arr = sc.readArray(n); int[] locked = sc.readArray(n); // out.println(Arrays.toString(arr) +" "+Arrays.toString(locked)); List<Integer> list = new ArrayList<>(); for(int i = 0;i<n;i++) { if(locked[i] == 0) list.add(arr[i]); } list.sort(Collections.reverseOrder()); int j = 0; for(int i = 0;i<n;i++) { if(locked[i] == 0) arr[i] = list.get(j++); } for(int i : arr) { out.print(i+" "); } out.println(); } private static final FastScanner sc = new FastScanner(); private static final PrintWriter out = new PrintWriter(System.out); public static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } public static long lcm(long a, long b) { return a * (b / gcd(a, b)); } public static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextString() { return next(); } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
e590a9dc3a805c59e93f04d6bed15447
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int tst = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); while(tst-->0){ int n = Integer.parseInt(br.readLine()); StringTokenizer st1 = new StringTokenizer(br.readLine()); StringTokenizer st2 = new StringTokenizer(br.readLine()); int[] arr = new int[n]; int[] status = new int[n]; ArrayList<Integer> lst = new ArrayList<>(); for(int i = 0; i<n; i++){ arr[i] = Integer.parseInt(st1.nextToken()); status[i] = Integer.parseInt(st2.nextToken()); if(status[i] == 0) lst.add(arr[i]); } lst.sort(Collections.reverseOrder()); int k = 0; for(int i = 0; i<n; i++){ if(status[i] == 0){ sb.append(lst.get(k)).append(" "); k++; } else sb.append(arr[i]).append(" "); } sb.append('\n'); } System.out.println(sb); } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
4622abb52663b7a9296372761296919e
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.io.PrintWriter; import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.Scanner; public class Main { static void swap(long ar[], int i, int j) { long temp = ar[ i]; ar[i]=ar[j]; ar[j]=temp; } static int gcd(int x, int y) { if(y==0) return x; else return gcd(y, x%y); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int tc = sc.nextInt(); while(tc-->0) { int n = sc.nextInt(); int ar[] = new int[n]; int lock[] = new int[n]; for(int i=0;i<n;i++) ar[i] =sc.nextInt(); for(int i=0;i<n;i++) lock[i]= sc.nextInt(); LinkedList<Integer> l =new LinkedList<Integer>(); for(int i=0;i<n;i++) { if(lock[i]==0) l.add(ar[i]); } Collections.sort(l); for(int i=0;i<n;i++) { if(lock[i]==1) System.out.print(ar[i]+" "); else System.out.print(l.pollLast()+" "); } System.out.println(); } out.close(); sc.close(); } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
e85e7365b8bd4146acc876b422f8b8ea
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
// Working program with FastReader import java.util.*; import java.io.*; public class Codechef { static final int mod = 1000000007; public static void main(String args[]) throws Exception{ FastReader sc = new FastReader(); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int arr[] = new int[n]; boolean locked[] = new boolean[n]; for(int i=0; i<n ; i++){ arr[i] = sc.nextInt(); } for(int i=0; i<n; i++){ if(sc.nextInt() == 1){ locked[i] = true; } } List<Integer> freeElements = new ArrayList<>(); for(int i=0; i<n; i++){ if(!locked[i]){ freeElements.add(arr[i]); } } Collections.sort( freeElements, Collections.reverseOrder()); int index = 0; for(int i=0; i<n; i++){ if(!locked[i]){ arr[i] = freeElements.get(index++); } } sc.printArray(arr); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } void print( char output){ System.out.print(output + " "); } void print( int output){ System.out.print(output + " "); } void print( long output){ System.out.print(output + " "); } void print( double output){ System.out.print(output + " "); } void print(String output){ System.out.println(output); } void print(StringBuilder output){ System.out.print(output); } void println( char output){ System.out.println(output); } void println( int output){ System.out.println(output); } void println( long output){ System.out.println(output); } void println(double output){ System.out.println(output); } void println(String output){ System.out.println(output); } void println(StringBuilder output){ System.out.println(output); } void printArray(char arr[]){ StringBuilder str = new StringBuilder(); int n = arr.length; for(int i=0; i<n; i++){ str.append(arr[i]+" "); } System.out.println(str); } void printArray(int arr[]){ StringBuilder str = new StringBuilder(); int n = arr.length; for(int i=0; i<n; i++){ str.append(arr[i]+" "); } System.out.println(str); } void printArray(long arr[]){ StringBuilder str = new StringBuilder(); int n = arr.length; for(int i=0; i<n; i++){ str.append(arr[i]+" "); } System.out.println(str); } void printArray(double arr[]){ StringBuilder str = new StringBuilder(); int n = arr.length; for(int i=0; i<n; i++){ str.append(arr[i]+" "); } System.out.println(str); } int maxNumber(){ return Integer.MAX_VALUE; } int minNumber(){ return Integer.MIN_VALUE; } static long pow(long base, int p, int mod_){ if(p == 0) return 1; long m = (base * base ) % mod; if(p % 2 == 0){ return pow(m, p/2)%mod; } else return (base * pow(m , p/2) ) % mod ; } static long pow(long base, int p){ if(p == 0) return 1; long product = base * base ; if(p % 2 == 0){ return pow(product, p/2); } else return base * pow(product , p/2) ; } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
86573052b271552d4ae38940fb66ca0b
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.util.*; import java.io.*; public class B { public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); int bruh = in.nextInt(); for (int cases = 0; cases < bruh; cases++) { int n = in.nextInt(); int[] nums = in.nextIntArr(n); int[] state = in.nextIntArr(n); ArrayList<Integer> unlocked = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { if (state[i] == 0) { unlocked.add(nums[i]); } } Collections.sort(unlocked, Collections.reverseOrder()); for (int i = 0; i < n; i++) { if (state[i] == 0) { out.print(unlocked.remove(0) + " "); } else { out.print(nums[i] + " "); } } out.println(); } out.close(); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream i) { br = new BufferedReader(new InputStreamReader(i)); st = new StringTokenizer(""); } public String next() throws IOException { if (st.hasMoreTokens()) { return st.nextToken(); } else st = new StringTokenizer(br.readLine()); return next(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); return nextLine(); } String ret = ""; while (st.hasMoreTokens()) { ret += st.nextToken(); } return ret; } public int[] nextIntArr(int size) throws IOException { int[] arr = new int[size]; for (int i = 0; i < arr.length; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArr(int size) throws IOException { long[] arr = new long[size]; for (int i = 0; i < arr.length; i++) { arr[i] = nextLong(); } return arr; } public double[] nextDoubleArr(int size) throws IOException { double[] arr = new double[size]; for (int i = 0; i < arr.length; i++) { arr[i] = nextDouble(); } return arr; } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
014536b2293c1e35414903aa525e4a7d
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { //solution start :-) Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] = sc.nextInt(); } int b[] = new int[n]; for(int i=0;i<n;i++) { b[i] = sc.nextInt(); } ArrayList<Integer> al = new ArrayList<>(); ArrayList<Integer> al1 = new ArrayList<>(); for(int i=0;i<n;i++) { if(b[i]==1) al.add(a[i]); else al1.add(a[i]); } Collections.sort(al1); //System.out.println(al1); int j=0; int sum=0; int m=0; int k = al1.size()-1; for(int i=0;i<n;i++) { if(b[i]==1) { System.out.print(al.get(j++)+" "); } else { System.out.print(al1.get(k--)+" "); } } System.out.println(); } //solution end <-_-> }}
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
8669af86f46906cc6e4e1308e4204e6e
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner s = new Scanner(System.in); int t = s.nextInt(); StringBuilder str = new StringBuilder(); while(t-->0) { int n = s.nextInt(); long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = s.nextLong(); List<Long> al = new ArrayList<>(); int f[] = new int[n]; for(int i=0;i<n;i++) { f[i] = s.nextInt(); if(f[i] == 0) al.add(a[i]); } Collections.sort(al); long sa = 0; int ans=0; int k=al.size()-1; for(int i=0;i<n;i++) { a[i] = f[i]==0 ?al.get(k--):a[i]; } for(int i=0;i<n;i++) str.append(a[i]+" "); str.append("\n"); } System.out.println(str); } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
011602d30cd97d8b70a898b304da4a2b
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Collections; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Task2 { public static void main(String[] args) throws IOException { new Task2().solve(); } private void solve() throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(f.readLine()); for (int t1 = 0; t1 < t; t1++) { int n = Integer.parseInt(f.readLine()); StringTokenizer tokenizer = new StringTokenizer(f.readLine()); int[] ar = new int[n]; for (int i = 0; i < n; i++) ar[i] = Integer.parseInt(tokenizer.nextToken()); tokenizer = new StringTokenizer(f.readLine()); int[] locked = new int[n]; for (int i = 0; i < n; i++) locked[i] = Integer.parseInt(tokenizer.nextToken()); PriorityQueue<Integer> notLocked = new PriorityQueue<Integer>(Collections.reverseOrder()); for (int i = 0; i < n; i++) { if (locked[i] == 0) notLocked.add(ar[i]); } for (int i = 0; i < n; i++) { if (locked[i] == 0) ar[i] = notLocked.remove(); } out.print(ar[0]); for (int i = 1; i < n; i++) { out.print(" "); out.print(ar[i]); } out.println(); } out.close(); } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
fb7698c8607dfe660664621e98c59ccf
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int[] array = new int[n]; int[] fixed = new int[n]; for(int j = 0; j < n; j++) { array[j] = sc.nextInt(); } for(int j = 0; j < n; j++) { fixed[j] = sc.nextInt(); } List<Integer[]> in = new ArrayList<>(); for(int j = 0; j < n; j++) { in.add(new Integer[]{array[j], fixed[j]}); } test(n, array, fixed, in); } } private static void test(int n, int[] array, int[] fixed, List<Integer[]> in) { List<Integer> movables = in.stream() .filter(integers -> integers[1] == 0) .map(integers -> integers[0]) .sorted() .collect(Collectors.toList()); Collections.reverse(movables); int p = Integer.MIN_VALUE; int movableIndex = 0; int[] answer = new int[n]; for(int i = 0; i < n; i++) { if(fixed[i] == 1) { answer[i] = array[i]; } else { answer[i] = movables.get(movableIndex++); } if (p == Integer.MIN_VALUE) p = 0; p += answer[i]; } System.out.println(Arrays.toString(answer) .replace("[", "") .replace("]", "") .replace(",", "") ); } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
0d40032969e9807a5798c9aef47790cf
train_004.jsonl
1600094100
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$; $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$; $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j &lt; 0$$$. If there are no $$$j$$$ such that $$$p_j &lt; 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class p1418b { public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); for(int tt =0 ;tt < t ;tt++) { int n = sc.nextInt(); int a[] = sc.readArray(n); int lock[] = sc.readArray(n); PriorityQueue<Integer> notLocked = new PriorityQueue<>(Collections.reverseOrder()); for(int i = 0 ; i < n ;i++) { if(lock[i] == 0)notLocked.add(a[i]); } for(int i = 0 ; i < n ;i++) { if(lock[i] == 0)a[i] = notLocked.remove(); } for(int i= 0 ;i < n ;i++) { System.out.print(a[i] + " "); } System.out.println(); } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } public long nextLong() {return Long.parseLong(next());} public double nextDouble() {return Double.parseDouble(next());} int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
Java
["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"]
2 seconds
["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"]
NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j &lt; 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k &lt; 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.
Java 8
standard input
[ "sortings", "greedy" ]
9ef180b33717e4d6a21b4c5bb855e15b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β€” the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$)Β β€” the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.
1,300
Print $$$n$$$ integersΒ β€” the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j &lt; 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.
standard output
PASSED
697b140623d6cdf09ef629a0c67b93be
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
//package Round_183; import java.util.*; import java.io.*; import static java.lang.Math.*; public class C { void print(int a[]){ for (int x : a) out.print(x + " "); out.println(); } void solve () throws IOException { int n = in.nextInt(); if (n % 2 == 0){ out.println(-1); return; } int a[] = new int[n]; int b[] = new int[n]; int c[] = new int[n]; for (int i = 0; i<n; i++){ a[i] = b[i] = i; c[i] = (i + i) % n; } print(a); print(b); print(c); } String input = ""; String output = ""; FastScanner in; PrintWriter out; void run () { try { BufferedReader bf; if (input.length() == 0) bf = new BufferedReader(new InputStreamReader(System.in)); else bf = new BufferedReader(new FileReader(input)); in = new FastScanner(bf); if (output.length() == 0) out = new PrintWriter(System.out); else out = new PrintWriter(new File(output)); solve(); out.close(); } catch (Exception ex) { out.close(); ex.printStackTrace(); } } public static void main (String[] args) { new C().run(); } class FastScanner { BufferedReader bf; StringTokenizer st; FastScanner(BufferedReader bf) { this.bf = bf; } String next () throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(bf.readLine()); 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()); } int[] readIntArray (int n) throws IOException { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray (int n) throws IOException { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
275ce54e7de7043435c7b8d526cba44d
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import static java.util.Arrays.deepToString; import java.io.*; import java.math.*; import java.util.*; public class Main { static void solve() { int n = nextInt(); if (n%2 == 0) { System.out.println(-1); return; } int[] c = new int[n]; StringBuffer sb = new StringBuffer(); for (int i = 0; i < n; i++) { sb.append(i); if (i!= n-1) { sb.append(" "); } c[i] = i*2%n; } System.out.println(sb); System.out.println(sb); StringBuilder sbb = new StringBuilder(); for (int i = 0; i < c.length; i++) { sbb.append(c[i]); if (i!=n-1) { sbb.append(" "); } } System.out.println(sbb); } public static void main(String[] args) throws Exception { reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(System.out); setTime(); solve(); printTime(); printMemory(); writer.close(); } static BufferedReader reader; static PrintWriter writer; static StringTokenizer tok = new StringTokenizer(""); static long systemTime; static void debug(Object... o) { System.err.println(deepToString(o)); } static void setTime() { systemTime = System.currentTimeMillis(); } static void printTime() { System.err.println("Time consumed: " + (System.currentTimeMillis() - systemTime)); } static void printMemory() { System.err.println("Memory consumed: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime() .freeMemory()) / 1000 + "kb"); } static String next() { while (!tok.hasMoreTokens()) { String w = null; try { w = reader.readLine(); } catch (Exception e) { e.printStackTrace(); } if (w == null) return null; tok = new StringTokenizer(w); } return tok.nextToken(); } static int nextInt() { return Integer.parseInt(next()); } static long nextLong() { return Long.parseLong(next()); } static double nextDouble() { return Double.parseDouble(next()); } static BigInteger nextBigInteger() { return new BigInteger(next()); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
b80158372b5783ac37a77d6b57e21a69
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.util.*; public class cf303a { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); if(n%2 == 0) System.out.println("-1"); else { for(int i=0; i<n; i++) System.out.print(i+ " "); System.out.println(); for(int i=0; i<n; i++) System.out.print(i+ " "); System.out.println(); for(int i=0; i<n; i++) System.out.print((2*i)%n+ " "); System.out.println(); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
286445e105c8b21a20ea4dbab67acf14
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.util.Scanner; public class P304c { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); if (n%2 ==0) { System.out.println("-1"); } else { StringBuffer sb1 = new StringBuffer(); StringBuffer sb2 = new StringBuffer(); for (int i = 0; i < n; i++) { sb1.append(i).append(" "); sb2.append((i+i)%n).append(" "); } System.out.println(sb1); System.out.println(sb1); System.out.println(sb2); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
dade1a8b15078ba897cfb127dc8ae523
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; public class Main { public static void main(String [] args ) { try{ String str; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedOutputStream bos = new BufferedOutputStream(System.out); String eol = System.getProperty("line.separator"); byte [] eolb = eol.getBytes(); byte[] spaceb= " ".getBytes(); str = br.readLine(); int n = Integer.parseInt(str); if((n%2)==0) { bos.write("-1".getBytes()); bos.write(eolb); bos.flush(); return; } boolean p = false; ArrayList<Integer> cop = new ArrayList<Integer>(); for(int i = 1 ; i < n ; i++) { int gcd = getGCD(i,n); if(gcd==1) { cop.add(i); } } Integer [] cp = new Integer[cop.size()]; cp = cop.toArray(cp); Arrays.sort(cp); int st = 0; int en = 0; for(int sum = 0 ; sum < cp.length ; sum++) { st = 0; en = cp.length - 1; while(st<en) { if((cp[st]+cp[en])==cp[sum]) { p = true; break; } else if ( (cp[st]+cp[en]) < cp[sum]) { st++; } else { en--; } } if(p) { break; } } if(p) { int a = cp[st]; int b = cp[en]; int x = 0; for(int i = 0 ; i < n ; i++) { if(i!=0) { bos.write(spaceb); } bos.write(new Integer(x).toString().getBytes()); x += a; x %= n; } bos.write(eolb); int y = 0; for(int i = 0 ; i < n ; i++) { if(i!=0) { bos.write(spaceb); } bos.write(new Integer(y).toString().getBytes()); y += b; y %= n; } bos.write(eolb); int z = 0; for(int i = 0 ; i < n ; i++) { if(i!=0) { bos.write(spaceb); } bos.write(new Integer(z).toString().getBytes()); z += (a+b); z %= n; } bos.write(eolb); } else { if(n==1) { bos.write("0".getBytes()); bos.write(eolb); bos.write("0".getBytes()); bos.write(eolb); bos.write("0".getBytes()); bos.write(eolb); } else { bos.write("-1".getBytes()); bos.write(eolb); } } bos.flush(); } catch(IOException ioe) { ioe.printStackTrace(); } } public static int getGCD(int a , int b) { if((b%a)==0) { return a; } else { return getGCD((b%a),a); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
1f721a60824b49000bfa5f73dd21e348
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.util.*; import java.lang.Math; public class Problem3 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[][] val = new int[n][3]; if(n == 1) for(int i=0; i<3; i++) System.out.println("0"); else if(n % 2 == 0) System.out.println("-1"); else { for(int i=0; i<n; i++) { val[i][0] = i; val[i][1] = i; val[i][2] = i*2 % n; } for(int i=0; i<3; i++) { for(int j=0; j<n; j++) System.out.print(val[j][i] + " "); System.out.print("\n"); } } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
9bd70621523166143ea36afdcc7b9b33
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.util.Scanner; public class CF_304C { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if (n % 2==0) { System.out.println(-1); return; } for (int i = 0; i < n; i++) { System.out.print(i+" "); } System.out.println(); for (int i = 0; i < n; i++) { System.out.print(i+" "); } System.out.println(); for (int i = 0; i < n; i++) { System.out.print((2*i) % n+" "); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
c400abc41362d7e70855abda31289d14
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.util.Scanner; 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(); if (n % 2 == 0)out.print(-1); else { for (int i = 0; i < n; ++i)out.print(i+" "); out.println(); for (int i = 0; i < n; ++i)out.print(i+" "); out.println(); for (int i = 0; i < n; ++i)out.print((i*2) % n+" "); out.println(); } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); 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
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
383359106464a7710e7c6a3e55c4fe1c
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.util.Scanner; public class _304C { static Scanner scanner = new Scanner(System.in); static int n; public static void main(String[] args) { n = scanner.nextInt(); if (n % 2 == 0) { System.out.println(-1); } else if (n == 1) { System.out.println("0\n0\n0"); } else { for (int i=0;i<n;++i) { System.out.print(i); if (i < n - 1) { System.out.print(" "); } else System.out.println(); } for (int i=2;;) { System.out.print(i); if (i != 1) { System.out.print(" "); } else { System.out.println(); break; } i = (i+1) % n; } for (int i=2;;) { System.out.print(i); if (i != 0) { System.out.print(" "); } else { System.out.println(); break; } i = (i+2) % n; } } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
709feb463f7e54cfd4bd0cd2f21e30b2
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class CC { /** * @param args * @throws IOException * @throws NumberFormatException */ public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader sc = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(sc.readLine()); if (n % 2 == 0) { System.out.println(-1); return; } for (int i = 0; i < n - 1; i++) { System.out.print(i + " "); } System.out.println(n - 1); for (int i = 1; i < n; i++) { System.out.print(i + " "); } System.out.println("0"); for (int i = 0; i < n - 1; i++) { System.out.print((i + i + 1) % n + " "); } System.out.println(n - 1); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
af3477819de24cc22b33fd0bafe4824a
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.*; import java.util.*; import java.math.*; final public class Main implements Runnable { private static boolean local; public static void main(String[] args) { if (args.length > 0 && args[0].equals("local")) { local = true; } new Thread(null, new Main(), "mainthread", 1 << 27).start(); } public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; try { if (local) { inputStream = new FileInputStream("X:/coding/workspace/java/main/io/input.txt"); outputStream = new FileOutputStream("X:/coding/workspace/java/main/io/output.txt"); } else { // inputStream = new FileInputStream("input.txt"); // outputStream = new FileOutputStream("output.txt"); } } catch (Exception e) { throw new RuntimeException(e); } Task solver = new Task(); solver.in = new InputReader(inputStream); solver.out = new OutputWriter(outputStream); if (local) { solver.dout = new DebugWriter(solver.out); } solver.solve(); solver.out.close(); } } final class Task { public InputReader in; public OutputWriter out; public DebugWriter dout; void err() { System.err.println("err"); } void err(String s) { System.err.println(s); } public void solve() { int n = in.readInt(); if ((n & 1) == 0) out.printLine(-1); else { for (int j = 0; j < 2; ++j) for (int i = 0; i < n; ++i) { out.print(i); if (i + 1 == n) out.printLine(); else out.print(" "); } for (int i = 0; i < n; ++i) { out.print(i * 2 % n); if (i + 1 == n) out.printLine(); else out.print(" "); } } } } final class InputReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1 << 13]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int peek() { if (numChars == -1) return -1; if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) return -1; } return buf[curChar]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) return readLine(); else return readLine0(); } public String readToEnd() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != -1) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } 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 void close() { try { stream.close(); } catch (IOException e) { throw new RuntimeException(); } } } final class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream), 1 << 13)); } 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 printFormat(String format, Object... objects) { writer.printf(format, objects); } public void print(char[] objects) { writer.print(objects); } public void printLine(char[] objects) { writer.println(objects); } public void printLine(char[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); } public void print(int[] objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(int[] objects) { print(objects); writer.println(); } public void printLine(int[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); } public void print(short[] objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(short[] objects) { print(objects); writer.println(); } public void printLine(short[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); } public void print(long[] objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(long[] objects) { print(objects); writer.println(); } public void printLine(long[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); } public void print(double[] objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(double[] objects) { print(objects); writer.println(); } public void printLine(double[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); } public void print(byte[] objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(byte[] objects) { print(objects); writer.println(); } public void printLine(byte[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); } public void print(boolean[] objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(boolean[] objects) { print(objects); writer.println(); } public void printLine(boolean[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); } public void close() { writer.close(); } public void flush() { writer.flush(); } } final class DebugWriter { private final OutputWriter writer; public DebugWriter(OutputWriter writer) { this.writer = writer; } private void printDebugMessage() { writer.print("DEBUG:\t"); } public void printLine(Object... objects) { flush(); printDebugMessage(); writer.printLine(objects); flush(); } public void printFormat(String format, Object... objects) { flush(); printDebugMessage(); writer.printFormat(format, objects); flush(); } public void printLine(char[] objects) { flush(); printDebugMessage(); writer.printLine(objects); flush(); } public void printLine(char[][] objects) { flush(); for (int i = 0; i < objects.length; ++i) printLine(objects[i]); flush(); } public void printLine(double[] objects) { flush(); printDebugMessage(); writer.printLine(objects); flush(); } public void printLine(double[][] objects) { flush(); for (int i = 0; i < objects.length; ++i) printLine(objects[i]); flush(); } public void printLine(int[] objects) { flush(); printDebugMessage(); writer.printLine(objects); flush(); } public void printLine(int[][] objects) { flush(); for (int i = 0; i < objects.length; ++i) printLine(objects[i]); flush(); } public void printLine(short[] objects) { flush(); printDebugMessage(); writer.printLine(objects); flush(); } public void printLine(short[][] objects) { flush(); for (int i = 0; i < objects.length; ++i) printLine(objects[i]); flush(); } public void printLine(long[] objects) { flush(); printDebugMessage(); writer.printLine(objects); flush(); } public void printLine(long[][] objects) { flush(); for (int i = 0; i < objects.length; ++i) printLine(objects[i]); flush(); } public void printLine(byte[] objects) { flush(); printDebugMessage(); writer.printLine(objects); flush(); } public void printLine(byte[][] objects) { flush(); for (int i = 0; i < objects.length; ++i) printLine(objects[i]); flush(); } public void printLine(boolean[] objects) { flush(); printDebugMessage(); writer.printLine(objects); flush(); } public void printLine(boolean[][] objects) { flush(); for (int i = 0; i < objects.length; ++i) printLine(objects[i]); flush(); } public void flush() { writer.flush(); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
c6abd8ae0a9bdfdd721af8561752253b
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.util.Scanner; /** * Created with IntelliJ IDEA. * User: Student_301_16 * Date: 10/15/13 * Time: 2:43 PM * To change this template use File | Settings | File Templates. */ public class triple { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); if(n%2==0){ System.out.println(-1); return; } for(int i=0; i<n;i++){ System.out.print(i+" "); } System.out.println(); for(int i=0; i<n;i++){ System.out.print(i+" "); } System.out.println(); for(int i=0; i<n;i++){ System.out.print((i*2)%n+" "); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
07f5093e32c2872ff9b50d355e9957db
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.*; import java.math.*; import java.util.*; //Codeforces public class MainCodeforces2 { private static MyScanner in; private static PrintStream out; private static boolean LOCAL_TEST = false; private static void solve() throws IOException { int N = in.nextInt(); if (N % 2 == 0) { out.println(-1); return; } int[] a = new int[N]; int[] b = new int[N]; int[] c = new int[N]; for (int i = 0; i < a.length; i++) { a[i] = i; b[i] = (a[i] + 1) % N; c[i] = (a[i] + b[i]) % N; } for (int i = 0; i < N; i++) { out.print("" + a[i] + " "); } out.println(); for (int i = 0; i < N; i++) { out.print("" + b[i] + " "); } out.println(); for (int i = 0; i < N; i++) { out.print("" + c[i] + " "); } } public static void main(String[] args) throws IOException { // helpers for input/output out = System.out; try { String cname = System.getenv("COMPUTERNAME"); LOCAL_TEST = (cname.equals("ALPHA530")); } catch (Exception e) { } if (LOCAL_TEST) { in = new MyScanner("E:\\zin.txt"); } else { boolean usingFileForIO = false; if (usingFileForIO) { // using input.txt and output.txt as I/O in = new MyScanner("input.txt"); out = new PrintStream("output.txt"); } else { in = new MyScanner(); out = System.out; } } solve(); } // ===================================== static class MyScanner { Scanner inp = null; public MyScanner() throws IOException { inp = new Scanner(System.in); } public MyScanner(String inputFile) throws IOException { inp = new Scanner(new FileInputStream(inputFile)); } public int nextInt() throws IOException { return inp.nextInt(); } public long nextLong() throws IOException { return inp.nextLong(); } public double nextDouble() throws IOException { return inp.nextDouble(); } public String nextString() throws IOException { return inp.next(); } public String nextLine() throws IOException { return inp.nextLine(); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
34558a653721f2bbe3cc6e907ad20c1c
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashSet; import java.util.InputMismatchException; public class B { static long mod = 1000000007; static PrintWriter out; public static void main(String[] args) { MScanner sc = new MScanner(); out = new PrintWriter(System.out); int N = sc.nextInt(); int[] A = new int[N]; int[] B = new int[N]; int[] C = new int[N]; HashSet<Integer> done = new HashSet<Integer>(); for(int a=0;a<N;a++){ A[a]=a; B[a]=(a+N+1)%N; C[a]=(A[a]+B[a])%N; if(done.contains(C[a])){ out.println("-1"); out.close(); } else done.add(C[a]); } for(int a=0;a<N;a++) out.print(A[a]+" "); out.println(); for(int a=0;a<N;a++) out.print(B[a]+" "); out.println(); for(int a=0;a<N;a++) out.print(C[a]+" "); out.println(); out.close(); } static class MScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public MScanner() { stream = System.in; // stream = new FileInputStream(new File("dec.in")); } int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } int nextInt() { return Integer.parseInt(next()); } int[] nextInt(int N) { int[] ret = new int[N]; for (int a = 0; a < N; a++) ret[a] = nextInt(); return ret; } int[][] nextInt(int N, int M) { int[][] ret = new int[N][M]; for (int a = 0; a < N; a++) ret[a] = nextInt(M); return ret; } long nextLong() { return Long.parseLong(next()); } long[] nextLong(int N) { long[] ret = new long[N]; for (int a = 0; a < N; a++) ret[a] = nextLong(); return ret; } double nextDouble() { return Double.parseDouble(next()); } double[] nextDouble(int N) { double[] ret = new double[N]; for (int a = 0; a < N; a++) ret[a] = nextDouble(); return ret; } String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } String[] next(int N) { String[] ret = new String[N]; for (int a = 0; a < N; a++) ret[a] = next(); return ret; } String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } String[] nextLine(int N) { String[] ret = new String[N]; for (int a = 0; a < N; a++) ret[a] = nextLine(); return ret; } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
63c35bad55642904fbd7ad6b9efd9c58
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class C { static int[] a; public static void main(String[] args){ FastScanner sc = new FastScanner(); int n = sc.nextInt(); if(n % 2 == 0){ System.out.println(-1); return; } int[] c = new int[n]; StringBuffer out = new StringBuffer(); for(int i=0;i<n;i++){ if(i==n-1){ out.append(i); }else{ out.append(i + " "); } c[i] = (i*2)%n; } System.out.println(out); System.out.println(out); out = new StringBuffer(); for(int i=0;i<n;i++){ if(i==n-1){ out.append(c[i]); }else{ out.append(c[i] + " "); } } /* Set<Integer> cs = new HashSet<Integer>(); boolean good = true; for(int i=0;i<n;i++){ int s = (2*i)%n; if(cs.contains(s)){ good = false; break; } cs.add(s); } System.out.println(good);*/ System.out.println(out); } public static void p(int toSet, int[] b, boolean[] used){ if(toSet==b.length){ Set<Integer> c = new HashSet<Integer>(); boolean good = true; for(int i=0;i<b.length;i++){ int s = (a[i]+b[i])%a.length; if(c.contains(s)){ good = false; break; } c.add(s); } if(good){ System.out.println(Arrays.toString(b)); } }else{ for(int i=0;i<b.length;i++){ if(!used[i]){ b[toSet] = i; used[i] = true; p(toSet+1, b, used); used[i] = false; } } } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
1a0ecc033c998845a55ea129be6aaeb0
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.util.*; public class SinIsh { public static void main(String [] args){ Scanner in =new Scanner(System.in); int n=in.nextInt(); if(n%2==0) System.out.print("-1"); else if(n%2==1){ StringBuilder str=new StringBuilder(); for(int i=0;i<n;i++){ str.append(i+" "); } System.out.println(str); str= new StringBuilder(); for(int i=1;i<n;i++){ str.append(i+" "); } str.append(0); System.out.println(str); str= new StringBuilder(); int a=0; for(int i=1;i<=n;i++){ if(i<=(n-1)/2) str.append((2*i-1)+ " "); if(i>(n-1)/2){ str.append((a)+ " "); a+=2; } } System.out.println(str); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
a945f29ef16457bd6ee8d9411fc2a640
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.util.*; //Scanner; import java.io.PrintWriter; //PrintWriter public class R183_Div2_C { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); solve(in, out); out.close(); in.close(); } public static void solve(Scanner in, PrintWriter out) { int n = in.nextInt(); if (n % 2 == 0) out.println(-1); else { int[] a = new int[n]; int[] b = new int[n]; int[] c = new int[n]; StringBuilder sa = new StringBuilder(); StringBuilder sb = new StringBuilder(); StringBuilder sc = new StringBuilder(); for (int i = 0; i < n; i++) { a[i] = i; b[i] = (i > 0) ? (i - 1) : n - 1; c[i] = (a[i] + b[i]) % n; sa.append(a[i]); if (i < n - 1) sa.append(" "); sb.append(b[i]); if (i < n - 1) sb.append(" "); sc.append(c[i]); if (i < n - 1) sc.append(" "); } out.println(sa); out.println(sb); out.println(sc); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
d36534b0c666223daa2f108147d9e2e2
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
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_183_C { public static void main(String[] args) { FastScanner sc = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); if(n%2==0){ out.println(-1); out.close(); return; } int[] a = new int[n]; int[] b = new int[n]; int[] c = new int[n]; for(int i = 0; i<n; i++){ a[i] = i; if(i==n-1){ b[i] = 0; } else{ b[i] = i+1; } c[i] = (a[i]+b[i])%n; } for(int i = 0; i< n; i++){ if(i!=0){ out.print(' '); } out.print(a[i]); } out.println(); for(int i = 0; i< n; i++){ if(i!=0){ out.print(' '); } out.print(b[i]); } out.println(); for(int i = 0; i< n; i++){ if(i!=0){ out.print(' '); } out.print(c[i]); } out.println(); 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
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
89320d2057980be898509d8a331ba0de
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
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.util.StringTokenizer; public class C { private static StringTokenizer tokenizer; private static BufferedReader bf; private static PrintWriter out; private static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private static long nextLong() throws IOException { return Long.parseLong(nextToken()); } private static String nextToken() throws IOException { while(tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(bf.readLine()); } return tokenizer.nextToken(); } public static void main(String[] args) throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = nextInt(); if(n % 2 == 0) out.println(-1); else { int k = (n-1)/2; if(k == 0) { out.println(0); out.println(0); out.println(0); } else { int[] a = new int[n]; int[] b = new int[n]; for(int i = 1; i < 2*k; i += 2) { a[i] = (i-1)/2; b[i] = (i+1)/2; } for(int i = 0; i <= 2*k; i += 2) { a[i] = k + i/2; b[i] = (k + i/2 + 1)%n; } for(int i = 0; i < n; i++) { out.print(a[i] + " "); } out.print("\n"); for(int i = 0; i < n; i++) { out.print(b[i] + " "); } out.print("\n"); for(int i = 0; i < n; i++) { out.print(i + " "); } } } out.close(); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
a93451dc18dca8da7049342e046d1726
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class C { public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine().trim()); int[] first = new int[N]; for (int i = 0; i < N; i++) { first[i] = i; } int[] last = new int[N]; for (int i = 0; i < N; i++) { last[i] = N - i - 1; } HashSet<Integer> test = new HashSet<>(); int[] mid = new int[N]; for (int i = 0; i < N; i++) { mid[i] = (N - first[i] + last[i]) % N; test.add(mid[i]); } if (test.size() == N) { toPrint(first, out); toPrint(mid, out); toPrint(last, out); } else { out.println(-1); } br.close(); out.close(); } private static void toPrint(int[] array, PrintWriter out) { for (int i = 0; i < array.length; i++) { if (i != array.length - 1) out.print(array[i] + " "); else out.println(array[i]); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
71a606e2573546c32ed1edd6485fc9bc
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
//package n183; import java.util.Scanner; public class Permut { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); int n = s.nextInt(); if (n % 2 == 0){ System.out.println(-1); //system("PAUSE"); return; } for (int i=0; i<n; i++){ System.out.print(i + " "); } System.out.println(); for (int i=0; i<n; i++){ System.out.print(i + " "); } System.out.println(); for (int i=0; i<n; i++){ System.out.print((i + i) % n + " "); } s.close(); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
529736a9c6074552ca0456c57cadd920
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.PrintWriter; import java.util.*; public class Program { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); new Program().solve(in, out); out.flush(); } void solve(Scanner in, PrintWriter out) { int n = in.nextInt(); if (n % 2 == 0) { out.println(-1); return; } for (int j = 0; j < 2; j++) { for (int i = 0; i < n; i++) { out.print(i); out.print(' '); } out.println(); } for (int i = 0, j = 0; i < n; i++, j += 2) { out.print(j % n); out.print(' '); } out.println(); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
800bc039e0c2696aa0fca68ef7cc3ba4
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.util.*; public class tuman { static public void solve(Scanner in){ int n = in.nextInt(); if (n%2==0) { System.out.println(-1); return; } for(int i=n-1; i>=0; i--) System.out.print(i+" "); System.out.println(); for(int i=1; i<n; i+=2) System.out.print(i+" "); for(int i=0; i<n; i+=2) System.out.print(i+" "); System.out.println(); for(int i=0; i<n; i++) System.out.print(i+" "); } public static void main(String[] args) { Scanner in = new Scanner(System.in); solve(in); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
b4fd2b681d7d5524e1b3ec50d599bc34
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.util.Scanner; public class Problem304C { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); if (n%2 ==0) { System.out.println("-1"); } else { StringBuilder sbAB = new StringBuilder(); StringBuilder sbC = new StringBuilder(); for (int i = 0; i < n; i++) { sbAB.append(i).append(" "); sbC.append((i+i)%n).append(" "); } System.out.println(sbAB); System.out.println(sbAB); System.out.println(sbC); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
cf279c2e23f06ddd761cb38d64c79108
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class C { public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); sc = new StringTokenizer(br.readLine()); int n = nxtInt(); if (n % 2 == 0) out.println(-1); else { int[] b = new int[n]; for (int i = 0; i < n; i++) out.print((i == 0 ? "" : " ") + (i)); out.println(); for (int i = n / 2; i < n; i++) b[i] = i - n / 2; for (int i = 0; i < n / 2; i++) b[i] = n / 2 + 1 + i; for (int i = 0; i < b.length; i++) out.print((i == 0 ? "" : " ") + b[i]); out.println(); for (int i = 0; i < b.length; i++) out.print((i == 0 ? "" : " ") + (b[i] + i) % n); out.println(); } br.close(); out.close(); } static BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); static StringTokenizer sc; static String nxtTok() throws IOException { while (!sc.hasMoreTokens()) { String s = br.readLine(); if (s == null) return null; sc = new StringTokenizer(s.trim()); } return sc.nextToken(); } static int nxtInt() throws IOException { return Integer.parseInt(nxtTok()); } static long nxtLng() throws IOException { return Long.parseLong(nxtTok()); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
be801e610711331a8c73fcac5a410436
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; public class C285 { FastScanner in; PrintWriter out; public void solve() throws IOException { int n = in.nextInt(); if (n % 2 == 0) { out.println(-1); return; } for (int i = 0; i < n; i++) { out.print(i + " "); } out.println(); int q = n - 1; q /= 2; if (q % 3 != 1) { int[] a = new int[n]; int cur = 1; for (int i = 1; i < n; i += 2) { a[i] = cur++; } for (int i = 0; i < n; i += 2) { a[i] = cur++; } a[n - 1] = 0; for (int i = 0; i < n; i++) { out.print(a[i] + " "); } out.println(); for (int i = 0; i < n; i++) { out.print(((a[i] + i) % n) + " "); } out.println(); } else { int ans[] = new int[n]; for (int i = 0; i < n; i++) { int w = (n - 1 + i) % n; out.print(w + " "); ans[i] = (w + i) % n; } out.println(); for (int i = 0; i < n; i++) { out.print(ans[i] + " "); } out.println(); } } public void run() { try { InputStream inputStream = System.in; in = new FastScanner(inputStream); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } private class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public boolean hasNext() { while (st == null || !st.hasMoreTokens()) { try { String line = br.readLine(); if (line == null) { return false; } st = new StringTokenizer(line); } catch (IOException e) { e.printStackTrace(); } } if (st != null && st.hasMoreTokens()) { return true; } return false; } public String next() { if (hasNext()) { return st.nextToken(); } return null; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] args) { new C285().run(); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
d949dc638809d0956c934a4240da670e
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class C { public static void main(String[] args) { MyScanner sc = new MyScanner(); int n = sc.nextInt(); if (n % 2 == 0) { System.out.println(-1); } else { for (int i = 0; i < n; i++) { System.out.print(i + " "); } System.out.println(); for (int i = 0; i < n; i++) { int j = (i + 1) % n; System.out.print(j + " "); } System.out.println(); for (int i = 0; i < n; i++) { int j = (i + i + 1) % n; System.out.print(j + " "); } System.out.println(); } } 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
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
2138b17e894edb206b378ccb40bd9f08
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class A { public void solve() throws Exception { int n = nextInt(); if(n % 2 == 0) { out.println("-1"); } else { for(int i = n/2, y = 0; i < n-1; i++, y++) { out.print(i + " " + y + " "); } out.println(n-1); for(int i = (n+1)/2, y = 1; y <= n/2; i++, y++) { out.print(i + " " + y + " "); } out.println(0); for(int i = 0; i < n; i++) { out.print(i + " "); } } } public static void main(String[] args) throws Exception { A problem = new A(); problem.solve(); problem.close(); } BufferedReader in; PrintWriter out; String curLine; StringTokenizer tok; final String delimeter = " "; final String endOfFile = ""; public A(BufferedReader in, PrintWriter out) throws Exception { this.in = in; this.out = out; curLine = in.readLine(); if (curLine == null || curLine == endOfFile) { tok = null; } else { tok = new StringTokenizer(curLine, delimeter); } } public A() throws Exception { this(new BufferedReader(new InputStreamReader(System.in)), new PrintWriter(System.out)); } public A(String filename) throws Exception { this(new BufferedReader(new FileReader(filename + ".in")), new PrintWriter(filename + ".out")); } public boolean hasMore() throws Exception { if (tok == null || curLine == null) { return false; } else { while (!tok.hasMoreTokens()) { curLine = in.readLine(); if (curLine == null || curLine.equalsIgnoreCase(endOfFile)) { tok = null; return false; } else { tok = new StringTokenizer(curLine); } } return true; } } public String nextWord() throws Exception { if (!hasMore()) { return null; } else { return tok.nextToken(); } } public int nextInt() throws Exception { return Integer.parseInt(nextWord()); } public long nextLong() throws Exception { return Long.parseLong(nextWord()); } public int[] readIntArray(int n) throws Exception { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } public void close() throws Exception { in.close(); out.close(); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
2b35e02e675c275a36509071fe73c53f
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.IOException; import java.util.InputMismatchException; public class LuckyPermutationTriple { public static void main(String[] args) { FasterScanner sc = new FasterScanner(); int N = sc.nextInt(); if (N % 2 == 0) { System.out.println(-1); return; } StringBuilder sb = new StringBuilder(); for (int t = 0; t < 2; t++) { for (int i = 0; i < N; i++) { sb.append(i + " "); } sb.append("\n"); } for (int i = 0; i < N; i++) { sb.append(((2 * i) % N) + " "); } System.out.println(sb.toString()); } 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) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = 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
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
e3dcd91f3b959ada03279e167b0ccb51
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args){ FastScanner sc = new FastScanner(); int n = sc.nextInt(); if(n % 2 == 0) { System.out.println(-1); return; } for(int i = 0; i < n; i++) { System.out.print(i + " "); } System.out.println(); System.out.print(n - 1 + " "); for(int i = 0; i < n - 1; i++) { System.out.print(i + " "); } System.out.println(); int current = n - 1; for(int i = 0; i < n; i++) { System.out.print((i + (current % n)) % n + " "); current++; } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
a05d62078d833ab7846c1795dfdb9230
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import static java.lang.System.*; import static java.lang.Math.*; import java.util.*; public class C304{ public static Scanner sc = new Scanner(in); //public static Random sc=new Random(); public void run(){ int n=sc.nextInt(); if(n%2==0){ ln(-1);return; } int[] a=new int[n]; for(int i=0;i<n;i++){ a[i]=i; } int[] c=new int[n]; for(int i=0;i<n;i++){ c[i]=(a[i]+i)%n; } for(int i=0;i<n;i++){ pr((i==0?"":" ")+a[i]); } ln(); for(int i=0;i<n;i++){ pr((i==0?"":" ")+i); } ln(); for(int i=0;i<n;i++){ pr((i==0?"":" ")+c[i]); } ln(); } public static void main(String[] _) { new C304().run(); } public int[] nextIntArray(int n){ int[] res=new int[n]; for(int i=0;i<n;i++){ res[i]=sc.nextInt(); } return res; } public static void pr(Object o) { out.print(o); } public static void ln(Object o) { out.println(o); } public static void ln() { out.println(); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
19dbe3e6d34c9b9f8cf2a26ae6937363
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.*; import java.util.*; public class C183 { public C183() { Scanner in = new Scanner(System.in); int n = Integer.parseInt(in.nextLine()); if(n % 2 == 1) { if(n == 1) { System.out.println(0); System.out.println(0); System.out.println(0); } else { for(int x = 0; x < n; x++) System.out.print(x + " "); System.out.println(); for(int x = 1; x < n; x++) System.out.print(x + " "); System.out.println(0); int count = 1; for(int x = 0; x < n; x++) { System.out.print(count + " "); count = (count + 2) % n; } } } else System.out.println(-1); } public static void main(String[] args) { new C183(); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
0b9bad38202dd8763222d3827bf94610
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.util.Scanner; public class Main2 { public static void main(String args[]){ Scanner input = new Scanner(System.in); int n = input.nextInt(); pytagoras(n); } public static void pytagoras(int n){ int[] a = new int[n]; int[] b = new int[n]; if(n % 2 == 0){ System.out.println(-1); }else{ for(int i = 0 ; i < n ; i++){ if(i != n-1){ System.out.print(i+" "); }else{ System.out.println(i); } a[i] = i; } for(int i = 0 ; i < n ; i++){ if(i != n-1){ System.out.print((i+1)+" "); b[i] = (i+1); }else{ System.out.println(0); b[i] = 0; } } for(int i = 0 ; i < n ; i++){ if(i != n-1){ System.out.print((a[i]+b[i]) % n+" "); }else{ System.out.println((a[i]+b[i]) % n); } } } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
d2c035e90c1d7c03221786ca68fce692
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.util.Scanner; import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); if (n % 2 == 0) { out.print("-1"); return; } for (int i = 0; i < n; i++) { out.print(i + " "); } out.println(); for (int i = 0; i < n; i++) { out.print(i + " "); } out.println(); for (int i = 0; i < n; i++) { out.print(((i + i) % n) + " "); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
20fda0c7db6b5ee20719f711d3cf6650
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.awt.Point; import java.util.HashMap; import java.util.Scanner; public class Main { static int ct = 0; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); if(n % 2 == 0) { System.out.println(-1); } else { for(int i = 0; i < n; i++) { System.out.print(i + (i == n - 1 ? "" : " ")); } System.out.println(); for(int i = 0; i < n; i++) { System.out.print(i + (i == n - 1 ? "" : " ")); } System.out.println(); for(int i = 0; i < n; i++) { System.out.print((2*i % n) + (i == n - 1 ? "" : " ")); } System.out.println(); } } public static long gcd(long a, long b) { if(b == 0) { return a; } else { return gcd(b, a % b); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
3a67a089d3a19875aa21feb4337237b3
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
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.util.Locale; import java.util.StringTokenizer; public class C_183 { static StringTokenizer st; static BufferedReader br; static PrintWriter pw; static class sort implements Comparable<sort> { int x, y; public sort(int x, int y) { this.x = x; this.y = y; } public int compareTo(sort arg0) { if (this.x > arg0.x) return 1; if (this.x < arg0.x) return -1; if (this.y > arg0.y) return 1; if (this.y < arg0.y) return -1; return 0; } } 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(); if (n % 2 == 0) { System.out.println(-1); return; } for (int i = 0; i < n; i++) { pw.print(i + " "); } pw.println(); for (int i = 0; i < n; i++) { pw.print(i + " "); } pw.println(); for (int i = 0; i < n; i++) { pw.print(2 * i % n + " "); } pw.close(); } 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
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
86cac193b5adfe53abedee0621a1e3c3
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.util.*; import java.io.*; import static java.util.Arrays.*; import static java.util.Collections.*; import static java.lang.Math.*; public class C { int INF = 1 << 28; //long INF = 1L << 62; double EPS = 1e-10; void run() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if( n%2 == 0 ) { System.out.println(-1); return; } for(int i=0;i<n;i++) System.out.print(i + (i==n-1? "\n":" ")); for(int i=0;i<n;i++) System.out.print(i + (i==n-1? "\n":" ")); for(int i=0;i<n;i++) System.out.print((i*2 % n) + (i==n-1? "\n":" ")); } // 0 1 2 3 4 5 6 7 // 1 2 3 4 5 6 7 0 // 2 3 4 5 6 7 0 1 // 3 4 5 6 7 0 1 2 // 4 5 6 7 0 1 2 3 // 5 6 7 0 1 2 3 4 // 6 7 0 1 2 3 4 5 // 7 0 1 2 3 4 5 6 void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } public static void main(String[] args) { new C().run(); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
3d9e0f387155b36aa8f3ad45a6336b4b
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.*; import java.util.*; /** * * @author N-AssassiN */ public class Main { private static BufferedReader reader; private static BufferedWriter out; private static StringTokenizer tokenizer; //private final static String filename = "filename"; /** * call this method to initialize reader for InputStream and OututStream */ private static void init(InputStream input, OutputStream output) { reader = new BufferedReader(new InputStreamReader(input)); out = new BufferedWriter(new OutputStreamWriter(output)); //reader = new BufferedReader(new FileReader(filename + ".in")); //out = new BufferedWriter(new FileWriter(filename + ".out")); tokenizer = new StringTokenizer(""); } /** * get next word */ private static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } 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()); } /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { init(System.in, System.out); int i; int n = nextInt(); //long startTime = System.currentTimeMillis(); if (n % 2 == 0) { out.write("-1\n"); } else { for (i = 0; i < n; i++) { out.write(i + " "); } out.write("\n"); for (i = 0; i < n; i++) { out.write(i + " "); } out.write("\n"); for (i = 0; i < n; i++) { out.write((i * 2) % n + " "); } out.write("\n"); } //long runTime = System.currentTimeMillis() - startTime; //out.write(runTime + "\n"); out.flush(); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
dbee3116299ec93c06c4f9ec7a589f9c
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.util.InputMismatchException; 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(); if (n == 1) { out.println("0"); out.println("0"); out.println("0"); return; } if (n % 2 == 0) { out.println("-1"); return; } for (int i = 0; i < n; ++i) out.print(i + " "); out.println(); for (int i = 1; i < n; ++i) out.print(i + " "); out.println(0); for (int i = 1; i < n; ++i) out.print((((i - 1) + i) % n) + " "); out.println(n - 1); } } class InputReader { BufferedReader in; StringTokenizer st; public InputReader(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); eat(""); } public int nextInt() { return Integer.parseInt(next()); } public String next() { while (!st.hasMoreTokens()) eat(nextLine()); return st.nextToken(); } public String nextLine() { try { return in.readLine(); } catch (IOException e) { throw new InputMismatchException(); } } public void eat(String str) { if (str == null) throw new InputMismatchException(); st = new StringTokenizer(str, " \t\n\r\f:"); } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
394353d9378dcfcacdf8bbd95c705555
train_004.jsonl
1368363600
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String arg[]) throws IOException { // BufferedReader r=new BufferedReader(new FileReader("testcase.txt")); BufferedReader r=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(r.readLine()); if(n%2==0) System.out.println(-1); else { for(int i=0;i<n;i++) System.out.print(i+" "); System.out.println(); for(int i=0;i<n;i++) System.out.print(i+" "); System.out.println(); for(int i=0;i<n;i++) System.out.print(((2*i)%n)+" "); } } }
Java
["5", "2"]
2 seconds
["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"]
NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
Java 7
standard input
[ "constructive algorithms" ]
2f0942c531fd5758b220104c3338b702
The first line contains a single integer n (1 ≀ n ≀ 105).
1,300
If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them.
standard output
PASSED
577cfca03a4b3a3f233f000aca0bb7d8
train_004.jsonl
1589707200
The statement of this problem is the same as the statement of problem C2. The only difference is that, in problem C1, $$$n$$$ is always even, and in C2, $$$n$$$ is always odd.You are given a regular polygon with $$$2 \cdot n$$$ vertices (it's convex and has equal sides and equal angles) and all its sides have length $$$1$$$. Let's name it as $$$2n$$$-gon.Your task is to find the square of the minimum size such that you can embed $$$2n$$$-gon in the square. Embedding $$$2n$$$-gon in the square means that you need to place $$$2n$$$-gon in the square in such way that each point which lies inside or on a border of $$$2n$$$-gon should also lie inside or on a border of the square.You can rotate $$$2n$$$-gon and/or the square.
256 megabytes
import java.util.*; import java.math.*; import java.text.DecimalFormat; public class Main { public static void main(String []args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); double d=(double)180/(2*n); double a1=(double)(Math.sin(Math.toRadians(d))); double a2=(double)(Math.cos(Math.toRadians(d))); double ans=(double)(a2/a1); // DecimalFormat df = new DecimalFormat("#.######"); // df.setRoundingMode(RoundingMode.CEILING); ans=(double)(Math.round(ans*1000000000))/1000000000; System.out.println(ans); } } }
Java
["3\n2\n4\n200"]
2 seconds
["1.000000000\n2.414213562\n127.321336469"]
null
Java 8
standard input
[ "binary search", "geometry", "ternary search", "math" ]
0c7a476716445c535a61f4e81edc7f75
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 200$$$)Β β€” the number of test cases. Next $$$T$$$ lines contain descriptions of test casesΒ β€” one per line. Each line contains single even integer $$$n$$$ ($$$2 \le n \le 200$$$). Don't forget you need to embed $$$2n$$$-gon, not an $$$n$$$-gon.
1,400
Print $$$T$$$ real numbersΒ β€” one per test case. For each test case, print the minimum length of a side of the square $$$2n$$$-gon can be embedded in. Your answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.
standard output
PASSED
3fe9c9ecbc2731b041d96e8dfed1c33c
train_004.jsonl
1589707200
The statement of this problem is the same as the statement of problem C2. The only difference is that, in problem C1, $$$n$$$ is always even, and in C2, $$$n$$$ is always odd.You are given a regular polygon with $$$2 \cdot n$$$ vertices (it's convex and has equal sides and equal angles) and all its sides have length $$$1$$$. Let's name it as $$$2n$$$-gon.Your task is to find the square of the minimum size such that you can embed $$$2n$$$-gon in the square. Embedding $$$2n$$$-gon in the square means that you need to place $$$2n$$$-gon in the square in such way that each point which lies inside or on a border of $$$2n$$$-gon should also lie inside or on a border of the square.You can rotate $$$2n$$$-gon and/or the square.
256 megabytes
import java.io.*; import java.util.*; public class Main { private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; private static final boolean OFFLINE_WITHOUT_FILES = false; double Get(int n, double angle, double R, double alpha) { double maxx = -1e9; double maxy = -1e9; double minx = 1e9; double miny = 1e9; for (int i = 0; i < n; i++) { double x = Math.cos(alpha * i + angle) * R; double y = Math.sin(alpha * i + angle) * R; maxx = Math.max(x, maxx); minx = Math.min(x, minx); maxy = Math.max(y, maxy); miny = Math.min(y, miny); } return Math.max(maxx - minx, maxy - miny); } void Solve() throws IOException { int t = readInt(); while (t-- > 0) { int n = readInt() * 2; double left = 0; double alpha = 2 * Math.PI / n; double right = alpha; double R = 0.5 / Math.sin(alpha / 2); double mid1 = 0; double mid2; double ans1, ans2; for (int itr = 0; itr < 50; itr++) { mid1 = left + (right - left) / 3; mid2 = right - (right - left) / 3; ans1 = Get(n, mid1, R, alpha); ans2 = Get(n, mid2, R, alpha); if (ans1 < ans2) { right = mid2; } else { left = mid1; } } out.print(Get(n, mid1, R, alpha) + "\n"); } } public static void main(String[] args) { new Main(); } BufferedReader in; PrintWriter out; StringTokenizer tok; Main() { try { long timeStart = System.currentTimeMillis(); if (ONLINE_JUDGE || OFFLINE_WITHOUT_FILES) { 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"); } tok = new StringTokenizer(""); Solve(); out.close(); long timeEnd = System.currentTimeMillis(); System.err.println("time = " + (timeEnd - timeStart) + " compiled"); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } String readLine() throws IOException { return in.readLine(); } String delimiter = " "; String readString() throws IOException { while (!tok.hasMoreTokens()) { String nextLine = readLine(); if (nextLine == null) return null; tok = new StringTokenizer(nextLine); } return tok.nextToken(delimiter); } int readInt() throws IOException { return Integer.parseInt(readString()); } }
Java
["3\n2\n4\n200"]
2 seconds
["1.000000000\n2.414213562\n127.321336469"]
null
Java 8
standard input
[ "binary search", "geometry", "ternary search", "math" ]
0c7a476716445c535a61f4e81edc7f75
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 200$$$)Β β€” the number of test cases. Next $$$T$$$ lines contain descriptions of test casesΒ β€” one per line. Each line contains single even integer $$$n$$$ ($$$2 \le n \le 200$$$). Don't forget you need to embed $$$2n$$$-gon, not an $$$n$$$-gon.
1,400
Print $$$T$$$ real numbersΒ β€” one per test case. For each test case, print the minimum length of a side of the square $$$2n$$$-gon can be embedded in. Your answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.
standard output