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
3e63f9212aec12c6aec7d03e306b74a7
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.util.*; import java.io.*; public class Main { void solve (FastScanner in, PrintWriter out) { int n = in.nextInt(); int[] live = new int[2]; int[] dead = new int[2]; for (int i=0; i<n; i++) { int temp = in.nextInt(); if (temp == 1) { live[0] += in.nextInt(); dead[0] += in.nextInt(); } else { live[1] += in.nextInt(); dead[1] += in.nextInt(); } } out.println((live[0]+dead[0])/2<=live[0]? "LIVE" : "DEAD"); out.println((live[1]+dead[1])/2<=live[1]? "LIVE" : "DEAD"); } void print (Object... ar) { System.out.println(Arrays.deepToString(ar)); } public static void main(String[] args) { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); Main main = new Main(); main.solve(in, out); in.close(); out.close(); } static class FastScanner { private InputStream in; private byte[] buffer = new byte[1024]; private int length = 0, p = 0; public FastScanner (InputStream stream) { in = stream; } public boolean hasNextByte () { if (p < length) return true; else { p = 0; try {length = in.read(buffer);} catch (Exception e) {e.printStackTrace();} if (length <= 0) return false; } return true; } public int readByte () { if (hasNextByte() == true) return buffer[p++]; return -1; } public boolean isPrintable (int n) {return 33<=n&&n<=126;} public void skip () { while (hasNextByte() && !isPrintable(buffer[p])) p++; } public boolean hasNext () {skip(); return hasNextByte();} public String next () { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int t = readByte(); while (isPrintable(t)) { sb.appendCodePoint(t); t = readByte(); } return sb.toString(); } public String[] nextArray (int n) { String[] ar = new String[n]; for (int i=0; i<n; i++) ar[i] = next(); return ar; } public int nextInt () {return Math.toIntExact(nextLong());} public int[] nextIntArray (int n) { int[] ar = new int[n]; for (int i=0; i<n; i++) ar[i] = nextInt(); return ar; } public long nextLong () { if (!hasNext()) throw new NoSuchElementException(); boolean minus = false; int temp = readByte(); if (temp == '-') { minus = true; temp = readByte(); } if (temp<'0' || '9'<temp) throw new NumberFormatException(); long n = 0; while (isPrintable(temp)) { if ('0'<=temp && temp<='9') { n *= 10; n += temp - '0'; } else throw new NumberFormatException(); temp = readByte(); } return minus? -n : n; } public double nextDouble () { return Double.parseDouble(next()); } public void close () { try {in.close();} catch(Exception e){} } } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 8
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
ef719fb7783931a2e4e0ecd5016ee12d
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in Actual solution is at the top * * @author viatsko */ 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); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { private final int N_SERVERS = 2; public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); TaskA.Stats[] stats = new TaskA.Stats[N_SERVERS + 1]; for (int i = 1; i <= N_SERVERS; i++) { stats[i] = new TaskA.Stats(); } while (n-- > 0) { int t = in.nextInt(); int x = in.nextInt(); int y = in.nextInt(); stats[t].received += x; stats[t].lost += y; } for (int i = 1; i <= N_SERVERS; i++) { if (stats[i].received >= stats[i].lost) { out.println("LIVE"); } else { out.println("DEAD"); } } } static class Stats { int received = 0; int lost = 0; } } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 8
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
e30a2b665f0c0fc9a9baa0028102d93e
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class SystemAdministrator { public static void main(String[] args) { // TODO Auto-generated method stub // Problem https://codeforces.com/problemset/problem/245/A Reader scan = new Reader(); int n = scan.nextInt(); int reachedA= 0,lostA= 0,reachedB = 0,lostB = 0,ping; for(int i=0;i<n;i++) { //Select if we want to ping A or B, then count the total package that got transmitted to A and how many package that //got lost during transition. ping = scan.nextInt(); if(ping==1) { reachedA += scan.nextInt(); lostA += scan.nextInt(); } if(ping==2) { reachedB += scan.nextInt(); lostB += scan.nextInt(); } } //If the sum of reached package is more or equal to the lost package, then the server is considered live, else dead. if(reachedA>=lostA) { System.out.println("LIVE"); } if(reachedA<lostA) { System.out.println("DEAD"); } if(reachedB>=lostB) { System.out.println("LIVE"); } if(reachedB<lostB) { System.out.println("DEAD"); } } static class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 8
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
80df013e8d73fcb473faa11412070350
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.util.Scanner; public class SystemAdministrator { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int reachedA= 0,lostA= 0,reachedB = 0,lostB = 0,ping; for(int i=0;i<n;i++) { ping = scan.nextInt(); if(ping==1) { reachedA += scan.nextInt(); lostA += scan.nextInt(); } if(ping==2) { reachedB += scan.nextInt(); lostB += scan.nextInt(); } } scan.close(); if(reachedA>=lostA) { System.out.println("LIVE"); } if(reachedA<lostA) { System.out.println("DEAD"); } if(reachedB>=lostB) { System.out.println("LIVE"); } if(reachedB<lostB) { System.out.println("DEAD"); } } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 8
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
1f3d59ca7a61a63054fa01a55fa6ad08
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int q=sc.nextInt(); int a=0,b=0; for(int i=1;i<=q;++i){ int t=sc.nextInt(),x=sc.nextInt(),y=sc.nextInt(); if(t==1){ a+=x-y; }else{ b+=x-y; } } System.out.println(((a>=0)?"LIVE":"DEAD")); System.out.println(((b>=0)?"LIVE":"DEAD")); } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 8
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
330ffffc79e2b9b9e24cfb7e6b754b15
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int q=sc.nextInt(); int a=0,b=0; for(int i=1;i<=q;++i){ if(sc.nextInt()==1){ a+=sc.nextInt()-sc.nextInt(); }else{ b+=sc.nextInt()-sc.nextInt(); } } System.out.println(((a>=0)?"LIVE":"DEAD")); System.out.println(((b>=0)?"LIVE":"DEAD")); } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 8
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
3b0da523bf702e71537fd8da5998c395
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.util.Scanner; public class SisAdmin { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int resplusOne = 0; int resminusOne = 0; int resplusTwo = 0; int resminusTwo = 0; for (int i = 0; i < n; i++) { int server = sc.nextInt(); int plus = sc.nextInt(); int minus = sc.nextInt(); if(server == 1) { resplusOne+=plus; resminusOne+=minus; }else { resplusTwo+=plus; resminusTwo+=minus; } } if(resplusOne >= resminusOne) { System.out.println("LIVE"); }else { System.out.println("DEAD"); } if(resplusTwo >= resminusTwo) { System.out.println("LIVE"); }else { System.out.println("DEAD"); } } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 8
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
71cb0497d9e495d3eac9a1a0f62f337d
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.util.Scanner; public class Administrator { public static void main(String[] args) { Scanner scanner = new Scanner (System.in); int n = scanner.nextInt(),r1=0,l1=0,r2=0,l2=0; for (int i=0;i<n;i++) { int z = scanner.nextInt();int x = scanner.nextInt();int y = scanner.nextInt(); if (z==1) {r1+=x;l1+=y;} if (z==2) {r2+=x;l2+=y;} } if (l1>r1) System.out.println("DEAD"); if (l1<=r1) System.out.println("LIVE"); if (l2>r2) System.out.println("DEAD"); if (l2<=r2) System.out.println("LIVE"); scanner.close(); }}
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 8
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
058f9b45c432a3790d4850b870b66cd8
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.util.Scanner; public class SystemAdministrator { public static void main(String[] args) { Scanner in = new Scanner(System.in); int count = in.nextInt(); int as = 0, af = 0, bs = 0, bf = 0; for (int i = 0; i < count; i++) { int p = in.nextInt(); if (p == 1) { as += in.nextInt(); af += in.nextInt(); } else { bs += in.nextInt(); bf += in.nextInt(); } } System.out.println( as >= af ? "LIVE" : "DEAD "); System.out.println( bs >= bf ? "LIVE" : "DEAD "); } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 6
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
04c94d649220a647fc55c4116595242a
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { /** * @param args * @throws IOException * @throws NumberFormatException */ public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int number = Integer.parseInt(reader.readLine()); int a1 = 0 ; int b1 = 0 ; int a2 = 0 ; int b2 = 0 ; for (int i = 0; i < number; i++) { String ar[] = reader.readLine().split(" "); if (Integer.parseInt(ar[0]) == 1) { a1 += Integer.parseInt(ar[1]); a2 += Integer.parseInt(ar[2]); } else { b1 += Integer.parseInt(ar[1]); b2 += Integer.parseInt(ar[2]); }} if (a1 >= a2) System.out.println("LIVE"); else System.out.println("DEAD"); if (b1 >= b2) System.out.println("LIVE"); else System.out.println("DEAD"); } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 6
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
a1e4704dfbf1f166c5a7516086942895
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.util.Scanner; public class A { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int a = 0, b = 0; int ta = 0, tb = 0; for (int i = 0; i < n; i++) { int t = in.nextInt(); if (t == 1) { ta += 10; int x = in.nextInt(); in.nextInt(); a += x; } else { tb += 10; int x = in.nextInt(); in.next(); b += x; } } if (a * 1.0 / ta >= 0.5) System.out.println("LIVE"); else System.out.println("DEAD"); if (b * 1.0 / tb >= 0.5) System.out.println("LIVE"); else System.out.println("DEAD"); } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 6
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
e1eefc80bdef9cfa2a1e7247ce84f437
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.io.*; import java.util.*; public class SystemAdmin { public static void main(String[] args) throws IOException { //Scanner in = new Scanner(new File("Sample Input.txt")); Scanner in = new Scanner(System.in); StringBuilder out = new StringBuilder(); //while(in.hasNextInt()) { int n = in.nextInt(); int a=0, b=0, aall=0, ball=0; for (int i = 0; i < n; i++) { if ( in.nextInt()==1) { aall+=10; a += in.nextInt(); } else { ball+=10; b += in.nextInt(); } in.nextInt(); } if ( a >= aall/2 ) out.append("LIVE\n"); else out.append("DEAD\n"); if ( b >= ball/2 ) out.append("LIVE\n"); else out.append("DEAD\n"); } System.out.print(out); } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 6
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
2644332ade86fb9b0b8c25da3aa60bb0
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class ProblemA { public static void main(String[] args) throws IOException { ProblemA solver = new ProblemA(); solver.init(); solver.solve(); } void init() { } private void solve() throws IOException { Reader in = new Reader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int aCorrect = 0; int aBad = 0; int bCorrect = 0; int bBad = 0; for (int n = in.nextInt(); n > 0; n--) { if (in.nextInt() == 1) { aCorrect += in.nextInt(); aBad += in.nextInt(); } else { bCorrect += in.nextInt(); bBad += in.nextInt(); } } out.println(aCorrect >= aBad ? "LIVE" : "DEAD"); out.println(bCorrect >= bBad ? "LIVE" : "DEAD"); out.flush(); out.close(); } private static class Reader { BufferedReader reader; StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ Reader(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ public String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } 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()); } } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 6
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
e4f090b3b7c3c297bc67038114296dc4
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int[] success = new int[2]; int[] fail = new int[2]; int n = in.nextInt(); int server; int s,f; for (int i = 0; i < n; i++) { server = in.nextInt(); s = in.nextInt(); f = in.nextInt(); success[server-1] += s; fail[server-1] += f; } if(success[0] < fail[0]) out.println("DEAD"); else out.println("LIVE"); if(success[1] < fail[1]) out.println("DEAD"); else out.println("LIVE"); } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 6
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
52cd6fe7a616c2d6a2ac5df526ea4356
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
//package KROK_MBTU_Qualification; import java.util.*; import java.io.*; public class a { String input = ""; String output = ""; FastScanner in; PrintWriter out; void solve() throws Exception { int x1 = 0; int y1 = 0; int x2 = 0; int y2 = 0; int n = in.nextInt(); for (int i = 0; i<n; i++){ int t = in.nextInt(); if (t == 1){ x1 += in.nextInt(); y1 += in.nextInt(); }else { x2 += in.nextInt(); y2 += in.nextInt(); } } if (x1 >= y1){ out.println("LIVE"); }else { out.println("DEAD"); } if (x2 >= y2){ out.println("LIVE"); }else { out.println("DEAD"); } } void run() { try { if (input.length() == 0) { InputStreamReader ins = new InputStreamReader(System.in); in = new FastScanner(new BufferedReader(ins)); } else { FileReader f = new FileReader(input); in = new FastScanner(new BufferedReader(f)); } if (output.length() == 0) { out = new PrintWriter(System.out); } else out = new PrintWriter(new File(output)); solve(); out.flush(); out.close(); } catch (Exception ex) { ex.printStackTrace(); out.close(); } } public static void main(String args[]) { new a().run(); } class FastScanner { BufferedReader bf; StringTokenizer st; FastScanner(BufferedReader bf) { this.bf = bf; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(bf.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 6
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
163ef3b94f1cd7dba42687ea17f07e2d
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.util.Scanner; public class SysAdmin { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a = 0, b = 0; int ai=0, bi=0; for(int i = 0; i < n; i++) { if(sc.nextInt() == 1) { a += sc.nextInt(); sc.nextInt(); ai+=10; } else { b += sc.nextInt(); sc.nextInt(); bi+=10; } } if(a >= ai/2 && ai != 0) { System.out.println("LIVE"); } else { System.out.println("DEAD"); } if(b >= bi/2 && bi != 0) { System.out.println("LIVE"); } else { System.out.println("DEAD"); } } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 6
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
b0febcced93678efd3d39ab68c7f6d46
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.util.Scanner; public class program { public static Scanner in = new Scanner(System.in); public static void main(String[] args) { int n = in.nextInt(); int numASuccess = 0; int numBSuccess = 0; int numAUnSuccess = 0; int numBUnSuccess = 0; for (int i = 0; i < n; ++i) { if (in.nextInt() == 1) { numASuccess += in.nextInt(); numAUnSuccess += in.nextInt(); } else { numBSuccess += in.nextInt(); numBUnSuccess += in.nextInt(); } } if (numASuccess >= numAUnSuccess) { System.out.println("LIVE"); } else { System.out.println("DEAD"); } if (numBSuccess >= numBUnSuccess) { System.out.println("LIVE"); } else { System.out.println("DEAD"); } } /*public static void main(String[] args) { int n = in.nextInt(); int t[] = new int[n]; int sum = 0; for (int i = 0; i < n; ++i) { t[i] = in.nextInt(); } if (n < 6) { for (int i = 0; i < n; ++i) { sum += t[i]; } System.out.println(sum); return; } int tmpSum = 0; for (int i = 1; i < n / 3; ++i) { tmpSum = 0; //Проверка на кратность if (n % (i + 1) != 0) { continue; } //Подсчёт сумм со смещениями for (int j = 0; j < i; ++j) {//смещение for (int k = 0; k < n / (i + 1); ++k) {//сумма с ним } } } }*/ /*public static String ROOT = "\\"; public static int gcd(int a, int b) { while (b != 0) { int tmp = a % b; a = b; b = tmp; } return a; } public static void main(String[] args) { int numCommands = in.nextInt(); Vector<String> commands = new Vector<String>(); for (int i = 0; i < numCommands; ++i) { commands.add(in.next()); } for (int i = 0; i < numCommands; ++i) { String[] parseCommand = commands.get(i).split("/"); for (int j = 0; j < parseCommand.length; ++j) { System.out.print(parseCommand[j] + "___"); } System.out.println(""); } String currentCD = ROOT; } public static String cd(String command, String currentPath) { String result = ""; Vector<String> parseCurrent; if (!command.contains("..")) { return currentPath; } else { String[] parseCurPath = currentPath.split("/"); String[] parseCommand = command.split("/"); System.out.println(parseCurPath); System.out.println(parseCommand); } return result; }*/ } /*public static int gcd(int a, int b) { while (b != 0) { int tmp = a % b; a = b; b = tmp; } return a; } public static void main(String[] args) { Vector<Integer> mass = new Vector<Integer>(); for (int i = 0; i < 4; ++i) { mass.add(in.nextInt()); } int d = in.nextInt(); //boolean flags[] = new boolean[d]; for (int i = 0; i < 4; ++i) { if (mass.get(i) == 1) { System.out.println(d); return; } } mass = normaliseMulties(mass); System.out.println(mass); int result; if (mass.size() == 4) { result = d / mass.get(0) + d / mass.get(1) + d / mass.get(2) + d / mass.get(3) - d / (mass.get(0) * mass.get(1)) - d / (mass.get(0) * mass.get(2)) - d / (mass.get(0) * mass.get(3)) - d / (mass.get(1) * mass.get(2)) - d / (mass.get(1) * mass.get(3)) - d / (mass.get(2) * mass.get(3)) + d / (mass.get(0) * mass.get(1) * mass.get(2)) + d / (mass.get(0) * mass.get(1) * mass.get(3)) + d / (mass.get(1) * mass.get(2) * mass.get(3)) - d / (mass.get(0) * mass.get(1) * mass.get(2) * mass.get(3)); } else if (mass.size() == 3) { result = d / mass.get(0) + d / mass.get(1) + d / mass.get(2) - d / (mass.get(0) * mass.get(1)) - d / (mass.get(0) * mass.get(2)) - d / (mass.get(1) * mass.get(2)) + d / (mass.get(0) * mass.get(1) * mass.get(2)); } else if (mass.size() == 2) { result = d / mass.get(0) + d / mass.get(1) - d / (mass.get(0) * mass.get(1)); } else { result = d / mass.get(0); } System.out.println(result); } public static Vector<Integer> normaliseMulties(Vector<Integer> vector) { //remove duplicates for (int i = 0; i < vector.size(); ++i) { for (int j = i + 1; j < vector.size() - 1; ++j) { if (vector.get(i).intValue() == vector.get(j).intValue()) { vector.removeElement(vector.get(j)); } } } for (int i = 0; i < vector.size(); ++i) { for (int j = i + 1; j < vector.size() - 1; ++j) { if (gcd(vector.get(i).intValue(), vector.get(j).intValue()) == Math.min(vector.get(i).intValue(), vector.get(j).intValue())) { vector.removeElement(Math.max(vector.get(i).intValue(), vector.get(j).intValue())); } } } return vector; }*/
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 6
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
7b0a682f5af8a7e86713bcca42db255d
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.io.*; import java.util.*; public class Example { BufferedReader in; PrintWriter out; StringTokenizer st; String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (Exception e) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } public void run() throws Exception { //in = new BufferedReader(new FileReader("D.IN")); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n = nextInt(); int[] dx = new int[3]; for (int i = 0; i < n; i++) { int t = nextInt(); int x = nextInt(); int y = nextInt(); dx[t] += x - y;; } for (int i = 1; i <= 2; i++) { if (dx[i] >= 0) { out.println("LIVE"); } else { out.println("DEAD"); } } out.flush(); out.close(); in.close(); } public static void main(String[] args) throws Exception { new Example().run(); } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 6
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
923cbbcefa65a4b7abddacea6b26a893
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.io.*; import java.util.*; public class Example { BufferedReader in; PrintWriter out; StringTokenizer st; String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (Exception e) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } public void run() throws Exception { //in = new BufferedReader(new FileReader("D.IN")); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n = nextInt(); int[] dx = new int[3]; for (int i = 0; i < n; i++) { int t = nextInt(); int x = nextInt(); int y = nextInt(); dx[t] += x - y;; } for (int i = 1; i <= 2; i++) { if (dx[i] >= 0) { out.println("LIVE"); } else { out.println("DEAD"); } } out.flush(); out.close(); in.close(); } public static void main(String[] args) throws Exception { new Example().run(); } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 6
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
798dd181cce85621951348154b9d6c18
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
import java.util.*; import java.io.*; public class Main { private int T, A1, B1, A2, B2; public Integer nextInt(String s) { return Integer.valueOf(s); } public void output(int A1, int B1) { if (A1 < B1) { System.out.println("DEAD"); } else { System.out.println("LIVE"); } } public void run() { try { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(bf.readLine()); T = nextInt(st.nextToken()); for (int i = 1; i <= T; i++) { st = new StringTokenizer(bf.readLine()); if (nextInt(st.nextToken()) == 1) { A1 += nextInt(st.nextToken()); B1 += nextInt(st.nextToken()); } else { A2 += nextInt(st.nextToken()); B2 += nextInt(st.nextToken()); } } output(A1, B1); output(A2, B2); } catch (IOException e) { } } public static void main(String[] args) { new Main().run(); } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 6
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
33ceb61fa3e75eb6bbd3a235a0660f69
train_002.jsonl
1353339000
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
256 megabytes
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.IOException; /** * * @author wijebandara */ public class Main { public static void main(String[] args)throws IOException { java.io.BufferedReader in =new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); int n =Integer.parseInt(in.readLine()); int ax =0; int bx =0; int a=0; int b=0; java.util.StringTokenizer st; for(int i=0;i<n;i++) { st =new java.util.StringTokenizer(in.readLine()); if(Integer.parseInt(st.nextToken())==1) { int h =Integer.parseInt(st.nextToken()); ax+=h; a+=h; a+=Integer.parseInt(st.nextToken()); } else { int h =Integer.parseInt(st.nextToken()); bx+=h; b+=h; b+=Integer.parseInt(st.nextToken()); } } if((float)ax/a<0.5) { System.out.println("DEAD"); } else { System.out.println("LIVE"); } if((float)bx/b<0.5) { System.out.println("DEAD"); } else { System.out.println("LIVE"); } } }
Java
["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"]
2 seconds
["LIVE\nLIVE", "LIVE\nDEAD"]
NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
Java 6
standard input
[ "implementation" ]
1d8870a705036b9820227309d74dd1e8
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
800
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server b in the similar format.
standard output
PASSED
69bbd65db90efb01b1f04c6800a995c2
train_002.jsonl
1589034900
For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers: $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$; $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$; $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$"1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author masterbios */ 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); FBinaryStringReconstruction solver = new FBinaryStringReconstruction(); solver.solve(1, in, out); out.close(); } static class FBinaryStringReconstruction { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- > 0) { int zero = in.nextInt(), one = in.nextInt(), conone = in.nextInt(); StringBuilder sb = new StringBuilder(); int par1 = zero % 2, par2 = one % 2, par3 = conone % 2; if (zero > 0 && one > 0 && conone > 0) { //&& par1 == par2 && par2 == par3 && par3 == 0) { // con -> zero -> one for (int i = 1; i <= conone + 1; i++) sb.append("1"); for (int i = 1; i <= zero + 1; i++) sb.append("0"); int bit = 0; for (int i = 2; i <= one; i++) { bit = 1 - bit; sb.append("" + bit); } } else { // con -> one -> zero if (conone > 0) { sb.append("11"); for (int i = 2; i <= conone; i++) sb.append("1"); } if (one > 0) { int bit = -1; if (sb.length() > 0) { if (sb.charAt(sb.length() - 1) == '1') { sb.append("0"); bit = 0; } } else { if (one % 2 == 0) { sb.append("01"); bit = 1; } else { sb.append("10"); bit = 0; } } for (int i = 2; i <= one; i++) { bit = 1 - bit; sb.append("" + bit); } } if (zero > 0) { if (sb.length() > 0) { if (sb.charAt(sb.length() - 1) == '1') { sb.append("00"); } else { sb.append("0"); } } else { sb.append("00"); } for (int i = 2; i <= zero; i++) sb.append("0"); } } out.println(sb.toString()); // int o = 0, z = 0, cc = 0; // for (int i = 0; i < sb.length() - 1; i++) { // int c1 = sb.charAt(i), c2 = sb.charAt(i + 1); // if (c1 != c2) o++; // else if (c1 == c2){ // if (c1 == '1') cc++; // else z++; // } // } // if (o == one && z == zero && cc == conone) { // out.println("right answer"); // } else { // out.println("failed"); // } } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreElements()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["7\n1 3 5\n1 1 1\n3 9 3\n0 1 0\n3 1 2\n0 0 3\n2 0 0"]
1 second
["1110011110\n0011\n0110001100101011\n10\n0000111\n1111\n000"]
null
Java 8
standard input
[ "constructive algorithms", "dfs and similar", "math" ]
4bbb078b66b26d6414e30b0aae845b98
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 \le n_0, n_1, n_2 \le 100$$$; $$$n_0 + n_1 + n_2 &gt; 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.
1,500
Print $$$t$$$ lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.
standard output
PASSED
f5e3e8906e4e3d85cb033f5cbe4a50ae
train_002.jsonl
1589034900
For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers: $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$; $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$; $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$"1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.
256 megabytes
import java.util.Scanner; public class F_1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int tc = scanner.nextInt(); for (int t = 0; t < tc; t++) { StringBuilder sb = new StringBuilder(); int n0 = scanner.nextInt(); int n1 = scanner.nextInt(); int n2 = scanner.nextInt(); if (n0 != 0) { for (int i = 0; i < n0 + 1; i++) { sb.append('0'); } } if (n2 != 0) { for (int i = 0; i < n2 + 1; i++) { sb.append('1'); } } if (n1 != 0) { if (n0 != 0 && n2 != 0) { for (int i = 0; i < n1 - 1; i++) { sb.append(i % 2); } } else if (n0 != 0) { for (int i = 0; i < n1; i++) { sb.append((i+1) % 2); } } else if (n2 != 0) { for (int i = 0; i < n1; i++) { sb.append(i % 2); } } else { for (int i = 0; i < n1 + 1; i++) { sb.append(i % 2); } } } System.out.println(sb.toString()); } } }
Java
["7\n1 3 5\n1 1 1\n3 9 3\n0 1 0\n3 1 2\n0 0 3\n2 0 0"]
1 second
["1110011110\n0011\n0110001100101011\n10\n0000111\n1111\n000"]
null
Java 8
standard input
[ "constructive algorithms", "dfs and similar", "math" ]
4bbb078b66b26d6414e30b0aae845b98
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 \le n_0, n_1, n_2 \le 100$$$; $$$n_0 + n_1 + n_2 &gt; 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.
1,500
Print $$$t$$$ lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.
standard output
PASSED
4ed9acd988bfcd72ca37170b9152179e
train_002.jsonl
1589034900
For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers: $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$; $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$; $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$"1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.
256 megabytes
import java.io.*; import java.util.*; public class Main{ static int mod = (int)(Math.pow(10, 9) + 7); 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 n1 = sc.nextInt(); int n2 = sc.nextInt(); int n3 = sc.nextInt(); if (n1 == 0 && n3 == 0){ String r = "0"; int turns = 0; while (n2-->0){ if (turns % 2 == 0) r += "1"; else r +="0"; turns++; } out.println(r); continue; } String res = ""; if (n3 > 0 && n1 > 0 )n2--; if (n1 > 0){ while (n1 -- >= 0){ res += '0'; } } if (n3 > 0){ while (n3-- >= 0){ res += '1'; } } if (res.charAt(0) == '0'){ int turns = 0; while (n2-- > 0){ if (turns % 2 == 0) res = '1' + res; else res = '0' + res; turns++; } } else{ int turns = 1; while (n2-- > 0){ if (turns % 2 == 0) res = '1' + res; else res = '0' + res; turns++; } } out.println(res); } out.close(); } static void mergeSort(int[] A){ // low to hi sort, single array only int n = A.length; if (n < 2) return; int[] l = new int[n/2]; int[] r = new int[n - n/2]; for (int i = 0; i < n/2; i++){ l[i] = A[i]; } for (int j = n/2; j < n; j++){ r[j-n/2] = A[j]; } mergeSort(l); mergeSort(r); merge(l, r, A); } static void merge(int[] l, int[] r, int[] a){ int i = 0, j = 0, k = 0; while (i < l.length && j < r.length && k < a.length){ if (l[i] < r[j]){ a[k] = l[i]; i++; } else{ a[k] = r[j]; j++; } k++; } while (i < l.length){ a[k] = l[i]; i++; k++; } while (j < r.length){ a[k] = r[j]; j++; k++; } } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //-------------------------------------------------------- }
Java
["7\n1 3 5\n1 1 1\n3 9 3\n0 1 0\n3 1 2\n0 0 3\n2 0 0"]
1 second
["1110011110\n0011\n0110001100101011\n10\n0000111\n1111\n000"]
null
Java 8
standard input
[ "constructive algorithms", "dfs and similar", "math" ]
4bbb078b66b26d6414e30b0aae845b98
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 \le n_0, n_1, n_2 \le 100$$$; $$$n_0 + n_1 + n_2 &gt; 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.
1,500
Print $$$t$$$ lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.
standard output
PASSED
c87f8c5e82a767d3980f96697a2fb93e
train_002.jsonl
1589034900
For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers: $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$; $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$; $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$"1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.
256 megabytes
//package com.company; import java.util.Scanner; public class CF_1252_F { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n0=sc.nextInt(); int n1=sc.nextInt(); int n2=sc.nextInt(); if(n0==0&&n1==0&&n2==0) System.out.println(); StringBuffer sb=new StringBuffer(); // for(int i=0;i<=n0;++i) // sb.append('0'); for(int i=0,c=1;i<=n1;++i,c=1-c) { sb.append(c); } StringBuffer sb2=new StringBuffer(); for(int i=1;i<=n2;++i) sb2.append('1'); StringBuffer sb3=new StringBuffer(); for(int i=1;i<=n0;++i) sb3.append('0'); // System.out.println(sb.toString()) String ans=n2>n0?sb2.toString()+"1":sb3.toString()+"0"; if(n1>0) ans=sb2.toString()+(sb.charAt(sb.length()-1)=='0'?sb.toString()+sb3.toString():sb.toString().substring(0,sb.length()-1)+sb3.toString()+"1"); System.out.println(ans); } } }
Java
["7\n1 3 5\n1 1 1\n3 9 3\n0 1 0\n3 1 2\n0 0 3\n2 0 0"]
1 second
["1110011110\n0011\n0110001100101011\n10\n0000111\n1111\n000"]
null
Java 8
standard input
[ "constructive algorithms", "dfs and similar", "math" ]
4bbb078b66b26d6414e30b0aae845b98
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 \le n_0, n_1, n_2 \le 100$$$; $$$n_0 + n_1 + n_2 &gt; 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.
1,500
Print $$$t$$$ lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.
standard output
PASSED
03ccf470ca61e2282e774680fec37ef2
train_002.jsonl
1589034900
For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers: $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$; $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$; $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$"1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.
256 megabytes
// Problem : F. Binary String Reconstruction // Contest : Codeforces - Codeforces Round #640 (Div. 4) // URL : https://codeforces.com/contest/1352/problem/F // Memory Limit : 256 MB // Time Limit : 1000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) import java.io.*; import java.util.*; public class a implements Runnable{ public static void main(String[] args) { new Thread(null, new a(), "process", 1<<26).start(); } public void run() { FastReader scan = new FastReader(); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); //PrintWriter out = new PrintWriter("file.out"); Task solver = new Task(); int t = scan.nextInt(); //int t = 1; for(int i = 1; i <= t; i++) solver.solve(i, scan, out); out.close(); } static class Task { static final int inf = Integer.MAX_VALUE; public void solve(int testNumber, FastReader sc, PrintWriter pw) { //CHECK FOR QUICKSORT TLE //***********************// //CHECK FOR INT OVERFLOW //***********************// int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); StringBuilder sb = new StringBuilder(); for(int i = 0; i < k; i++){ if(i == 0) { sb.append("11"); } else { sb.append("1"); } } for(int i = 0; i < n; i++){ if(i == 0){ if(k > 0) m--; sb.append("0"); } sb.append("0"); } if(n == 0 && m > 0) { sb.append(0); if(k != 0) m--; } for(int i = 0; i < m; i++){ sb.append((i + 1) % 2); } pw.println(sb); } } static long binpow(long a, long b, long m) { a %= m; long res = 1; while (b > 0) { if ((b & 1) == 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } static void sort(int[] x){ shuffle(x); Arrays.sort(x); } static void sort(long[] x){ shuffle(x); Arrays.sort(x); } static class tup implements Comparable<tup>{ int a, b; tup(int a,int b){ this.a=a; this.b=b; } @Override public int compareTo(tup o){ return Integer.compare(o.b,b); } } static void shuffle(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } } static void shuffle(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 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
["7\n1 3 5\n1 1 1\n3 9 3\n0 1 0\n3 1 2\n0 0 3\n2 0 0"]
1 second
["1110011110\n0011\n0110001100101011\n10\n0000111\n1111\n000"]
null
Java 8
standard input
[ "constructive algorithms", "dfs and similar", "math" ]
4bbb078b66b26d6414e30b0aae845b98
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 \le n_0, n_1, n_2 \le 100$$$; $$$n_0 + n_1 + n_2 &gt; 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.
1,500
Print $$$t$$$ lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.
standard output
PASSED
c33b9975e7e3b1e7d098260c0c5ec14d
train_002.jsonl
1589034900
For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers: $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$; $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$; $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$"1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.
256 megabytes
import java.util.*; import java.io.*; public class Main{ public static void main(String[] args)throws IOException{ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String A = br.readLine(); int a,b,c,d,e,f; a = Integer.parseInt(A); for(b = 0;b <a ;b++){ A = br.readLine(); String B[] = A.split(" "); d = Integer.parseInt(B[0]); e = Integer.parseInt(B[1]); f = Integer.parseInt(B[2]); String C = ""; if(d >= 1){ C = C + "00"; for(c = 1;c < d;c++){ C = C + "0"; } } if(e >= 1){ C = "1"+ C; } if(d == 0){ C = C +"0"; } for(c = 1;c < e;c= c+2){ C = C + "10"; } if(e-c == 2){ C = C + "1"; } if(e == 0){ C = "1" + C; } for(c= 0;c < f;c++){ C = "1" + C; } if(e ==0 && f == 0){ C= C.substring(1,C.length()); C = C + "0"; } //if(d == 2){ //C= C.substring(1,C.length()); //} if(e % 2 == 0){ C= C.substring(0,C.length()-1); } System.out.println(C); } } }
Java
["7\n1 3 5\n1 1 1\n3 9 3\n0 1 0\n3 1 2\n0 0 3\n2 0 0"]
1 second
["1110011110\n0011\n0110001100101011\n10\n0000111\n1111\n000"]
null
Java 8
standard input
[ "constructive algorithms", "dfs and similar", "math" ]
4bbb078b66b26d6414e30b0aae845b98
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 \le n_0, n_1, n_2 \le 100$$$; $$$n_0 + n_1 + n_2 &gt; 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.
1,500
Print $$$t$$$ lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.
standard output
PASSED
2f9a40aea36af876e686793a463460c0
train_002.jsonl
1589034900
For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers: $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$; $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$; $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$"1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Sri Mouli (sri.vcmk@gmail.com) */ 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); FBinaryStringReconstruction solver = new FBinaryStringReconstruction(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class FBinaryStringReconstruction { public void solve(int testNumber, InputReader in, OutputWriter out) { int n0 = in.readInt(); int n1 = in.readInt(); int n2 = in.readInt(); StringBuilder sb = new StringBuilder(); if (n0 > 0) { for (int i = 0; i <= n0; i++) { sb.append('0'); } } if (n2 > 0) { for (int i = 0; i <= n2; i++) { sb.append('1'); } } StringBuilder sb2 = new StringBuilder(); if (n0 != 0 && n2 != 0) { n1 -= 1; if (n1 >= 0) { for (int i = 0; i < (n1 / 2); i++) { sb2.append("01"); } if (n1 % 2 == 1) { sb2.append("0"); } sb.append(sb2); } } else if (n0 == 0 && n2 == 0) { for (int i = 0; i < (n1 + 1) / 2; i++) { sb.append("01"); } if (n1 % 2 == 0) { sb.append('0'); } } else { if (n1 >= 0) { for (int i = 0; i < n1 / 2; i++) { if (n2 != 0) sb2.append("01"); else sb2.append("10"); } if (n1 % 2 != 0) { if (n2 != 0) sb2.append("0"); else sb2.append("1"); } sb.append(sb2); } } out.printLine(sb.toString()); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int numChars; private int curChar; 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 String next() { return readString(); } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } 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 interface SpaceCharFilter { boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["7\n1 3 5\n1 1 1\n3 9 3\n0 1 0\n3 1 2\n0 0 3\n2 0 0"]
1 second
["1110011110\n0011\n0110001100101011\n10\n0000111\n1111\n000"]
null
Java 8
standard input
[ "constructive algorithms", "dfs and similar", "math" ]
4bbb078b66b26d6414e30b0aae845b98
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 \le n_0, n_1, n_2 \le 100$$$; $$$n_0 + n_1 + n_2 &gt; 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.
1,500
Print $$$t$$$ lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.
standard output
PASSED
f01ffce31f24274cfe031e0fa7dd7c50
train_002.jsonl
1589034900
For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers: $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$; $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$; $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$"1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.
256 megabytes
/*input 10 1 3 5 1 1 1 3 9 3 0 1 0 3 1 2 0 0 3 2 0 0 0 4 0 0 2 3 3 2 0 */ import java.util.*; public class Easy3{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(),n0,n1,n2,i; StringBuilder ans; while(t>0) { t--; ans = new StringBuilder(""); n0 = sc.nextInt(); n1 = sc.nextInt(); n2 = sc.nextInt(); //even n1 if(n1!=0 && (n1&1)==0){ if(n0!=0 && n2==0){ ans.append("010"); for(i=3;i<=n1;i+=2) { ans.append("10"); } for(i=1;i<=n0;i++) ans.append("0"); } else if(n2!=0 && n0==0){ ans.append("101"); for(i=3;i<=n1;i+=2) { ans.append("01"); } for(i=1;i<=n2;i++) ans.append("1"); } else if(n0==0 && n2==0){ ans.append("010"); for(i=3;i<=n1;i+=2) { ans.append("10"); } } else{ n1--; for(i=0;i<=n0 && n0!=0 ;i++) { ans.append("0"); } for(i=3;i<=n1;i+=2) { ans.append("10"); } for(i=0;i<=n2 && n2!=0 ;i++) { ans.append("1"); } ans.append("0"); } } else if(n1!=0 && n0==0 && n2 == 0) { ans.append("10"); for(i=2;i<=n1;i+=2) { ans.append("1"); if(i+1<=n1) ans.append("0"); } } else if(n1!=0 && n0==0) { int ele_beg = ((n1&1)==0?1:0); int next = (ele_beg^1); for(i=1;i<=n1;i+=2) { ans.append(ele_beg); if(i+1<=n1) ans.append(next); } for(i=0;i<=n2;i++) { ans.append("1"); } } else if(n1!=0 && n2==0) { for(i=0;i<=n0;i++) { ans.append("0"); } int ele_beg = 1; int next = (ele_beg^1); for(i=1;i<=n1;i+=2) { ans.append("1"); if(i+1<=n1) ans.append("0"); } } else{ for(i=0;i<=n0 && n0!=0 ;i++) { ans.append("0"); } for(i=3;i<=n1;i+=2) { ans.append("10"); } for(i=0;i<=n2 && n2!=0 ;i++) { ans.append("1"); } } System.out.println(ans); } sc.close(); } }
Java
["7\n1 3 5\n1 1 1\n3 9 3\n0 1 0\n3 1 2\n0 0 3\n2 0 0"]
1 second
["1110011110\n0011\n0110001100101011\n10\n0000111\n1111\n000"]
null
Java 8
standard input
[ "constructive algorithms", "dfs and similar", "math" ]
4bbb078b66b26d6414e30b0aae845b98
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 \le n_0, n_1, n_2 \le 100$$$; $$$n_0 + n_1 + n_2 &gt; 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.
1,500
Print $$$t$$$ lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.
standard output
PASSED
6e17bdd9d7b69d2c2cb8dc5d8326a149
train_002.jsonl
1589034900
For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers: $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$; $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$; $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$"1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while(t-- > 0) { int n0 = in.nextInt(), n1 = in.nextInt(), n2 = in.nextInt(); StringBuilder sb = new StringBuilder(); if(n2 > 0) n2++; while(n2-- > 0) sb.append(1); if(sb.length() != 0 && n0 > 0) n1--; if((sb.length() == 0 || sb.charAt(sb.length() - 1) != '0') && n0 > 0) n0++; while(n0-- > 0) sb.append(0); if(sb.length() == 0 && n1 > 0) n1++; while(n1-- > 0) { if(sb.length() == 0 || sb.charAt(sb.length() - 1) == '0') sb.append(1); else sb.append(0); } System.out.println(sb); } } }
Java
["7\n1 3 5\n1 1 1\n3 9 3\n0 1 0\n3 1 2\n0 0 3\n2 0 0"]
1 second
["1110011110\n0011\n0110001100101011\n10\n0000111\n1111\n000"]
null
Java 8
standard input
[ "constructive algorithms", "dfs and similar", "math" ]
4bbb078b66b26d6414e30b0aae845b98
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 \le n_0, n_1, n_2 \le 100$$$; $$$n_0 + n_1 + n_2 &gt; 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.
1,500
Print $$$t$$$ lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.
standard output
PASSED
6177b45a047bccb630ba8f5277075626
train_002.jsonl
1589034900
For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers: $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$; $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$; $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$"1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.
256 megabytes
import java.util.Scanner; public class AZ { public static void main(String[] args) { Scanner cin= new Scanner(System.in); int t=cin.nextInt(); for (int i = 0; i <t; i++) { int zero=cin.nextInt(); int one=cin.nextInt(); int second=cin.nextInt(); StringBuilder str= new StringBuilder(); StringBuilder str1= new StringBuilder(); StringBuilder str2= new StringBuilder(); for (int j = 0; j <=zero; j++) { str.append(0); } if(zero==0) { str.deleteCharAt(0); } for (int j = 0; j <=second; j++) { str2.append(1); } if(second==0) { str2.deleteCharAt(0); } if(zero==0&&second>0) { for (int j = 0; j <one; j++) { if(j%2==0) { str1.append(0); } else { str1.append(1); } } } else { if(zero>0&&second>0) { one--; } if(zero==0&&second==0) { one++; } if(second>0) { for (int j = 0; j <one; j++) { if(j%2==0) { str1.append(0); } else { str1.append(1); } } } else { if(zero>0&&second==0) { for (int j = 0; j <one; j++) { if(j%2==0) { str1.append(1); } else { str1.append(0); } } } else { for (int j = 0; j <one; j++) { if(j%2==0) { str1.append(1); } else { str1.append(0); } } } } } System.out.println(str.append(str2).append(str1)); } } }
Java
["7\n1 3 5\n1 1 1\n3 9 3\n0 1 0\n3 1 2\n0 0 3\n2 0 0"]
1 second
["1110011110\n0011\n0110001100101011\n10\n0000111\n1111\n000"]
null
Java 8
standard input
[ "constructive algorithms", "dfs and similar", "math" ]
4bbb078b66b26d6414e30b0aae845b98
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 \le n_0, n_1, n_2 \le 100$$$; $$$n_0 + n_1 + n_2 &gt; 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.
1,500
Print $$$t$$$ lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.
standard output
PASSED
968ed1de0238d05bde7e006b2cf89e3f
train_002.jsonl
1589034900
For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers: $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$; $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$; $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$"1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.
256 megabytes
// package com.company; import java.util.*; import java.lang.*; import java.io.*; //****Use Integer Wrapper Class for Arrays.sort()**** public class CX6 { static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); public static void main(String[] Args){ FastReader scan=new FastReader(); int t=1; t=scan.nextInt(); while(t-->0){ int n0=scan.nextInt(); int n1=scan.nextInt(); int n2=scan.nextInt(); StringBuilder ans=new StringBuilder(); for (int i = 0; i <n0; i++) { ans.append("0"); } int on1=n1; if(n1>0){ ans.append("01"); n1--; while(n1>1){ n1-=2; ans.append("01"); } }else if(n0>0){ ans.append("0"); } for(int i=0;i<n2;i++){ ans.append("1"); } if(on1==0&&n2>0){ ans.append("1"); } if(n1!=0){ ans.append("0"); } out.println(ans); } out.flush(); out.close(); } 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
["7\n1 3 5\n1 1 1\n3 9 3\n0 1 0\n3 1 2\n0 0 3\n2 0 0"]
1 second
["1110011110\n0011\n0110001100101011\n10\n0000111\n1111\n000"]
null
Java 8
standard input
[ "constructive algorithms", "dfs and similar", "math" ]
4bbb078b66b26d6414e30b0aae845b98
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 \le n_0, n_1, n_2 \le 100$$$; $$$n_0 + n_1 + n_2 &gt; 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.
1,500
Print $$$t$$$ lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.
standard output
PASSED
6b77f1b29627dc025d8b217798f31c69
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.io.*; public class special { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t; t=Integer.parseInt(br.readLine()); while(t>0) { int l; l=Integer.parseInt(br.readLine()); int arr[]=new int[l]; int k=0; int temp=1; boolean test = false; int flag=0; // for(int i=1;i<=l;i++) int i=l; while(i>0) { // System.out.println("value of i "+i); if(temp!=i) { for(int element:arr) { if(element==i) { test=true; break; } } if(test!=true) { arr[k]=i; ++k; System.out.print(i+" "); ++temp; flag=1; } test=false; } if(flag==1) { i=l; flag=0; }else{ i--; } } t--; System.out.println(); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
cb44ddfc6bf1094b8fb5190d6b19d602
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Solution { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int t = Integer.parseInt(br.readLine()); while(t-->0){ int n = Integer.parseInt(br.readLine()); for(int i =2 ;i<=n; i++ ) { System.out.print(i+" "); } System.out.println(1); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
771159d095115c0d85307c2e09b2ae2e
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.*; public class SpecialPermutations { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- >0) { int n = sc.nextInt(); int arr[] = new int[n]; for(int i=2;i<=n;i++) { System.out.print(i + " "); } System.out.println(1); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
7c081761668eda5fd589f73bb9050ab0
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
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 p=n; int []arr=new int[n]; for(int i=0;i<p;i++){ arr[i]=n; n--; } for(int i=0;i<p;i++){ if(arr[i]==i+1){ int temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } for(int a:arr){ System.out.print(a+" "); } System.out.println(); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
6bff8a3a08a5badd7d9a45f7f3ac24c6
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.*; public class problems { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t>0) { int n=sc.nextInt(); solve(n); System.out.println(); t--; } } static void solve(int n) { for(int i=2;i<=n;i++) { System.out.print(i+" "); } System.out.println(1); } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
8a1138532960751d7da3d75f0fb7372a
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.*; import java.math.*; public class A { static boolean isprime(int n) { for(int i=2;i<=Math.sqrt(n);i++) if(n%i==0) return false; return true; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int t,n,i,j,sum=0,c=0,k,fg=0,temp,l,x,m,c0,c1,h,y,ans=2; t=sc.nextInt(); String s; while(t-->0) { n=sc.nextInt(); System.out.print(n+" "); for(i=1;i<n;i++) System.out.print(i+" "); System.out.println(); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
8c9aacc8c53a93b9c21a26a6bd6e2861
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.*; public class Main { private static void solve() {} public static void main(String[] args) { int t = scanner.nextInt(); for (int i = 0; i < t; i++) { int n = scanner.nextInt(); List<String> list = new ArrayList<>(); for (int j = n, k = 0; j >= 1; j--, k++) { if (j != k + 1) { list.add(j + ""); } else { list.add(list.get(k - 1)); list.set(k - 1, j + ""); } } System.out.println(String.join(" ", list)); } } private static int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); } return arr; } private static final Scanner scanner = new Scanner(System.in); }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
751aaf962257695f8442b25b528d93df
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class FirstPage { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t > 0) { int n=sc.nextInt(); ArrayList<Integer> al = new ArrayList<>(); for(int i=1;i<=n;i++){ al.add(i); } Collections.sort(al); for(int i=0;i<n-1;i++){ int temp=al.get(i); al.set(i,al.get(i+1)); al.set(i+1,temp); } for(int i=0;i<n;i++){ System.out.print(al.get(i)+" "); } System.out.println(); t--; } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
ab80cc36316fa031d91cbd11df40149c
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.*; import java.io.*; public class Solution{ public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(System.in); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = sc.nextInt(); while(t-- > 0){ int n = sc.nextInt(); for(int i = 1;i<n;i++){ bw.write(i+1 + " "); } bw.write(1 + "\n"); } bw.flush(); } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
648becdc59fb8cdd37e6a124a6ecf809
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int i=0;i<t;i++){ int n = s.nextInt(); int[] arr = new int[n]; for(int j=n-1;j>=0;j--){ arr[j]=j+1; } if(n%2!=0){ int temp = arr[(n/2)]; arr[(n/2)] = arr[(n/2)+1]; arr[(n/2)+1] = temp; } for(int l=n-1;l>=0;l--){ System.out.print(arr[l]+" "); } System.out.println(); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
c248e9f67c19ec841acb6de0960d7557
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.*; public class SpecialPermutation { static Scanner sc = new Scanner(System.in); public static void main(String arg[]) { int t = sc.nextInt(); for(int i=0; i<t; i++) { solve(); } } static void solve() { int n = sc.nextInt(); if(n%2 == 0) { for(int i=n; i>=1; i--) { System.out.print(i+" "); } System.out.println(); } else { System.out.print((n/2 +1)+" "); for(int i=n; i>=1; i--) { if(i != (n/2+1)) { System.out.print(i+" "); } else { continue; } } System.out.println(); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
667e370477e2f2b9eb896ea897110408
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t=scan.nextInt(); while(t-->0){ int n=scan.nextInt(); int arr[]= new int[n+1]; if(n>1){ for(int i=1;i<n;i++){ if(i%2==0) arr[i+1]=i; else{ arr[i+1]=i; } } arr[1]=n; for(int i=1;i<=n;i++){ System.out.print(arr[i]+" "); } } System.out.println(); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
407543073d592bcb75540432e0758acf
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.*; public class Sol { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n; n = sc.nextInt(); for (int i=0;i<n-1;i++){ System.out.print((i+2)+" "); } System.out.println(1+" "); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
05449d32cdda970b05b13bca161ac89e
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { public static void main(String [] args){ Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int n [] = new int[a]; for (int i = 0; i < a; i ++){ n[i] = sc.nextInt(); } for (int i = 0; i < a; i ++){ int x[] = new int[n[i]]; if (n[i]%2 == 0){ for (int j = 0; j < n[i]; j ++){ System.out.print((n[i] - j) + " "); } }else{ for (int j = 1; j < n[i] - 1; j ++){ System.out.print((n[i] - j) + " "); } System.out.print(n[i] + " "); System.out.print(1); } System.out.println(); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
21a8ec578e5824bf8526318be87f7805
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.*; public class Solution { public static void main(String [] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-->0) { int n = scan.nextInt(); int a[] = new int[n]; for(int i=1;i<=n;i++) a[i-1]=i; for(int i=0;i<n-1;i++) { int x=a[i]; a[i]=a[i+1]; a[i+1]=x; } for(int i=0;i<n;i++) { System.out.println(a[i]+" "); } } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
8b6627371906a6b3037fb3a4d84e7005
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.io.*; import java.util.*; public class Prb2 { static PrintWriter pw; static Scanner sc; static void print(final String arg) { pw.write(arg); pw.flush(); } static void runFile() throws Exception { sc = new Scanner(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); } static void runIo() throws Exception { pw =new PrintWriter(System.out); sc = new Scanner(System.in); } static long mod = 1000000000+7; public static void main(String[] args) throws Exception { // runFile(); runIo(); int t = sc.nextInt(); while( t-- > 0 ) { int n = sc.nextInt(); if( n %2 == 0){ for(int i = n; i>=1; i--) print(i+" "); } else{ int ar[] = new int[n]; int N = n; for(int i = 0; i<N; i++) ar[i] = n--; int tt = ar[ar.length/2]; ar[ar.length/2] = ar[ar.length/2+1]; ar[ar.length/2+1] = tt; for(int k : ar) print(k+" "); } print("\n"); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
6d3a9b855d55e9ea0656ee51afb6eb9d
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); while(--T >= 0) { int N = in.nextInt(); for(int i = 0; i < N / 2; i++) { System.out.print((N - i) + " "); } for(int i = N - 1; i >= N / 2; i--) { System.out.print((N - i) + " "); } System.out.println(); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
7ba601181580adbf8c8a3b746829746e
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.Scanner; public class Main { public static void main (String [] args) throws Exception{ Scanner input = new Scanner(System.in); int n = input.nextInt(); while (n-- != 0) { int m = input.nextInt(); for (int i = 2; i <= m ; i++) { System.out.print(i + " "); } System.out.println("1"); } input.close(); } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
5625adf5708cce98e7330d31f8cb8fcb
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.io.*; import java.util.*; public class SpecialPermutation { static final Scanner in = new Scanner(System.in); public static void main(String[] args) { int t=in.nextInt(); for (int i=1; i<=t; ++i) { new Solver(); } in.close(); } static class Solver { Solver() { int n = in.nextInt(); boolean odd = (n % 2 != 0); if (n == 3) { System.out.println("3 1 2"); return; } for (int i = n; i > 1; i--) { if (odd) { if (i == n/2+1) continue; } System.out.print(i + " "); } String str = ""; if (odd) { str = (n/2+1) + " 1"; } else str = "1"; System.out.println(str); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
b1a055362794ca146f3c70418b5ef6a2
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.Scanner; public class A686 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); if(n%2==0) { for(int i=n;i>0;i--) System.out.print(i+" "); } else { for(int i=n;i>0;i--) { if(i==(n+1)/2) continue; else System.out.print(i+" "); } System.out.print((n+1)/2); } System.out.println(); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
5a5fe7d8e43a6f29e5c2d882789a5179
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.math.*; import java.io.*; import java.util.*; public class test { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); for(int i = 1 ; i<n;i++) { System.out.print(i+1 +" "); } System.out.print(1); System.out.println(); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
8baa0ef4ae439475bc317db62767c263
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.*; import java.io.*; public class Solution { private static FastReader in = new FastReader(); public static void main(String[] args) throws Exception { int t = in.nextInt(); while (t-- > 0) { solve(); } } static void solve() { int n = in.nextInt(); if(n % 2 == 0){ for(int i=n; i>0; i--){ System.out.print(i+ " "); } } else{ for(int i=n; i>0; i--){ if((n+1)/2 == i) continue; System.out.print(i+" "); } System.out.print((n+1)/2 + " "); } System.out.println(); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
3737ca8c6f9d9794610d0cfa0791e25a
train_002.jsonl
1606228500
You are given one integer $$$n$$$ ($$$n &gt; 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
256 megabytes
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; public class CodeForces { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for(int i =0;i<n;i++){ int a = scanner.nextInt(); System.out.print(a+" "); for(int j =1;j<a;j++)System.out.print(j+" "); System.out.println(""); } } }
Java
["2\n2\n5"]
1 second
["2 1\n2 1 5 3 4"]
null
Java 11
standard input
[ "constructive algorithms", "probabilities" ]
f4779e16e0857e6507fdde55e6d4f982
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.
null
For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n &gt; 1$$$.
standard output
PASSED
46adcc2e3dd2f508c91f3a835ec050f1
train_002.jsonl
1559399700
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C.There are $$$n$$$ flights from A to B, they depart at time moments $$$a_1$$$, $$$a_2$$$, $$$a_3$$$, ..., $$$a_n$$$ and arrive at B $$$t_a$$$ moments later.There are $$$m$$$ flights from B to C, they depart at time moments $$$b_1$$$, $$$b_2$$$, $$$b_3$$$, ..., $$$b_m$$$ and arrive at C $$$t_b$$$ moments later.The connection time is negligible, so one can use the $$$i$$$-th flight from A to B and the $$$j$$$-th flight from B to C if and only if $$$b_j \ge a_i + t_a$$$.You can cancel at most $$$k$$$ flights. If you cancel a flight, Arkady can not use it.Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $$$k$$$ flights. If you can cancel $$$k$$$ or less flights in such a way that it is not possible to reach C at all, print $$$-1$$$.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner( System.in ); int ans = 0; int number1 = in.nextInt(); int number2 = in.nextInt(); int time1 = in.nextInt(); int time2 = in.nextInt(); int k = in.nextInt(); int array1[] = new int[number1 + 1]; int array2[] = new int[number2 + 1]; for( int sa = 1; sa <= number1; sa ++ ) { array1[sa] = in.nextInt(); } for( int sa = 1; sa <= number2; sa ++ ) { array2[sa] = in.nextInt(); } if( k >= Math.min( number1, number2 ) ) { System.out.println( -1 ); } else { for( int sa = 0, wq = 1; sa <= k; sa ++ ) { while( wq <= number2 && array1[sa + 1] + time1 > array2[wq] ) { wq += 1; } if( wq + k - sa > number2 ) { ans = -1; break; } ans = Math.max( ans, array2[wq + k - sa] + time2 ); } System.out.println( ans ); } } }
Java
["4 5 1 1 2\n1 3 5 7\n1 2 3 9 10", "2 2 4 4 2\n1 10\n10 20", "4 3 2 3 1\n1 999999998 999999999 1000000000\n3 4 1000000000"]
1 second
["11", "-1", "1000000003"]
NoteConsider the first example. The flights from A to B depart at time moments $$$1$$$, $$$3$$$, $$$5$$$, and $$$7$$$ and arrive at B at time moments $$$2$$$, $$$4$$$, $$$6$$$, $$$8$$$, respectively. The flights from B to C depart at time moments $$$1$$$, $$$2$$$, $$$3$$$, $$$9$$$, and $$$10$$$ and arrive at C at time moments $$$2$$$, $$$3$$$, $$$4$$$, $$$10$$$, $$$11$$$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $$$4$$$, and take the last flight from B to C arriving at C at time moment $$$11$$$.In the second example you can simply cancel all flights from A to B and you're done.In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
Java 11
standard input
[ "two pointers", "binary search", "brute force" ]
bf60899aa2bd7350c805437d0fee1583
The first line contains five integers $$$n$$$, $$$m$$$, $$$t_a$$$, $$$t_b$$$ and $$$k$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$, $$$1 \le k \le n + m$$$, $$$1 \le t_a, t_b \le 10^9$$$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $$$n$$$ distinct integers in increasing order $$$a_1$$$, $$$a_2$$$, $$$a_3$$$, ..., $$$a_n$$$ ($$$1 \le a_1 &lt; a_2 &lt; \ldots &lt; a_n \le 10^9$$$) — the times the flights from A to B depart. The third line contains $$$m$$$ distinct integers in increasing order $$$b_1$$$, $$$b_2$$$, $$$b_3$$$, ..., $$$b_m$$$ ($$$1 \le b_1 &lt; b_2 &lt; \ldots &lt; b_m \le 10^9$$$) — the times the flights from B to C depart.
1,600
If you can cancel $$$k$$$ or less flights in such a way that it is not possible to reach C at all, print $$$-1$$$. Otherwise print the earliest time Arkady can arrive at C if you cancel $$$k$$$ flights in such a way that maximizes this time.
standard output
PASSED
696146f63c0b175627c1844a68f1740c
train_002.jsonl
1605450900
You have a knapsack with the capacity of $$$W$$$. There are also $$$n$$$ items, the $$$i$$$-th one has weight $$$w_i$$$. You want to put some of these items into the knapsack in such a way that their total weight $$$C$$$ is at least half of its size, but (obviously) does not exceed it. Formally, $$$C$$$ should satisfy: $$$\lceil \frac{W}{2}\rceil \le C \le W$$$. Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.
256 megabytes
import java.util.*; import java.io.*; public class Main { 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(); } } public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(int arr[],int l1,int r1,int l2,int r2) { int tmp[]=new int[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(long arr[],int l1,int r1,int l2,int r2) { long tmp[]=new long[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void main(String args[]) throws IOException { Reader input=new Reader(); StringBuilder ans=new StringBuilder(""); int test=input.nextInt(); for(int tt=1;tt<=test;tt++) { int n=input.nextInt(); long wei=input.nextLong(); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=input.nextInt(); } ans.append(solve(n,wei,arr)); } System.out.println(ans); } public static StringBuilder solve(int n,long w,int arr[]) { long lim=(w/2)+(w%2); for(int i=0;i<n;i++) { if(arr[i]>=lim && arr[i]<=w) { return new StringBuilder("1\n"+(i+1)+"\n"); } } int indx[]=new int[n]; for(int i=0;i<n;i++) { indx[i]=i; } sort(arr,indx,n); int cnt=0; boolean taken[]=new boolean[n]; long total=0; for(int i=0;i<n;i++) { if(total+arr[i]>w) { break; } cnt++; total+=arr[i]; taken[indx[i]]=true; } if(total<lim) { return new StringBuilder("-1\n"); } StringBuilder ans=new StringBuilder(""); ans.append(cnt+"\n"); for(int i=0;i<n;i++) { if(taken[i]) { ans.append((i+1)+" "); } } ans.append("\n"); return ans; } static void buildMaxHeap(int arr[],int brr[], int n) { for (int i = 1; i < n; i++) { // if child is bigger than parent if (arr[i] > arr[(i - 1) / 2]) { int j = i; // swap child and parent until // parent is smaller while (arr[j] > arr[(j - 1) / 2]) { swap(arr, j, (j - 1) / 2); swap(brr, j, (j - 1) / 2); j = (j - 1) / 2; } } } } static void sort(int arr[],int brr[], int n) { buildMaxHeap(arr,brr, n); for (int i = n - 1; i > 0; i--) { // swap value of first indexed // with last indexed swap(arr, 0, i); swap(brr, 0, i); // maintaining heap property // after each swapping int j = 0, index; do { index = (2 * j + 1); // if left child is smaller than // right child point index variable // to right child if (index < (i - 1) && arr[index] < arr[index + 1]) index++; // if parent is smaller than child // then swapping parent with child // having higher value if (index < i && arr[j] < arr[index]) { swap(arr, j, index); swap(brr, j, index); } j = index; } while (index < i); } } public static void swap(int[] a, int i, int j) { int temp = a[i]; a[i]=a[j]; a[j] = temp; } }
Java
["3\n1 3\n3\n6 2\n19 8 19 69 9 4\n7 12\n1 1 1 17 1 1 1"]
2 seconds
["1\n1\n-1\n6\n1 2 3 5 6 7"]
NoteIn the first test case, you can take the item of weight $$$3$$$ and fill the knapsack just right.In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is $$$-1$$$.In the third test case, you fill the knapsack exactly in half.
Java 11
standard input
[ "constructive algorithms", "sortings", "greedy", "math" ]
afe8710473db82f8d53d28dd32646774
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains integers $$$n$$$ and $$$W$$$ ($$$1 \le n \le 200\,000$$$, $$$1\le W \le 10^{18}$$$). The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 10^9$$$) — weights of the items. The sum of $$$n$$$ over all test cases does not exceed $$$200\,000$$$.
1,300
For each test case, if there is no solution, print a single integer $$$-1$$$. If there exists a solution consisting of $$$m$$$ items, print $$$m$$$ in the first line of the output and $$$m$$$ integers $$$j_1$$$, $$$j_2$$$, ..., $$$j_m$$$ ($$$1 \le j_i \le n$$$, all $$$j_i$$$ are distinct) in the second line of the output  — indices of the items you would like to pack into the knapsack. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.
standard output
PASSED
e1aff9aca5f36a3927c4463c7bc25e4a
train_002.jsonl
1605450900
You have a knapsack with the capacity of $$$W$$$. There are also $$$n$$$ items, the $$$i$$$-th one has weight $$$w_i$$$. You want to put some of these items into the knapsack in such a way that their total weight $$$C$$$ is at least half of its size, but (obviously) does not exceed it. Formally, $$$C$$$ should satisfy: $$$\lceil \frac{W}{2}\rceil \le C \le W$$$. Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); int t=Integer.parseInt(bu.readLine()); while(t-->0) { String s[]=bu.readLine().split(" "); int n=Integer.parseInt(s[0]); long w=Long.parseLong(s[1]),max=w,min=(w+1)/2; s=bu.readLine().split(" "); int i,mn=Integer.MAX_VALUE,a[][]=new int[n][2]; long sum=0; ArrayList<Integer> al=new ArrayList<>(); for(i=0;i<n;i++) { a[i][0]=Integer.parseInt(s[i]); if(a[i][0]>=min && a[i][0]<=max) {al.add(i+1); break;} sum+=a[i][0]; mn=Math.min(a[i][0],mn); a[i][1]=i+1; } if(al.size()>0) { sb.append(al.size()+"\n"); for(int x:al) sb.append(x+" "); sb.append("\n"); continue; } if(sum<min || mn>max) {sb.append("-1\n"); continue;} sum=0; Arrays.sort(a, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { if(o1[0]<o2[0]) return 1; else if(o1[0]==o2[0]) return o1[1]>o2[1]?1:-1; else return -1; }}); for(i=0;i<n;i++) if(a[i][0]>max) continue; else { sum+=a[i][0]; al.add(a[i][1]); if(sum>=min && sum<=max) break; } if(sum<min || sum>max) {sb.append("-1\n"); continue;} sb.append(al.size()+"\n"); for(int x:al) sb.append(x+" "); sb.append("\n"); } System.out.print(sb); } }
Java
["3\n1 3\n3\n6 2\n19 8 19 69 9 4\n7 12\n1 1 1 17 1 1 1"]
2 seconds
["1\n1\n-1\n6\n1 2 3 5 6 7"]
NoteIn the first test case, you can take the item of weight $$$3$$$ and fill the knapsack just right.In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is $$$-1$$$.In the third test case, you fill the knapsack exactly in half.
Java 11
standard input
[ "constructive algorithms", "sortings", "greedy", "math" ]
afe8710473db82f8d53d28dd32646774
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains integers $$$n$$$ and $$$W$$$ ($$$1 \le n \le 200\,000$$$, $$$1\le W \le 10^{18}$$$). The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 10^9$$$) — weights of the items. The sum of $$$n$$$ over all test cases does not exceed $$$200\,000$$$.
1,300
For each test case, if there is no solution, print a single integer $$$-1$$$. If there exists a solution consisting of $$$m$$$ items, print $$$m$$$ in the first line of the output and $$$m$$$ integers $$$j_1$$$, $$$j_2$$$, ..., $$$j_m$$$ ($$$1 \le j_i \le n$$$, all $$$j_i$$$ are distinct) in the second line of the output  — indices of the items you would like to pack into the knapsack. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.
standard output
PASSED
171111868e4200c1f0db35b52003fac6
train_002.jsonl
1605450900
You have a knapsack with the capacity of $$$W$$$. There are also $$$n$$$ items, the $$$i$$$-th one has weight $$$w_i$$$. You want to put some of these items into the knapsack in such a way that their total weight $$$C$$$ is at least half of its size, but (obviously) does not exceed it. Formally, $$$C$$$ should satisfy: $$$\lceil \frac{W}{2}\rceil \le C \le W$$$. Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static Reader sc = new Reader(); //public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static int max(int x, int y) { return x > y ? x:y; } static class Pair implements Comparable<Pair>{ int x; long y; public Pair(int a, long b) { x=a; y=b; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub if (y < o.y) { return 1; } else if (y > o.y) { return -1; } return 0; } public String toString() { return "" + x; } } static boolean works(long w, long p) { return p >= (w+1)/2 && p <= w; } public static void main(String[] args) throws IOException{ int t = sc.nextInt(); while (t-->0){ int n = sc.nextInt(); long w = sc.nextLong(); long[] a = new long[n]; int autoworks = -1; List<Pair> v = new ArrayList<Pair>(); for (int i = 0; i < n; i++) { a[i] = sc.nextLong(); if (works(w, a[i])) autoworks = i; else if (a[i] > w) { } else { v.add(new Pair(i, a[i])); } } if (autoworks != -1) { out.println(1); out.println((autoworks+1)); } else if (v.size() == 0) { out.println(-1); } else { Collections.sort(v); // prefix long sum = v.get(0).y; boolean nworks = false; for (int i = 1; i < v.size(); i++) { sum += v.get(i).y; if (works(w,sum)) { out.println((i+1)); for (int j = 0; j <= i; j++) { out.print((v.get(j).x+ 1)+ " "); } nworks = true; break; } } if (!nworks) out.println(-1); } } out.close(); } static long ceil(long a, long b) { return (a+b-1)/b; } static long powMod(long base, long exp, long mod) { if (base == 0 || base == 1) return base; if (exp == 0) return 1; if (exp == 1) return base % mod; long R = powMod(base, exp/2, mod) % mod; R *= R; R %= mod; if ((exp & 1) == 1) { return base * R % mod; } else return R % mod; } static long pow(long base, long exp) { if (base == 0 || base == 1) return base; if (exp == 0) return 1; if (exp == 1) return base; long R = pow(base, exp/2); if ((exp & 1) == 1) { return R * R * base; } else return R * R; } 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, Integer.max(cnt-1, 0)); } 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(); } } public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); }
Java
["3\n1 3\n3\n6 2\n19 8 19 69 9 4\n7 12\n1 1 1 17 1 1 1"]
2 seconds
["1\n1\n-1\n6\n1 2 3 5 6 7"]
NoteIn the first test case, you can take the item of weight $$$3$$$ and fill the knapsack just right.In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is $$$-1$$$.In the third test case, you fill the knapsack exactly in half.
Java 11
standard input
[ "constructive algorithms", "sortings", "greedy", "math" ]
afe8710473db82f8d53d28dd32646774
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains integers $$$n$$$ and $$$W$$$ ($$$1 \le n \le 200\,000$$$, $$$1\le W \le 10^{18}$$$). The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 10^9$$$) — weights of the items. The sum of $$$n$$$ over all test cases does not exceed $$$200\,000$$$.
1,300
For each test case, if there is no solution, print a single integer $$$-1$$$. If there exists a solution consisting of $$$m$$$ items, print $$$m$$$ in the first line of the output and $$$m$$$ integers $$$j_1$$$, $$$j_2$$$, ..., $$$j_m$$$ ($$$1 \le j_i \le n$$$, all $$$j_i$$$ are distinct) in the second line of the output  — indices of the items you would like to pack into the knapsack. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.
standard output
PASSED
1422350a2776af7f1424fe02162c1499
train_002.jsonl
1605450900
You have a knapsack with the capacity of $$$W$$$. There are also $$$n$$$ items, the $$$i$$$-th one has weight $$$w_i$$$. You want to put some of these items into the knapsack in such a way that their total weight $$$C$$$ is at least half of its size, but (obviously) does not exceed it. Formally, $$$C$$$ should satisfy: $$$\lceil \frac{W}{2}\rceil \le C \le W$$$. Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.
256 megabytes
import java.util.*; public class c { static Scanner in = new Scanner(System.in); public static void main(String[] args) { int t = in.nextInt(); while(t-- > 0) { int n = in.nextInt(); long w = in.nextLong(); long h = w/2; if(w%2==1) h++; Vertex[] a = new Vertex[n]; List<Integer> ans = new ArrayList<>(); for(int i = 0; i < n; i++) a[i] = new Vertex(in.nextInt(), i+1); Arrays.sort(a); long k = 0; long sum = 0; for(int i = n-1; i >= 0; i--) { if(a[i].getX() + sum <= w) { sum += a[i].getX(); ans.add(a[i].getY()); } } if(sum < h) System.out.println(-1); else { Collections.reverse(ans); System.out.println(ans.size()); for(int x : ans) { System.out.print(x + " "); } System.out.println(); } } } } class Vertex implements Comparable<Vertex>{ private int x; private int y; Vertex(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } @Override public boolean equals(Object o) { if (o == null) { return false; } if (!(o instanceof Vertex)) { return false; } return (x == ((Vertex) o).x && y == ((Vertex) o).y); } @Override public int compareTo(Vertex o) { return this.x - o.x; } }
Java
["3\n1 3\n3\n6 2\n19 8 19 69 9 4\n7 12\n1 1 1 17 1 1 1"]
2 seconds
["1\n1\n-1\n6\n1 2 3 5 6 7"]
NoteIn the first test case, you can take the item of weight $$$3$$$ and fill the knapsack just right.In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is $$$-1$$$.In the third test case, you fill the knapsack exactly in half.
Java 11
standard input
[ "constructive algorithms", "sortings", "greedy", "math" ]
afe8710473db82f8d53d28dd32646774
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains integers $$$n$$$ and $$$W$$$ ($$$1 \le n \le 200\,000$$$, $$$1\le W \le 10^{18}$$$). The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 10^9$$$) — weights of the items. The sum of $$$n$$$ over all test cases does not exceed $$$200\,000$$$.
1,300
For each test case, if there is no solution, print a single integer $$$-1$$$. If there exists a solution consisting of $$$m$$$ items, print $$$m$$$ in the first line of the output and $$$m$$$ integers $$$j_1$$$, $$$j_2$$$, ..., $$$j_m$$$ ($$$1 \le j_i \le n$$$, all $$$j_i$$$ are distinct) in the second line of the output  — indices of the items you would like to pack into the knapsack. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.
standard output
PASSED
b067280d9aa5c0bfef8ccf2b4189c271
train_002.jsonl
1605450900
You have a knapsack with the capacity of $$$W$$$. There are also $$$n$$$ items, the $$$i$$$-th one has weight $$$w_i$$$. You want to put some of these items into the knapsack in such a way that their total weight $$$C$$$ is at least half of its size, but (obviously) does not exceed it. Formally, $$$C$$$ should satisfy: $$$\lceil \frac{W}{2}\rceil \le C \le W$$$. Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.
256 megabytes
import java.util.*; import java.io.*; public class Solution { static class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Ele implements Comparable<Ele> { public int x,y; Ele(int x,int y) { this.x=x;this.y=y; } public int compareTo(Ele ob) { if(ob.x!=x)return x-ob.x; return this.y-ob.y; } } int a[][];int b[][]; long gcd(long a,long b) { long min=Math.min(a,b); long max=Math.max(a,b); while (max%min!=0) { a=max%min; max=min;min=a; } return min; } public static void main(String[] args) throws IOException { Reader sc=new Reader();Solution G=new Solution(); PrintWriter o = new PrintWriter(System.out); int t=1;t=sc.nextInt(); long x,x0,x1,x2;int y,y0,y1,y2;int s,s0,s1,s2; int n,m;int a[],b[],in[],in1[]; long k,l;boolean b1,b2;String ss1[],ss; //long l;long a[]; ArrayList<ArrayList<Integer>> ll=new ArrayList<>(); ArrayList<Integer> a1=new ArrayList<>(); ArrayList<Integer> a2=new ArrayList<>(); ArrayList<Integer> a3=new ArrayList<>(); ArrayDeque<Integer> deq=new ArrayDeque<>(); TreeSet<Integer> h0=new TreeSet<>(); TreeSet<Integer> h1=new TreeSet<>(); HashMap<Integer,Integer> h=new HashMap<>(); try{ while (t-->0) { n=sc.nextInt();l=sc.nextLong();a=new int[n]; long mm; if (l%2==1)mm=l/2+1; else mm=l/2; //o.println(mm); long z=-1;k=0; for (int i=0;i<n;i++) { x=sc.nextInt(); if (x<mm) { if (k<mm) {k+=x;a1.add(i+1);} } else if(mm<=x && x<=l)z=i+1; //o.println(k+" "+mm+" "+x+" "+l); } if (z!=-1) o.println("1\n"+z); else if (k>=mm && k<=l) { o.println(a1.size()); for (int i:a1) o.print(i+" "); o.println(); } else o.println(-1); //o.println(); //o.println(h); //o.println(x2); //o.println(); h0.clear();ll.clear();a1.clear();a2.clear();h1.clear(); } } catch (Throwable e) { e.printStackTrace(); } //o.println("HI"); o.flush(); o.close(); } }
Java
["3\n1 3\n3\n6 2\n19 8 19 69 9 4\n7 12\n1 1 1 17 1 1 1"]
2 seconds
["1\n1\n-1\n6\n1 2 3 5 6 7"]
NoteIn the first test case, you can take the item of weight $$$3$$$ and fill the knapsack just right.In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is $$$-1$$$.In the third test case, you fill the knapsack exactly in half.
Java 11
standard input
[ "constructive algorithms", "sortings", "greedy", "math" ]
afe8710473db82f8d53d28dd32646774
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains integers $$$n$$$ and $$$W$$$ ($$$1 \le n \le 200\,000$$$, $$$1\le W \le 10^{18}$$$). The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 10^9$$$) — weights of the items. The sum of $$$n$$$ over all test cases does not exceed $$$200\,000$$$.
1,300
For each test case, if there is no solution, print a single integer $$$-1$$$. If there exists a solution consisting of $$$m$$$ items, print $$$m$$$ in the first line of the output and $$$m$$$ integers $$$j_1$$$, $$$j_2$$$, ..., $$$j_m$$$ ($$$1 \le j_i \le n$$$, all $$$j_i$$$ are distinct) in the second line of the output  — indices of the items you would like to pack into the knapsack. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.
standard output
PASSED
7ea5a1c0f9df368214492d9a75dd467e
train_002.jsonl
1605450900
You have a knapsack with the capacity of $$$W$$$. There are also $$$n$$$ items, the $$$i$$$-th one has weight $$$w_i$$$. You want to put some of these items into the knapsack in such a way that their total weight $$$C$$$ is at least half of its size, but (obviously) does not exceed it. Formally, $$$C$$$ should satisfy: $$$\lceil \frac{W}{2}\rceil \le C \le W$$$. Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class C { final static int MAXN = 100_005; final static long MOD = (long) 1e9 + 7; public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); outer: for(int tt =0 ;tt < t ; tt++) { int n = sc.nextInt() ; long w = sc.nextLong(); pair a[] = new pair[n]; for(int i = 0 ; i< n ; i++)a[i] = new pair(sc.nextInt() , i+1); Arrays.sort(a); if(a[0].val > w) { System.out.println(-1); continue outer; } List<Integer> list = new ArrayList<>(); for(int i = 0 ; i< n ; i++) { if(a[i].val >= (w+1)/2 && a[i].val <= w ) { System.out.println(1); System.out.println(a[i].index); continue outer; } } int k = 0; long sum = 0; while(k < n && a[k].val < (w+1)/2 ) { sum += a[k].val; k++; } if(sum < (w+1)/2) { System.out.println(-1); continue outer; } sum = 0; for(int i = 0 ; i< k ; i++) { sum += a[i].val; list.add(a[i].index); if(sum >= (w+1)/2) { System.out.println(list.size()); for(int item : list) { System.out.print(item + " "); } System.out.println(); continue outer; } } } } static class pair implements Comparable<pair>{ int val ; int index ; pair(int val , int index){ this.val = val; this.index = index; } @Override public int compareTo(pair o) { return this.val - o.val; } } public static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static long[] factorial; public static void setFactorial() { factorial = new long[MAXN]; factorial[0] = 1; for (int i = 1; i < MAXN; ++i) factorial[i] = factorial[i - 1] * i % MOD; } public static long getFactorial(int n) { if (factorial == null) setFactorial(); return factorial[n]; } public static long ncr(int n, int r) { if (r > n) return 0; if (factorial == null) setFactorial(); long numerator = factorial[n]; long denominator = factorial[r] * factorial[n - r] % MOD; return numerator * pow(denominator, MOD - 2, MOD) % MOD; } public static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static long pow(long base, long exp, long MOD) { base %= MOD; long ret = 1; while (exp > 0) { if ((exp & 1) == 1) ret = ret * base % MOD; base = base * base % MOD; exp >>= 1; } return ret; } public static long max(long... ar) { long ret = ar[0]; for (long itr : ar) ret = Math.max(ret, itr); return ret; } public static int max(int... ar) { int ret = ar[0]; for (int itr : ar) ret = Math.max(ret, itr); return ret; } public static long min(long... ar) { long ret = ar[0]; for (long itr : ar) ret = Math.min(ret, itr); return ret; } public static int min(int... ar) { int ret = ar[0]; for (int itr : ar) ret = Math.min(ret, itr); return ret; } public static long sum(long... ar) { long sum = 0; for (long itr : ar) sum += itr; return sum; } public static long sum(int... ar) { long sum = 0; for (int itr : ar) sum += itr; return sum; } 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()); } 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; } } }
Java
["3\n1 3\n3\n6 2\n19 8 19 69 9 4\n7 12\n1 1 1 17 1 1 1"]
2 seconds
["1\n1\n-1\n6\n1 2 3 5 6 7"]
NoteIn the first test case, you can take the item of weight $$$3$$$ and fill the knapsack just right.In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is $$$-1$$$.In the third test case, you fill the knapsack exactly in half.
Java 11
standard input
[ "constructive algorithms", "sortings", "greedy", "math" ]
afe8710473db82f8d53d28dd32646774
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains integers $$$n$$$ and $$$W$$$ ($$$1 \le n \le 200\,000$$$, $$$1\le W \le 10^{18}$$$). The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 10^9$$$) — weights of the items. The sum of $$$n$$$ over all test cases does not exceed $$$200\,000$$$.
1,300
For each test case, if there is no solution, print a single integer $$$-1$$$. If there exists a solution consisting of $$$m$$$ items, print $$$m$$$ in the first line of the output and $$$m$$$ integers $$$j_1$$$, $$$j_2$$$, ..., $$$j_m$$$ ($$$1 \le j_i \le n$$$, all $$$j_i$$$ are distinct) in the second line of the output  — indices of the items you would like to pack into the knapsack. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.
standard output
PASSED
8796dbdc111454aae253149106ba9404
train_002.jsonl
1605450900
You have a knapsack with the capacity of $$$W$$$. There are also $$$n$$$ items, the $$$i$$$-th one has weight $$$w_i$$$. You want to put some of these items into the knapsack in such a way that their total weight $$$C$$$ is at least half of its size, but (obviously) does not exceed it. Formally, $$$C$$$ should satisfy: $$$\lceil \frac{W}{2}\rceil \le C \le W$$$. Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.
256 megabytes
//============================================================================ /* "There is nothing that can take the pain away. But eventually you will find a way to live with it. There will be nightmares. And every day when you wake up, it will be the first thing you think about. Until one day, it will be the second thing." */ // Author : Murad // Online Judge: Codeforces.cpp & Atcoder.cpp // Description : Problem name //============================================================================ /* Riven && Vladimir && Ekko */ import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader inp = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int t=inp.nextInt(); while(t-->0){ long n=inp.nextLong(); long w=inp.nextLong(); Pair []pairs=new Pair[(int)n]; ArrayList<Integer>mo=new ArrayList<>(); for(int i=0;i<n;i++){ int val=inp.nextInt(); pairs[i]=new Pair(val,i+1); } Arrays.sort(pairs); boolean ok=false; long sum=0; long half=w/2; if(w%2==1)half++; for(int i=0;i<pairs.length;i++) { if(pairs[i].value<half){ sum+=pairs[i].value; mo.add((int)pairs[i].idx); if(sum>=half){ ok|=true; break; } } else if(pairs[i].value>=half && pairs[i].value<=w){ ok|=true; mo.clear(); mo.add((int)pairs[i].idx); break; } } if(ok){ out.println(mo.size()); for(int i=0;i<mo.size();i++) out.print(mo.get(i)+" "); out.println(); } else out.println(-1); out.flush(); } } static class Pair<C, I extends Number> implements Comparable<Pair<C, Number>> { long value; long idx; Pair(long v, long i) { value = v; idx = i; } @Override public int compareTo(Pair<C, Number> p) { return (int)(value - p.value); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public long[] readLongArray(int n) { long[] x = new long[n]; for (int i = 0; i < n; i++) { x[i] = nextLong(); } return x; } public int[] readIntArray(int n) { int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = nextInt(); } return x; } } static class NumberTheory{ public static long gcd(long a,long b){ long c; while (a != 0) { c = a; a = b % a; b = c; } return b; } } //Relatively Prime :- if diffrence between two number is equal to 1 }
Java
["3\n1 3\n3\n6 2\n19 8 19 69 9 4\n7 12\n1 1 1 17 1 1 1"]
2 seconds
["1\n1\n-1\n6\n1 2 3 5 6 7"]
NoteIn the first test case, you can take the item of weight $$$3$$$ and fill the knapsack just right.In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is $$$-1$$$.In the third test case, you fill the knapsack exactly in half.
Java 11
standard input
[ "constructive algorithms", "sortings", "greedy", "math" ]
afe8710473db82f8d53d28dd32646774
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains integers $$$n$$$ and $$$W$$$ ($$$1 \le n \le 200\,000$$$, $$$1\le W \le 10^{18}$$$). The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 10^9$$$) — weights of the items. The sum of $$$n$$$ over all test cases does not exceed $$$200\,000$$$.
1,300
For each test case, if there is no solution, print a single integer $$$-1$$$. If there exists a solution consisting of $$$m$$$ items, print $$$m$$$ in the first line of the output and $$$m$$$ integers $$$j_1$$$, $$$j_2$$$, ..., $$$j_m$$$ ($$$1 \le j_i \le n$$$, all $$$j_i$$$ are distinct) in the second line of the output  — indices of the items you would like to pack into the knapsack. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.
standard output
PASSED
991668de0ba9231e66ce987fab8ee5bb
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
// Working program with FastReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader in=new FastReader(); StringBuilder st = new StringBuilder(); int t = in.nextInt(); while(t-->0) { int n = in.nextInt(); int m = in.nextInt(); int sum = 0; int arr[] = new int[n]; for(int i = 0;i<n;i++) { arr[i] = in.nextInt(); sum += arr[i]; } if(m<n||n==2) { st.append(-1+"\n"); continue; } st.append(sum*2+"\n"); for(int i = 1; i <n;i++) { st.append(i+" "+(i+1)+"\n"); } st.append(n+" "+1+"\n"); // st.append(an+"\n"); } System.out.print(st.toString()); } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
d2cf83b52dde4e8bdd1b9557f0f65854
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { FastReader read = new FastReader(); StringBuilder out = new StringBuilder(); int t = read.nextInt(); while (t-- > 0) { int n = read.nextInt(); int m = read.nextInt(); Pair f[] = new Pair[n]; for (int i = 0; i < n; i++) { f[i] = new Pair(i + 1, read.nextInt()); } Arrays.sort(f); int cost = 0; StringBuilder link = new StringBuilder(); if (n == 2) { out.append("-1").append('\n'); continue; } if (n > m) { out.append("-1").append('\n'); continue; } else { for (int i = 0; i < n - 1; i++) { cost += f[i].w + f[i + 1].w; link.append(f[i].id).append(' ').append(f[i + 1].id).append('\n'); m--; } cost += f[0].w + f[n - 1].w; link.append(f[0].id).append(' ').append(f[n - 1].id).append('\n'); m--; while (m-- > 0) { cost += f[1].w + f[0].w; link.append(f[1].id).append(' ').append(f[0].id).append('\n'); } } out.append(cost).append('\n'); out.append(link); } System.out.print(out); } } class Pair implements Comparable<Pair> { int id; int w; public Pair(int id, int w) { this.id = id; this.w = w; } @Override public int compareTo(Pair p) { if (this.w != p.w) { return this.w - p.w; } return this.id - p.id; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
ad5941fd7dac939c1ca1c057a70af0e1
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Jenish */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); BFridgeLockers solver = new BFridgeLockers(); solver.solve(1, in, out); out.close(); } static class BFridgeLockers { public void solve(int testNumber, ScanReader in, PrintWriter out) { int t = in.scanInt(); while (t-- > 0) { int n = in.scanInt(); int m = in.scanInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = in.scanInt(); long sum = 0; for (int k : arr) sum += (2 * k); if (m < n || n == 2) { out.println(-1); } else { int re = m - n; long min = Long.MAX_VALUE; int ii = -1; int jj = -1; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] + arr[j] < min) { min = arr[i] + arr[j]; ii = i; jj = j; } } } out.println(sum + re * (min)); for (int i = 0; i < n; i++) { int a = i; int b = (i + 1) % n; out.println((a + 1) + " " + (b + 1)); } while (re-- > 0) { out.println((ii + 1) + " " + (jj + 1)); } } } } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int INDEX; private BufferedInputStream in; private int TOTAL; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (INDEX >= TOTAL) { INDEX = 0; try { TOTAL = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (TOTAL <= 0) return -1; } return buf[INDEX++]; } public int scanInt() { int I = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { I *= 10; I += n - '0'; n = scan(); } } return neg * I; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
010c6dd7da1904390409ca810a5ae008
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.util.*; import java.io.*; import java.text.DecimalFormat; public class Exam { public static long mod = (long)Math.pow(10, 9)+7 ; public static double epsilon=0.00000000008854;//value of epsilon public static InputReader sc = new InputReader(System.in); public static PrintWriter pw = new PrintWriter(System.out); public static void d(int n,int s,int a[],String ans){ if(a[0]==1){ return; } if(n<=0){ if(Math.sqrt(s)-(int)Math.sqrt(s)==0) { a[0]=1; pw.println(ans); } return; } for(int i=1;i<10;i++){ d(n-1,s+i*i,a,ans+i); } } public static int gr(int s,int e,int a[][],int k,int dp[][]){ int t=0; if(dp[s][k]!=0) return dp[s][k]; if(s==e&&k==0){ dp[s][k]=1; return 1; } if(k<=0){ dp[s][k]=0; return 0; } for(int i=0;i<a[s].length;i++){ if(a[s][i]==1){ t+=gr(i,e,a,k-1,dp); } } dp[s][k]=t; return t; } public static int f(int n,int k,int v[],int dp[][]){ if(n==0) return 0; if(n<0||k<=0) return -1; if(dp[n][k]!=0){ return dp[n][k]; } int c=f(n-k,k,v,dp); if(c==-1){ dp[n][k]=f(n, k-1, v, dp); } else{ dp[n][k]=Math.max(c+v[k-1],f(n, k-1, v, dp)); } return dp[n][k]; } public static int f1(int n,int v[],int dp[]){ if(n==0) return v[0]; else if(n==1) return Math.max(v[0],v[1]); if(dp[n]!=0) return dp[n]; else{ dp[n]=Math.max(f1(n-2,v,dp)+v[n], f1(n-1,v,dp)); return dp[n]; } } public static double dis(int x,int y,int x1[],int y1[],int x2[],int y2[],int x3[],int y3[]){ double d1[]=new double [x1.length]; double d2[]=new double [x2.length]; Arrays.fill(d2,Long.MAX_VALUE); for(int i=0;i<x1.length;i++){ d1[i]=Math.sqrt(Math.pow(x1[i]-x, 2)+Math.pow(y1[i]-y, 2)); } for(int i=0;i<x2.length;i++){ for(int j=0;j<x3.length;j++){ d2[i]=Math.min(Math.sqrt(Math.pow(x2[i]-x3[j], 2)+Math.pow(y2[i]-y3[j], 2)), d2[i]); } } double min=Long.MAX_VALUE; for(int i=0;i<x1.length;i++){ for(int j=0;j<x2.length;j++){ min=Math.min(d1[i]+d2[j]+Math.sqrt(Math.pow(x1[i]-x2[j], 2)+Math.pow(y1[i]-y2[j], 2)), min); } } return min; } public static void main(String[] args) { // code starts.. int q=sc.nextInt(); while(q-->0){ int n=sc.nextInt(); int m=sc.nextInt(); int a[]=scanArray(n); long c=0; if(m<n||n<3) { pw.println("-1"); continue; } Arrays.sort(a); for(int i=0;i<n;i++){ c+=2*a[i]; } c+=(m-n)*(a[0]+a[1]); pw.println(c); for(int i=0;i<m;i++){ if(i<n){ pw.println((i+1)+" "+((i+1)%n+1)); } else pw.println(1+" "+2); } } // Code ends... pw.flush(); pw.close(); } static class tripletL implements Comparable<tripletL> { Long x, y, z; tripletL(long x, long y, long z) { this.x = x; this.y = y; this.z = z; } public int compareTo(tripletL o) { int result = x.compareTo(o.x); if (result == 0) result = y.compareTo(o.y); if (result == 0) result = z.compareTo(o.z); return result; } public boolean equlas(Object o) { if (o instanceof tripletL) { tripletL p = (tripletL) o; return (x - p.x == 0) && (y - p.y ==0 ) && (z - p.z == 0); } return false; } public String toString() { return x + " " + y + " " + z; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode() + new Long(z).hashCode(); } } public static String Doubleformate(double a){ DecimalFormat f =new DecimalFormat("#.00"); return f.format(a); } public static Comparator<Integer[]> column(int i){ return new Comparator<Integer[]>() { @Override public int compare(Integer[] o1, Integer[] o2) { Integer quantityOne = o1[i]; Integer quantityTwo = o2[i]; return quantityOne.compareTo(quantityTwo); } }; } public static Comparator<Integer[]> pair(){ return new Comparator<Integer[]>() { @Override public int compare(Integer[] o1, Integer[] o2) { int result=o1[0].compareTo(o2[0]); if(result==0) result=o1[1].compareTo(o2[1]); return result; } }; } public static Comparator<Long[]> Triplet(){ return new Comparator<Long[]>() { @Override public int compare(Long[] o1, Long[] o2) { int result=o1[0].compareTo(o2[0]); if(result==0) result=o1[1].compareTo(o2[1]); if(result==0) result=o1[2].compareTo(o2[2]); return result; } }; } public static String reverseString(String s){ StringBuilder input1 = new StringBuilder(); input1.append(s); input1 = input1.reverse(); return input1.toString(); } public static int[] scanArray(int n){ int a[]=new int [n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); return a; } public static long[] scanLongArray(int n){ long a[]=new long [n]; for(int i=0;i<n;i++) a[i]=sc.nextLong(); return a; } public static String [] scanStrings(int n){ String a[]=new String [n]; for(int i=0;i<n;i++) a[i]=sc.nextLine(); return a; } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
e4c4ba3ba5a1ab627c0ad842b93f72c4
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.util.*; public class simple { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int t=scan.nextInt(); while(t>0) { int n=scan.nextInt(); int i; int m=scan.nextInt(); int [] a=new int [n]; long sum=0; for(i=0;i<n;i++) { a[i]=scan.nextInt(); sum=sum+a[i]; } if(n>m||n==2) { System.out.println("-1"); } else if(n==m) { System.out.println(2*sum); for(i=1;i<n;i++) { System.out.println(i+" "+(i+1)); } System.out.println(n+" "+1); } t--; } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
1e18273b5b4cc525805fb3de6515dd25
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class Main { static int cost = 0; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int testCase = Integer.parseInt(br.readLine()); while (testCase-- > 0) { cost = 0; StringTokenizer st = new StringTokenizer(br.readLine(), " "); int fridge_num = Integer.parseInt(st.nextToken()); int chain_num = Integer.parseInt(st.nextToken()); int[] weights = new int[fridge_num]; init(weights, br); if (fridge_num > chain_num) { System.out.println(-1); continue; } ArrayList<Integer>[] adj = new ArrayList[fridge_num]; for (int i = 0; i < fridge_num; i++) { adj[i] = new ArrayList<Integer>(); } // Do private Chaining ArrayList<String> chainList = new ArrayList<String>(); doChaining(0, 0, adj, weights, chainList); if (!checkPrivate(adj)) { System.out.println(-1); } else { int same = chain_num - fridge_num; int[] shortest = find(weights); int from = shortest[0]; int to = shortest[1]; cost += (weights[from] + weights[to]) * same; System.out.println(cost); StringBuilder sb = new StringBuilder(); for (int i = 0; i < same; i++) { sb.append(from+1 + " " + (to+1) + "\n"); } for (String str : chainList) { sb.append(str + "\n"); } System.out.print(sb.toString()); } } } private static int[] find(int[] weights) { int min = Integer.MAX_VALUE; int minIndex = 0; for (int i = 0; i < weights.length; i++) { if (min > weights[i]) { min = weights[i]; minIndex = i; } } min = Integer.MAX_VALUE; int nextIndex = 0; for (int i = 0; i < weights.length; i++) { if (i != minIndex) { if (min > weights[i]) { min = weights[i]; nextIndex = i; } } } return new int[] {minIndex, nextIndex}; } private static void doChaining(int pos, int count, ArrayList<Integer>[] adj, int[] weights, ArrayList<String> chainList) { if (pos == weights.length || count == weights.length) { return; } if (adj[pos].size() < 2) { int to = getTarget(pos, adj); adj[pos].add(to); adj[to].add(pos); chainList.add(pos+1 + " " + (to+1)); cost += weights[pos] + weights[to]; count++; doChaining(pos, count, adj, weights, chainList); } else { doChaining(pos+1, count, adj, weights, chainList); } } private static int getTarget(int from, ArrayList<Integer>[] adj) { int index = 0; for (int i = 0; i < adj.length; i++) { if (i != from && !adj[i].contains(from)) { if (adj[i].size() == 0) { return i; } else if (adj[i].size() < 2) { index = i; } } } return index; } private static boolean checkPrivate(ArrayList<Integer>[] adj) { for (ArrayList<Integer> list : adj) { if (list.size() < 2) { return false; } } return true; } private static void init(int[] weights, BufferedReader br) throws Exception { StringTokenizer st = new StringTokenizer(br.readLine(), " "); for (int i = 0; i < weights.length; i++) { weights[i] = Integer.parseInt(st.nextToken()); } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
ea0686098b77176108f2ae0b6889764e
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.*; import java.util.*; public class GFG { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while (t-->0) { int n=sc.nextInt(); int m=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); if(m<n) {System.out.println(-1);continue;} if(n==2){System.out.println(-1);continue;} int b[]=new int[n]; for(int i=0;i<n;i++) b[i]=arr[i]; Arrays.sort(b); int c[]=new int[n]; HashSet<Integer> h=new HashSet<Integer>(); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(b[i]==arr[j]&&!h.contains(j+1)){ c[i]=j+1;h.add(j+1);break;} } } int a[][]=new int[m][2]; long sum=0; int p=0,q=1,tmp=0,c1=0; for(int i=0;i<m;i++) { if(p==n-1){ a[i][0]=c[n-1]; a[i][1]=c[0];p=0;tmp=1;c1++; sum=sum+b[n-1]+b[0]; break;} a[i][0]=c[p]; a[i][1]=c[q]; sum=sum+b[p]+b[q];p++;q++;c1++; } for(int i=c1;i<m;i++) { a[i][0]=c[0]; a[i][1]=c[1]; sum=sum+b[0]+b[1]; } System.out.println(sum); for(int i=0;i<m;i++) System.out.println(a[i][0]+" "+a[i][1]); } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
e85d70eebfe8e27965dd21de0edbef74
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; import java.util.Objects; import java.util.List; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author anand.oza */ 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); BFridgeLockers solver = new BFridgeLockers(); solver.solve(1, in, out); out.close(); } static class BFridgeLockers { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); for (int i = 0; i < t; i++) { solve(in, out); } } private void solve(InputReader in, PrintWriter out) { int n = in.nextInt(), m = in.nextInt(); int[] a = in.readIntArray(n); if (n <= 2 || m < n) { out.println(-1); return; } Util.ASSERT(n == m); List<Pair<Integer, Integer>> edges = solve(a, m); Util.ASSERT(verify(n, m, edges)); out.println(findCost(a, edges)); for (Pair<Integer, Integer> e : edges) { out.println(Util.join(e.first + 1, e.second + 1)); } } private static boolean verify(int n, int m, List<Pair<Integer, Integer>> edges) { HashSet<Integer>[] neighbors = new HashSet[n]; for (int i = 0; i < n; i++) { neighbors[i] = new HashSet<>(); } for (Pair<Integer, Integer> e : edges) { neighbors[e.first].add(e.second); neighbors[e.second].add(e.first); } if (edges.size() != m) { return false; } for (HashSet<Integer> s : neighbors) { if (s.size() < 2) { return false; } } return true; } private static int findCost(int[] a, List<Pair<Integer, Integer>> edges) { int cost = 0; for (Pair<Integer, Integer> e : edges) { cost += a[e.first]; cost += a[e.second]; } return cost; } private static List<Pair<Integer, Integer>> solve(int[] a, int m) { int n = a.length; if (n <= 2 || m < n) { return null; } if (n == m) { List<Pair<Integer, Integer>> answer = new ArrayList<>(); for (int i = 0; i < n; i++) { answer.add(Pair.of(i, (i + 1) % n)); } return answer; } return null; } } static class Pair<F, S> { public final F first; public final S second; public Pair(F first, S second) { this.first = first; this.second = second; } public static <F, S> Pair<F, S> of(F first, S second) { return new Pair<>(first, second); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return Objects.equals(first, pair.first) && Objects.equals(second, pair.second); } public int hashCode() { return Objects.hash(first, second); } public String toString() { return "(" + first + ", " + second + ')'; } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] readIntArray(int n) { int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = nextInt(); } return x; } } static class Util { public static String join(int... i) { StringBuilder sb = new StringBuilder(); for (int o : i) { sb.append(o); sb.append(" "); } if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } public static void ASSERT(boolean assertion) { if (!assertion) throw new AssertionError(); } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
6efcda5195646b672dcd68d817a8e58a
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author anand.oza */ 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); BFridgeLockers solver = new BFridgeLockers(); solver.solve(1, in, out); out.close(); } static class BFridgeLockers { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); for (int i = 0; i < t; i++) { solve(in, out); } } private void solve(InputReader in, PrintWriter out) { int n = in.nextInt(), m = in.nextInt(); int[] a = in.readIntArray(n); if (n <= 2 || m < n) { out.println(-1); return; } Util.ASSERT(n == m); int cost = 0; for (int x : a) { cost += 2 * x; } out.println(cost); for (int i = 0; i < n; i++) { out.println(Util.join(i + 1, (i + 1) % n + 1)); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] readIntArray(int n) { int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = nextInt(); } return x; } } static class Util { public static String join(int... i) { StringBuilder sb = new StringBuilder(); for (int o : i) { sb.append(o); sb.append(" "); } if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } public static void ASSERT(boolean assertion) { if (!assertion) throw new AssertionError(); } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
2f9ca31a79a8a3e13802a26154099677
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.util.Deque; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; import java.util.Objects; import java.util.List; import java.util.StringTokenizer; import java.io.BufferedReader; import java.util.Comparator; import java.util.ArrayDeque; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author anand.oza */ 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); BFridgeLockers solver = new BFridgeLockers(); solver.solve(1, in, out); out.close(); } static class BFridgeLockers { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); for (int i = 0; i < t; i++) { solve(in, out); } } private void solve(InputReader in, PrintWriter out) { int n = in.nextInt(), m = in.nextInt(); int[] a = in.readIntArray(n); List<Pair<Integer, Integer>> edges = findEdges(a, m); if (edges == null) { out.println(-1); return; } int cost = findCost(a, edges); out.println(cost); for (Pair<Integer, Integer> e : edges) { out.println(Util.join(e.first + 1, e.second + 1)); } Util.ASSERT(verifyDegrees(n, m, edges)); Util.ASSERT(cost == findTheoreticalMin(a, m)); } private static boolean verifyDegrees(int n, int m, List<Pair<Integer, Integer>> edges) { HashSet<Integer>[] neighbors = new HashSet[n]; for (int i = 0; i < n; i++) { neighbors[i] = new HashSet<>(); } for (Pair<Integer, Integer> e : edges) { neighbors[e.first].add(e.second); neighbors[e.second].add(e.first); } if (edges.size() != m) { return false; } for (HashSet<Integer> s : neighbors) { if (s.size() < 2) { return false; } } return true; } private static int findCost(int[] a, List<Pair<Integer, Integer>> edges) { int cost = 0; for (Pair<Integer, Integer> e : edges) { cost += a[e.first]; cost += a[e.second]; } return cost; } private static int findTheoreticalMin(int[] a, int m) { int n = a.length; int[] s = a.clone(); Arrays.sort(s); Util.ASSERT(m >= n); int min = s[0]; int min2 = s[1]; int tot = 0; for (int x : s) { tot += x; } int cost = 0; cost += 2 * tot; // deg(each) >= 2 // deg(min) <= sum[deg(rest)] therefore deg(min) <= totaldeg - deg(min) // therefore 2deg(min) <= totaldeg = m * 2 // every node has at least 1 edge not including min. so there are at least // n-1 unavailable degree. so deg(min) <= 2*m - (n-1) - deg(min) // deg(min) <= m - (n-1)/2 int deg2OfEach = 2 * (n - 1); int degMin = Math.min(m - (n - 1 + 1) / 2, 2 * m - deg2OfEach); cost += (degMin - 2) * min; int degMin2 = 2 * m - 2 * (n - 2) - degMin; cost += (degMin2 - 2) * min2; return cost; } private static List<Pair<Integer, Integer>> findEdges(int[] a, int m) { int n = a.length; if (n <= 2 || m < n) { return null; } if (n == m) { List<Pair<Integer, Integer>> answer = new ArrayList<>(); for (int i = 0; i < n; i++) { answer.add(Pair.of(i, (i + 1) % n)); } return answer; } Integer[] indices = new Integer[n]; for (int i = 0; i < n; i++) { indices[i] = i; } Arrays.sort(indices, Comparator.comparingInt((Integer i) -> a[i])); Deque<Integer> big = new ArrayDeque<>(); for (int i = 2; i < n; i++) { big.add(indices[i]); } int min = indices[0]; int min2 = indices[1]; int degMin = Math.min(m - (n - 1 + 1) / 2, 2 * m - 2 * (n - 1)); int degMin2 = 2 * m - 2 * (n - 2) - degMin; //System.out.format("n=%d, m=%d, degMin=%d, degMin2=%d%n", n, m, degMin, degMin2); List<Pair<Integer, Integer>> answer = new ArrayList<>(); while (degMin2 > 2) { answer.add(Pair.of(min, min2)); degMin--; degMin2--; } while (degMin > 2) { if (big.size() < 2) break; int i = big.pollFirst(), j = big.pollFirst(); answer.add(Pair.of(min, i)); answer.add(Pair.of(min, j)); answer.add(Pair.of(i, j)); degMin -= 2; } List<Integer> remaining = new ArrayList<>(); remaining.addAll(big); remaining.add(min); remaining.add(min2); Util.ASSERT(degMin == 2); Util.ASSERT(degMin2 == 2); Util.ASSERT(m - answer.size() == remaining.size()); for (int i = 0; i < remaining.size(); i++) { answer.add(Pair.of(remaining.get(i), remaining.get((i + 1) % remaining.size()))); } return answer; } } static class Pair<F, S> { public final F first; public final S second; public Pair(F first, S second) { this.first = first; this.second = second; } public static <F, S> Pair<F, S> of(F first, S second) { return new Pair<>(first, second); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return Objects.equals(first, pair.first) && Objects.equals(second, pair.second); } public int hashCode() { return Objects.hash(first, second); } public String toString() { return "(" + first + ", " + second + ')'; } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] readIntArray(int n) { int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = nextInt(); } return x; } } static class Util { public static String join(int... i) { StringBuilder sb = new StringBuilder(); for (int o : i) { sb.append(o); sb.append(" "); } if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } public static void ASSERT(boolean assertion) { if (!assertion) throw new AssertionError(); } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
8d58c4c251944a79ed101ed9117a3c26
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.util.Scanner; public class div601B { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int t=sc.nextInt(); for(int z=0;z<t;z++) { int flag=0; int n=sc.nextInt(); int chain=sc.nextInt(); int sum=0; for(int i=1;i<=n;i++) { int a=sc.nextInt(); sum +=a; } if(chain<n || n<3) { flag=1; System.out.println("-1"); } if(flag ==0) { System.out.println(2*sum); for(int i=1;i<=n;i++) System.out.println(i+" "+(i==n?1:i+1)); } } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
e9894dde1091e21019aea90880421005
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.util.*; public class Solution{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int m = sc.nextInt(); int[] w = new int[n]; int cost = 0; for(int i=0;i<n;i++){ w[i] = sc.nextInt(); } if(m<n || n==2){ System.out.println(-1); }else{ StringBuffer sb = new StringBuffer(); int l1 = 0; int l2 = 0; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i=0;i<n;i++){ map.put(i, w[i]); } map = sortByValue(map); for(int i=0;i<n-1;i++){ cost+=w[i]+w[i+1]; sb.append((i+1)+" "+(i+2)+"\n"); m--; } if(n>2){ cost+=w[n-1]+w[0]; sb.append(1+" "+n+"\n"); m--; } if(m>0 && n>2){ l1 = map.get(map.keySet().toArray()[0]); l2 = map.get(map.keySet().toArray()[1]); while(m-->0){ cost+= l1+l2; sb.append(1+" "+2+"\n"); } } System.out.println(cost); System.out.println(sb.toString().trim()); } } } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
1eafbdc0d29ad9990b4040b3c27b19d8
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.*; import java.util.*; public class Main { static boolean prime[]; public static void main(String ag[]) { Scanner sc=new Scanner(System.in); int i,j,k; int T=sc.nextInt(); while(T-- >0) { int N=sc.nextInt(); int M=sc.nextInt(); int arr[]=new int[N]; Pair a[]=new Pair[N]; for(i=0;i<N;i++){ arr[i]=sc.nextInt(); a[i]=new Pair(arr[i],i+1); } if(M<N || N==2) System.out.println(-1); else { long sum=0; for(i=0;i<N-1;i++) { sum+=(arr[i]+arr[i+1]); } sum+=arr[0]+arr[N-1]; Arrays.sort(arr); Arrays.sort(a,new Tcomp()); M=M-N; for(i=1;i<=M;i++) sum+=(arr[0]+arr[1]); System.out.println(sum); for(i=1;i<N;i++) System.out.println(i+" "+(i+1)); System.out.println(1+" "+(N)); int x=a[0].b; int y=a[1].b; for(i=1;i<=M;i++) System.out.println(x+" "+y); } } } } class Pair { int a,b; Pair(int a,int b) { this.a=a; this.b=b; } } class Tcomp implements Comparator<Pair> { public int compare(Pair p,Pair q) { return p.a-q.a; } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
9bace666fbd0353d5b06158e17af33ed
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Q2 { public static void main(String[] args) { mYScanner in = new mYScanner(); int t = in.nextInt(); for(int i = 0;i<t;i++) { int n = in.nextInt(); int m = in.nextInt(); int sum = 0; int f[] = new int[n+1]; for(int j = 1;j<=n;j++) { f[j] = in.nextInt(); sum+=f[j]; } if(n==2 || m<n) System.out.println(-1); else { int index1 = 1; for(int j = 2;j<=n;j++) if(f[index1]>f[j]) index1 = j; int wieght = f[index1]; f[index1] = Integer.MAX_VALUE; int index2 = 1; for(int j = 2;j<=n;j++) if(f[index2]>f[j]) index2 = j; f[index1] = wieght; System.out.println((2*sum)+(m-n)*(f[index1]+f[index2])); int index = n; while(n>0) { if(n==1) { System.out.println(index+" "+1); m--; n--; } else { System.out.println(n+" "+(n-1)); n--; m--; } } while(m>0) { System.out.println(index1 + " "+index2); m--; } } } } } class mYScanner { BufferedReader br ; StringTokenizer st; public mYScanner() { br = new BufferedReader(new InputStreamReader(System.in)); st = null; } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
94741eefa393d3f108e4ed472cb30016
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.*; import java.util.*; public class HelloWorld{ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Pair implements Comparable<Pair> { Integer val; Integer i; public int compareTo(Pair p) { return this.val.compareTo(p.val); } } public static void main(String []args){ FastReader s=new FastReader(); int n = s.nextInt(); while (n-- > 0) { int x = s.nextInt(); int y = s.nextInt(); if(x > 0) { int arr[] = new int[x]; for(int i = 0 ; i < x ; i++) { int curr = s.nextInt(); arr[i] = curr; } if(x < 3 || y < x) { System.out.println(-1); continue; } long ans = 0; List<String> l = new ArrayList<>(); for(int i = 0 ; i < x ; i++) { ans += (arr[i] + arr[i]); if(i != x - 1) { l.add((i + 1) + " "+ (i + 2)); } else{ l.add(x + " "+ 1); } } System.out.println(ans); for(String str : l) System.out.println(str); } } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
06f85eb08059d5f6ae95e22153536e7d
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Day9 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); int T = Integer.parseInt(reader.readLine()); for(int q = 0; q < T; ++q){ StringTokenizer st = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); if(m < n || n == 2){ st = new StringTokenizer(reader.readLine()); writer.println(-1); continue; } ArrayList<String> ans = new ArrayList<>(); int counter = 0; int[] array = new int[n]; st = new StringTokenizer(reader.readLine()); for(int i = 0; i < n; ++i){ array[i] = Integer.parseInt(st.nextToken()); counter += array[i] * 2; ans.add((i + 1) + " " + ((i + 1) % n + 1)); } int min1 = Integer.MAX_VALUE; int min2 = Integer.MAX_VALUE; int ind1 = 0; int ind2 = 0; for(int i = 0; i < n; ++i){ if(array[i] < min1){ min2 = min1; min1 = array[i]; ind2 = ind1; ind1 = i; } else if(array[i] < min2){ min2 = array[i]; ind2 = i; } } m -= n; for(int i = 0; i < m; ++i){ ans.add((ind1 + 1) + " " + (ind2 + 1)); counter += min1 + min2; } writer.println(counter); for(String s : ans){ writer.println(s); } } writer.close(); } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
80749fcb25d930594df16f3642ff9c40
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; public class Main { // static Scanner scanner = new Scanner(System.in); // static BufferedReader scanner=new BufferedReader(new InputStreamReader(System.in)); // static PrintWriter writer = new PrintWriter(System.out); static Reader scanner = new Reader(System.in); public static void main(String[] args) throws IOException { try { int t = scanner.nextInt(); while (t-- > 0) { int n = scanner.nextInt(); int m = scanner.nextInt(); int arr[]=new int[n]; for (int i = 0; i <n ; i++) { arr[i]=scanner.nextInt(); } if (m < n||n==2) { System.out.println(-1); continue; } long sum=0; for (int i = 0; i <n ; i++) { sum+=arr[i]; } sum=sum*2; int temp=m-n; int min1=1000000; int min2=1000000; int pos1=-1,pos2=-1; if (temp > 0) { for (int i = 0; i < n; i++) { if (min1 > arr[i]) { min1 = arr[i]; pos1 = i; } } arr[pos1] = 1000000; for (int i = 0; i < n; i++) { if (min2 > arr[i]) { min2 = arr[i]; pos2 = i; arr[i] = 1000000; } } } System.out.println(sum+((min1+min2)*(m-n))); for (int i = 0; i <n-1 ; i++) { System.out.println((i+1) +" "+(i+2)); } System.out.println(1+" "+n); for (int i = 0; i <m-n ; i++) { System.out.println((pos1+1)+" "+(pos2+1)); } } }catch(Exception e){ e.printStackTrace(); return; } } static class Reader { final private int BUFFER_SIZE = 1 << 17; public DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader(InputStream in) { din = new DataInputStream(in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextString() throws Exception { StringBuffer sb = new StringBuffer(""); byte c = read(); while (c <= ' ') { c = read(); } do { sb.append((char) c); c = read(); } while (c > ' '); return sb.toString(); } public char nextChar() throws Exception { byte c = read(); while (c <= ' ') { c = read(); } return (char) c; } public int nextInt() throws Exception { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); return ret; } public void skipNext() throws Exception { byte c = read(); while (isSpaceChar(c)) { c = read(); } do { c = read(); } while (!isSpaceChar(c)); } private static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private void fillBuffer() throws Exception { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws Exception { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
6d85c63915fd50e0c0b029652e6caecb
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.util.*; public final class FridgeLockersB { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int t = in.nextInt(); while(t-->0) { int n = in.nextInt(); int m = in.nextInt(); int arr[] = new int[n+1]; for(int i =1; i<= n; i++) { arr[i] = in.nextInt(); } int p =0; if(m<n || n==2){ System.out.println("-1"); continue; } List<Pair> list = new ArrayList<>(); for(int i =1; i<=n; i++){ list.add(new Pair(arr[i], i)); } Collections.sort(list, (a,b)-> a.a - b.a); List<Pair> re = new ArrayList<>(); for(int i = 1; i<n; i++) re.add(new Pair(i, i+1)); re.add(new Pair(1, n)); m -=n; while(m-->0){ re.add(new Pair(list.get(0).b, list.get(1).b)); } for(Pair pp: re){ p += arr[pp.a] + arr[pp.b]; } System.out.println(p); for(Pair pp: re){ System.out.println(pp.a + " "+pp.b); } } } } class Pair{ int a; int b; Pair(int a, int b){ this.a =a; this.b = b; } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
bf706b1f1dba374e79cc4be0acddae58
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.*; import java.util.*; public class div2601b { static int[] arr = new int[1001]; static LinkedList<Integer> adj[] = new LinkedList[1001]; public static void main(String[] args) throws Exception { for (int i = 1; i <= 1000; i++) { adj[i] = new LinkedList<>(); for (int j = 1 ; j <+ 1000;j++) { adj[i].add(j); } } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int k = Integer.parseInt(br.readLine()); for (int i = 0; i < k; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int sum = 0; while(st.hasMoreTokens()) { sum+= Integer.parseInt(st.nextToken()); } if (m < n || n ==2) { System.out.println(-1); continue; } System.out.println(sum*2); int counter = 1; for (int j = 0; j < m;j++) { if (j==m-1) { System.out.println((j+1) + " " + 1); } else { System.out.println((j+1) + " " + (j+2)); } } } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
32cb36d1e107dde1b055f24e2f7322f5
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
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); PrintWriter out = new PrintWriter(System.out); int T = sc.nextInt(); for(int t = 0; t<T;t++){ int n = sc.nextInt(); int m = sc.nextInt(); int arr[] = new int[n]; int sum = 0; for(int i = 0; i<n; i++){ arr[i] = sc.nextInt(); } if(m < n || n == 2)out.println(-1); else{ for(int i = 1; i<n; i++){ sum += arr[i] + arr[i - 1]; } out.println(sum + arr[arr.length - 1] + arr[0]); for(int i = 2; i<=n; i++){ out.println((i - 1) + " " + i); } out.println(n + " " + 1); } } out.flush(); } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
a96e39e50830606f0afcca8ce20208c5
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.util.Scanner; public class CodeForces601B { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); for (int i = 0; i < T; i++) { int n = scanner.nextInt(); int m = scanner.nextInt(); int ans = 0; StringBuilder sb = new StringBuilder(); for (int j = 1; j <= n; j++) { ans += scanner.nextInt(); sb.append("\n").append(j).append(" ").append(j % n + 1); } ans = m < n || n == 2 ? -1 : ans * 2; System.out.println(ans); if (ans > -1) { System.out.println(sb.substring(1)); } } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
7849e7a48e3f505f6a483789f7e3c76b
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.Scanner; import java.util.Collections; import java.util.ArrayList; /** * 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); BFridgeLockers solver = new BFridgeLockers(); solver.solve(1, in, out); out.close(); } static class BFridgeLockers { public void solve(int testNumber, Scanner in, PrintWriter out) { int queries = in.nextInt(); for (int i = 0; i < queries; i++) { int n = in.nextInt(); int m = in.nextInt(); long sum = 0; List<Integer> weights = new ArrayList<>(); for (int j = 0; j < n; j++) { int x = in.nextInt(); weights.add(x); sum = sum + x; } if (m < n || m == 2) { out.println(-1); continue; } Collections.sort(weights); long result = (2 * sum) + ((m - n) * (weights.get(0) + weights.get(1))); out.println(result); int j; for (j = 0; j < n - 1; j++) { out.println((j + 1) + " " + (j + 2)); } out.println(1 + " " + n); } } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
95f39a69f2176404444afd126f0a1f6d
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Stack; import java.util.TreeMap; public class temp2 { static int mini; static int miniv; static long time = 0, mp = Integer.MAX_VALUE, k = 0, cnt = 0, edge = 0, no = 0; static int[] goal; static int[] init; static int[] col; static char[][] g; static String sb = ""; static ArrayList<Integer>[] a; static ArrayList<Integer> p = new ArrayList<>(); static int mod = (int) (1e9 + 7); public static void main(String[] args) throws IOException { Reader scn = new Reader(); int t = scn.nextInt(); while (t-- > 0) { int n = scn.nextInt(); int m = scn.nextInt(); pair a[] = new pair[n]; for (int i = 0; i < n; i++) { int x = scn.nextInt(); a[i] = new pair(x, i + 1); } Arrays.sort(a); StringBuilder sb = new StringBuilder(); int min1 = a[0].val; int min2 = a[1].val; if (n > m || n == 2) { System.out.println(-1); } else { int ct = 0; long ans = 0; int i = 0; while (i < n) { sb.append(a[i].idx + " " + a[(i + 1) % n].idx + "\n"); ans += a[i].val + a[(i + 1) % n].val; i++; ct++; } if (ct <= m) { for ( i = ct; i < m; i++) { sb.append(a[0].idx + " " + a[1].idx + "\n"); ans += min1 + min2; } System.out.println(ans); System.out.println(sb); } } } } public static class pair implements Comparable<pair> { int val; int idx; // int cnt; pair(int a, int b) { val = a; idx = b; // cnt = x; } @Override public int compareTo(pair o) { return this.val - o.val; } } // -----------PrintWriter for faster output--------------------------------- public static PrintWriter out; public 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[100000 + 1]; // 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(); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public int[][] nextInt2DArray(int m, int n) throws IOException { int[][] arr = new int[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) arr[i][j] = nextInt(); } return arr; } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
2ae20473ff681c219d2f3cd48b0551e1
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; import java.util.LinkedList; import java.util.Queue; public final class C { static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<ArrayList<Integer>> g; static long mod=1000000007; static boolean set[]; public static void main(String args[])throws IOException { int T=i(); while(T-->0) { int N=i(); int K=i(); long s=0; for(int i=0; i<N; i++) { s+=in.nextInt(); } s*=2; if(K<N || N==2) System.out.println(-1); else { System.out.println(s); ans.append(1+" "+N); for(int i=1; i<N; i++) { ans.append("\n"+i+" "+(i+1)); } System.out.println(ans); ans.delete(0,ans.length()); } } } static int sD(long n) { if (n % 2 == 0 ) return 2; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0 ) return i; } return (int)n; } static void setGraph(int N) { set=new boolean[N+1]; g=new ArrayList<ArrayList<Integer>>(); for(int i=0; i<=N; i++) g.add(new ArrayList<Integer>()); } static void DFS(int N,int d) { set[N]=true; d++; for(int i=0; i<g.get(N).size(); i++) { int c=g.get(N).get(i); if(set[c]==false) { DFS(c,d); } } } static long pow(long a,long b) { long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } } //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
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
d1ccb6c6519a85d92c556aecde68b2b5
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); for(int i=0;i<t;i++) { int n=s.nextInt(); int m=s.nextInt(); hold[] arr=new hold[n]; for(int j=0;j<n;j++) { hold h=new hold(); h.value=s.nextInt(); h.index=j; arr[j]=h; } Arrays.sort(arr,new comp()); if(n==2) { System.out.println(-1); } else { if(m<n) System.out.println(-1); else { long sum=0; for(int j=0;j<n-1;j++) { sum=sum+arr[j].value+arr[j+1].value; } sum=sum+arr[0].value+arr[n-1].value; System.out.println(sum); for(int j=1;j<n;j++) { System.out.println(j+" "+(j+1)); } System.out.println(1+" "+n); } } } } } class comp implements Comparator<hold> { public int compare(hold a,hold b) { if(a.value<b.value) return -1; else if(a.value==b.value) return 0; else return 1; } } class hold { int value; int index; } class pair { int a; int b; }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
a0e5ac10eef878423680274c29469e34
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner Sc=new Scanner(System.in); int T=Sc.nextInt(); for(int i=0;i<T;i++) { int n=Sc.nextInt(); int m=Sc.nextInt(); int a[]=new int[n]; int sum=0; for(int j=0;j<n;j++) { a[j]=Sc.nextInt(); sum=sum+a[j]; } if(m<n||n==2) System.out.println(-1); else { System.out.println(2*sum); for(int j=0;j<n;j++) if(j!=n-1) System.out.println((j+1)+" "+(j+2)); else System.out.println((j+1)+" 1"); } } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
2e426d01b6a937620f4178d3fd9a99d5
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.util.*; public class square { public static void main(String ar[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int k=1;k<=t;k++) { int n=sc.nextInt(),m=sc.nextInt(); int[] weights=new int[n+1]; for(int i=1;i<=n;i++)weights[i]=sc.nextInt(); if(m<n || n==2){System.out.println("-1");continue;} int sum=0;int minw1=Integer.MAX_VALUE,minw2=Integer.MAX_VALUE,mine1=0,mine2=0; for(int i=1;i<=n;i++) { sum+=weights[i]; if(minw1>weights[i]) { minw1=weights[i];mine1=i; } } sum*=2; for(int i=1;i<=n;i++) { if(i==mine1)continue; if(minw2>weights[i]) { minw2=weights[i];mine2=i; } } m-=n; sum+=m*(minw1+minw2); System.out.println(sum); for(int i=1;i<n;i++)System.out.println(i+" "+(i+1)); System.out.println(n+" "+1); while(m>0) { System.out.println(mine1+" "+mine2); m--; } } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
fd08f3c34575bd9219820efb1ab3814f
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.util.*; public class Mohammad { public static class xy{ int x; int y; xy(int x, int y){ this.x = x; this.y = y;} } public static void Mohammad_AboHasan(){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), m = sc.nextInt(), s = 0; int[] a = new int[n]; ArrayList<xy> l = new ArrayList(); for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); s += a[i]*2; l.add(new xy(a[i], i+1)); } Arrays.sort(a); if(m < n || n < 3){ System.out.println(-1); continue; } System.out.println(s); for (int i = 0; i < n-1; i++) System.out.println(l.get(i).y + " " + l.get(i+1).y); System.out.println(l.get(n-1).y + " " + 1); } } public static void main(String[] args){ Mohammad_AboHasan(); } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
bfa156b1a492f19c3ab499bd890415f2
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.util.*; public class b { static int mod = (int) 1e9 + 7; static int Infinity=Integer.MAX_VALUE; static int negInfinity=Integer.MIN_VALUE; public static void main(String args[]) { try { Scanner d= new Scanner(System.in); int t,i,n,j,m,s,k; t=d.nextInt(); for(i=0;i<t;i++) { n=d.nextInt(); m=d.nextInt(); s=0; int a[]=new int[n]; for(j=0;j<n;j++) a[j]=d.nextInt(); if(m<n || n==2) System.out.println(-1); else{ int min,min1; int i1,i2; i1=i2=-1; min=min1=Infinity; for(j=0;j<n;j++) { if(a[j]<min) { min=a[j]; i1=j; } } for(j=0;j<n;j++) { if(a[j]<min1 && a[j]>min) { min1=a[j]; i2=j; } } if(i2==-1) { for(j=0;j<n;j++) { if(j!=i1 && a[j]==min) { min1=a[j]; i2=j; } } } for(j=0;j<n-1;j++) { s=s+a[j]+a[j+1]; } s=s+a[n-1]+a[0]; j++; while(j<m) { s=s+a[i1]+a[i2]; j++; } System.out.println(s); for(j=0;j<n-1;j++) { System.out.println((j+1)+" "+(j+2)); } System.out.println(n+" "+1); j++; while(j<m) { System.out.println((i1+1)+" "+(i2+1)); j++; } } } } catch(Exception e) { System.out.println(0); } } void sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { // If prime[p] is not changed, then it is a prime if(prime[p] == true) { // Update all multiples of p for(int i = p*p; i <= n; i += p) prime[i] = false; } } for(int i = 2; i <= n; i++) { if(prime[i] == true) System.out.print(i + " "); } } static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
64fe4bcf8255cb64323ec4a82e7a9512
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.util.*; public class Solutions { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int test=sc.nextInt(); while(test>0) { int n=sc.nextInt(); int m=sc.nextInt(); int arr[]=new int [n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } if(m<n || m==2 || n==2) { System.out.println(-1); } else { int totalcost=0; for(int i=0;i<n;i++) { totalcost += arr[i]; } int j=1; System.out.println(totalcost*2); for(int i=1;i<m+1;i++) { if(i==j) { continue; } System.out.println(i+" "+j); j++; } System.out.println(m+" "+1); } test--; } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
68a7a4dbece7a20edc9055fbf5f392ad
train_002.jsonl
1574174100
Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.util.InputMismatchException; ///import ChangingVolums.InputReader; public class FridgeLocks { public static void main(String[] args) { // TODO Auto-generated method stub InputReader input =new InputReader(System.in); int t = input.nextInt(); while(t-->0) { int n = input.nextInt(); int m = input.nextInt(); long total_cost =0; int[] arr =new int[n]; int min = Integer.MAX_VALUE; int min1_index = -1; int min2_index = -1; int min2 = Integer.MAX_VALUE; long sum=0; for(int i=0; i<n;i++) { arr[i] = input.nextInt(); sum+=arr[i]; if(arr[i]<min) { min2 = min; min = arr[i]; min2_index = min1_index; min1_index = i; } else if(arr[i]<min2) { min2 = arr[i]; min2_index = i; } } if(n<=2) { System.out.println(-1); continue; } total_cost+=2*sum; if(m<n) { System.out.println(-1); continue; } total_cost+=(m-n)*(min+min2); System.out.println(total_cost); for(int i=1; i<n;i++) { System.out.println(i +" "+(i+1)); } System.out.println(n+" "+1); for(int i=0; i<m-n;i++) { System.out.println(min1_index+" "+min2_index); } } } 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 static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3"]
1 second
["8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"]
null
Java 8
standard input
[ "implementation", "graphs" ]
f6e219176e846b16c5f52dee81601c8e
Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.
1,100
For each test case: If there is no solution, print a single integer $$$-1$$$. Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.
standard output
PASSED
e7ce698aef2f862ae6da57983150aad4
train_002.jsonl
1554550500
You have received your birthday gifts — $$$n$$$ triples of integers! The $$$i$$$-th of them is $$$\lbrace a_{i}, b_{i}, c_{i} \rbrace$$$. All numbers are greater than or equal to $$$0$$$, and strictly smaller than $$$2^{k}$$$, where $$$k$$$ is a fixed integer.One day, you felt tired playing with triples. So you came up with three new integers $$$x$$$, $$$y$$$, $$$z$$$, and then formed $$$n$$$ arrays. The $$$i$$$-th array consists of $$$a_i$$$ repeated $$$x$$$ times, $$$b_i$$$ repeated $$$y$$$ times and $$$c_i$$$ repeated $$$z$$$ times. Thus, each array has length $$$(x + y + z)$$$.You want to choose exactly one integer from each array such that the XOR (bitwise exclusive or) of them is equal to $$$t$$$. Output the number of ways to choose the numbers for each $$$t$$$ between $$$0$$$ and $$$2^{k} - 1$$$, inclusive, modulo $$$998244353$$$.
256 megabytes
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.Charset; import java.util.Arrays; public class CF1119H { public static void main(String[] args) throws Exception { boolean local = System.getProperty("ONLINE_JUDGE") == null; boolean async = false; Charset charset = Charset.forName("ascii"); FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset); Task task = new Task(io, new Debug(local)); if (async) { Thread t = new Thread(null, task, "dalt", 1 << 27); t.setPriority(Thread.MAX_PRIORITY); t.start(); t.join(); } else { task.run(); } if (local) { io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M"); } io.flush(); } public static class Task implements Runnable { final FastIO io; final Debug debug; int inf = (int) 1e8; static int mod = 998244353; static long inv2 = Mathematics.inverse(2, mod); public Task(FastIO io, Debug debug) { this.io = io; this.debug = debug; } @Override public void run() { solve(); } public void solve() { int n = io.readInt(); int k = 1 << io.readInt(); long actualA = io.readInt(); long actualB = io.readInt(); long actualC = io.readInt(); int[][] points = new int[3][n]; int sumXor = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { points[j][i] = io.readInt(); } sumXor ^= points[0][i]; } long[][] ps = new long[4][k]; for (int i = 0; i < n; i++) { int p1 = 0; int p2 = points[0][i] ^ points[1][i]; int p3 = points[0][i] ^ points[2][i]; int p4 = p2 ^ p3; ps[0][p1] += 1; ps[1][p2] += 1; ps[2][p3] += 1; ps[3][p4] += 1; } for (int i = 0; i < 4; i++) { XorFWT(ps[i], 0, k - 1); } int[] p = new int[k]; for (int i = 0; i < k; i++) { long x = (ps[0][i] + ps[1][i] + ps[2][i] + ps[3][i]) / 4; long y = (ps[0][i] + ps[3][i]) / 2 - x; long z = (ps[0][i] + ps[1][i]) / 2 - x; long w = (ps[0][i] + ps[2][i]) / 2 - x; p[i] = 1; p[i] = (int) ((long) p[i] * Mathematics.pow(Mathematics.mod(actualA + actualB + actualC + 0, mod), Mathematics.mod(x, mod - 1), mod) % mod); p[i] = (int) ((long) p[i] * Mathematics.pow(Mathematics.mod(actualA - actualB - actualC + 0, mod), Mathematics.mod(y, mod - 1), mod) % mod); p[i] = (int) ((long) p[i] * Mathematics.pow(Mathematics.mod(actualA + actualB - actualC + 0, mod), Mathematics.mod(z, mod - 1), mod) % mod); p[i] = (int) ((long) p[i] * Mathematics.pow(Mathematics.mod(actualA - actualB + actualC + 0, mod), Mathematics.mod(w, mod - 1), mod) % mod); } XorIFWT(p, 0, k - 1); for (int i = 0; i < k; i++) { io.cache.append(p[i ^ sumXor]).append(' '); } } public static void XorFWT(long[] p, int l, int r) { if (l == r) { return; } int m = (l + r) >> 1; XorFWT(p, l, m); XorFWT(p, m + 1, r); for (int i = 0, until = m - l; i <= until; i++) { long a = p[l + i]; long b = p[m + 1 + i]; p[l + i] = a + b; p[m + 1 + i] = a - b; } } public static void XorIFWT(int[] p, int l, int r) { if (l == r) { return; } int m = (l + r) >> 1; for (int i = 0, until = m - l; i <= until; i++) { int a = p[l + i]; int b = p[m + 1 + i]; p[l + i] = Mathematics.mod((a + b) * inv2, mod); p[m + 1 + i] = Mathematics.mod((a - b) * inv2, mod); } XorIFWT(p, l, m); XorIFWT(p, m + 1, r); } } public static class Mathematics { public static int ceilPowerOf2(int x) { return 32 - Integer.numberOfLeadingZeros(x - 1); } public static int floorPowerOf2(int x) { return 31 - Integer.numberOfLeadingZeros(x); } public static long modmul(long a, long b, long mod) { return b == 0 ? 0 : ((modmul(a, b >> 1, mod) << 1) % mod + a * (b & 1)) % mod; } /** * Get the greatest common divisor of a and b */ public static int gcd(int a, int b) { return a >= b ? gcd0(a, b) : gcd0(b, a); } private static int gcd0(int a, int b) { return b == 0 ? a : gcd0(b, a % b); } public static int extgcd(int a, int b, int[] coe) { if (a >= b) { return extgcd0(a, b, coe); } else { int g = extgcd0(b, a, coe); int tmp = coe[0]; coe[0] = coe[1]; coe[1] = tmp; return g; } } private static int extgcd0(int a, int b, int[] coe) { if (b == 0) { coe[0] = 1; coe[1] = 0; return a; } int g = extgcd0(b, a % b, coe); int n = coe[0]; int m = coe[1]; coe[0] = m; coe[1] = n - m * (a / b); return g; } /** * Get the greatest common divisor of a and b */ public static long gcd(long a, long b) { return a >= b ? gcd0(a, b) : gcd0(b, a); } private static long gcd0(long a, long b) { return b == 0 ? a : gcd0(b, a % b); } public static long extgcd(long a, long b, long[] coe) { if (a >= b) { return extgcd0(a, b, coe); } else { long g = extgcd0(b, a, coe); long tmp = coe[0]; coe[0] = coe[1]; coe[1] = tmp; return g; } } private static long extgcd0(long a, long b, long[] coe) { if (b == 0) { coe[0] = 1; coe[1] = 0; return a; } long g = extgcd0(b, a % b, coe); long n = coe[0]; long m = coe[1]; coe[0] = m; coe[1] = n - m * (a / b); return g; } /** * Get y where x * y = 1 (% mod) */ public static int inverse(int x, int mod) { return pow(x, mod - 2, mod); } /** * Get x^n(% mod) */ public static int pow(int x, int n, int mod) { int bit = 31 - Integer.numberOfLeadingZeros(n); long product = 1; for (; bit >= 0; bit--) { product = product * product % mod; if (((1 << bit) & n) != 0) { product = product * x % mod; } } return (int) product; } public static long longpow(long x, long n, long mod) { if (n == 0) { return 1; } long prod = longpow(x, n >> 1, mod); prod = modmul(prod, prod, mod); if ((n & 1) == 1) { prod = modmul(prod, x, mod); } return prod; } /** * Get x % mod */ public static int mod(int x, int mod) { x %= mod; if (x < 0) { x += mod; } return x; } public static int mod(long x, int mod) { x %= mod; if (x < 0) { x += mod; } return (int) x; } /** * Get n!/(n-m)! */ public static long permute(int n, int m) { return m == 0 ? 1 : n * permute(n - 1, m - 1); } /** * Put all primes less or equal to limit into primes after offset */ public static int eulerSieve(int limit, int[] primes, int offset) { boolean[] isComp = new boolean[limit + 1]; int wpos = offset; for (int i = 2; i <= limit; i++) { if (!isComp[i]) { primes[wpos++] = i; } for (int j = offset, until = limit / i; j < wpos && primes[j] <= until; j++) { int pi = primes[j] * i; isComp[pi] = true; if (i % primes[j] == 0) { break; } } } return wpos - offset; } /** * Round x into integer */ public static int intRound(double x) { if (x < 0) { return -(int) (-x + 0.5); } return (int) (x + 0.5); } /** * Round x into long */ public static long longRound(double x) { if (x < 0) { return -(long) (-x + 0.5); } return (long) (x + 0.5); } } public static class FastIO { public final StringBuilder cache = new StringBuilder(); private final InputStream is; private final OutputStream os; private final Charset charset; private StringBuilder defaultStringBuf = new StringBuilder(1 << 8); private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastIO(InputStream is, OutputStream os, Charset charset) { this.is = is; this.os = os; this.charset = charset; } public FastIO(InputStream is, OutputStream os) { this(is, os, Charset.forName("ascii")); } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } 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 long readLong() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } long 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 double readDouble() { boolean sign = true; skipBlank(); if (next == '+' || next == '-') { sign = next == '+'; next = read(); } long val = 0; while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } if (next != '.') { return sign ? val : -val; } next = read(); long radix = 1; long point = 0; while (next >= '0' && next <= '9') { point = point * 10 + next - '0'; radix = radix * 10; next = read(); } double result = val + (double) point / radix; return sign ? result : -result; } 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); } public int readLine(char[] data, int offset) { int originalOffset = offset; while (next != -1 && next != '\n') { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(char[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(byte[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (byte) next; next = read(); } return offset - originalOffset; } public char readChar() { skipBlank(); char c = (char) next; next = read(); return c; } public void flush() { try { os.write(cache.toString().getBytes(charset)); os.flush(); cache.setLength(0); } catch (IOException e) { throw new RuntimeException(e); } } public boolean hasMore() { skipBlank(); return next != -1; } } public static class Debug { private boolean allowDebug; public Debug(boolean allowDebug) { this.allowDebug = allowDebug; } public void assertTrue(boolean flag) { if (!allowDebug) { return; } if (!flag) { fail(); } } public void fail() { throw new RuntimeException(); } public void assertFalse(boolean flag) { if (!allowDebug) { return; } if (flag) { fail(); } } private void outputName(String name) { System.out.print(name + " = "); } public void debug(String name, int x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, long x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, double x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, int[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, long[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, double[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, Object x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, Object... x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.deepToString(x)); } } }
Java
["1 1\n1 2 3\n1 0 1", "2 2\n1 2 1\n0 1 2\n1 2 3", "4 3\n1 2 3\n1 3 7\n0 2 5\n1 0 6\n3 3 2"]
1.5 seconds
["2 4", "4 2 4 6", "198 198 126 126 126 126 198 198"]
NoteIn the first example, the array we formed is $$$(1, 0, 0, 1, 1, 1)$$$, we have two choices to get $$$0$$$ as the XOR and four choices to get $$$1$$$.In the second example, two arrays are $$$(0, 1, 1, 2)$$$ and $$$(1, 2, 2, 3)$$$. There are sixteen $$$(4 \cdot 4)$$$ choices in total, $$$4$$$ of them ($$$1 \oplus 1$$$ and $$$2 \oplus 2$$$, two options for each) give $$$0$$$, $$$2$$$ of them ($$$0 \oplus 1$$$ and $$$2 \oplus 3$$$) give $$$1$$$, $$$4$$$ of them ($$$0 \oplus 2$$$ and $$$1 \oplus 3$$$, two options for each) give $$$2$$$, and finally $$$6$$$ of them ($$$0 \oplus 3$$$, $$$2 \oplus 1$$$ and four options for $$$1 \oplus 2$$$) give $$$3$$$.
Java 8
standard input
[ "math", "fft" ]
379a57ff01d337203bc2fc2cf9f63508
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^{5}$$$, $$$1 \leq k \leq 17$$$) — the number of arrays and the binary length of all numbers. The second line contains three integers $$$x$$$, $$$y$$$, $$$z$$$ ($$$0 \leq x,y,z \leq 10^{9}$$$) — the integers you chose. Then $$$n$$$ lines follow. The $$$i$$$-th of them contains three integers $$$a_{i}$$$, $$$b_{i}$$$ and $$$c_{i}$$$ ($$$0 \leq a_{i} , b_{i} , c_{i} \leq 2^{k} - 1$$$) — the integers forming the $$$i$$$-th array.
3,200
Print a single line containing $$$2^{k}$$$ integers. The $$$i$$$-th of them should be the number of ways to choose exactly one integer from each array so that their XOR is equal to $$$t = i-1$$$ modulo $$$998244353$$$.
standard output
PASSED
43d744df624f9f128ff4d8c7a5c9bfcd
train_002.jsonl
1554550500
You have received your birthday gifts — $$$n$$$ triples of integers! The $$$i$$$-th of them is $$$\lbrace a_{i}, b_{i}, c_{i} \rbrace$$$. All numbers are greater than or equal to $$$0$$$, and strictly smaller than $$$2^{k}$$$, where $$$k$$$ is a fixed integer.One day, you felt tired playing with triples. So you came up with three new integers $$$x$$$, $$$y$$$, $$$z$$$, and then formed $$$n$$$ arrays. The $$$i$$$-th array consists of $$$a_i$$$ repeated $$$x$$$ times, $$$b_i$$$ repeated $$$y$$$ times and $$$c_i$$$ repeated $$$z$$$ times. Thus, each array has length $$$(x + y + z)$$$.You want to choose exactly one integer from each array such that the XOR (bitwise exclusive or) of them is equal to $$$t$$$. Output the number of ways to choose the numbers for each $$$t$$$ between $$$0$$$ and $$$2^{k} - 1$$$, inclusive, modulo $$$998244353$$$.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class cfGlobal2H { static final int P = 998244353; static final int INV2 = (P + 1) / 2; void fwhtMod(int[] a) { int n = a.length; for (int s = 1; s < n; s <<= 1) { for (int i = 0; i < n; i += s << 1) { for (int j = i; j < i + s; j++) { int k = j + s; int v = a[j]; int u = a[k]; a[j] += u; if (a[j] >= P) { a[j] -= P; } a[k] = v - u; if (a[k] < 0) { a[k] += P; } } } } } static final int M = 3; static final boolean test(int mask, int i) { return ((mask >> i) & 1) == 1; } void submit() { int n = nextInt(); int k = nextInt(); int[] xs = new int[M]; for (int i = 0; i < M; i++) { xs[i] = nextInt(); } int[] arr = new int[1 << (k + M)]; for (int i = 0; i < n; i++) { int[] tmp = new int[M]; for (int j = 0; j < M; j++) { tmp[j] = nextInt(); } for (int mask = 0; mask < 1 << M; mask++) { int idx = mask << k; for (int j = 0; j < M; j++) { if (test(mask, j)) { idx ^= tmp[j]; } } // System.err.println(mask + " " + idx); arr[idx]++; } } fwhtMod(arr); int[][] ps = new int[1 << M][]; for (int i = 0; i < 1 << M; i++) { int sum = 0; for (int j = 0; j < M; j++) { if (test(i, j)) { sum -= xs[j]; if (sum < 0) { sum += P; } } else { sum += xs[j]; if (sum >= P) { sum -= P; } } } ps[i] = pows(sum, n); } int i2m = 1; for (int i = 0; i < M; i++) { i2m = (int) ((long) i2m * INV2 % P); } for (int i = 0; i < arr.length; i++) { arr[i] = (int) ((long) arr[i] * i2m % P); } int[] ans = new int[1 << k]; for (int i = 0; i < 1 << k; i++) { ans[i] = 1; for (int j = 0; j < 1 << M; j++) { ans[i] = (int) ((long) ans[i] * ps[j][arr[i | (j << k)]] % P); } } fwhtMod(ans); long inv2k = 1; for (int i = 0; i < k; i++) { inv2k = inv2k * INV2 % P; } for (int i = 0; i < ans.length; i++) { out.print(ans[i] * inv2k % P + " "); } out.println(); } int[] pows(int x, int b) { int[] ret = new int[b + 1]; ret[0] = 1; for (int i = 1; i < ret.length; i++) { ret[i] = (int) ((long) ret[i - 1] * x % P); } return ret; } int f(long x) { // return (int) Math.floorMod(x, P); x %= P; return (int) (x < 0 ? x + P : x); } void test() { } void stress() { for (int tst = 0;; tst++) { if (false) { throw new AssertionError(); } System.err.println(tst); } } cfGlobal2H() throws IOException { is = System.in; out = new PrintWriter(System.out); submit(); // stress(); // test(); out.close(); } static final Random rng = new Random(); static final int C = 5; static int rand(int l, int r) { return l + rng.nextInt(r - l + 1); } public static void main(String[] args) throws IOException { new cfGlobal2H(); } private InputStream is; PrintWriter out; private byte[] buf = new byte[1 << 14]; private int bufSz = 0, bufPtr = 0; private int readByte() { if (bufSz == -1) throw new RuntimeException("Reading past EOF"); if (bufPtr >= bufSz) { bufPtr = 0; try { bufSz = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufSz <= 0) return -1; } return buf[bufPtr++]; } private boolean isTrash(int c) { return c < 33 || c > 126; } private int skip() { int b; while ((b = readByte()) != -1 && isTrash(b)) ; return b; } String nextToken() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!isTrash(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextString() { int b = readByte(); StringBuilder sb = new StringBuilder(); while (!isTrash(b) || b == ' ') { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } double nextDouble() { return Double.parseDouble(nextToken()); } char nextChar() { return (char) skip(); } int nextInt() { int ret = 0; int b = skip(); if (b != '-' && (b < '0' || b > '9')) { throw new InputMismatchException(); } boolean neg = false; if (b == '-') { neg = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { ret = ret * 10 + (b - '0'); } else { if (b != -1 && !isTrash(b)) { throw new InputMismatchException(); } return neg ? -ret : ret; } b = readByte(); } } long nextLong() { long ret = 0; int b = skip(); if (b != '-' && (b < '0' || b > '9')) { throw new InputMismatchException(); } boolean neg = false; if (b == '-') { neg = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { ret = ret * 10 + (b - '0'); } else { if (b != -1 && !isTrash(b)) { throw new InputMismatchException(); } return neg ? -ret : ret; } b = readByte(); } } }
Java
["1 1\n1 2 3\n1 0 1", "2 2\n1 2 1\n0 1 2\n1 2 3", "4 3\n1 2 3\n1 3 7\n0 2 5\n1 0 6\n3 3 2"]
1.5 seconds
["2 4", "4 2 4 6", "198 198 126 126 126 126 198 198"]
NoteIn the first example, the array we formed is $$$(1, 0, 0, 1, 1, 1)$$$, we have two choices to get $$$0$$$ as the XOR and four choices to get $$$1$$$.In the second example, two arrays are $$$(0, 1, 1, 2)$$$ and $$$(1, 2, 2, 3)$$$. There are sixteen $$$(4 \cdot 4)$$$ choices in total, $$$4$$$ of them ($$$1 \oplus 1$$$ and $$$2 \oplus 2$$$, two options for each) give $$$0$$$, $$$2$$$ of them ($$$0 \oplus 1$$$ and $$$2 \oplus 3$$$) give $$$1$$$, $$$4$$$ of them ($$$0 \oplus 2$$$ and $$$1 \oplus 3$$$, two options for each) give $$$2$$$, and finally $$$6$$$ of them ($$$0 \oplus 3$$$, $$$2 \oplus 1$$$ and four options for $$$1 \oplus 2$$$) give $$$3$$$.
Java 8
standard input
[ "math", "fft" ]
379a57ff01d337203bc2fc2cf9f63508
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^{5}$$$, $$$1 \leq k \leq 17$$$) — the number of arrays and the binary length of all numbers. The second line contains three integers $$$x$$$, $$$y$$$, $$$z$$$ ($$$0 \leq x,y,z \leq 10^{9}$$$) — the integers you chose. Then $$$n$$$ lines follow. The $$$i$$$-th of them contains three integers $$$a_{i}$$$, $$$b_{i}$$$ and $$$c_{i}$$$ ($$$0 \leq a_{i} , b_{i} , c_{i} \leq 2^{k} - 1$$$) — the integers forming the $$$i$$$-th array.
3,200
Print a single line containing $$$2^{k}$$$ integers. The $$$i$$$-th of them should be the number of ways to choose exactly one integer from each array so that their XOR is equal to $$$t = i-1$$$ modulo $$$998244353$$$.
standard output
PASSED
de0659a72237e9e71c538a8898831f37
train_002.jsonl
1554550500
You have received your birthday gifts — $$$n$$$ triples of integers! The $$$i$$$-th of them is $$$\lbrace a_{i}, b_{i}, c_{i} \rbrace$$$. All numbers are greater than or equal to $$$0$$$, and strictly smaller than $$$2^{k}$$$, where $$$k$$$ is a fixed integer.One day, you felt tired playing with triples. So you came up with three new integers $$$x$$$, $$$y$$$, $$$z$$$, and then formed $$$n$$$ arrays. The $$$i$$$-th array consists of $$$a_i$$$ repeated $$$x$$$ times, $$$b_i$$$ repeated $$$y$$$ times and $$$c_i$$$ repeated $$$z$$$ times. Thus, each array has length $$$(x + y + z)$$$.You want to choose exactly one integer from each array such that the XOR (bitwise exclusive or) of them is equal to $$$t$$$. Output the number of ways to choose the numbers for each $$$t$$$ between $$$0$$$ and $$$2^{k} - 1$$$, inclusive, modulo $$$998244353$$$.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; public class cfGlobal2H { static final int P = 998244353; static final int INV2 = (P + 1) / 2; void fwhtJust(int[] a) { int n = a.length; for (int s = 1; s < n; s <<= 1) { for (int i = 0; i < n; i += s << 1) { for (int j = i; j < i + s; j++) { int k = j + s; int v = a[j]; int u = a[k]; a[j] += u; // if (a[j] >= P) { // a[j] -= P; // } a[k] = v - u; // if (a[k] < 0) { // a[k] += P; // } } } } } void fwhtMod(int[] a) { int n = a.length; for (int s = 1; s < n; s <<= 1) { for (int i = 0; i < n; i += s << 1) { for (int j = i; j < i + s; j++) { int k = j + s; int v = a[j]; int u = a[k]; a[j] += u; if (a[j] >= P) { a[j] -= P; } a[k] = v - u; if (a[k] < 0) { a[k] += P; } } } } } void submit() { int n = nextInt(); int k = nextInt(); long x = nextInt(); long y = nextInt(); long z = nextInt(); int[] m1 = new int[n]; int[] m2 = new int[n]; int xorAll = 0; for (int i = 0; i < n; i++) { int a = nextInt(); int b = nextInt(); int c = nextInt(); xorAll ^= a; m1[i] = a ^ b; m2[i] = a ^ c; } int[] justB = new int[1 << k]; int[] justC = new int[1 << k]; int[] justXor = new int[1 << k]; for (int i = 0; i < n; i++) { justB[m1[i]]++; justC[m2[i]]++; justXor[m1[i] ^ m2[i]]++; } fwhtJust(justB); fwhtJust(justC); fwhtJust(justXor); int[][] ps = { pows(f(x + y + z), n), pows(f(x - y + z), n), pows(f(x + y - z), n), pows(f(x - y - z), n) }; int[] arr = new int[1 << k]; for (int i = 0; i < 1 << k; i++) { int[] xs = solve(n, justB[i], justC[i], justXor[i]); arr[i] = 1; for (int j = 0; j < 4; j++) { arr[i] = (int) ((long) arr[i] * ps[j][xs[j]] % P); } } fwhtMod(arr); long inv2k = 1; for (int i = 0; i < k; i++) { inv2k = inv2k * INV2 % P; } for (int i = 0; i < arr.length; i++) { out.print(arr[i ^ xorAll] * inv2k % P + " "); } out.println(); } int[] solve(int all, int yw, int zw, int yz) { yw = (all - yw) / 2; zw = (all - zw) / 2; yz = (all - yz) / 2; int yzw = (yw + zw + yz) / 2; int y = yzw - zw; int z = yzw - yw; int w = yzw - yz; int x = all - y - z - w; return new int[] { x, y, z, w }; } int[] pows(int x, int b) { int[] ret = new int[b + 1]; ret[0] = 1; for (int i = 1; i < ret.length; i++) { ret[i] = (int) ((long) ret[i - 1] * x % P); } return ret; } int f(long x) { return (int) Math.floorMod(x, P); } void test() { } void stress() { for (int tst = 0;; tst++) { if (false) { throw new AssertionError(); } System.err.println(tst); } } cfGlobal2H() throws IOException { is = System.in; out = new PrintWriter(System.out); submit(); // stress(); // test(); out.close(); } static final Random rng = new Random(); static final int C = 5; static int rand(int l, int r) { return l + rng.nextInt(r - l + 1); } public static void main(String[] args) throws IOException { new cfGlobal2H(); } private InputStream is; PrintWriter out; private byte[] buf = new byte[1 << 14]; private int bufSz = 0, bufPtr = 0; private int readByte() { if (bufSz == -1) throw new RuntimeException("Reading past EOF"); if (bufPtr >= bufSz) { bufPtr = 0; try { bufSz = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufSz <= 0) return -1; } return buf[bufPtr++]; } private boolean isTrash(int c) { return c < 33 || c > 126; } private int skip() { int b; while ((b = readByte()) != -1 && isTrash(b)) ; return b; } String nextToken() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!isTrash(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextString() { int b = readByte(); StringBuilder sb = new StringBuilder(); while (!isTrash(b) || b == ' ') { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } double nextDouble() { return Double.parseDouble(nextToken()); } char nextChar() { return (char) skip(); } int nextInt() { int ret = 0; int b = skip(); if (b != '-' && (b < '0' || b > '9')) { throw new InputMismatchException(); } boolean neg = false; if (b == '-') { neg = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { ret = ret * 10 + (b - '0'); } else { if (b != -1 && !isTrash(b)) { throw new InputMismatchException(); } return neg ? -ret : ret; } b = readByte(); } } long nextLong() { long ret = 0; int b = skip(); if (b != '-' && (b < '0' || b > '9')) { throw new InputMismatchException(); } boolean neg = false; if (b == '-') { neg = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { ret = ret * 10 + (b - '0'); } else { if (b != -1 && !isTrash(b)) { throw new InputMismatchException(); } return neg ? -ret : ret; } b = readByte(); } } }
Java
["1 1\n1 2 3\n1 0 1", "2 2\n1 2 1\n0 1 2\n1 2 3", "4 3\n1 2 3\n1 3 7\n0 2 5\n1 0 6\n3 3 2"]
1.5 seconds
["2 4", "4 2 4 6", "198 198 126 126 126 126 198 198"]
NoteIn the first example, the array we formed is $$$(1, 0, 0, 1, 1, 1)$$$, we have two choices to get $$$0$$$ as the XOR and four choices to get $$$1$$$.In the second example, two arrays are $$$(0, 1, 1, 2)$$$ and $$$(1, 2, 2, 3)$$$. There are sixteen $$$(4 \cdot 4)$$$ choices in total, $$$4$$$ of them ($$$1 \oplus 1$$$ and $$$2 \oplus 2$$$, two options for each) give $$$0$$$, $$$2$$$ of them ($$$0 \oplus 1$$$ and $$$2 \oplus 3$$$) give $$$1$$$, $$$4$$$ of them ($$$0 \oplus 2$$$ and $$$1 \oplus 3$$$, two options for each) give $$$2$$$, and finally $$$6$$$ of them ($$$0 \oplus 3$$$, $$$2 \oplus 1$$$ and four options for $$$1 \oplus 2$$$) give $$$3$$$.
Java 8
standard input
[ "math", "fft" ]
379a57ff01d337203bc2fc2cf9f63508
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^{5}$$$, $$$1 \leq k \leq 17$$$) — the number of arrays and the binary length of all numbers. The second line contains three integers $$$x$$$, $$$y$$$, $$$z$$$ ($$$0 \leq x,y,z \leq 10^{9}$$$) — the integers you chose. Then $$$n$$$ lines follow. The $$$i$$$-th of them contains three integers $$$a_{i}$$$, $$$b_{i}$$$ and $$$c_{i}$$$ ($$$0 \leq a_{i} , b_{i} , c_{i} \leq 2^{k} - 1$$$) — the integers forming the $$$i$$$-th array.
3,200
Print a single line containing $$$2^{k}$$$ integers. The $$$i$$$-th of them should be the number of ways to choose exactly one integer from each array so that their XOR is equal to $$$t = i-1$$$ modulo $$$998244353$$$.
standard output
PASSED
6790fdd002ae07ba11cf29c621dbb6e0
train_002.jsonl
1590935700
This is an interactive problem.Ayush devised a new scheme to set the password of his lock. The lock has $$$k$$$ slots where each slot can hold integers from $$$1$$$ to $$$n$$$. The password $$$P$$$ is a sequence of $$$k$$$ integers each in the range $$$[1, n]$$$, $$$i$$$-th element of which goes into the $$$i$$$-th slot of the lock.To set the password of his lock, Ayush comes up with an array $$$A$$$ of $$$n$$$ integers each in the range $$$[1, n]$$$ (not necessarily distinct). He then picks $$$k$$$ non-empty mutually disjoint subsets of indices $$$S_1, S_2, ..., S_k$$$ $$$(S_i \underset{i \neq j} \cap S_j = \emptyset)$$$ and sets his password as $$$P_i = \max\limits_{j \notin S_i} A[j]$$$. In other words, the $$$i$$$-th integer in the password is equal to the maximum over all elements of $$$A$$$ whose indices do not belong to $$$S_i$$$.You are given the subsets of indices chosen by Ayush. You need to guess the password. To make a query, you can choose a non-empty subset of indices of the array and ask the maximum of all elements of the array with index in this subset. You can ask no more than 12 queries.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; public class Round646 { 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(); } } public static long[] initArray(int n, Reader scan) throws IOException { long arr[] = new long[n]; for (int i = 0; i < n; i++) { arr[i] = scan.nextLong(); } return arr; } public static long sum(long arr[]) { long sum = 0; for (long i : arr) { sum += (long) i; } return sum; } public static long max(long arr[]) { long max = Long.MIN_VALUE; for (long i : arr) { max = Math.max(i, max); } return max; } public static long min(long arr[]) { long min = Long.MAX_VALUE; for (long i : arr) { min = Math.min(i, min); } return min; } public static List<Integer>[] initAdjacency(int n, int e, Reader scan, boolean type) throws IOException{ List<Integer> adj[]=new ArrayList[n+1]; for(int i=0;i<e;i++) { int u=scan.nextInt(); int v=scan.nextInt(); if(adj[u]==null) adj[u]=new ArrayList<>(); if(type&&adj[v]==null) adj[v]=new ArrayList<>(); adj[u].add(v); if(type) adj[v].add(u); } return adj; } public static void main(String[] args) throws IOException { Scanner scan=new Scanner(System.in); int t=scan.nextInt(); while(t-->0) { int n=scan.nextInt(); int k=scan.nextInt(); String ans= D(n, k, scan); } } public static void A(long arr[], int n, int k) { int cO=0; int cE=0; for(long x: arr) { if(x%2==0) { cE++; }else { cO++; } } if(cO==0) { System.out.println("No"); return; } for(int i=1;i<=Math.min(k,cO);i+=2) { if(cE>=(k-i)) { System.out.println("Yes"); return; } } System.out.println("No"); } public static long B(String s) { long c1[]=new long[s.length()]; long c0[]=new long[s.length()]; if(s.charAt(0)=='1') { c1[0]=1; }else{ c0[0]=1; } for(int i=1;i<s.length();i++) { if(s.charAt(i)=='1') { c1[i]=1; }else { c0[i]=1; } c1[i]+=c1[i-1]; c0[i]+=c0[i-1]; } // System.out.println(Arrays.toString(c0)); // System.out.println(Arrays.toString(c1)); long min=Integer.MAX_VALUE; int n=s.length(); for(int i=0;i<s.length();i++) { long ans1= c0[i]+ c1[n-1]-c1[i]; long ans2= c1[i]+c0[n-1]-c0[i]; min=Math.min(min, Math.min(ans1, ans2)); } return min; } public static void C(int n, Reader scan, int x) throws IOException { List<Integer> adj[]=new ArrayList[n+1]; for(int i=0;i<n-1;i++) { int u=scan.nextInt(); int v=scan.nextInt(); if(adj[u]==null) adj[u]=new ArrayList<>(); if(adj[v]==null) adj[v]=new ArrayList<>(); adj[u].add(v); adj[v].add(u); } if(n==1) { System.out.println("Ayush"); return; } if(adj[x].size()==1||adj[x].size()==0) { System.out.println("Ayush"); return; } int turn=0; int val= n- 3; turn= val%2; if(turn==0) { System.out.println("Ashish"); }else { System.out.println("Ayush"); } } public static String D(int n, int k, Scanner scan) { Set<Integer> idx[]=new HashSet[k]; for(int i=0;i<k;i++) { idx[i]=new HashSet<>(); int num=scan.nextInt(); for(int j=0;j<num;j++) { idx[i].add(scan.nextInt()); } } int max=0; System.out.print("? "+n+" "); for(int i=1;i<=n;i++) { System.out.print(i+" "); } System.out.println(); System.out.println(); max=scan.nextInt(); System.out.flush(); int l=1; int h=n; while(l<h) { int mid= l+(h-l)/2; System.out.print("? "+(mid-l+1)+" "); for(int i=l;i<=mid;i++) { System.out.print(i+" "); } System.out.println(); System.out.println(); int curMax= scan.nextInt(); System.out.flush(); if(curMax==max) { h=mid; }else { l=mid+1; } } int val=max; int ind=0; for(;ind<k;ind++) { if(idx[ind].contains(l)) { break; } } if(ind==k) { System.out.print("! "); for(int i=0;i<k;i++) { System.out.print(max+" "); } System.out.println(); System.out.println(); String s=scan.next(); System.out.flush(); return s; } System.out.print("? "+(n-idx[ind].size())+" "); for(int j=1;j<=n;j++) { if(idx[ind].contains(j)) continue; System.out.print(j+" "); } System.out.println(); System.out.println(); val=scan.nextInt(); System.out.flush(); System.out.print("! "); for(int i=0;i<k;i++) { if(i==ind) { System.out.print(val+" "); }else { System.out.print(max+" "); } } System.out.println(); System.out.println(); String s=scan.next(); System.out.flush(); return s; } }
Java
["1\n4 2\n2 1 3\n2 2 4\n\n1\n\n2\n\n3\n\n4\n\nCorrect"]
2 seconds
["? 1 1\n\n? 1 2\n\n? 1 3\n\n? 1 4\n\n! 4 3"]
NoteThe array $$$A$$$ in the example is $$$[1, 2, 3, 4]$$$. The length of the password is $$$2$$$. The first element of the password is the maximum of $$$A[2]$$$, $$$A[4]$$$ (since the first subset contains indices $$$1$$$ and $$$3$$$, we take maximum over remaining indices). The second element of the password is the maximum of $$$A[1]$$$, $$$A[3]$$$ (since the second subset contains indices $$$2$$$, $$$4$$$).Do not forget to read the string "Correct" / "Incorrect" after guessing the password.
Java 11
standard input
[ "math", "binary search", "implementation", "interactive" ]
b0bce8524eb69b695edc1394ff86b913
The first line of the input contains a single integer $$$t$$$ $$$(1 \leq t \leq 10)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ $$$(2 \leq n \leq 1000, 1 \leq k \leq n)$$$ — the size of the array and the number of subsets. $$$k$$$ lines follow. The $$$i$$$-th line contains an integer $$$c$$$ $$$(1 \leq c \lt n)$$$ — the size of subset $$$S_i$$$, followed by $$$c$$$ distinct integers in the range $$$[1, n]$$$  — indices from the subset $$$S_i$$$. It is guaranteed that the intersection of any two subsets is empty.
2,100
null
standard output
PASSED
8a658e3a8977fce862be74ba25760706
train_002.jsonl
1590935700
This is an interactive problem.Ayush devised a new scheme to set the password of his lock. The lock has $$$k$$$ slots where each slot can hold integers from $$$1$$$ to $$$n$$$. The password $$$P$$$ is a sequence of $$$k$$$ integers each in the range $$$[1, n]$$$, $$$i$$$-th element of which goes into the $$$i$$$-th slot of the lock.To set the password of his lock, Ayush comes up with an array $$$A$$$ of $$$n$$$ integers each in the range $$$[1, n]$$$ (not necessarily distinct). He then picks $$$k$$$ non-empty mutually disjoint subsets of indices $$$S_1, S_2, ..., S_k$$$ $$$(S_i \underset{i \neq j} \cap S_j = \emptyset)$$$ and sets his password as $$$P_i = \max\limits_{j \notin S_i} A[j]$$$. In other words, the $$$i$$$-th integer in the password is equal to the maximum over all elements of $$$A$$$ whose indices do not belong to $$$S_i$$$.You are given the subsets of indices chosen by Ayush. You need to guess the password. To make a query, you can choose a non-empty subset of indices of the array and ask the maximum of all elements of the array with index in this subset. You can ask no more than 12 queries.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.Array; import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; public class SolutionD extends Thread { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static final FastReader scanner = new FastReader(); private static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { new Thread(null, new SolutionD(), "Main", 1 << 26).start(); } public void run() { int t = scanner.nextInt(); for (int i = 0; i < t; i++) { solve(); } out.close(); } private static void solve() { int n = scanner.nextInt(); int k = scanner.nextInt(); List<Integer>[] subset = new List[k]; for (int i = 0; i < k; i++) { int ci = scanner.nextInt(); List<Integer> elements = new ArrayList<>(); for (int j = 0; j < ci; j++) { int el = scanner.nextInt(); elements.add(el); } subset[i] = elements; } int max = -1; System.out.println("? " + n + " " + IntStream.rangeClosed(1, n).mapToObj(String::valueOf).collect(Collectors.joining(" "))); max = scanner.nextInt(); if (k == 1) { System.out.println("? " + (n - subset[0].size()) + " " + IntStream.rangeClosed(1, n) .filter(i -> !subset[0].contains(i)) .mapToObj(String::valueOf) .collect(Collectors.joining(" "))); int secondMax = scanner.nextInt(); System.out.println("! " + secondMax); scanner.next(); return; } int low = 1; int high = n+2; while (high - low > 1) { int mid = (low + high) / 2; System.out.println("? " + (mid - low) + " " + IntStream.range(low, mid).mapToObj(String::valueOf).collect(Collectors.joining(" "))); int maxInRange = scanner.nextInt(); if (maxInRange == max) { high = mid; } else { low = mid; } } int indexSetWithoutMax = -1; for (int i = 0; i < k; i++) { for (int j = 0; j < subset[i].size(); j++) { if (subset[i].get(j) == low) { indexSetWithoutMax = i; break; } } } if (indexSetWithoutMax == -1) { int finalMax = max; System.out.println("! " + IntStream.range(0, k).mapToObj(i -> String.valueOf(finalMax)).collect(Collectors.joining(" "))); scanner.next(); return; } int finalIndexSetWithoutMax = indexSetWithoutMax; System.out.println("? " + (n - subset[indexSetWithoutMax].size()) + " " + IntStream.rangeClosed(1, n) .filter(i -> !subset[finalIndexSetWithoutMax].contains(i)) .mapToObj(String::valueOf) .collect(Collectors.joining(" "))); int secondMax = scanner.nextInt(); List<Integer> output = new ArrayList<>(); for (int i = 0; i < k; i++) { if (i == finalIndexSetWithoutMax) { output.add(secondMax); } else { output.add(max); } } System.out.println("! " + output.stream().map(String::valueOf).collect(Collectors.joining(" "))); String guessVerdict = scanner.next(); } }
Java
["1\n4 2\n2 1 3\n2 2 4\n\n1\n\n2\n\n3\n\n4\n\nCorrect"]
2 seconds
["? 1 1\n\n? 1 2\n\n? 1 3\n\n? 1 4\n\n! 4 3"]
NoteThe array $$$A$$$ in the example is $$$[1, 2, 3, 4]$$$. The length of the password is $$$2$$$. The first element of the password is the maximum of $$$A[2]$$$, $$$A[4]$$$ (since the first subset contains indices $$$1$$$ and $$$3$$$, we take maximum over remaining indices). The second element of the password is the maximum of $$$A[1]$$$, $$$A[3]$$$ (since the second subset contains indices $$$2$$$, $$$4$$$).Do not forget to read the string "Correct" / "Incorrect" after guessing the password.
Java 11
standard input
[ "math", "binary search", "implementation", "interactive" ]
b0bce8524eb69b695edc1394ff86b913
The first line of the input contains a single integer $$$t$$$ $$$(1 \leq t \leq 10)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ $$$(2 \leq n \leq 1000, 1 \leq k \leq n)$$$ — the size of the array and the number of subsets. $$$k$$$ lines follow. The $$$i$$$-th line contains an integer $$$c$$$ $$$(1 \leq c \lt n)$$$ — the size of subset $$$S_i$$$, followed by $$$c$$$ distinct integers in the range $$$[1, n]$$$  — indices from the subset $$$S_i$$$. It is guaranteed that the intersection of any two subsets is empty.
2,100
null
standard output
PASSED
a2bc4fe2ec2325b312bafe8cec1e782a
train_002.jsonl
1590935700
This is an interactive problem.Ayush devised a new scheme to set the password of his lock. The lock has $$$k$$$ slots where each slot can hold integers from $$$1$$$ to $$$n$$$. The password $$$P$$$ is a sequence of $$$k$$$ integers each in the range $$$[1, n]$$$, $$$i$$$-th element of which goes into the $$$i$$$-th slot of the lock.To set the password of his lock, Ayush comes up with an array $$$A$$$ of $$$n$$$ integers each in the range $$$[1, n]$$$ (not necessarily distinct). He then picks $$$k$$$ non-empty mutually disjoint subsets of indices $$$S_1, S_2, ..., S_k$$$ $$$(S_i \underset{i \neq j} \cap S_j = \emptyset)$$$ and sets his password as $$$P_i = \max\limits_{j \notin S_i} A[j]$$$. In other words, the $$$i$$$-th integer in the password is equal to the maximum over all elements of $$$A$$$ whose indices do not belong to $$$S_i$$$.You are given the subsets of indices chosen by Ayush. You need to guess the password. To make a query, you can choose a non-empty subset of indices of the array and ask the maximum of all elements of the array with index in this subset. You can ask no more than 12 queries.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; public class D { public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb; int t = Integer.parseInt(bf.readLine()); for (int i = 0; i < t; i++) { StringTokenizer st = new StringTokenizer(bf.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); //ArrayList<ArrayList<Integer>> subsets = new ArrayList<ArrayList<Integer>>(); ArrayList<Node> subsets = new ArrayList<Node>(); for (int j = 0; j < k; j++) { st = new StringTokenizer(bf.readLine()); Node cur = new Node(j); subsets.add(cur); int c = Integer.parseInt(st.nextToken()); for (int l = 0; l < c; l++) { cur.subset.add(Integer.parseInt(st.nextToken())); } } // now build complete tree ArrayList<Node> curLevel = subsets; while (curLevel.size() > 2) { ArrayList<Node> newLevel = new ArrayList<Node>(); for (int j = 0; j < curLevel.size(); j += 2) { Node left = curLevel.get(j); if (j + 1 < curLevel.size()) { Node right = curLevel.get(j + 1); Node merged = new Node(-1); merged.left = left; merged.right = right; for (Integer val : left.subset) { merged.subset.add(val); } for (Integer val : right.subset) { merged.subset.add(val); } newLevel.add(merged); } else { newLevel.add(left); } } curLevel = newLevel; } // cur level size is now 2 // can begin queries int[] password = new int[k]; int numRevealed = 0; Node left = curLevel.get(0); Node right = (curLevel.size() > 1 ? curLevel.get(1) : null); int fromAbove = 0; int maxUnused = -1; sb = new StringBuilder(); sb.append("? "); /*sb.append(left.subset.size() + right.subset.size()); for (int j : left.subset) { sb.append(" "); sb.append(j); } for (int j : right.subset) { sb.append(" "); sb.append(j); }*/ sb.append(n); for (int j = 0; j < n; j++) { sb.append(" "); sb.append(j + 1); } System.out.println(sb.toString()); System.out.flush(); fromAbove = Integer.parseInt(bf.readLine()); Node newLeft; Node newRight; Node last = left; if (right != null) { while (left != null) { while (right == null) { newLeft = left.left; newRight = left.right; left = newLeft; right = newRight; } // query left sb = new StringBuilder(); sb.append("? "); sb.append(left.subset.size()); for (int subn : left.subset) { sb.append(" "); sb.append(subn); } System.out.println(sb.toString()); System.out.flush(); int result = Integer.parseInt(bf.readLine()); if (result < fromAbove) { if (result > maxUnused) maxUnused = result; // everything on left gets set to from above numRevealed += propagate(left, password, fromAbove); // don't change from above last = right; newLeft = last.left; newRight = last.right; } else { // everything on right gets set to from above numRevealed += propagate(right, password, fromAbove); // also don't change from above?? last = left; newLeft = last.left; newRight = last.right; } left = newLeft; right = newRight; } } // now just need to uncover value of last //if (maxUnused == -1) { // spend extra query to find it out? if (true) { boolean[] seen = new boolean[n]; for (int j : last.subset) { seen[j-1] = true; } ArrayList<Integer> finalQ = new ArrayList<Integer>(); for (int j = 0; j < n; j++) { if (!seen[j]) { finalQ.add(j+1); } } /*for (Node node : subsets) { if (node.v != last.v) { for (Integer aya : node.subset) { finalQ.add(aya); } } }*/ sb = new StringBuilder(); sb.append("? "); sb.append(finalQ.size()); for (int subn : finalQ) { sb.append(" "); sb.append(subn); } System.out.println(sb.toString()); System.out.flush(); maxUnused = Integer.parseInt(bf.readLine()); } password[last.v] = maxUnused; sb = new StringBuilder(); sb.append("!"); for (int j = 0; j < k; j++) { sb.append(" "); sb.append(password[j]); } System.out.println(sb.toString()); System.out.flush(); bf.readLine(); } System.exit(0); } public static int propagate(Node cur, int[] password, int value) { if (cur.v != -1) { password[cur.v] = value; return 1; } int r = 0; if (cur.left != null) { r += propagate(cur.left, password, value); } if (cur.right != null) { r += propagate(cur.right, password, value); } return r; } } class Node { public int v; public ArrayList<Integer> subset; public Node left; public Node right; public Node(int v) { this.v = v; this.subset = new ArrayList<Integer>(); this.left = null; this.right = null; } }
Java
["1\n4 2\n2 1 3\n2 2 4\n\n1\n\n2\n\n3\n\n4\n\nCorrect"]
2 seconds
["? 1 1\n\n? 1 2\n\n? 1 3\n\n? 1 4\n\n! 4 3"]
NoteThe array $$$A$$$ in the example is $$$[1, 2, 3, 4]$$$. The length of the password is $$$2$$$. The first element of the password is the maximum of $$$A[2]$$$, $$$A[4]$$$ (since the first subset contains indices $$$1$$$ and $$$3$$$, we take maximum over remaining indices). The second element of the password is the maximum of $$$A[1]$$$, $$$A[3]$$$ (since the second subset contains indices $$$2$$$, $$$4$$$).Do not forget to read the string "Correct" / "Incorrect" after guessing the password.
Java 11
standard input
[ "math", "binary search", "implementation", "interactive" ]
b0bce8524eb69b695edc1394ff86b913
The first line of the input contains a single integer $$$t$$$ $$$(1 \leq t \leq 10)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ $$$(2 \leq n \leq 1000, 1 \leq k \leq n)$$$ — the size of the array and the number of subsets. $$$k$$$ lines follow. The $$$i$$$-th line contains an integer $$$c$$$ $$$(1 \leq c \lt n)$$$ — the size of subset $$$S_i$$$, followed by $$$c$$$ distinct integers in the range $$$[1, n]$$$  — indices from the subset $$$S_i$$$. It is guaranteed that the intersection of any two subsets is empty.
2,100
null
standard output