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
7d9baf94bdd80986235683a4505169de
train_003.jsonl
1566743700
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: $$$f(0) = a$$$; $$$f(1) = b$$$; $$$f(n) = f(n-1) \oplus f(n-2)$$$ when $$$n > 1$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. You are given three integers $$$a$$$, $$$b$$$, and $$$n$$$, calculate $$$f(n)$$$.You have to answer for $$$T$$$ independent test cases.
256 megabytes
import java.util.Scanner; public class solution{ public static void main(String[] args){ Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-->0){ int a = s.nextInt(); int b = s.nextInt(); int n = s.nextInt(); int rem = n%3; if(rem==0){ System.out.println(a); }else if(rem==1){ System.out.println(b); }else{ System.out.println(a^b); } } } }
Java
["3\n3 4 2\n4 5 0\n325 265 1231232"]
1 second
["7\n4\n76"]
NoteIn the first example, $$$f(2) = f(0) \oplus f(1) = 3 \oplus 4 = 7$$$.
Java 8
standard input
[ "math" ]
64a375c7c49591c676dbdb039c93d218
The input contains one or more independent test cases. The first line of input contains a single integer $$$T$$$ ($$$1 \le T \le 10^3$$$), the number of test cases. Each of the $$$T$$$ following lines contains three space-separated integers $$$a$$$, $$$b$$$, and $$$n$$$ ($$$0 \le a, b, n \le 10^9$$$) respectively.
900
For each test case, output $$$f(n)$$$.
standard output
PASSED
67c2787874a65141ec74f8b2d85a4839
train_003.jsonl
1566743700
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: $$$f(0) = a$$$; $$$f(1) = b$$$; $$$f(n) = f(n-1) \oplus f(n-2)$$$ when $$$n > 1$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. You are given three integers $$$a$$$, $$$b$$$, and $$$n$$$, calculate $$$f(n)$$$.You have to answer for $$$T$$$ independent test cases.
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 Tanzim Ibn Patowary */ 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 { public void solve(int testNumber, Scanner in, PrintWriter out) { for (int test = 0; test < testNumber; test++) { int n = in.nextInt(); while (n-- != 0) { int a = in.nextInt(); int b = in.nextInt(); int x = in.nextInt(); if (x % 3 == 0) { out.println(a); } else if (x % 3 == 1) { out.println(b); } else { out.println(a ^ b); } } } } } }
Java
["3\n3 4 2\n4 5 0\n325 265 1231232"]
1 second
["7\n4\n76"]
NoteIn the first example, $$$f(2) = f(0) \oplus f(1) = 3 \oplus 4 = 7$$$.
Java 8
standard input
[ "math" ]
64a375c7c49591c676dbdb039c93d218
The input contains one or more independent test cases. The first line of input contains a single integer $$$T$$$ ($$$1 \le T \le 10^3$$$), the number of test cases. Each of the $$$T$$$ following lines contains three space-separated integers $$$a$$$, $$$b$$$, and $$$n$$$ ($$$0 \le a, b, n \le 10^9$$$) respectively.
900
For each test case, output $$$f(n)$$$.
standard output
PASSED
3903a9c9f76c429c702ed297ee7e6550
train_003.jsonl
1566743700
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: $$$f(0) = a$$$; $$$f(1) = b$$$; $$$f(n) = f(n-1) \oplus f(n-2)$$$ when $$$n &gt; 1$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. You are given three integers $$$a$$$, $$$b$$$, and $$$n$$$, calculate $$$f(n)$$$.You have to answer for $$$T$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); for (int tt = 0; tt < t; tt++) { int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); if (c%3 == 0) { out.println(a); } else if (c%3 == 1) { out.println(b); } else { out.println(a^b); } } } } private static int naib(int a, int b) { return a / (nod(a, b)) * b; } private static int nod(int a, int b) { while (a != 0 && b != 0) { if (a > b) { a = a % b; } else { b = b % a; } } return a + b; } 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 Double nextDouble() { return Double.parseDouble(next()); } public int[] readArray(int n) { int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } } }
Java
["3\n3 4 2\n4 5 0\n325 265 1231232"]
1 second
["7\n4\n76"]
NoteIn the first example, $$$f(2) = f(0) \oplus f(1) = 3 \oplus 4 = 7$$$.
Java 8
standard input
[ "math" ]
64a375c7c49591c676dbdb039c93d218
The input contains one or more independent test cases. The first line of input contains a single integer $$$T$$$ ($$$1 \le T \le 10^3$$$), the number of test cases. Each of the $$$T$$$ following lines contains three space-separated integers $$$a$$$, $$$b$$$, and $$$n$$$ ($$$0 \le a, b, n \le 10^9$$$) respectively.
900
For each test case, output $$$f(n)$$$.
standard output
PASSED
245a66f9cfb0b8ffe82e1fb93c379916
train_003.jsonl
1566743700
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: $$$f(0) = a$$$; $$$f(1) = b$$$; $$$f(n) = f(n-1) \oplus f(n-2)$$$ when $$$n &gt; 1$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. You are given three integers $$$a$$$, $$$b$$$, and $$$n$$$, calculate $$$f(n)$$$.You have to answer for $$$T$$$ independent test cases.
256 megabytes
import java.util.Scanner; /** * @author dzimiks * Date: 25-08-2019 at 15:58 */ public class TaskA { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { int a = in.nextInt(); int b = in.nextInt(); int n = in.nextInt(); if (n == 0) { System.out.println(a); } else if (n == 1) { System.out.println(b); } else { if (n % 3 == 0) { System.out.println(a); } else if (n % 3 == 2) { System.out.println(a ^ b); } else if (n % 3 == 1) { System.out.println(b); } } } in.close(); } }
Java
["3\n3 4 2\n4 5 0\n325 265 1231232"]
1 second
["7\n4\n76"]
NoteIn the first example, $$$f(2) = f(0) \oplus f(1) = 3 \oplus 4 = 7$$$.
Java 8
standard input
[ "math" ]
64a375c7c49591c676dbdb039c93d218
The input contains one or more independent test cases. The first line of input contains a single integer $$$T$$$ ($$$1 \le T \le 10^3$$$), the number of test cases. Each of the $$$T$$$ following lines contains three space-separated integers $$$a$$$, $$$b$$$, and $$$n$$$ ($$$0 \le a, b, n \le 10^9$$$) respectively.
900
For each test case, output $$$f(n)$$$.
standard output
PASSED
49160f60bc1918666f4488c02610b771
train_003.jsonl
1566743700
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: $$$f(0) = a$$$; $$$f(1) = b$$$; $$$f(n) = f(n-1) \oplus f(n-2)$$$ when $$$n &gt; 1$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. You are given three integers $$$a$$$, $$$b$$$, and $$$n$$$, calculate $$$f(n)$$$.You have to answer for $$$T$$$ independent test cases.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int i = 0; i < T; i++) { int a = sc.nextInt(); int b = sc.nextInt(); int n = sc.nextInt(); if(n % 3 == 0) System.out.println(a); else if(n % 3 == 1) System.out.println(b); else System.out.println(a ^ b); } } }
Java
["3\n3 4 2\n4 5 0\n325 265 1231232"]
1 second
["7\n4\n76"]
NoteIn the first example, $$$f(2) = f(0) \oplus f(1) = 3 \oplus 4 = 7$$$.
Java 8
standard input
[ "math" ]
64a375c7c49591c676dbdb039c93d218
The input contains one or more independent test cases. The first line of input contains a single integer $$$T$$$ ($$$1 \le T \le 10^3$$$), the number of test cases. Each of the $$$T$$$ following lines contains three space-separated integers $$$a$$$, $$$b$$$, and $$$n$$$ ($$$0 \le a, b, n \le 10^9$$$) respectively.
900
For each test case, output $$$f(n)$$$.
standard output
PASSED
082953f6c71fbbd1150180d208a0a3d5
train_003.jsonl
1566743700
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: $$$f(0) = a$$$; $$$f(1) = b$$$; $$$f(n) = f(n-1) \oplus f(n-2)$$$ when $$$n &gt; 1$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. You are given three integers $$$a$$$, $$$b$$$, and $$$n$$$, calculate $$$f(n)$$$.You have to answer for $$$T$$$ independent test cases.
256 megabytes
import java.util.*; public class simpleDP{ public static void main(String str[]){ int t; Scanner s= new Scanner(System.in); t = s.nextInt(); for(int i=0;i<t;i++){ int a = s.nextInt(); int b = s.nextInt(); int n = s.nextInt(); int arr[] =new int[3]; arr[0] = a; arr[1] = b; arr[2] = a^b; int x = (n+1)%3; if(x==0) System.out.println(arr[2]); else if(x==1) System.out.println(arr[0]); else System.out.println(arr[1]); } } }
Java
["3\n3 4 2\n4 5 0\n325 265 1231232"]
1 second
["7\n4\n76"]
NoteIn the first example, $$$f(2) = f(0) \oplus f(1) = 3 \oplus 4 = 7$$$.
Java 8
standard input
[ "math" ]
64a375c7c49591c676dbdb039c93d218
The input contains one or more independent test cases. The first line of input contains a single integer $$$T$$$ ($$$1 \le T \le 10^3$$$), the number of test cases. Each of the $$$T$$$ following lines contains three space-separated integers $$$a$$$, $$$b$$$, and $$$n$$$ ($$$0 \le a, b, n \le 10^9$$$) respectively.
900
For each test case, output $$$f(n)$$$.
standard output
PASSED
4a03425663d55422862c545bfc0c73da
train_003.jsonl
1566743700
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: $$$f(0) = a$$$; $$$f(1) = b$$$; $$$f(n) = f(n-1) \oplus f(n-2)$$$ when $$$n &gt; 1$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. You are given three integers $$$a$$$, $$$b$$$, and $$$n$$$, calculate $$$f(n)$$$.You have to answer for $$$T$$$ independent test cases.
256 megabytes
import java.util.Scanner; public class XORinacci { public static void main(String[] args) { Scanner xScanner = new Scanner(System.in); int iNumTestCases = xScanner.nextInt(); for(int i = 0; i < iNumTestCases; i++) { int a = xScanner.nextInt(); int b = xScanner.nextInt(); int n = xScanner.nextInt(); //-- the sequence is a, b, a ^ b, a, b, a ^ b, ... if(n % 3 == 0) System.out.println(a); else if( n % 3 == 1) System.out.println(b); else System.out.println(a ^ b); } } //-- rekursiv private static int XORinacciRecursive(int a, int b, int n) { if(n == 0) return a; if(n == 1) return b; return XORinacciRecursive(a, b, n-1) ^ XORinacciRecursive(a, b, n-2); } private static int XORinacciIterative(int a, int b, int n) { if(n == 0) return a; else if(n == 1) return b; else if (n == 2) return a ^ b; else { int[] aXORinacciNumbers = new int[n]; aXORinacciNumbers[0] = a; aXORinacciNumbers[1] = b; for(int i = 2; i < n ; i++) { aXORinacciNumbers[i] = aXORinacciNumbers[i-1] ^ aXORinacciNumbers[i-2]; } return aXORinacciNumbers[n-1]; } } }
Java
["3\n3 4 2\n4 5 0\n325 265 1231232"]
1 second
["7\n4\n76"]
NoteIn the first example, $$$f(2) = f(0) \oplus f(1) = 3 \oplus 4 = 7$$$.
Java 8
standard input
[ "math" ]
64a375c7c49591c676dbdb039c93d218
The input contains one or more independent test cases. The first line of input contains a single integer $$$T$$$ ($$$1 \le T \le 10^3$$$), the number of test cases. Each of the $$$T$$$ following lines contains three space-separated integers $$$a$$$, $$$b$$$, and $$$n$$$ ($$$0 \le a, b, n \le 10^9$$$) respectively.
900
For each test case, output $$$f(n)$$$.
standard output
PASSED
740c3d1feccad135abcd7e5f70550cad
train_003.jsonl
1566743700
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: $$$f(0) = a$$$; $$$f(1) = b$$$; $$$f(n) = f(n-1) \oplus f(n-2)$$$ when $$$n &gt; 1$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. You are given three integers $$$a$$$, $$$b$$$, and $$$n$$$, calculate $$$f(n)$$$.You have to answer for $$$T$$$ independent test cases.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Dynamic_programming { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)) ; int t=Integer.parseInt(br.readLine()); while(t-->0) { String line[]=br.readLine().split(" "); int a=Integer.parseInt(line[0]); int b=Integer.parseInt(line[1]); int n=Integer.parseInt(line[2]); if((n)%3==0) System.out.println(a); else if (n%3==1) System.out.println(b); else if (n%3==2) System.out.println(a^b); } } }
Java
["3\n3 4 2\n4 5 0\n325 265 1231232"]
1 second
["7\n4\n76"]
NoteIn the first example, $$$f(2) = f(0) \oplus f(1) = 3 \oplus 4 = 7$$$.
Java 8
standard input
[ "math" ]
64a375c7c49591c676dbdb039c93d218
The input contains one or more independent test cases. The first line of input contains a single integer $$$T$$$ ($$$1 \le T \le 10^3$$$), the number of test cases. Each of the $$$T$$$ following lines contains three space-separated integers $$$a$$$, $$$b$$$, and $$$n$$$ ($$$0 \le a, b, n \le 10^9$$$) respectively.
900
For each test case, output $$$f(n)$$$.
standard output
PASSED
6912a4bd76c08d52e632b0f6a6ee6034
train_003.jsonl
1566743700
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: $$$f(0) = a$$$; $$$f(1) = b$$$; $$$f(n) = f(n-1) \oplus f(n-2)$$$ when $$$n &gt; 1$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. You are given three integers $$$a$$$, $$$b$$$, and $$$n$$$, calculate $$$f(n)$$$.You have to answer for $$$T$$$ independent test cases.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num_Of_Elements = input.nextInt(); for (int i = 0; i < num_Of_Elements; i++) { int arr[] = new int[3]; for (int j = 0; j < 3; j++){ arr[j] = input.nextInt(); } if (arr[2]%3 == 0){ System.out.println(arr[0]); }else if (arr[2] % 3 == 1){ System.out.println(arr[1]); }else { System.out.println(arr[0]^arr[1]); } } } }
Java
["3\n3 4 2\n4 5 0\n325 265 1231232"]
1 second
["7\n4\n76"]
NoteIn the first example, $$$f(2) = f(0) \oplus f(1) = 3 \oplus 4 = 7$$$.
Java 8
standard input
[ "math" ]
64a375c7c49591c676dbdb039c93d218
The input contains one or more independent test cases. The first line of input contains a single integer $$$T$$$ ($$$1 \le T \le 10^3$$$), the number of test cases. Each of the $$$T$$$ following lines contains three space-separated integers $$$a$$$, $$$b$$$, and $$$n$$$ ($$$0 \le a, b, n \le 10^9$$$) respectively.
900
For each test case, output $$$f(n)$$$.
standard output
PASSED
3d56ae4826b7a3ad654655e2fc92440f
train_003.jsonl
1566743700
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: $$$f(0) = a$$$; $$$f(1) = b$$$; $$$f(n) = f(n-1) \oplus f(n-2)$$$ when $$$n &gt; 1$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. You are given three integers $$$a$$$, $$$b$$$, and $$$n$$$, calculate $$$f(n)$$$.You have to answer for $$$T$$$ independent test cases.
256 megabytes
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int t=ni(); while(t--!=0){ int a=ni(); int b=ni(); int p=ni(); long t1=0,t2=0; long xor[]=new long[3]; xor[0]=a; xor[1]=b; xor[2]=a^b; out.println(xor[p%3]); }// } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); } }
Java
["3\n3 4 2\n4 5 0\n325 265 1231232"]
1 second
["7\n4\n76"]
NoteIn the first example, $$$f(2) = f(0) \oplus f(1) = 3 \oplus 4 = 7$$$.
Java 8
standard input
[ "math" ]
64a375c7c49591c676dbdb039c93d218
The input contains one or more independent test cases. The first line of input contains a single integer $$$T$$$ ($$$1 \le T \le 10^3$$$), the number of test cases. Each of the $$$T$$$ following lines contains three space-separated integers $$$a$$$, $$$b$$$, and $$$n$$$ ($$$0 \le a, b, n \le 10^9$$$) respectively.
900
For each test case, output $$$f(n)$$$.
standard output
PASSED
ebd43710125be6a09131d1634520037f
train_003.jsonl
1566743700
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: $$$f(0) = a$$$; $$$f(1) = b$$$; $$$f(n) = f(n-1) \oplus f(n-2)$$$ when $$$n &gt; 1$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. You are given three integers $$$a$$$, $$$b$$$, and $$$n$$$, calculate $$$f(n)$$$.You have to answer for $$$T$$$ independent test cases.
256 megabytes
import java.util.Scanner; public class Main { public static void main (String... args) { Scanner s = new Scanner (System.in); int t = s.nextInt(); for (; t > 0; t--) { int a = s.nextInt(); int b = s.nextInt(); int n = (s.nextInt())%3; int[] m = {a, b, a ^ b}; System.out.println(m[n]); } } }
Java
["3\n3 4 2\n4 5 0\n325 265 1231232"]
1 second
["7\n4\n76"]
NoteIn the first example, $$$f(2) = f(0) \oplus f(1) = 3 \oplus 4 = 7$$$.
Java 8
standard input
[ "math" ]
64a375c7c49591c676dbdb039c93d218
The input contains one or more independent test cases. The first line of input contains a single integer $$$T$$$ ($$$1 \le T \le 10^3$$$), the number of test cases. Each of the $$$T$$$ following lines contains three space-separated integers $$$a$$$, $$$b$$$, and $$$n$$$ ($$$0 \le a, b, n \le 10^9$$$) respectively.
900
For each test case, output $$$f(n)$$$.
standard output
PASSED
c154557419645b3c4bbc79a029ac3a1d
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class a{ public static void main( String [] args) throws IOException{ FastScanner sc=new FastScanner(); int n=sc.nextInt(); List<int[]> point=new ArrayList<>(); long ttl=0; for(int i=0;i<n;i++){ int [] a=sc.nextIntArray(2); point.add(a); } point.sort((a,b)->{ int x=Math.abs(a[0])+Math.abs(a[1]); int y=Math.abs(b[0])+Math.abs(b[1]); return x-y; }); StringBuilder sb=new StringBuilder(""); for(int i=0;i<n;i++){ int x=point.get(i)[0]; int y=point.get(i)[1]; if(x>0){sb.append(1+" "+x+" R\n");ttl++;} if(x<0){sb.append(1+" "+(-x)+" L\n");ttl++;} if(y>0){sb.append(1+" "+y+" U\n");ttl++;} if(y<0){sb.append(1+" "+(-y)+" D\n");ttl++;} sb.append("2\n");ttl++; if(x>0){sb.append(1+" "+x+" L\n");ttl++;} if(x<0){sb.append(1+" "+(-x)+" R\n");ttl++;} if(y>0){sb.append(1+" "+y+" D\n");ttl++;} if(y<0){sb.append(1+" "+(-y)+" U\n");ttl++;} sb.append("3\n");ttl++; } System.out.println(ttl); System.out.println(sb.toString().trim()); } } class FastScanner{ private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public FastScanner() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastScanner( 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]; int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public String next() throws IOException{ byte c = read(); while(Character.isWhitespace(c)){ c = read(); } StringBuilder builder = new StringBuilder(); builder.append((char)c); c = read(); while(!Character.isWhitespace(c)){ builder.append((char)c); c = read(); } return builder.toString(); } 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 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 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 long[] nextLongArray( int n) throws IOException { long arr[] = new long[n]; for(int i = 0; i < n; i++){ arr[i] = nextLong(); } return arr; } public char nextChar() throws IOException{ byte c = read(); while(Character.isWhitespace(c)){ c = read(); } return (char) c; } 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; } public double[] nextDoubleArray( int n) throws IOException { double arr[] = new double[n]; for(int i = 0; i < n; i++){ arr[i] = nextDouble(); } return arr; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
467fa5d15205e499d610fc03ec6b821a
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
// TO LOVE IS TO KNOW WHAT'S YOUR WORTH. // // Author :- Saurabh// //BIT MESRA, RANCHI// import java.io.*; import java.util.*; import static java.lang.Math.*; public class bombs { static void Bolo_Jai_Mata_Di() { n = ni(); tsc(); Comparator<two> c = new Comparator<two>() { @Override public int compare(two a, two b) { return (abs(a.a)+abs(a.b))-(abs(b.a)+abs(b.b)); } }; PriorityQueue<two> ar = new PriorityQueue<>(c); for (int i = 0; i < n; i++) ar.add(new two(ni(), ni())); StringBuilder ans = new StringBuilder(); long count = 0; while (!ar.isEmpty()) { two x = ar.poll(); int r = 0, u = 0; if (x.a != 0) { ans.append(1 + " " + (abs(x.a)) + " " + (x.a > 0 ? 'R' : 'L') + "\n"); r++; count++; } if (x.b != 0) { ans.append(1 + " " + (abs(x.b)) + " " + (x.b > 0 ? 'U' : 'D') + "\n"); u++; count++; } ans.append(2 + "\n"); count++; if (u > 0) { ans.append(1 + " " + (abs(x.b)) + " " + (x.b > 0 ? 'D' : 'U') + "\n"); count++; } if (r > 0) { ans.append(1 + " " + (abs(x.a)) + " " + (x.a > 0 ? 'L' : 'R') + "\n"); count++; } ans.append("3\n"); count++; } pl(count); pl(ans); flush(); } static class two{ int a,b; two(int a,int b){ this.a=a;this.b=b; } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //THE DON'T CARE ZONE BEGINS HERE...// static Calendar ts, te; //For time calculation static int mod9 = 1000000007; static int n, m, k, t, mod = 998244353; static Lelo input = new Lelo(System.in); static PrintWriter pw = new PrintWriter(System.out, true); public static void main(String[] args) { //threading has been used to increase the stack size. new Thread(null, null, "BlackRise", 1 << 25) //the last parameter is stack size desired., { public void run() { try { Bolo_Jai_Mata_Di(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } static class Lelo { //Lelo class for fast input private InputStream ayega; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public Lelo(InputStream ayega) { this.ayega = ayega; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = ayega.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } // functions to take input// static int ni() { return input.nextInt(); } static long nl() { return input.nextLong(); } static double nd() { return input.nextDouble(); } static String ns() { return input.readString(); } //functions to give output static void pl() { pw.println(); } static void p(Object o) { pw.print(o + " "); } static void pws(Object o) { pw.print(o + ""); } static void pl(Object o) { pw.println(o); } static void tsc() //calculates the starting time of execution { ts = Calendar.getInstance(); ts.setTime(new Date()); } static void tec() //calculates the ending time of execution { te = Calendar.getInstance(); te.setTime(new Date()); } static void pwt() //prints the time taken for execution { pw.printf("\nExecution time was :- %f s\n", (te.getTimeInMillis() - ts.getTimeInMillis()) / 1000.00); } static void sort(int ar[], int n) { for (int i = 0; i < n; i++) { int ran = (int) (Math.random() * n); int temp = ar[i]; ar[i] = ar[ran]; ar[ran] = temp; } Arrays.sort(ar); } static void sort(long ar[], int n) { for (int i = 0; i < n; i++) { int ran = (int) (Math.random() * n); long temp = ar[i]; ar[i] = ar[ran]; ar[ran] = temp; } Arrays.sort(ar); } static void sort(char ar[], int n) { for (int i = 0; i < n; i++) { int ran = (int) (Math.random() * n); char temp = ar[i]; ar[i] = ar[ran]; ar[ran] = temp; } Arrays.sort(ar); } static void flush() { tec(); //ending time of execution //pwt(); //prints the time taken to execute the program pw.flush(); pw.close(); } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
b8471de545dc2aaeef8e733c308aac0f
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); StringBuilder sb = new StringBuilder(); int ans = 0; Pair[] arr = new Pair[n]; for (int i = 0; i < arr.length; i++) { arr[i] = new Pair(sc.nextInt(), sc.nextInt()); } Arrays.sort(arr); for (int i = 0; i < n; i++) { Pair cur = arr[i]; int x = cur.x , y = cur.y; if(x!=0) { ++ans; char c = x>0? 'R':'L'; sb.append("1 "+Math.abs(x)+" "+c+"\n"); } if(y!=0) { ++ans; char c = y>0? 'U':'D'; sb.append("1 "+Math.abs(y)+" "+c+"\n"); } ++ans; sb.append("2\n"); if(y!=0) { ++ans; char c = y>0? 'D':'U'; sb.append("1 "+Math.abs(y)+" "+c+"\n"); } if(x!=0) { ++ans; char c = x>0? 'L':'R'; sb.append("1 "+Math.abs(x)+" "+c+"\n"); } ++ans; sb.append("3\n"); } System.out.println(ans); System.out.print(sb); } static class Pair implements Comparable<Pair> { int x , y ; double d; public Pair(int a,int b) { x = a; y = b; d = Math.sqrt((1l*x*x)+(1l*y*y)); } public int compareTo(Pair o) { return Double.compare(d, o.d); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public Scanner(String s) throws FileNotFoundException {br = new BufferedReader(new FileReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public String nextLine() throws IOException {return br.readLine();} public long nextLong() throws IOException {return Long.parseLong(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} public char nextChar() throws IOException{return next().charAt(0);} public boolean ready() throws IOException {return br.ready();} public int[] nextIntArr() throws IOException{ st = new StringTokenizer(br.readLine()); int[] res = new int[st.countTokens()]; for (int i = 0; i < res.length; i++) res[i] = nextInt(); return res; } public char[] nextCharArr() throws IOException{ st = new StringTokenizer(br.readLine()); char[] res = new char[st.countTokens()]; for (int i = 0; i < res.length; i++) res[i] = nextChar(); return res; } } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
d798f57378b7bf3d07daa4e78ffee556
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner s=new Scanner(System.in); int n=s.nextInt(); ArrayList<pair> list=new ArrayList<>(); ArrayList<pair> list2=new ArrayList<>(); ArrayList<pair> list3=new ArrayList<>(); ArrayList<pair> list4=new ArrayList<>(); for(int i=0;i<n;i++) { int x=s.nextInt(); int y=s.nextInt(); if(x>0) { if(y>=0) { pair p=new pair(x,y); list.add(p); }else { pair p=new pair(x,y); list2.add(p); } }else if(x<0){ if(y>=0) { pair p=new pair(x,y); list4.add(p); }else { pair p=new pair(x,y); list3.add(p); } }else { if(x==0) { if(y>0) { pair p=new pair(x,y); list.add(p); }else { pair p=new pair(x,y); list3.add(p); } } } } if(list.size()>1) { Collections.sort(list,new comp()); } if(list2.size()>1) { Collections.sort(list2,new comp2()); } if(list3.size()>1) { Collections.sort(list3,new comp3()); } if(list4.size()>1) { Collections.sort(list4,new comp4()); } // for(int i=0;i<list.size();i++) { // System.out.print(list.get(i).x+" "); // // // System.out.print(list.get(i).y+" "); // System.out.println(); // // // } long currx=0;long curry=0; long count=0; StringBuilder sb=new StringBuilder(); for(int i=0;i<list.size();i++) { if(list.get(i).x>currx) { sb.append("1"+" "+(list.get(i).x-currx) +" "+"R"+"\n"); count++; currx=list.get(i).x; } if(list.get(i).x<currx) { sb.append("1"+" "+(-list.get(i).x+currx) +" "+"L"+"\n"); count++; currx=list.get(i).x; } if(list.get(i).y>curry) { sb.append("1"+" "+(list.get(i).y-curry)+" "+"U"+"\n"); count++; curry=list.get(i).y; } if(list.get(i).y<curry) { sb.append("1"+" "+(-list.get(i).y+curry)+" "+"D"+"\n"); count++; curry=list.get(i).y; } sb.append(2+"\n"); count++; if(curry>0) { sb.append("1"+" "+(curry-0) +" "+"D"+"\n"); count++; curry=0; } if(curry<0) { sb.append("1"+" "+(0-curry) +" "+"U"+"\n"); count++; curry=0; } if(currx>0) { sb.append("1"+" "+(currx-0)+" "+"L"+"\n"); count++; currx=0; } if(currx<0) { sb.append("1"+" "+(0-currx)+" "+"R"+"\n"); count++; currx=0; } sb.append(3+"\n"); count++; } for(int i=0;i<list2.size();i++) { if(list2.get(i).x>currx) { sb.append("1"+" "+(list2.get(i).x-currx) +" "+"R"+"\n"); count++; currx=list2.get(i).x; } if(list2.get(i).x<currx) { sb.append("1"+" "+(-list2.get(i).x+currx) +" "+"L"+"\n"); count++; currx=list2.get(i).x; } if(list2.get(i).y>curry) { sb.append("1"+" "+(list2.get(i).y-curry)+" "+"U"+"\n"); count++; curry=list2.get(i).y; } if(list2.get(i).y<curry) { sb.append("1"+" "+(-list2.get(i).y+curry)+" "+"D"+"\n"); count++; curry=list2.get(i).y; } sb.append(2+"\n"); count++; if(curry>0) { sb.append("1"+" "+(curry-0) +" "+"D"+"\n"); count++; curry=0; } if(curry<0) { sb.append("1"+" "+(0-curry) +" "+"U"+"\n"); count++; curry=0; } if(currx>0) { sb.append("1"+" "+(currx-0)+" "+"L"+"\n"); count++; currx=0; } if(currx<0) { sb.append("1"+" "+(0-currx)+" "+"R"+"\n"); count++; currx=0; } sb.append(3+"\n"); count++; } for(int i=0;i<list4.size();i++) { if(list4.get(i).x>currx) { sb.append("1"+" "+(list4.get(i).x-currx) +" "+"R"+"\n"); count++; currx=list4.get(i).x; } if(list4.get(i).x<currx) { sb.append("1"+" "+(-list4.get(i).x+currx) +" "+"L"+"\n"); count++; currx=list4.get(i).x; } if(list4.get(i).y>curry) { sb.append("1"+" "+(list4.get(i).y-curry)+" "+"U"+"\n"); count++; curry=list4.get(i).y; } if(list4.get(i).y<curry) { sb.append("1"+" "+(-list4.get(i).y+curry)+" "+"D"+"\n"); count++; curry=list4.get(i).y; } sb.append(2+"\n"); count++; if(curry>0) { sb.append("1"+" "+(curry-0) +" "+"D"+"\n"); count++; curry=0; } if(curry<0) { sb.append("1"+" "+(0-curry) +" "+"U"+"\n"); count++; curry=0; } if(currx>0) { sb.append("1"+" "+(currx-0)+" "+"L"+"\n"); count++; currx=0; } if(currx<0) { sb.append("1"+" "+(0-currx)+" "+"R"+"\n"); count++; currx=0; } sb.append(3+"\n"); count++; } for(int i=0;i<list3.size();i++) { if(list3.get(i).x>currx) { sb.append("1"+" "+(list3.get(i).x-currx) +" "+"R"+"\n"); count++; currx=list3.get(i).x; } if(list3.get(i).x<currx) { sb.append("1"+" "+(-list3.get(i).x+currx) +" "+"L"+"\n"); count++; currx=list3.get(i).x; } if(list3.get(i).y>curry) { sb.append("1"+" "+(list3.get(i).y-curry)+" "+"U"+"\n"); count++; curry=list3.get(i).y; } if(list3.get(i).y<curry) { sb.append("1"+" "+(-list3.get(i).y+curry)+" "+"D"+"\n"); count++; curry=list3.get(i).y; } sb.append(2+"\n"); count++; if(curry>0) { sb.append("1"+" "+(curry-0) +" "+"D"+"\n"); count++; curry=0; } if(curry<0) { sb.append("1"+" "+(0-curry) +" "+"U"+"\n"); count++; curry=0; } if(currx>0) { sb.append("1"+" "+(currx-0)+" "+"L"+"\n"); count++; currx=0; } if(currx<0) { sb.append("1"+" "+(0-currx)+" "+"R"+"\n"); count++; currx=0; } sb.append(3+"\n"); count++; } System.out.println(count); System.out.println(sb); } } class pair{ long x; long y; public pair(long x,long y){ this.x=x; this.y=y; } } class comp implements Comparator<pair>{ public int compare(pair a,pair b) { if(a.x>b.x) { return 1; }else if(a.x==b.x) { if(a.y>b.y) { return 1; }else { return -1; } }else { return -1; } } } class comp2 implements Comparator<pair>{ public int compare(pair a,pair b) { if(a.x>b.x) { return 1; }else if(b.x==a.x) { if(a.y>b.y) { return -1; }else { return 1; } }else { return -1; } } } class comp3 implements Comparator<pair>{ public int compare(pair a,pair b) { if(a.x>b.x) { return -1; }else if(a.x==b.x) { if(a.y>b.y) { return -1; }else { return 1; } }else { return 1; } } } class comp4 implements Comparator<pair>{ public int compare(pair a,pair b) { if(a.x>b.x) { return -1; }else if(a.x==b.x) { if(a.y>b.y) { return 1; }else { return -1; } }else { return 1; } } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
8253529f418b0eacd6da87b875e33ee4
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.*; import java.util.*; public class CF350C { static class B { int x, y; B(int x, int y) { this.x = x; this.y = y; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); B[] bb = new B[n]; int cnt = 0; for (int i = 0; i < n; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); bb[i] = new B(x, y); cnt += x == 0 || y == 0 ? 4 : 6; } Arrays.sort(bb, (a, b) -> Math.abs(a.x) + Math.abs(a.y) - Math.abs(b.x) - Math.abs(b.y)); PrintWriter pw = new PrintWriter(System.out); pw.println(cnt); for (int i = 0; i < n; i++) { int x = bb[i].x; int y = bb[i].y; if (x > 0) pw.println("1 " + x + " R"); else if (x < 0) pw.println("1 " + -x + " L"); if (y > 0) pw.println("1 " + y + " U"); else if (y < 0) pw.println("1 " + -y + " D"); pw.println("2"); if (x > 0) pw.println("1 " + x + " L"); else if (x < 0) pw.println("1 " + -x + " R"); if (y > 0) pw.println("1 " + y + " D"); else if (y < 0) pw.println("1 " + -y + " U"); pw.println("3"); } pw.close(); } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
c4dcbc4fd609d4a59b29fc47c812e9b6
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.Buffer; import java.util.*; public class Watermelon{ public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int[][] info=new int[n][3]; int x,y,dist; for(int i=0;i<n;i++){ x=sc.nextInt();y=sc.nextInt(); dist=Math.abs(x)+Math.abs(y); info[i]=new int[3]; info[i][0]=dist;info[i][1]=x;info[i][2]=y; } sortbyColumn(info,0); // for(int i=0;i<n;i++)System.out.println(Arrays.toString(info[i])); StringBuilder sb=new StringBuilder(); int steps=0; for(int i=0;i<n;i++){ x=info[i][1];y=info[i][2]; if(x>0 || x<0)steps+=2; if(y>0 || y<0)steps+=2; steps+=2; } sb.append(steps+"\n"); for(int i=0;i<n;i++){ x=info[i][1];y=info[i][2]; if(x>0)sb.append("1 "+Math.abs(info[i][1])+" R\n"); if(x<0)sb.append("1 "+Math.abs(info[i][1])+" L\n"); if(y>0)sb.append("1 "+Math.abs(info[i][2])+" U\n"); if(y<0)sb.append("1 "+Math.abs(info[i][2])+" D\n"); sb.append("2\n"); if(x>0)sb.append("1 "+Math.abs(info[i][1])+" L\n"); if(x<0)sb.append("1 "+Math.abs(info[i][1])+" R\n"); if(y>0)sb.append("1 "+Math.abs(info[i][2])+" D\n"); if(y<0)sb.append("1 "+Math.abs(info[i][2])+" U\n"); sb.append("3"+((i==(n-1))?"":"\n")); } System.out.println(sb); } public static void sortbyColumn(int arr[][], int col) { // Using built-in sort function Arrays.sort Arrays.sort(arr, new Comparator<int[]>() { @Override // Compare values according to columns public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; else if(entry1[col] < entry2[col]) return -1; else return 0; } }); // End of function call sort(). } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
f4d92f16591775f61f681e9439b64e86
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.*; import java.util.*; public class Cf350C { static class Pair implements Comparator<Pair>{ int a,b; Pair(){} Pair(int a, int b){ this.a = a; this.b = b; } @Override public int compare(Pair p, Pair q) { if(Math.abs(p.a) > Math.abs(q.a)) return 1; else if(Math.abs(p.a) < Math.abs(q.a)) return -1; else{ if(Math.abs(p.b) > Math.abs(q.b)) return 1; else if(Math.abs(p.b) < Math.abs(q.b)) return -1; else return 0; } } } static int k = 0; static List<String> ans = new ArrayList<>(); public static void main(String args[]) throws IOException { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n = in.nextInt(); List<Pair> inp = new ArrayList<>(); for(int i = 0; i < n; i++){ int x = in.nextInt(); int y = in.nextInt(); inp.add(new Pair(x, y)); } Collections.sort(inp, new Pair()); for(int i = 0; i < n; i++){ int x = inp.get(i).a; int y = inp.get(i).b; k += solve(x, y, true); k += solve(-x, -y, false); k++; ans.add("3"); } w.println(k); for(int i = 0; i < ans.size(); i++){ w.println(ans.get(i)); } w.close(); } public static int solve(int x, int y, boolean rev){ int k = 0, xmov = 0, ymov = 0; if(x > 0){ k++; xmov = x - xmov; x = 0; ans.add("1 " + xmov + " R"); if(y == 0 && rev){ k++; ans.add("2"); } } if(x < 0){ k++; xmov = Math.abs(x - xmov); x = 0; ans.add("1 " + xmov + " L"); if(y == 0 && rev) { k++; ans.add("2"); } } if(y > 0){ k++; ymov = y - ymov; ans.add("1 " + ymov + " U"); if(x == 0 && rev) { k++; ans.add("2"); } } if(y < 0){ k++; ymov = Math.abs(y - ymov); ans.add("1 " + ymov + " D"); if(x == 0 && rev) { k++; ans.add("2"); } } return k; } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
74f7c74bb2f94be49b2aa08769a961f7
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Collections; public class C { static StreamTokenizer st; static PrintWriter out; static ArrayList<Point> lst; public static void main(String[] args) throws IOException { st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); int n = ri(); lst = new ArrayList(n); int steps = 0; for (int i = 0; i < n; i++) { int x = ri(); int y = ri(); if (x != 0) { steps += 2; } if (y != 0) { steps += 2; } steps += 2; lst.add(new Point(x, y)); } Collections.sort(lst, (Point p1, Point p2) -> { int d1 = Math.abs(p1.x) + Math.abs(p1.y); int d2 = Math.abs(p2.x) + Math.abs(p2.y); return Integer.compare(d1, d2); }); out.println(steps); for (Point p : lst) { printSteps(0, 0, p.x, p.y); out.println('2'); printSteps(p.x, p.y, 0, 0); out.println('3'); } out.close(); } static void printSteps(int x1, int y1, int x2, int y2) { while (x1 != x2 || y1 != y2) { out.print(1); out.print(' '); if (y1 > y2) { out.print(y1 - y2); out.print(' '); out.println('D'); y1 = y2; } else if (y1 < y2) { out.print(y2 - y1); out.print(' '); out.println('U'); y1 = y2; } else if (x1 > x2) { out.print(x1 - x2); out.print(' '); out.println('L'); x1 = x2; } else if (x1 < x2) { out.print(x2 - x1); out.print(' '); out.println('R'); x1 = x2; } } } static int ri() throws IOException { st.nextToken(); return (int) st.nval; } static class Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
c05ffd85162140fb8631b2b288207ebc
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); Scanner sc = new Scanner(System.in); input.init(System.in); int n = input.nextInt(); Pair[] a = new Pair[n]; for(int i = 0; i < n; i++) a[i] = new Pair(input.nextInt(), input.nextInt()); Arrays.sort(a); ArrayList<String> op = new ArrayList<>(); for(int i = 0; i < n; i++) { // System.out.println(a[i].x + " " + a[i].y); int lr = 0, ud = 0; StringBuilder forSpeed = new StringBuilder("1 "); boolean wentRL = false, wentUD = false; if(a[i].x != 0) { wentRL = true; forSpeed.append(Math.abs(a[i].x) + " "); if(a[i].x < 0) { forSpeed.append("L"); lr--; } else { forSpeed.append("R"); lr++; } op.add(forSpeed.toString()); forSpeed = new StringBuilder("1 "); } if(a[i].y != 0) { wentUD = true; forSpeed.append(Math.abs(a[i].y) + " "); if(a[i].y < 0) { forSpeed.append("D"); ud--; } else { forSpeed.append("U"); ud++; } op.add(forSpeed.toString()); forSpeed = new StringBuilder("1 "); } op.add("2"); if(wentUD) { if(ud < 0) forSpeed.append(Math.abs(a[i].y) + " U"); else forSpeed.append(Math.abs(a[i].y) + " D"); op.add(forSpeed.toString()); forSpeed = new StringBuilder("1 "); } if(wentRL) { if(lr < 0) forSpeed.append(Math.abs(a[i].x) + " R"); else forSpeed.append(Math.abs(a[i].x) + " L"); op.add(forSpeed.toString()); } op.add("3"); } out.println(op.size()); for(int i = 0; i < op.size(); i++) out.println(op.get(i)); out.close(); } } class Pair implements Comparable<Pair> { int x, y; public Pair(int i, int j) { x = i; y = j; } public int compareTo(Pair o) { int manDist = Math.abs(x) + Math.abs(y); int manDistO = Math.abs(o.x) + Math.abs(o.y); if(manDist == manDistO) { if(o.x == x) return Math.abs(y) - Math.abs(o.y); return Math.abs(x) - Math.abs(o.x); } return manDist - manDistO; } } class input { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
460353a4d5712df0a5368a4c250526cd
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.util.*; import java.io.*; public class C{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = scan.nextInt(); Pair arr[] = new Pair[n]; for(int i = 0;i < n;i++) arr[i] = new Pair(scan.nextInt(),scan.nextInt()); Arrays.sort(arr); long count = 0; StringBuilder str = new StringBuilder(); for(int i = 0;i < n;i++) { int x = arr[i].x; int y = arr[i].y; boolean bit[] = new boolean[4]; String addition[] = new String[4]; if(x < 0) { int fact = -1*x; str.append(1+" "+fact+" L\n"); count+=2; bit[0] = true; addition[0] = 1+" "+fact+" R\n"; } else if (x > 0) { str.append(1+" "+x+" R\n"); count+=2; bit[1] = true; addition[1] = 1+" "+x+" L\n"; } if(y < 0) { int fact = -1*y; str.append(1+" "+fact+" D\n"); count+=2; bit[2] = true; addition[2] = 1+" "+fact+" U\n"; } else if(y > 0) { str.append(1+" "+y+" U\n"); count+=2; bit[3] = true; addition[3] = 1+" "+y+" D\n"; } str.append("2\n"); count++; if(bit[3]) { str.append(addition[3]); } if(bit[2]) { str.append(addition[2]); } if(bit[1]) { str.append(addition[1]); } if(bit[0]) { str.append(addition[0]); } str.append("3\n"); count++; } pw.println(count); pw.println(str); pw.close(); } } class Pair implements Comparable<Pair>{ int x,y; Pair(int x,int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair p) { if(Math.abs(this.x) == Math.abs(p.x)) { int a = this.y; int b = p.y; return Math.abs(a) - Math.abs(b); } int a = this.x; int b = p.x; return Math.abs(a) - Math.abs(b); } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
6f2a96407e6b7b987fb3895c84b495c1
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.*; import java.util.*; import java.math.BigInteger; /* * This file was created by ayush in attempt to solve the problem problems */ public class main { public void solve(Point[] points, PrintWriter out) { Arrays.sort(points); String ans=""; int operations = 4 * points.length; for (int i = 0; i < points.length; i++) { if (points[i].x != 0 && points[i].y != 0) operations += 2; } out.println(operations); for(int i=0;i<points.length;i++){ //int curX=0; //int curY=0; if(points[i].x>0){ out.println("1 "+points[i].x+" R"); }else if(points[i].x<0){ out.println("1 "+(-points[i].x)+" L"); } if(points[i].y>0){ out.println("1 "+points[i].y+" U"); }else if(points[i].y<0){ out.println("1 "+(-points[i].y)+" D"); } out.println("2"); if(points[i].x<0){ out.println("1 "+(-points[i].x)+" R"); }else if(points[i].x>0){ out.println("1 "+points[i].x+" L"); } if(points[i].y<0){ out.println("1 "+(-points[i].y)+" U"); }else if(points[i].y>0){ out.println("1 "+points[i].y+" D"); } out.println("3"); } } public static void main(String[] args) throws Exception { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); Point[] points=new Point[n]; Date t1 = new Date(); main solver = new main(); for(int i=0;i<n;i++){ points[i]=new Point(in.nextInt(),in.nextInt()); } solver.solve(points, out); out.flush(); Date t2 = new Date(); System.err.println(String.format("Your program took %f seconds", (t2.getTime() - t1.getTime()) / 1000.0)); out.close(); } static class Point implements Comparable{ int x; int y; public Point(int X,int Y){ this.x=X; this.y=Y; } public int compareTo(Object that){ Point th=(Point)that; if(Math.abs(th.x)+Math.abs(th.y)>Math.abs(this.x)+Math.abs(this.y)){ return -1; }else if(Math.abs(th.x)+Math.abs(th.y)<Math.abs(this.x)+Math.abs(this.y)){ return 1; } return 0; } } static class FastScanner { private static BufferedReader reader; private static StringTokenizer tokenizer; public FastScanner(InputStream in) throws Exception { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = new StringTokenizer(reader.readLine().trim()); } public int numTokens() throws Exception { if (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine().trim()); return numTokens(); } return tokenizer.countTokens(); } public boolean hasNext() throws Exception { if (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine().trim()); return hasNext(); } return true; } public String next() throws Exception { if (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine().trim()); return next(); } return tokenizer.nextToken(); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public float nextFloat() throws Exception { return Float.parseFloat(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public int nextInt() throws Exception { return Integer.parseInt(next()); } public int[] nextIntArray() throws Exception { String[] line = reader.readLine().trim().split(" "); int[] out = new int[line.length]; for (int i = 0; i < line.length; i++) { out[i] = Integer.valueOf(line[i]); } return out; } public double[] nextDoubleArray() throws Exception { String[] line = reader.readLine().trim().split(" "); double[] out = new double[line.length]; for (int i = 0; i < line.length; i++) { out[i] = Double.valueOf(line[i]); } return out; } public Integer[] nextIntegerArray() throws Exception { String[] line = reader.readLine().trim().split(" "); Integer[] out = new Integer[line.length]; for (int i = 0; i < line.length; i++) { out[i] = Integer.valueOf(line[i]); } return out; } public BigInteger[] nextBigIngtegerArray() throws Exception { String[] line = reader.readLine().trim().split(" "); BigInteger[] out = new BigInteger[line.length]; for (int i = 0; i < line.length; i++) { out[i] = new BigInteger(line[i]); } return out; } public String nextLine() throws Exception { return reader.readLine().trim(); } public BigInteger nextBigInteger() throws Exception { return new BigInteger(next()); } } static class DEBUG { public static void DebugInfo(String disp) { System.err.println("DEBUG Info: " + disp); } public static void DebugVariable(String variable, String value) { System.err.println("DEBUG Info: " + variable + " => " + value); } public static void DebugVariable(String variable, int value) { System.err.println("DEBUG Info: " + variable + " => " + value); } public static void DebugVariable(String variable, long value) { System.err.println("DEBUG Info: " + variable + " => " + value); } public static void DebugArray(int[] value) { for (int i = 0; i < value.length; i++) { System.err.println("DEBUG Info: " + i + " => " + value[i]); } } } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
38e845f7b2187312a86706291271178e
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Arrays; import java.util.StringTokenizer; public class C { static class Point implements Comparable { public long x, y; public Point(long ox, long oy) { x = ox; y = oy; } public int compareTo(Point b) { return 0; } @Override public int compareTo(Object o) { long d1 = x * x + y * y; Point b = (Point) o; long d2 = b.x * b.x + b.y * b.y; if (d1 < d2) { return -1; } else { return 1; } } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int ops = 0; Point[] points = new Point[n]; for (int i = 0; i < n; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); points[i] = new Point(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())); ops += 2 + 2 * ((points[i].x == 0 ? 0 : 1) + (points[i].y == 0 ? 0 : 1)); } Arrays.sort(points); BufferedWriter write = new BufferedWriter(new OutputStreamWriter(System.out)); write.write(String.valueOf(ops) + "\n"); for (int i = 0; i < n; i++) { long x = points[i].x; long y = points[i].y; if (x < 0) { write.write("1 " + (-x) + " L\n"); } else if (x > 0) { write.write("1 " + x + " R\n"); } if (y < 0) { write.write("1 " + (-y) + " D\n"); } else if (y > 0) { write.write("1 " + y + " U\n"); } write.write("2\n"); if (x < 0) { write.write("1 " + (-x) + " R\n"); } else if (x > 0) { write.write("1 " + x + " L\n"); } if (y < 0) { write.write("1 " + (-y) + " U\n"); } else if (y > 0) { write.write("1 " + y + " D\n"); } write.write("3"); if (i != n - 1) write.write("\n"); } write.close(); } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
d0c87692865070af2c4e903ea2eefb24
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.ObjectInputStream.GetField; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; public class Q2 { static long MOD = 1000000007; static boolean b[]; static ArrayList<Integer>[] amp; static int sum[],dist[]; static long ans = 0; static int p = 0; static FasterScanner sc = new FasterScanner(); static Queue<Integer> q = new LinkedList<>(); static ArrayList<String>[] arr; static ArrayList<Integer> parent = new ArrayList<>(); static BufferedWriter log; static HashMap<Long,Long> hm; static HashSet<String> hs = new HashSet<>(); static Stack<Integer> s = new Stack<>(); static Pair prr[]; public static void main(String[] args) throws IOException { log = new BufferedWriter(new OutputStreamWriter(System.out)); int n = sc.nextInt(); Pair prr[] = new Pair[n]; for(int i = 0;i<n;i++) prr[i] = new Pair(sc.nextInt(),sc.nextInt()); Arrays.sort(prr); //for(int i = 0; i<n;i++) System.out.println(prr[i]); ArrayList<String> ans = new ArrayList<>(); for(int i = 0;i<n;i++){ int x = prr[i].u, y = prr[i].v; if(x==0){ if(y>0){ ans.add(1+" "+y+" "+"U"); ans.add("2"); ans.add(1+" "+y+" "+"D"); ans.add("3"); } else if(y<0){ ans.add(1+" "+(-y)+" "+"D"); ans.add("2"); ans.add(1+" "+(-y)+" "+"U"); ans.add("3"); } } else if(x>0){ ans.add(1+" "+x+" R"); if(y>0){ ans.add(1+" "+y+" U"); ans.add("2"); ans.add(1+" "+y+" D"); } else if(y<0){ ans.add(1+" "+(-y)+" D"); ans.add("2"); ans.add(1+" "+(-y)+" U"); } else ans.add("2"); ans.add(1+" "+x+" L"); ans.add("3"); } else{ ans.add(1+" "+(-x)+" L"); if(y>0){ ans.add(1+" "+y+" U"); ans.add("2"); ans.add(1+" "+y+" D"); } else if(y<0){ ans.add(1+" "+(-y)+" D"); ans.add("2"); ans.add(1+" "+(-y)+" U"); } else ans.add("2"); ans.add(1+" "+(-x)+" R"); ans.add("3"); } } log.write(ans.size()+"\n"); for(String s:ans){ log.write(s+"\n"); } log.close(); } private static void permutation(String prefix, String str) { int n = str.length(); if (n == 0) hs.add(prefix); else { for (int i = 0; i < n; i++) permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n)); } } public static char give(char c1,char c2){ if(c1!='a' && c2!='a') return 'a'; if(c1!='b' && c2!='b') return 'b'; return 'c'; } static class Graph{ int vertex; long weight; } public static void buildTree(int n){ int arr[] = sc.nextIntArray(n); for(int i = 0;i<n;i++){ int x = arr[i]-1; amp[i+1].add(x); amp[x].add(i+1); } } public static void bfs(int x,long arr[]){ b[x] = true; q.add(x); while(!q.isEmpty()){ int y = q.poll(); for(int i = 0;i<amp[y].size();i++) { if(!b[amp[y].get(i)]){ q.add(amp[y].get(i)); b[amp[y].get(i)] = true; } } } } public static void buildGraph(int n){ for(int i =0;i<n;i++){ int x = sc.nextInt(), y = sc.nextInt(); amp[--x].add(--y); amp[y].add(x); } } public static void dfs(int x){ b[x] = true; for(int i =0 ;i<amp[x].size();i++){ if(!b[amp[x].get(i)]){ dfs(amp[x].get(i)); } } } static class Pair implements Comparable<Pair> { int u; int v; int z; public Pair(){ } public Pair(int u, int v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31*hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return (u == other.u && v == other.v); } public int compareTo(Pair other) { return Integer.compare(Math.abs(u), Math.abs(other.u)) != 0 ? (Integer.compare(Math.abs(u), Math.abs(other.u))) : (Integer.compare(Math.abs(v), Math.abs(other.v))); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } static class SegmentTree{ int st[]; SegmentTree(int arr[], int n){ int size = 4*n+1; st = new int[size]; build(arr,0,n-1,0); } int build(int arr[], int ss, int se, int si){ if(ss==se){ st[si] = arr[ss]; return st[si]; } int mid = (ss+se)/2; st[si] = build(arr,ss,mid,si*2+1)^build(arr,mid+1,se,si*2+2); return st[si]; } int getXor(int qs, int qe, int ss, int se, int si){ if(qe<ss || qs>se){ return 0; } if(qs<=ss && qe>=se) return st[si]; int mid = (ss+se)/2; return getXor(qs,qe,ss,mid,2*si+1)^getXor(qs,qe, mid+1, se, 2*si+2); } void print(){ for(int i = 0;i<st.length;i++){ System.out.print(st[i]+" "); System.out.println("----------------------"); } } } static int convert(int x){ int cnt = 0; String str = Integer.toBinaryString(x); //System.out.println(str); for(int i = 0;i<str.length();i++){ if(str.charAt(i)=='1'){ cnt++; } } int ans = (int) Math.pow(3, 6-cnt); return ans; } static class Node2{ Node2 left = null; Node2 right = null; Node2 parent = null; int data; } static class BinarySearchTree{ Node2 root = null; int height = 0; int max = 0; int cnt = 1; ArrayList<Integer> parent = new ArrayList<>(); HashMap<Integer, Integer> hm = new HashMap<>(); public void insert(int x){ Node2 n = new Node2(); n.data = x; if(root==null){ root = n; } else{ Node2 temp = root,temp2 = null; while(temp!=null){ temp2 = temp; if(x>temp.data) temp = temp.right; else temp = temp.left; } if(x>temp2.data) temp2.right = n; else temp2.left = n; n.parent = temp2; parent.add(temp2.data); } } public Node2 getSomething(int x, int y, Node2 n){ if(n.data==x || n.data==y) return n; else if(n.data>x && n.data<y) return n; else if(n.data<x && n.data<y) return getSomething(x,y,n.right); else return getSomething(x,y,n.left); } public Node2 search(int x,Node2 n){ if(x==n.data){ max = Math.max(max, n.data); return n; } if(x>n.data){ max = Math.max(max, n.data); return search(x,n.right); } else{ max = Math.max(max, n.data); return search(x,n.left); } } public int getHeight(Node2 n){ if(n==null) return 0; height = 1+ Math.max(getHeight(n.left), getHeight(n.right)); return height; } } static long findDiff(long[] arr, long[] brr, int m){ int i = 0, j = 0; long fa = 1000000000000L; while(i<m && j<m){ long x = arr[i]-brr[j]; if(x>=0){ if(x<fa) fa = x; j++; } else{ if((-x)<fa) fa = -x; i++; } } return fa; } public static long max(long x, long y, long z){ if(x>=y && x>=z) return x; if(y>=x && y>=z) return y; return z; } public static void seive(long n){ b = new boolean[(int) (n+1)]; Arrays.fill(b, true); for(int i = 2;i*i<=n;i++){ if(b[i]){ for(int p = 2*i;p<=n;p+=i){ b[p] = false; } } } } static long modInverse(long a, long mOD2){ return power(a, mOD2-2, mOD2); } static long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y/2, m) % m; p = (p * p) % m; return (y%2 == 0)? p : (x * p) % m; } static long power2(long x,BigInteger y,long m){ long ans = 1; BigInteger two = new BigInteger("2"); while(y.compareTo(BigInteger.ZERO)>0){ if(y.getLowestSetBit()==y.bitCount()){ x = (x*x)%MOD; y = y.divide(two); } else{ ans = (ans*x)%MOD; y = y.subtract(BigInteger.ONE); } } return ans; } static BigInteger power2(BigInteger x, BigInteger y, BigInteger m){ BigInteger ans = new BigInteger("1"); BigInteger one = new BigInteger("1"); BigInteger two = new BigInteger("2"); BigInteger zero = new BigInteger("0"); while(y.compareTo(zero)>0){ //System.out.println(y); if(y.mod(two).equals(one)){ ans = ans.multiply(x).mod(m); y = y.subtract(one); } else{ x = x.multiply(x).mod(m); y = y.divide(two); } } return ans; } static BigInteger power(BigInteger x, BigInteger y, BigInteger m) { if (y.equals(0)) return (new BigInteger("1")); BigInteger p = power(x, y.divide(new BigInteger("2")), m).mod(m); p = (p.multiply(p)).mod(m); return (y.mod(new BigInteger("2")).equals(0))? p : (p.multiply(x)).mod(m); } static long d,x,y; public static void extendedEuclidian(long a, long b){ if(b == 0) { d = a; x = 1; y = 0; } else { extendedEuclidian(b, a%b); int temp = (int) x; x = y; y = temp - (a/b)*y; } } public static long gcd(long n, long m){ if(m!=0) return gcd(m,n%m); else return n; } public static class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
56652ca8ab6ce9343b1afa4831e55937
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class Main { static final long MOD = 1000000007L; static final int INF = 50000000; public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int N = sc.ni(); int[][] nums = new int[N][2]; for (int i = 0; i < N; i++) { nums[i][0] = sc.ni(); nums[i][1] = sc.ni(); } nums = sort(nums); ArrayList<Operation> ops = new ArrayList<Operation>(); for (int i = 0; i < N; i++) { int a = nums[i][0]; int b = nums[i][1]; if (a > 0) { ops.add(new Operation(1,a,"R")); } else if (a < 0) { ops.add(new Operation(1,0-a,"L")); } if (b > 0) { ops.add(new Operation(1,b,"U")); } else if (b < 0) { ops.add(new Operation(1,0-b,"D")); } ops.add(new Operation(2)); if (a > 0) { ops.add(new Operation(1,a,"L")); } else if (a < 0) { ops.add(new Operation(1,0-a,"R")); } if (b > 0) { ops.add(new Operation(1,b,"D")); } else if (b < 0) { ops.add(new Operation(1,0-b,"U")); } ops.add(new Operation(3)); } pw.println(ops.size()); for (Operation op: ops) pw.println(op); pw.close(); } public static int[][] sort(int[][] arr) { //Sort an array (immune to quicksort TLE) Random rgen = new Random(); for (int i = 0; i < arr.length; i++) { int randomPosition = rgen.nextInt(arr.length); int[] temp = arr[i]; arr[i] = arr[randomPosition]; arr[randomPosition] = temp; } Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(int[] arr1, int[] arr2) { return (Math.abs(arr1[0])+Math.abs(arr1[1])-Math.abs(arr2[0])-Math.abs(arr2[1])); } }); return arr; } static class Operation { int t; int d; String dir; public Operation(int type, int dist, String p) { t = type; d = dist; dir = p; } public Operation(int type) { t = type; d = 0; dir = ""; } public String toString() { if (d == 0) { return Integer.toString(t); } else { return (t + " " + d + " " + dir); } } } //Fast exponentiation (x^y mod m) public static long power(long x, long y, long m) { long ans = 1; x %= m; while (y > 0) { if(y % 2 == 1) ans = (ans * x) % m; y /= 2; x = (x * x) % m; } return ans; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { 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 ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
444013f6538b5cecf98242cd08fb7bd0
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Comparator; import java.util.PriorityQueue; public class Main1 { static class Point{ int x; int y; int dist; } static class IntCompare implements Comparator<Point> { public int compare(Point o1, Point o2) { if(o1.dist>o2.dist) { return 1; }else if(o1.dist<o2.dist) { return -1; } return 0; } } public static void main(String[]args) throws NumberFormatException, IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); Comparator<Point>comp = new IntCompare(); PriorityQueue<Point>q = new PriorityQueue<Point>(n,comp); String[]in; int steps = 0; for(int i = 0;i<n;i++){ in = br.readLine().split(" "); Point p = new Point(); p.x = Integer.parseInt(in[0]); p.y = Integer.parseInt(in[1]); int temp = 0; if(Math.abs(p.x)!=0){ temp++; } if(Math.abs(p.y)!=0){ temp++; } temp*=2; temp+=2; steps+=temp; p.dist = Math.abs(p.x)+Math.abs(p.y); q.add(p); } StringBuilder b = new StringBuilder(); b.append(steps+"\n"); while(!q.isEmpty()){ Point p = q.poll(); if(Math.abs(p.x)!=0){ if(p.x<0){ b.append("1 "+Math.abs(p.x)+" L\n"); }else{ b.append("1 "+Math.abs(p.x)+" R\n"); } } if(Math.abs(p.y)!=0){ if(p.y<0){ b.append("1 "+Math.abs(p.y)+" D\n"); }else{ b.append("1 "+Math.abs(p.y)+" U\n"); } } b.append(2+"\n"); if(Math.abs(p.x)!=0){ if(p.x<0){ b.append("1 "+Math.abs(p.x)+" R\n"); }else{ b.append("1 "+Math.abs(p.x)+" L\n"); } } if(Math.abs(p.y)!=0){ if(p.y<0){ b.append("1 "+Math.abs(p.y)+" U\n"); }else{ b.append("1 "+Math.abs(p.y)+" D\n"); } } b.append(3+"\n"); } System.out.println(b.toString()); } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
5e864a28ba2b88bae0cf3e8893ab572c
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.*; import java.util.List; import java.util.ArrayList; import java.util.Collections; public class c { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(in.readLine()); List<Bomb> bombs = new ArrayList<>(); for (int i = 0; i < n; ++i) { String[] input = in.readLine().split("\\s"); bombs.add(new Bomb(Integer.parseInt(input[0]), Integer.parseInt(input[1]))); } in.close(); Collections.sort(bombs); int ans = 0; for (int i = 0; i < n; ++i) { ans += bombs.get(i).findDist(); } out.write(Integer.toString(ans)); out.newLine(); for (int i = 0; i < n; ++i) { bombs.get(i).printPath(out); } out.close(); } } class Bomb implements Comparable<Bomb> { public int x, y; public Bomb(int x, int y) { this.x = x; this.y = y; } public int findDist() { int dist = 2; if (this.x != 0) { dist += 2; } if (this.y != 0) { dist += 2; } return dist; } public void printPath(final BufferedWriter out) throws IOException { if (this.x != 0) { out.write("1 " + Math.abs(this.x) + " " + (this.x > 0 ? 'R' : 'L')); out.newLine(); } if (this.y != 0) { out.write("1 " + Math.abs(this.y) + " " + (this.y > 0 ? 'U' : 'D')); out.newLine(); } out.write("2"); out.newLine(); if (this.x != 0) { out.write("1 " + Math.abs(this.x) + " " + (this.x > 0 ? 'L' : 'R')); out.newLine(); } if (this.y != 0) { out.write("1 " + Math.abs(this.y) + " " + (this.y > 0 ? 'D' : 'U')); out.newLine(); } out.write("3"); out.newLine(); } public int compareTo(Bomb bomb) { int dist = Math.abs(this.x) + Math.abs(this.y); int bombDist = Math.abs(bomb.x) + Math.abs(bomb.y); if (dist == bombDist) { return 0; } return dist < bombDist ? -1 : 1; } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
e1e0ab48b83569b0c9d3c49bcd55eabc
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Collections; public class c { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(in.readLine()); ArrayList<Bomb> bombs = new ArrayList<>(); for (int i = 0; i < n; i++) { String[] input = in.readLine().split("\\s"); bombs.add(new Bomb(Integer.parseInt(input[0]), Integer.parseInt(input[1]))); } in.close(); Collections.sort(bombs); int ans = 0; for (int i = 0; i < n; i++) { //out.write("(" + bombs.get(i).x + ", " + bombs.get(i).y + ")\n"); ans += bombs.get(i).findDist(); } out.write(ans + "\n"); for (int i = 0; i < n; i++) { bombs.get(i).printPath(out); } out.close(); } } class Bomb implements Comparable<Bomb> { public final int x, y; public Bomb(int x, int y) { this.x = x; this.y = y; } public int findDist() { int dist = 2; if (this.x != 0) { dist += 2; } if (this.y != 0) { dist += 2; } return dist; } public void printPath(final BufferedWriter out) throws IOException { // Get to the bomb. if (this.x != 0) { out.write("1 " + Math.abs(this.x) + " " + (this.x > 0 ? 'R' : 'L') + "\n"); } if (this.y != 0) { out.write("1 " + Math.abs(this.y) + " " + (this.y > 0 ? 'U' : 'D') + "\n"); } out.write("2\n"); if (this.x != 0) { out.write("1 " + Math.abs(this.x) + " " + (this.x > 0 ? 'L' : 'R') + "\n"); } if (this.y != 0) { out.write("1 " + Math.abs(this.y) + " " + (this.y > 0 ? 'D' : 'U') + "\n"); } out.write("3\n"); } public int compareTo(Bomb bomb) { int dist = Math.abs(this.x) + Math.abs(this.y); int bombDist = Math.abs(bomb.x) + Math.abs(bomb.y); if (dist == bombDist) { return 0; } return dist < bombDist ? -1 : 1; } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
2d0c28dc61d37fb815b033bff34bbec2
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.IOException; import java.util.Arrays; import java.io.UnsupportedEncodingException; import java.util.InputMismatchException; import java.util.Comparator; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author gridnevvvit */ public class mC { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); C solver = new C(); solver.solve(1, in, out); out.close(); } } class C { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.readInt(); Pair pts[] = new Pair[n]; int szAns = 0; for(int i = 0; i < n; i++) { int x = in.readInt(); int y = in.readInt(); szAns += 2; if (x != 0) szAns += 2; if (y != 0) szAns += 2; pts[i]= new Pair(x, y); } Arrays.sort(pts, new Comparator<Pair>() { @Override public int compare(Pair o1, Pair o2) { int dist1 = Math.abs(o1.first) + Math.abs(o1.second); int dist2 = Math.abs(o2.first) + Math.abs(o2.second); Integer v1 = dist1; Integer v2 = dist2; return v1.compareTo(v2); } }); out.println(szAns); for(int i = 0; i < n; i++) { if (pts[i].first > 0) out.println(1 + " " + pts[i].first + " R"); if (pts[i].first < 0) out.println(1 + " " + -pts[i].first + " L"); if (pts[i].second > 0) out.println(1 + " " + pts[i].second + " U"); if (pts[i].second < 0) out.println(1 + " " + -pts[i].second + " D"); out.println(2); if (pts[i].first > 0) out.println(1 + " " + pts[i].first + " L"); if (pts[i].first < 0) out.println(1 + " " + -pts[i].first + " R"); if (pts[i].second > 0) out.println(1 + " " + pts[i].second + " D"); if (pts[i].second < 0) out.println(1 + " " + -pts[i].second + " U"); out.println(3); } } public class Pair { int first = 0, second = 0; public Pair (int x, int y) { this.first = x; this.second = y; } } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
fabc4465ed59fc6f920bef59f2e30908
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class CF350C { //***************************************** VARIABLE DECLERATION SECTION ********************************************************************; long x,y; double z; //***************************************** VARIABLE DECLERATION ENDS ********************************************************************; //************************************************ MAIN FUNCTION *************************************************************************; public static void main(String[] args) { FastReader scan=new FastReader(); int n=scan.nextInt(); CF350C[] arr=new CF350C[n]; for(int i=0;i<n;i++) { long a=scan.nextLong(),b=scan.nextLong(); double d=Math.sqrt(a*a+b*b); arr[i]=new CF350C(a,b,d); } Arrays.sort(arr,new Comp()); int cnt=0; StringBuilder sb=new StringBuilder(); for(int i=0;i<n;i++) { if(arr[i].x>0&&arr[i].y>0) { sb.append(1+" "+arr[i].x+" "+'R'+'\n'); sb.append(1+" "+arr[i].y+" "+'U'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+arr[i].x+" "+'L'+'\n'); sb.append(1+" "+arr[i].y+" "+'D'+'\n'); cnt+=5; } else if(arr[i].x>0&&arr[i].y<0) { sb.append(1+" "+arr[i].x+" "+'R'+'\n'); sb.append(1+" "+-arr[i].y+" "+'D'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+arr[i].x+" "+'L'+'\n'); sb.append(1+" "+-arr[i].y+" "+'U'+'\n'); cnt+=5; } else if(arr[i].x<0&&arr[i].y>0) { sb.append(1+" "+-arr[i].x+" "+'L'+'\n'); sb.append(1+" "+arr[i].y+" "+'U'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+-arr[i].x+" "+'R'+'\n'); sb.append(1+" "+arr[i].y+" "+'D'+'\n'); cnt+=5; } else if(arr[i].x<0&&arr[i].y<0) { sb.append(1+" "+-arr[i].x+" "+'L'+'\n'); sb.append(1+" "+-arr[i].y+" "+'D'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+-arr[i].x+" "+'R'+'\n'); sb.append(1+" "+-arr[i].y+" "+'U'+'\n'); cnt+=5; } else if(arr[i].x==0) { if(arr[i].y>0) { sb.append(1+" "+arr[i].y+" "+'U'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+arr[i].y+" "+'D'+'\n'); } else { sb.append(1+" "+-arr[i].y+" "+'D'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+-arr[i].y+" "+'U'+'\n'); } cnt+=3; } else { if(arr[i].x>0) { sb.append(1+" "+arr[i].x+" "+'R'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+arr[i].x+" "+'L'+'\n'); } else { sb.append(1+" "+-arr[i].x+" "+'L'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+-arr[i].x+" "+'R'+'\n'); } cnt+=3; } //sb.append(2+" "+'\n'); sb.append(3+" "+'\n'); cnt+=1; } System.out.println(cnt); System.out.println(sb); } //*********************************************** MAIN FUNCTION ENDS *********************************************************************; //********************************************** AUXILIARY FUNCTIONS **********************************************************************; CF350C(long a,long b,double c) { x=a;y=b;z=c; } // ********************************************** AUXILIARY FUNCTIONS ENDS **********************************************************************; //********************************************** INPUT FUNCTIONS ******************************************************************************; 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; } } //********************************************** INPUT FUNCTIONS ******************************************************************************; } class Comp implements Comparator<CF350C> { public int compare(CF350C o1,CF350C o2) { if(o1.z==o2.z)return 0; if(o1.z>o2.z)return 1; return -1; } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
d1f92ef3f7095ad03892738a81a937c2
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class CF350C { //***************************************** VARIABLE DECLERATION SECTION ********************************************************************; long x,y; double z; //***************************************** VARIABLE DECLERATION ENDS ********************************************************************; //************************************************ MAIN FUNCTION *************************************************************************; public static void main(String[] args) { FastReader scan=new FastReader(); int n=scan.nextInt(); CF350C[] arr=new CF350C[n]; for(int i=0;i<n;i++) { long a=scan.nextLong(),b=scan.nextLong(); //double d=Math.sqrt(a*a+b*b); double d=Math.abs(a)+Math.abs(b); arr[i]=new CF350C(a,b,d); } Arrays.sort(arr,new Comp()); int cnt=0; StringBuilder sb=new StringBuilder(); for(int i=0;i<n;i++) { if(arr[i].x>0&&arr[i].y>0) { sb.append(1+" "+arr[i].x+" "+'R'+'\n'); sb.append(1+" "+arr[i].y+" "+'U'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+arr[i].x+" "+'L'+'\n'); sb.append(1+" "+arr[i].y+" "+'D'+'\n'); cnt+=5; } else if(arr[i].x>0&&arr[i].y<0) { sb.append(1+" "+arr[i].x+" "+'R'+'\n'); sb.append(1+" "+-arr[i].y+" "+'D'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+arr[i].x+" "+'L'+'\n'); sb.append(1+" "+-arr[i].y+" "+'U'+'\n'); cnt+=5; } else if(arr[i].x<0&&arr[i].y>0) { sb.append(1+" "+-arr[i].x+" "+'L'+'\n'); sb.append(1+" "+arr[i].y+" "+'U'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+-arr[i].x+" "+'R'+'\n'); sb.append(1+" "+arr[i].y+" "+'D'+'\n'); cnt+=5; } else if(arr[i].x<0&&arr[i].y<0) { sb.append(1+" "+-arr[i].x+" "+'L'+'\n'); sb.append(1+" "+-arr[i].y+" "+'D'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+-arr[i].x+" "+'R'+'\n'); sb.append(1+" "+-arr[i].y+" "+'U'+'\n'); cnt+=5; } else if(arr[i].x==0) { if(arr[i].y>0) { sb.append(1+" "+arr[i].y+" "+'U'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+arr[i].y+" "+'D'+'\n'); } else { sb.append(1+" "+-arr[i].y+" "+'D'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+-arr[i].y+" "+'U'+'\n'); } cnt+=3; } else { if(arr[i].x>0) { sb.append(1+" "+arr[i].x+" "+'R'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+arr[i].x+" "+'L'+'\n'); } else { sb.append(1+" "+-arr[i].x+" "+'L'+'\n'); sb.append(2+" "+'\n'); sb.append(1+" "+-arr[i].x+" "+'R'+'\n'); } cnt+=3; } //sb.append(2+" "+'\n'); sb.append(3+" "+'\n'); cnt+=1; } System.out.println(cnt); System.out.println(sb); } //*********************************************** MAIN FUNCTION ENDS *********************************************************************; //********************************************** AUXILIARY FUNCTIONS **********************************************************************; CF350C(long a,long b,double c) { x=a;y=b;z=c; } // ********************************************** AUXILIARY FUNCTIONS ENDS **********************************************************************; //********************************************** INPUT FUNCTIONS ******************************************************************************; 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; } } //********************************************** INPUT FUNCTIONS ******************************************************************************; } class Comp implements Comparator<CF350C> { public int compare(CF350C o1,CF350C o2) { if(o1.z==o2.z)return 0; if(o1.z>o2.z)return 1; return -1; } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
78b4040f3f1fe5d55c4446d8e213bb94
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.util.*;import java.io.*;import java.math.*; public class Main { static class Node{ int x; int y; Node(int x,int y){ this.x=x; this.y=y; } } public static class CustomSort implements Comparator<Node>{ public int compare(Node a,Node b){ if(Math.abs(a.x)!=Math.abs(b.x)) return (Math.abs(a.x)-Math.abs(b.x)); return Math.abs(a.y)-Math.abs(b.y); } } public static void process()throws IOException { int n=ni(); ArrayList<String>list=new ArrayList<>(); Node[]A=new Node[n]; for(int i=0;i<n;i++) A[i]=new Node(ni(),ni()); Arrays.sort(A,new CustomSort()); for(int i=0;i<n;i++) { int x=A[i].x; int y=A[i].y; if(x!=0) list.add(ins(x,1)); if(y!=0) list.add(ins(y,2)); list.add("2"); if(x!=0) list.add(ins(x,-1)); if(y!=0) list.add(ins(y,-2)); list.add("3"); } pn(list.size()); for(int i=0;i<list.size();i++) pn(list.get(i)); } static String ins(int x,int a) { String s="1 "+Integer.toString(Math.abs(x))+" "; if(Math.abs(a)==1) { if(x*a>0) s+="R"; else s+="L"; } else { if(x*a>0) s+="U"; else s+="D"; } return s; } static AnotherReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { boolean oj = System.getProperty("ONLINE_JUDGE") != null; if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);} else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");} int t=1; // t=ni(); while(t-->0) {process();} out.flush();out.close(); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static void pni(Object o){out.println(o);out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;} static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
0965d8b359949e28cdd2b15368b93adc
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Hello { 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 class pair { int fi = 0, se = 0; public pair(int x, int y) { this.fi = x; this.se = y; } } public static void main(String[] args) { FastReader in = new FastReader(); OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); // start code here int n = in.nextInt(); pair a[] = new pair[n]; int res = 0; for (int i = 0; i < n; ++i) { int x = in.nextInt(); int y = in.nextInt(); res += 2; if (x != 0) res += 2; if (y != 0) res += 2; a[i] = new pair(x, y); } // StackOverflow help taken :) Arrays.sort(a, new Comparator<pair>() { @Override public int compare(pair a, pair b) { int d1 = Math.abs(a.fi) + Math.abs(a.se); int d2 = Math.abs(b.fi) + Math.abs(b.se); Integer v1 = d1; Integer v2 = d2; return v1.compareTo(v2); } }); out.println(res); for (int i = 0; i < n; ++i) { if (a[i].fi > 0) out.println("1 " + a[i].fi + " R"); else if (a[i].fi < 0) out.println("1 " + -a[i].fi + " L"); if (a[i].se > 0) out.println("1 " + a[i].se + " U"); else if (a[i].se < 0) out.println("1 " + -a[i].se + " D"); out.println("2"); if (a[i].fi > 0) out.println("1 " + a[i].fi + " L"); else if (a[i].fi < 0) out.println("1 " + -a[i].fi + " R"); if (a[i].se > 0) out.println("1 " + a[i].se + " D"); else if (a[i].se < 0) out.println("1 " + -a[i].se + " U"); out.println("3"); } out.close(); } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
aba9c66ce53fee54e6335457bb7cf2b9
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.util.Comparator; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author KharYusuf */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CBombs solver = new CBombs(); solver.solve(1, in, out); out.close(); } static class CBombs { public void solve(int testNumber, FastReader s, PrintWriter w) { int n = s.nextInt(), tot = n << 1; pair<Integer, Integer> p[] = new pair[n]; for (int i = 0; i < n; i++) { p[i] = new pair(s.nextInt(), s.nextInt()); if (Math.abs(p[i].x) > 0) { tot += 2; } if (Math.abs(p[i].y) > 0) { tot += 2; } } Arrays.sort(p, new Comparator<pair<Integer, Integer>>() { public int compare(pair<Integer, Integer> o1, pair<Integer, Integer> o2) { if (Math.abs(o1.x) > Math.abs(o2.x)) return 1; if (Math.abs(o1.x) < Math.abs(o2.x)) return -1; if (Math.abs(o1.y) > Math.abs(o2.y)) return 1; if (Math.abs(o1.y) < Math.abs(o2.y)) return -1; return 0; } }); w.println(tot); for (int i = 0; i < n; i++) { if (p[i].x > 0) { w.println(1 + " " + Math.abs(p[i].x) + " R"); } else if (p[i].x < 0) { w.println(1 + " " + Math.abs(p[i].x) + " L"); } if (p[i].y > 0) { w.println(1 + " " + Math.abs(p[i].y) + " U"); } else if (p[i].y < 0) { w.println(1 + " " + Math.abs(p[i].y) + " D"); } w.println(2); if (p[i].y > 0) { w.println(1 + " " + Math.abs(p[i].y) + " D"); } else if (p[i].y < 0) { w.println(1 + " " + Math.abs(p[i].y) + " U"); } if (p[i].x > 0) { w.println(1 + " " + Math.abs(p[i].x) + " L"); } else if (p[i].x < 0) { w.println(1 + " " + Math.abs(p[i].x) + " R"); } w.println(3); } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<pair<U, V>> { public U x; public V y; public pair(U x, V y) { this.x = x; this.y = y; } public int compareTo(pair<U, V> other) { if (this.x.compareTo(other.x) != 0) return this.x.compareTo(other.x); return this.y.compareTo(other.y); } public String toString() { return x.toString() + "," + y.toString(); } public boolean equals(Object obj) { pair<U, V> other = (pair<U, V>) obj; return this.x.equals(other.x) && this.y.equals(other.y); } public int hashCode() { return toString().hashCode(); } } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
1238b94ad8e69fc97ebdc4fbe4a497be
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.awt.*; import java.io.*; import java.util.*; public class Main { static Main.MyScanner sc = new Main.MyScanner(); static PrintWriter out = new PrintWriter(System.out); // static PrintStream out = System.out; public static void main(String[] args) { int n = sc.nextInt() ; Point arr[] = new Point[n] ; for(int i = 0 ; i < n ; i++) arr[i] = (new Point(sc.nextInt(), sc.nextInt())); Arrays.sort(arr,new Comparator<Point>() { @Override public int compare(Point a, Point b) { // if(a.x == b.x) // return Math.abs(a.y) - Math.abs(b.y) ; // return Math.abs(a.x) - Math.abs(b.x) ; return Math.abs(a.x) + Math.abs(a.y) - (Math.abs(b.x) + Math.abs(b.y)) ; } }); int ans = 0 ; StringBuilder print = new StringBuilder("") ; for (Point x : arr ) { ans += 2 ; if(x.x != 0) ans ++ ; if(x.y != 0) ans ++ ; if(x.x != 0) ans ++ ; if(x.y != 0) ans ++ ; if(x.x != 0) if( x.x > 0) print.append("1 " + Math.abs(x.x) + " R\n" ); else if( x.x < 0) print.append("1 " + Math.abs(x.x) + " L\n") ; if(x.y != 0) if( x.y > 0) print.append("1 " + Math.abs(x.y) + " U\n") ; else if( x.y < 0) print.append("1 " + Math.abs(x.y) + " D\n") ; print.append("2\n") ; if(x.y != 0) if( x.y > 0) print.append("1 " + Math.abs(x.y) + " D\n") ; else if( x.y < 0) print.append("1 " + Math.abs(x.y) + " U\n") ; if(x.x != 0) if( x.x > 0) print.append("1 " + Math.abs(x.x) + " L\n" ); else if( x.x < 0) print.append("1 " + Math.abs(x.x) + " R\n" ); print.append("3\n") ; } out.println(ans) ; out.print(print) ; out.close(); } static int findGCD(int number1, int number2) { //base case if(number2 == 0){ return number1; } return findGCD(number2, number1%number2); } static private class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public int mod(long x) { // TODO Auto-generated method stub return (int) x % 1000000007; } public int mod(int x) { return x % 1000000007; } boolean hasNext() { if (st.hasMoreElements()) return true; try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.hasMoreTokens(); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public long nextLong() { return Long.parseLong(next()); } } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
c5afccf6d2903ea13c8b7071b7f44d54
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
/* Keep solving problems. */ import java.util.*; import java.io.*; public class CFA { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; final long MOD = 1000L * 1000L * 1000L * 2 + 7; int[] dx = {-1, 1, 0, 0}; int[] dy = {0, 0, -1, 1}; class Node implements Comparable<Node> { int x; int y; public Node(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Node o) { return Math.abs(this.x) + Math.abs(this.y) - Math.abs(o.x) - Math.abs(o.y); } } void solve() throws IOException { int n = nextInt(); Node[] arr = new Node[n]; for (int i = 0; i < n; i++) { int xi = nextInt(); int yi = nextInt(); arr[i] = new Node(xi, yi); } Arrays.sort(arr); List<String> res = new ArrayList<>(); for (int i = 0; i < n; i++) { Node des = arr[i]; int dx = des.x; int dy = des.y; String up = "1 " + Math.abs(dx); if (dx > 0) { up += " R"; res.add(up); } else if (dx < 0) { up += " L"; res.add(up); } String hor = "1 " + Math.abs(dy); if (dy > 0) { hor += " U"; res.add(hor); } else if (dy < 0) { hor += " D"; res.add(hor); } res.add("2"); up = "1 " + Math.abs(dx); if (dx > 0) { up += " L"; res.add(up); } else if (dx < 0) { up += " R"; res.add(up); } hor = "1 " + Math.abs(dy); if (dy > 0) { hor += " D"; res.add(hor); } else if (dy < 0) { hor += " U"; res.add(hor); } res.add("3"); } outln(res.size()); for (int i = 0; i < res.size(); i++) { outln(res.get(i)); } } void shuffle(int[] a) { int n = a.length; for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } int gcd(int a, int b) { while(a != 0 && b != 0) { int c = b; b = a % b; a = c; } return a + b; } private void outln(Object o) { out.println(o); } private void out(Object o) { out.print(o); } public CFA() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new CFA(); } public long[] nextLongArr(int n) throws IOException{ long[] res = new long[n]; for(int i = 0; i < n; i++) res[i] = nextLong(); return res; } public int[] nextIntArr(int n) throws IOException { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } public String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
4ff4e661d23f72b0f4892f5226864010
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.util.*; import java.io.*; public class Main { class pair{ int x; int y; } class sort implements Comparator<pair>{ public int compare(pair a, pair b) { if(Math.abs(a.y)==Math.abs(b.y)) { return Math.abs(a.x) - Math.abs(b.x); } else { return Math.abs(a.y) - Math.abs(b.y); } } } public static void main(String args[]){ InputReader obj = new InputReader(System.in); Main ob = new Main(); int n=obj.nextInt(); pair arr[] = new pair[n]; int k=0; for(int j=0;j<n;j++){ arr[j] = ob.new pair(); arr[j].x=obj.nextInt(); arr[j].y=obj.nextInt(); if(arr[j].x==0 || arr[j].y==0){ k+=4; } else{ k+=6; } } Arrays.sort(arr, ob.new sort()); System.out.println(k); StringBuilder str = new StringBuilder(); for(int j=0;j<n;j++){ if(arr[j].x==0){ if(arr[j].y>0){ str.append(1+" "+arr[j].y+" U"); str.append("\n"); str.append(2); str.append("\n"); str.append(1+" "+arr[j].y+" D"); str.append("\n"); str.append(3); str.append("\n"); } else{ str.append(1+" "+Math.abs(arr[j].y)+" D"); str.append("\n"); str.append(2); str.append("\n"); str.append(1+" "+Math.abs(arr[j].y)+" U"); str.append("\n"); str.append(3); str.append("\n"); } } else if(arr[j].y==0){ if(arr[j].x>0){ str.append(1+" "+arr[j].x+" R"); str.append("\n"); str.append(2); str.append("\n"); str.append(1+" "+arr[j].x+" L"); str.append("\n"); str.append(3); str.append("\n"); } else{ str.append(1+" "+Math.abs(arr[j].x)+" L"); str.append("\n"); str.append(2); str.append("\n"); str.append(1+" "+Math.abs(arr[j].x)+" R"); str.append("\n"); str.append(3); str.append("\n"); } } else{ if(arr[j].x>0){ str.append(1+" "+arr[j].x+" R"); str.append("\n"); } else{ str.append(1+" "+Math.abs(arr[j].x)+" L"); str.append("\n"); } if(arr[j].y>0){ str.append(1+" "+arr[j].y+" U"); str.append("\n"); } else{ str.append(1+" "+Math.abs(arr[j].y)+" D"); str.append("\n"); } str.append(2); str.append("\n"); if(arr[j].x>0){ str.append(1+" "+arr[j].x+" L"); str.append("\n"); } else{ str.append(1+" "+Math.abs(arr[j].x)+" R"); str.append("\n"); } if(arr[j].y>0){ str.append(1+" "+arr[j].y+" D"); str.append("\n"); } else{ str.append(1+" "+Math.abs(arr[j].y)+" U"); str.append("\n"); } str.append(3); str.append("\n"); } } System.out.println(str); } public 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()); } } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
3a915e133c4388fdeab611c07e588b61
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Bombs { static StringBuilder sb = new StringBuilder(); public static void destroy(int x,int y) { if (x==0) { if (y>0) { sb.append("1 "+y+" U\n"); sb.append("2\n"); sb.append("1 "+y+" D\n"); sb.append("3\n"); return; } else { y*=-1; sb.append("1 "+y+" D\n"); sb.append("2\n"); sb.append("1 "+y+" U\n"); sb.append("3\n"); return; } } if (y==0) { if (x>0) { sb.append("1 "+x+" R\n"); sb.append("2\n"); sb.append("1 "+x+" L\n"); sb.append("3\n"); return; } else { x*=-1; sb.append("1 "+x+" L\n"); sb.append("2\n"); sb.append("1 "+x+" R\n"); sb.append("3\n"); return; } } if (x>0) sb.append("1 "+x+" R\n"); else sb.append("1 "+(x*-1)+" L\n"); if (y>0) sb.append("1 "+y+" U\n"); else sb.append("1 "+(y*-1)+" D\n"); sb.append("2\n"); if (y>0) sb.append("1 "+y+" D\n"); else sb.append("1 "+(y*-1)+" U\n"); if (x>0) sb.append("1 "+x+" L\n"); else sb.append("1 "+(x*-1)+" R\n"); sb.append("3\n"); } public static void main(String args[]) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); PriorityQueue<bomb> q = new PriorityQueue<bomb>(); int steps = 0; for (int i=0;i<n;i++) { st = new StringTokenizer(br.readLine()); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); q.add(new bomb(x,y)); if (x==0 || y==0) steps+=4; else steps+=6; } System.out.println(steps); while (!q.isEmpty()) { bomb b = q.remove(); // System.out.println(b.x+" "+b.y); destroy(b.x, b.y); } System.out.println(sb); } public static class bomb implements Comparable { int x; int y; public bomb(int x,int y) { this.x = x; this.y = y; } @Override public int compareTo(Object o) { bomb b = (bomb) o; if (this.y==0 && b.y==0) return Math.abs(this.x)-Math.abs(b.x); // if (this.x==) if (this.y==0) return -1; if (b.y==0) return 1; if (this.x==0 && b.x==0) return Math.abs(this.y)-Math.abs(b.y); if (this.x==0) return -1; if (b.x==0) return 1; if (b.y==this.y) return Math.abs(this.x)-Math.abs(b.x); return Math.abs(this.y)-Math.abs(b.y); } } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
7964bc5aaf203ed219b52ccad11f8f34
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class Bombs { static StringBuilder sb; static int total; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); Point o = new Point(0, 0); Point a[] = new Point[n]; total = 0; sb = new StringBuilder(); for (int i = 0; i < n; i++) { int x = sc.nextInt(), y = sc.nextInt(); a[i] = new Point(x, y); } Arrays.sort(a); for (int i = 0; i < n; i++) { Point cur = a[i]; go(o, new Point(cur.x, cur.y)); sb.append(2 + "\n"); total++; go(new Point(cur.x, cur.y), o); sb.append(3 + "\n"); total++; } out.println(total); out.println(sb); out.flush(); out.close(); } static void go(Point p, Point q) { if (p.x < q.x) { sb.append(1 + " " + (q.x-p.x) + " " + "R\n"); total++; } else if (p.x > q.x) { sb.append(1 + " " + (p.x-q.x) + " " + "L\n"); total++; } if (p.y < q.y) { sb.append(1 + " " + (q.y-p.y) + " " + "U\n"); total++; } else if (p.y > q.y) { sb.append(1 + " " + (p.y-q.y) + " " + "D\n"); total++; } } static long distance(Point p, Point q) { return sq(p.x-q.x) + sq(p.y-q.y); } static long sq(long n) { return n*n; } static class Point implements Comparable<Point> { int x, y; long dist; public Point(int x, int y) { this.x = x; this.y = y; dist = sq(x) + sq(y); } @Override public int compareTo(Point o) { if (dist != o.dist) return dist > o.dist ? 1 : -1; if (x != o.x) return x > o.x ? 1 : -1; return y > o.y ? 1 : -1; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(FileReader f) { br = new BufferedReader(f); } public Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean Ready() throws IOException { return br.ready(); } public void waitForInput(long time) { long ct = System.currentTimeMillis(); while(System.currentTimeMillis() - ct < time) {}; } } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
16640e61bdc65841dc276f42623cbd74
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
//package c203; import java.util.Arrays; import java.util.Scanner; public class q3 { static ver[] v; public static void main(String args[]) throws Exception { //System.setProperty("java.util.Arrays.useLegacyMergeSort", "true"); Scanner s=new Scanner(System.in); int n=s.nextInt(); v =new ver[n]; for(int i=0;i<n;i++) { v[i]=new ver(s.nextLong(),s.nextLong()); } Arrays.sort(v); long ans=0; for(int i=0;i<n;i++) { //System.out.println(v[i].x+" "+v[i].y); ans+=print(v[i],new ver(0,0)); ans++; st.append("3 \n"); } System.out.println(ans); System.out.println(st); } static StringBuilder st=new StringBuilder(); public static int print(ver v,ver tmp) { if(v.x==tmp.x && v.y==tmp.y) { st.append("2").append("\n"); return 1; } if(v.x!=tmp.x) { long dif=tmp.x-v.x; if(dif<0) st.append("1 ").append(Math.abs(dif)).append(" R\n"); else st.append("1 ").append(Math.abs(dif)).append(" L\n"); tmp.x=v.x; long at=print(v,tmp); if(dif<0) st.append("1 ").append(Math.abs(dif)).append(" L\n"); else st.append("1 ").append(Math.abs(dif)).append(" R\n"); return 2+(int)at; } else { long dif=tmp.y-v.y; if(dif<0) st.append("1 ").append(Math.abs(dif)).append(" U\n"); else st.append("1 ").append(Math.abs(dif)).append(" D\n"); tmp.y=v.y; long at=print(v,tmp); if(dif<0) st.append("1 ").append(Math.abs(dif)).append(" D\n"); else st.append("1 ").append(Math.abs(dif)).append(" U\n"); return 2+(int)at; } } } class ver implements Comparable<ver> { long x,y; public ver(long x,long y) { this.x=x; this.y=y; } @Override public int compareTo(ver arg0) { // TODO Auto-generated method stub double a=Math.sqrt(x*x+y*y); double b=Math.sqrt(arg0.x * arg0.x+arg0.y * arg0.y); //System.out.println(a+" sjhd"+b); if(a>b) return +1; else if(a==b) return 0; else return -1; } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
b50c783cd28b0acb9fbba79dad3f2082
train_003.jsonl
1380641400
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; import java.util.HashMap; public class main { static class Reader { private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;} public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];} public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;} public String s(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();} public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;} public int i(){int c = read() ;while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }int res = 0;do{if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read() ;}while(!isSpaceChar(c));return res * sgn;} public double d() throws IOException {return Double.parseDouble(s()) ;} public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////// static class pair { int x; int y; public pair (int k, int p) { x = k; y = p; } } static class PriorityComparator implements Comparator<pair> { public int compare(pair first, pair second) { return Math.abs(first.x)+Math.abs(first.y)-Math.abs(second.x)-Math.abs(second.y); } } public static void main(String args[]) { Reader sc=new Reader(); PrintWriter out=new PrintWriter(System.out); PriorityComparator pc = new PriorityComparator(); int n=sc.i(); PriorityQueue<pair> pq=new PriorityQueue<pair>(pc); for(int i=0;i<n;i++) pq.add(new pair(sc.i(),sc.i())); int counter=0; StringBuilder s=new StringBuilder(""); while(pq.size()!=0) { pair p=pq.remove(); int x=p.x; int y=p.y; if(x>0&&y>0) { s=s.append(1+" "+x+" R\n"); s=s.append(1+" "+y+" U\n"); s=s.append("2\n"); s=s.append(1+" "+y+" D\n"); s=s.append(1+" "+x+" L\n"); counter+=6; } else if(x<0&&y<0) { s=s.append(1+" "+-x+" L\n"); s=s.append(1+" "+-y+" D\n"); s=s.append("2\n"); s=s.append(1+" "+-y+" U\n"); s=s.append(1+" "+-x+" R\n"); counter+=6; } else if(x<0&&y>0) { s=s.append(1+" "+-x+" L\n"); s=s.append(1+" "+y+" U\n"); s=s.append("2\n"); s=s.append(1+" "+y+" D\n"); s=s.append(1+" "+-x+" R\n"); counter+=6; } else if(x>0&&y<0) { s=s.append(1+" "+x+" R\n"); s=s.append(1+" "+-y+" D\n"); s=s.append("2\n"); s=s.append(1+" "+-y+" U\n"); s=s.append(1+" "+x+" L\n"); counter+=6; } else if(x>0) { s=s.append(1+" "+x+" R\n"); s=s.append("2\n"); s=s.append(1+" "+x+" L\n"); counter+=4; } else if(y>0) { s=s.append(1+" "+y+" U\n"); s=s.append("2\n"); s=s.append(1+" "+y+" D\n"); counter+=4; } else if(x<0) { s=s.append(1+" "+-x+" L\n"); s=s.append("2\n"); s=s.append(1+" "+-x+" R\n"); counter+=4; } else { s=s.append(1+" "+-y+" D\n"); s=s.append("2\n"); s=s.append(1+" "+-y+" U\n"); counter+=4; } s=s.append("3\n"); } out.println(counter); out.println(s); out.flush(); } }
Java
["2\n1 1\n-1 -1", "3\n5 0\n0 5\n1 0"]
2 seconds
["12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3", "12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"]
null
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
a7c1b9845ab0569e0175853db9ed5c32
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
1,600
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
standard output
PASSED
b1d7e701e4b0243f3614a2adcf6daa82
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
//package leto; import java.io.*; import java.util.StringTokenizer; public class rofl { FastScanner in; PrintWriter out; public static void main(String[] arg) { new rofl().run(); } public void solve() throws IOException { // Scanner scan = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[][] gr = new int[n + 1][m + 1]; int[][] d = new int[Math.max(n, m) + 1][5]; int[][] sum = new int[Math.max(n, m) + 1][3]; for (int i = 1; i <= Math.max(n,m); i++) { d[i][1] = Integer.MAX_VALUE; d[i][2] = Integer.MAX_VALUE; d[i][3] = Integer.MAX_VALUE; d[i][4] = Integer.MAX_VALUE; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { gr[i][j] = in.nextInt(); if (gr[i][j] == 1) { sum[i][1]++; sum[j][2]++; if (j - 1 <= d[i][1]) { d[i][1] = j-1; } if (m - j <= d[i][2]) { d[i][2] = m - j; } if (i - 1 <= d[j][3]) { d[j][3] = i - 1; } if (n - i <= d[j][4]) { d[j][4] = n - i; } } } } int sum1 = 0; for (int i = 1; i <= n; i++) { if (d[i][1] != Integer.MAX_VALUE) { sum1 += 2 * m - 2 * sum[i][1] - d[i][1] - d[i][2]; } } for (int i = 1; i <= m; i++) { if (d[i][3] != Integer.MAX_VALUE) { sum1 += 2 * n - 2 * sum[i][2] - d[i][3] - d[i][4]; } } out.println(sum1); } public void run() { try { in = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(BufferedReader bufferedReader) { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
2ba508df1a6005c34afb909fdbd87dde
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
// Author @ BlackRise :) // // Birla Institute of Technology, Mesra// import java.io.*; import java.util.*; public class spotLights { static void Blackrise() { //The name Blackrise is my pen name... you can change the name according to your wish int n = ni(), m = ni(); int a[][] = new int[n+1][m+1]; int s[][]=new int[n+1][m+1]; tsc(); for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ a[i][j]=ni(); s[i][j] = a[i][j] + s[i-1][j] + s[i][j-1] - s[i-1][j-1]; } } int ans = 0; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(a[i][j]==1) continue; if(s[i][j]-s[i-1][j] > 0) ans++;; if(s[i][j]-s[i][j-1] > 0) ans++;; if((s[n][j]-s[i][j])-s[n][j-1]+s[i][j-1] > 0) ans++; if((s[i][m]-s[i][j])-s[i-1][m]+s[i-1][j] > 0) ans++; } } pl(ans); tec(); //calculates the ending time of execution //pwt(); //prints the time taken to execute the program flush(); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// static Calendar ts, te; //For time calculation static int mod9 = 1000000007; static int n,t,MOD=998244353; static Lelo input = new Lelo(System.in); static PrintWriter pw = new PrintWriter(System.out, true); public static void main(String[] args) { //threading has been used to increase the stack size. new Thread(null, null, "BlackRise", 1<<25) //the last parameter is stack size which is desired, { public void run() { try { Blackrise(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } static class Lelo { //Lelo class for fast input private InputStream ayega; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public Lelo(InputStream ayega) { this.ayega = ayega; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = ayega.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } // functions to take input// static int ni() { return input.nextInt(); } static long nl() { return input.nextLong(); } static double nd() { return input.nextDouble(); } static String ns() { return input.readString(); } //functions to give output static void pl() { pw.println(); } static void p(Object o) { pw.print(o + " "); } static void pws(Object o){ pw.print(o+""); } static void pl(Object o) { pw.println(o); } static void tsc() //calculates the starting time of execution { ts = Calendar.getInstance(); ts.setTime(new Date()); } static void tec() //calculates the ending time of execution { te = Calendar.getInstance(); te.setTime(new Date()); } static void pwt() //prints the time taken for execution { pw.printf("\nExecution time was :- %f s\n", (te.getTimeInMillis() - ts.getTimeInMillis()) / 1000.00); } static void flush() { pw.flush();pw.close(); } static void sort(int ar[]) { for(int i=0;i<n;i++) { int ran=(int)(Math.random()*n); int temp=ar[i]; ar[i]=ar[ran]; ar[ran]=temp; } Arrays.sort(ar); } static void sort(long ar[]) { for(int i=0;i<n;i++) { int ran=(int)(Math.random()*n); long temp=ar[i]; ar[i]=ar[ran]; ar[ran]=temp; } Arrays.sort(ar); } static void sort(char ar[]) { for(int i=0;i<n;i++) { int ran=(int)(Math.random()*n); char temp=ar[i]; ar[i]=ar[ran]; ar[ran]=temp; } Arrays.sort(ar); } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
cad523f3ffc817190ba22c0eef7b5d63
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main (String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String st[] = br.readLine().split(" "); int n=Integer.parseInt(st[0]); int m=Integer.parseInt(st[1]); int input[][]=new int[n][m]; for(int i=0;i<n;i++){ st=br.readLine().split(" "); for(int j=0;j<m;j++){ input[i][j]=Integer.parseInt(st[j]); } } boolean left[][]=new boolean[n][m]; boolean right[][]=new boolean[n][m]; boolean up[][]=new boolean[n][m]; boolean down[][]=new boolean[n][m]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(input[i][j]==1){ left[i][j]=true; up[i][j]=true; } if(j>0&&!left[i][j]){ left[i][j]=left[i][j-1]; } if(i>0&&!up[i][j]){ up[i][j]=up[i-1][j]; } } } for(int i=n-1;i>=0;i--){ for(int j=m-1;j>=0;j--){ if(input[i][j]==1){ down[i][j]=true; right[i][j]=true; } if(j<m-1&&!right[i][j]){ right[i][j]=right[i][j+1]; } if(i<n-1&&!down[i][j]){ down[i][j]=down[i+1][j]; } } } int count=0; int output[][]=new int[n][m]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(input[i][j]==0){ if(up[i][j])count++; if(down[i][j])count++; if(right[i][j])count++; if(left[i][j])count++; } // System.out.print(output[i][j]+" "); } } System.out.println(count); // System.out.println(up[1][1]+" "+down[1][1]+" "+right[1][1]+" "+left[1][1]); // System.out.print(right[0][0]+" "+right[1][1]+" "+right[1][2]); } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
627a185c3d61da9d28e38a04ef949194
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; import java.lang.*; import static java.lang.Math.*; public class Main implements Runnable { public static void main(String[] args) { new Thread(null, new Main(), "Check2", 1 << 28).start();// to increse stack size in java } void init(ArrayList <Integer> adj[], int n){ for(int i=0;i<=n;i++)adj[i]=new ArrayList<>(); } static long mod = (long) (1e9+7); public void run() { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n=in.nextInt(); int m=in.nextInt(); int a[][]=new int[n][m]; for(int i=0;i<n;i++)for(int j=0;j<m;j++)a[i][j]=in.nextInt(); int row[][]=new int[n][m]; int col[][]=new int[n][m]; int row1[][]=new int[n][m]; int col1[][]=new int[n][m]; for(int i=0;i<n;i++){ int co=0; for(int j=0;j<m;j++){ co=co+a[i][j]; row[i][j]=co; } co=0; for(int j=m-1;j>=0;j--){ co=co+a[i][j]; row1[i][j]=co; } } for(int i=0;i<m;i++){ int co=0; for(int j=0;j<n;j++){ co=co+a[j][i]; col[j][i]=co; } co=0; for(int j=n-1;j>=0;j--){ co=co+a[j][i]; col1[j][i]=co; } } int ans=0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(a[i][j]==0){ if(row[i][j]>0)ans++; if(row1[i][j]>0)ans++; } } } for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(a[j][i]==0){ if(col[j][i]>0)ans++; if(col1[j][i]>0)ans++; } } } w.println(ans); w.close(); } class pair2{ int a,b; pair2(int a,int b){ this.a=a; this.b=b; } } class pair { int a; long b; pair(int a,long b){ this.a=a; this.b=b; } public boolean equals(Object obj) { // override equals method for object to remove tem from arraylist and sets etc....... if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; pair other = (pair) obj; if (b!= other.b||a!=other.a) return false; return true; } } static long modinv(long a,long b) { long p=power(b,mod-2); p=a%mod*p%mod; p%=mod; return p; } static long power(long x,long y){ if(y==0)return 1%mod; if(y==1)return x%mod; long res=1; x=x%mod; while(y>0){ if((y%2)!=0){ res=(res*x)%mod; } y=y/2; x=(x*x)%mod; } return res; } static long gcd(long a,long b){ if(b==0)return a; return gcd(b,a%b); } static void sev(int a[],int n){ for(int i=2;i<=n;i++)a[i]=i; for(int i=2;i<=n;i++){ if(a[i]!=0){ for(int j=2*i;j<=n;){ a[j]=0; j=j+i; } } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
a13086b66bc771119fd4e1f82e9036c2
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class B729 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(reader.readLine()); int R = Integer.parseInt(tokenizer.nextToken()); int C = Integer.parseInt(tokenizer.nextToken()); boolean[][] A = new boolean[R][C]; for (int r=0; r<R; r++) { tokenizer = new StringTokenizer(reader.readLine()); for (int c=0; c<C; c++) { A[r][c] = (Integer.parseInt(tokenizer.nextToken()) == 1); } } int[] minR = new int[R]; int[] maxR = new int[R]; for (int r=0; r<R; r++) { minR[r] = Integer.MAX_VALUE; maxR[r] = Integer.MIN_VALUE; for (int c=0; c<C; c++) { if (A[r][c]) { minR[r] = Math.min(minR[r], c); maxR[r] = c; } } } int[] minC = new int[C]; int[] maxC = new int[C]; for (int c=0; c<C; c++) { minC[c] = Integer.MAX_VALUE; maxC[c] = Integer.MIN_VALUE; for (int r=0; r<R; r++) { if (A[r][c]) { minC[c] = Math.min(minC[c], r); maxC[c] = r; } } } int answer = 0; for (int r=0; r<R; r++) { for (int c=0; c<C; c++) { if (!A[r][c]) { if (minR[r] < c) { answer++; } if (maxR[r] > c) { answer++; } if (minC[c] < r) { answer++; } if (maxC[c] > r) { answer++; } } } } System.out.println(answer); } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
477482984067aabfe50755e30dace9d7
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class C304_546SoldiersCards { public static void main(String args[]) throws FileNotFoundException { InputReader in = new InputReader(System.in); OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); StringBuilder sb = new StringBuilder(); // ----------My Code---------- /* * int n=in.nextInt(); int n1,n2; int a[],b[]; n1=in.nextInt(); * a=in.nextIntArray(n1, 0); n2=in.nextInt(); b=in.nextIntArray(n2, 0); */ int n, m; n = in.nextInt(); m = in.nextInt(); int arr[][] = new int[n + 1][m + 1]; for (int i = 1; i <= n; i++) { arr[i] = in.nextIntArray(m, 1); } int cnt = 0; for (int i = 1; i <= n; i++) { boolean hasLeft = false; for (int j = 1; j <= m; j++) { if (arr[i][j] == 1) { hasLeft = true; } else if (hasLeft && arr[i][j] == 0) { cnt++; } } boolean hasRight = false; for (int j = m ; j >= 1; j--) { if (arr[i][j] == 1) { hasRight = true; } else if (hasRight && arr[i][j] == 0) { cnt++; } } } for (int i = 1; i <= m; i++) { boolean hasTop = false; for (int j = 1; j <= n; j++) { if (arr[j][i] == 1) { hasTop = true; } else if (arr[j][i] == 0 && hasTop) { cnt++; } } boolean hasDown = false; for (int j = n; j >= 1; j--) { if (arr[j][i] == 1) { hasDown = true; } else if (arr[j][i] == 0 && hasDown) { cnt++; } } } out.println(cnt); // ---------------The End------------------ out.close(); } // ---------------Extra Methods------------------ public static long pow(long x, long n, long mod) { long res = 1; x %= mod; while (n > 0) { if (n % 2 == 1) { res = (res * x) % mod; } x = (x * x) % mod; n /= 2; } return res; } public static boolean isPal(String s) { for (int i = 0, j = s.length() - 1; i <= j; i++, j--) { if (s.charAt(i) != s.charAt(j)) return false; } return true; } public static String rev(String s) { StringBuilder sb = new StringBuilder(s); sb.reverse(); return sb.toString(); } public static long gcd(long x, long y) { if (x % y == 0) return y; else return gcd(y, x % y); } public static int gcd(int x, int y) { if (x % y == 0) return y; else return gcd(y, x % y); } public static long gcdExtended(long a, long b, long[] x) { if (a == 0) { x[0] = 0; x[1] = 1; return b; } long[] y = new long[2]; long gcd = gcdExtended(b % a, a, y); x[0] = y[1] - (b / a) * y[0]; x[1] = y[0]; return gcd; } public static int abs(int a, int b) { return (int) Math.abs(a - b); } public static long abs(long a, long b) { return (long) Math.abs(a - b); } public static int max(int a, int b) { if (a > b) return a; else return b; } public static int min(int a, int b) { if (a > b) return b; else return a; } public static long max(long a, long b) { if (a > b) return a; else return b; } public static long min(long a, long b) { if (a > b) return b; else return a; } // ---------------Extra Methods------------------ public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream inputstream) { reader = new BufferedReader(new InputStreamReader(inputstream)); tokenizer = null; } public String nextLine() { String fullLine = null; while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { fullLine = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return fullLine; } return fullLine; } 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 char nextChar() { return next().charAt(0); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n, int f) { if (f == 0) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } else { int[] arr = new int[n + 1]; for (int i = 1; i < n + 1; i++) { arr[i] = nextInt(); } return arr; } } public long[] nextLongArray(int n, int f) { if (f == 0) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } else { long[] arr = new long[n + 1]; for (int i = 1; i < n + 1; i++) { arr[i] = nextLong(); } return arr; } } public double[] nextDoubleArray(int n, int f) { if (f == 0) { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = nextDouble(); } return arr; } else { double[] arr = new double[n + 1]; for (int i = 1; i < n + 1; i++) { arr[i] = nextDouble(); } return arr; } } } static class Pair implements Comparable<Pair> { int a, b; Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair o) { // TODO Auto-generated method stub if (this.a != o.a) return Integer.compare(this.a, o.a); else return Integer.compare(this.b, o.b); } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair) o; return p.a == a && p.b == b; } return false; } public int hashCode() { return new Integer(a).hashCode() * 31 + new Integer(b).hashCode(); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
02f78e43c4285890ed722e73798a09ff
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; import static java.lang.Math.abs; public class B { public static void main(String[] args) { MyScanner in = new MyScanner(); int n = in.nextInt(); int m = in.nextInt(); int[][] stage = new int[n][m]; int[][] actorsRows = new int[n + 1][m + 1]; int[][] actorsCols = new int[n + 1][m + 1]; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { stage[i][j] = in.nextInt(); if (stage[i][j] == 1) { actorsRows[i + 1][j + 1] = actorsRows[i + 1][j] + 1; actorsCols[i + 1][j + 1] = actorsCols[i][j + 1] + 1; } else { actorsRows[i + 1][j + 1] = actorsRows[i + 1][j]; actorsCols[i + 1][j + 1] = actorsCols[i][j + 1]; } } } int result = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (stage[i][j] == 0) { if (actorsRows[i + 1][j + 1] - actorsRows[i + 1][0] > 0) { // System.out.println(i+" "+j); result++; } if (actorsRows[i+1][m] - actorsRows[i + 1][j + 1] > 0) { // System.out.println(i+" "+j); result++; } if (actorsCols[i + 1][j + 1] - actorsCols[0][j + 1] > 0) { // System.out.println(i+" "+j); result++; } if (actorsCols[n][j+1] - actorsCols[i + 1][j + 1] > 0) { // System.out.println(i+" "+j); result++; } } } } System.out.println(result); } // -----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // -------------------------------------------------------- }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
37a17a1ce4587bee89aa3addfbc2a1ca
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
//package com.pb.codeforces.practice; import java.util.Scanner; public class CF729B { public static int processUp(int[][] mat) { int cnt = 0; int n = mat.length; int m = mat[0].length; for(int i=0; i<m; i++) { boolean set = false; for(int j=n-1; j>=0; j--) { if(mat[j][i] == 1) set = true; else cnt += set ? 1 : 0; } } return cnt; } public static int processDown(int[][] mat) { int cnt = 0; int n = mat.length; int m = mat[0].length; for(int i=0; i<m; i++) { boolean set = false; for(int j=0; j<n; j++) { if(mat[j][i] == 1) set = true; else cnt += set ? 1 : 0; } } return cnt; } public static int processLeft(int[][] mat) { int cnt = 0; int n = mat.length; int m = mat[0].length; for(int i=0; i<n; i++) { boolean set = false; for(int j=m-1; j>=0; j--) { if(mat[i][j] == 1) set = true; else cnt += set ? 1 : 0; } } return cnt; } public static int processRight(int[][] mat) { int cnt = 0; int n = mat.length; int m = mat[0].length; for(int i=0; i<n; i++) { boolean set = false; for(int j=0; j<m; j++) { if(mat[i][j] == 1) set = true; else cnt += set ? 1 : 0; } } return cnt; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[][] mat = new int[n][m]; for(int i=0; i<mat.length; i++) { for(int j=0; j<mat[0].length; j++) { mat[i][j] = in.nextInt(); } } int gcnt = 0; gcnt += processUp(mat); gcnt += processDown(mat); gcnt += processLeft(mat); gcnt += processRight(mat); System.out.println(gcnt); } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
cef56f98d9a90a78d3d6b8c3cd3587e8
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.*; import java.util.*; public class NoB { public class car { int c, v; } public void solve() throws IOException { int n = nextInt(); int m = nextInt(); int numF = 0; int numL = 0; int ans = 0; int count1 = 0; boolean ch = false; int data[][] = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { data[i][j] = nextInt(); if (data[i][j] == 1) { if (!ch) { numF = j; numL = j; ch = true; count1++; } else { count1++; numL = j; } } } if (ch) { ans += numL - numF - 1 - 2 * count1 + 2 + m; } count1 = 0; ch = false; numL = 0; numF = 0; } count1 = 0; ch = false; numL = 0; numF = 0; for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { if (data[i][j] == 1) { if (!ch) { numF = i; numL = i; ch = true; count1++; } else { count1++; numL = i; } } } if (ch) { ans += numL - numF - 1 - 2 * count1 + 2 + n; } count1 = 0; ch = false; numL = 0; numF = 0; } System.out.println(ans); } BufferedReader br; StringTokenizer sc; PrintWriter out; public String nextToken() throws IOException { while (sc == null || !sc.hasMoreTokens()) { try { sc = new StringTokenizer(br.readLine()); } catch (Exception e) { return null; } } return sc.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public boolean hasNext() { while (sc == null || !sc.hasMoreTokens()) { try { String s = br.readLine(); if (s == null) { return false; } sc = new StringTokenizer(s); } catch (IOException e) { throw new RuntimeException(e); } } return sc.hasMoreTokens(); } public static void main(String[] args) throws IOException { Locale.setDefault(Locale.US); new NoB().run(); } public void run() { try { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); //br = new BufferedReader(new FileReader("retro.in")); //out = new PrintWriter("retro.out"); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
0151ea26a59656aed7554d033e414224
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { FastScanner sc=new FastScanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); int c=0; int a[][]=new int[n][m]; for (int i = 0; i < n; i++) { int z=0; boolean f=false; for (int j = 0; j < m; j++) { int x=sc.nextInt(); a[i][j]=x; if(x==0){ if(f)c++; z++; }else { f=true; c+=z; z=0; } } } for (int i = 0; i < m; i++) { int z=0; boolean f=false; for (int j = 0; j < n; j++) { int x=a[j][i]; if(x==0){ if(f)c++; z++; }else { f=true; c+=z; z=0; } } } System.out.println(c); } //__________________________________________________________ static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream i) { br = new BufferedReader(new InputStreamReader(i)); st = new StringTokenizer(""); } public String next() throws IOException { if(st.hasMoreTokens())return st.nextToken(); else st = new StringTokenizer(br.readLine()); return next(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
844472bb0d857d21310700b74003c621
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String args[]) { try { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int a[][] = new int[n][m]; int sum = 0; boolean fD[] = new boolean[m]; for (int i = 0; i < n; i++) { boolean fL = false; for (int j = 0; j < m; j++) { a[i][j] = sc.nextInt(); if (a[i][j] == 1) { fL = true; fD[j] = true; } else { if (fL) sum += 1; if (fD[j]) sum += 1; } } } boolean fU[] = new boolean[m]; for (int i = n - 1; i >= 0; i--) { boolean fR = false; for (int j = m - 1; j >= 0; j--) { if (a[i][j] == 1) { fR = true; fU[j] = true; } else { if (fR ) sum += 1; if (fU[j]) sum += 1; } } } System.out.println(sum); } catch(Exception e) { } } } class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
fde6d5f5d761c5b4febb9349db5577c0
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String args[]) { try { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int a[][] = new int[n][m]; int sum = 0; boolean fD[] = new boolean[m]; for (int i = 0; i < n; i++) { boolean fL = false; for (int j = 0; j < m; j++) { a[i][j] = sc.nextInt(); if (a[i][j] == 1) { fL = true; fD[j] = true; } else { if (fL) sum += 1; if (fD[j]) sum += 1; } } } boolean fU[] = new boolean[m]; for (int i = n - 1; i >= 0; i--) { boolean fR = false; for (int j = m - 1; j >= 0; j--) { if (a[i][j] == 1) { fR = true; fU[j] = true; } else { if (fR ) sum += 1; if (fU[j]) sum += 1; } } } System.out.println(sum); } catch(Exception e) { } } } class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public boolean ready() throws IOException { return br.ready(); } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
dce0dec99c6c3ca6614da142e9a198fd
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
/** * Created by ankeet on 1/3/17. */ import java.io.*; import java.util.*; public class B729 { static FastReader in = null; static PrintWriter out = null; public static void solve() { int n = in.nextInt(); int m = in.nextInt(); boolean[][] mat = new boolean[n][m]; boolean[][] good = new boolean[n][m]; for(int i=0; i<n; i++) for(int j=0; j<m; j++) mat[i][j] = in.nextInt() == 1; int ct = 0; // up boolean[] has = new boolean[m]; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ if(has[j] && !mat[i][j]) ct++; if(mat[i][j]) has[j] = true; } } //down Arrays.fill(has, false); for(int i=n-1; i>=0; i--){ for(int j=0; j<m; j++){ if(has[j] && !mat[i][j]) ct++; if(mat[i][j]) has[j] = true; } } //right has = new boolean[n]; for(int j=m-1; j>=0; j--){ for(int i=0; i<n; i++){ if(has[i] && !mat[i][j]) ct++; if(mat[i][j]) has[i] = true; } } //left has = new boolean[n]; for(int j=0; j<m; j++){ for(int i=0; i<n; i++){ if(has[i] && !mat[i][j]) ct++; if(mat[i][j]) has[i] = true; } } out.println(ct); } public static void main(String[] args) { in = new FastReader(System.in); out = new PrintWriter(System.out); solve(); out.flush(); out.close(); } static class FastReader { BufferedReader read; StringTokenizer tokenizer; public FastReader(InputStream in) { read = new BufferedReader(new InputStreamReader(in)); } public String next() { while(tokenizer == null || !tokenizer.hasMoreTokens()) { try{ tokenizer = new StringTokenizer(read.readLine()); }catch(Exception e){ throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine(){ try { return read.readLine(); } catch(Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArr(int n) { int[] a = new int[n]; for(int i=0; i<n; ++i) { a[i] = nextInt(); } return a; } public long[] nextLongArr(int n) { long[] a = new long[n]; for(int i=0; i<n; ++i) { a[i] = nextLong(); } return a; } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
fcc39ef10a3ed19465f2eaa86484583f
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Parser in = new Parser(System.in); //Scanner in = new Scanner(new File("input.txt")); //PrintWriter out = new PrintWriter(new File("output.txt")); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int m = in.nextInt(); int[][] mas = new int[n+1][m+1]; int[][] stolbc = new int[n][m]; int[][] strocs = new int[n][m]; for(int i = 0; i < n; i++) for (int j = 0; j < m; j++) { mas[i][j] = in.nextInt(); if (i > 0) strocs[i][j] = strocs[i-1][j]; if(j > 0) stolbc[i][j] = stolbc[i][j-1]; if (mas[i][j] == 1) {stolbc[i][j]++; strocs[i][j]++;} } int sum = 0; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { if (mas[i][j] == 0){ if(strocs[i][j] > 0) sum++; if(Math.abs(strocs[i][j] - strocs[n-1][j]) > 0) sum++; if(stolbc[i][j] > 0) sum++; if(Math.abs(stolbc[i][j] - stolbc[i][m-1]) > 0) sum++; } } out.println(sum); out.close(); } static class Parser { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Parser(InputStream in) { din = new DataInputStream(in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextString(int maxSize) { byte[] ch = new byte[maxSize]; int point = 0; try { byte c = read(); while (c == ' ' || c == '\n' || c=='\r') c = read(); while (c != ' ' && c != '\n' && c!='\r') { ch[point++] = c; c = read(); } } catch (Exception e) {} return new String(ch, 0,point); } public int nextInt() { int ret = 0; boolean neg; try { byte c = read(); while (c <= ' ') c = read(); neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; } catch (Exception e) {} return ret; } public long nextLong() { long ret = 0; boolean neg; try { byte c = read(); while (c <= ' ') c = read(); neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; } catch (Exception e) {} return ret; } private void fillBuffer() { try { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); } catch (Exception e) {} if (bytesRead == -1) buffer[ 0] = -1; } private byte read() { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
755a40dd95dc4a86abc284c9c02a1db5
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.util.*; import java.io.*; public class cf { private static final Scanner sc = new Scanner(System.in); private static final PrintWriter pw = new PrintWriter(System.out); private static StringBuffer ans = new StringBuffer(); public static void main(String args[]) throws Exception { int n = sc.nextInt(), m = sc.nextInt(), sum = 0; int[][] a = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = sc.nextInt(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++){ if(a[i][j] == 1) { for(int k = i - 1; k >= 0 && a[k][j] == 0; k--)sum++; for(int k = i + 1; k < n && a[k][j] == 0; k++)sum++; for(int k = j - 1; k >= 0 && a[i][k] == 0; k--)sum++; for(int k = j + 1; k < m && a[i][k] == 0; k++)sum++; } } } ans.append(sum); pw.print(ans); sc.close(); pw.close(); } } class Scanner { private final BufferedReader br; private StringTokenizer st; public Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = new StringTokenizer(""); } public String next() throws IOException { if(!st.hasMoreTokens())st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public void close() throws IOException { br.close(); } } class PrintWriter { private final BufferedOutputStream pw; public PrintWriter(OutputStream out) { pw = new BufferedOutputStream(out); } public void print(Object o) throws IOException { pw.write(o.toString().trim().getBytes()); pw.flush(); } public void close() throws IOException { pw.close(); } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
34c56b230ae274da79de01e6fa62572d
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
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 */ 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); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.ni(); int m = in.ni(); int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) arr[i][j] = in.ni(); } int count = 0; for (int i = 0; i < n; i++) { boolean possible = (arr[i][0] == 1); for (int j = 1; j < m; j++) { if (arr[i][j] == 0 && possible) count++; possible = (arr[i][j] == 1) || possible; } possible = (arr[i][m - 1] == 1); for (int j = m - 2; j >= 0; j--) { if (arr[i][j] == 0 && possible) count++; possible = possible || (arr[i][j] == 1); } } for (int i = 0; i < m; i++) { boolean possible = (arr[0][i] == 1); for (int j = 1; j < n; j++) { if (arr[j][i] == 0 && possible) count++; possible = possible || (arr[j][i] == 1); } possible = (arr[n - 1][i] == 1); for (int j = n - 2; j >= 0; j--) { if (arr[j][i] == 0 && possible) count++; possible = possible || (arr[j][i] == 1); } } out.println(count); } } static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream inputStream) { br = new BufferedReader(new InputStreamReader(inputStream)); } public String n() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(); } } return st.nextToken(); } public int ni() { return Integer.parseInt(n()); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
342eedd8ec49dbe923b832ff3504e6f7
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author kanak893 */ 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); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader fi, PrintWriter out) { int n, m, i, j, people; n = fi.nextInt(); m = fi.nextInt(); int mat[][] = new int[n + 1][m + 1]; int horizontal[][] = new int[n + 1][m + 1]; int vertical[][] = new int[n + 1][m + 1]; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { mat[i][j] = fi.nextInt(); horizontal[i][j] = mat[i][j] + horizontal[i][j - 1]; vertical[i][j] = mat[i][j] + vertical[i - 1][j]; } } long ans = 0; /* for (i=1;i<=n;i++){ for (j=1;j<=m;j++) { out.print(horizontal[i][j] + " "); } out.println(); }*/ for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (mat[i][j] == 0) { // right people = horizontal[i][m] - horizontal[i][j]; if (people > 0) ans++; people = horizontal[i][j]; if (people > 0) ans++; people = vertical[n][j] - vertical[i][j]; if (people > 0) ans++; people = vertical[i][j]; if (people > 0) ans++; } } } out.println(ans); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int snumChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
f1eaf111915bb67abf15d9f4f84b6652
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import static java.lang.Boolean.*; import static java.lang.Integer.*; import static java.lang.Long.*; import static java.lang.Float.*; import static java.lang.Double.*; import java.io.InputStreamReader; import java.io.BufferedReader; import java.util.StringTokenizer; import java.io.PrintWriter; import java.io.IOException; import java.lang.NullPointerException; import java.lang.ArrayIndexOutOfBoundsException; public class Test { static Reader in; static PrintWriter out; /* ****************************** CLASS SOLVE *************************** */ static class Solve { final int N = 1010; final int M = 1010; boolean p[][] = new boolean[N][M]; boolean l[][] = new boolean[N][M]; boolean r[][] = new boolean[N][M]; boolean u[][] = new boolean[N][M]; boolean d[][] = new boolean[N][M]; void main() throws IOException { int n, m; n = in.nextInt(); m = in.nextInt(); for(int i= 0; i<n; i++) { for(int j=0; j<m; j++) { int k = in.nextInt(); if(k == 1) p[i][j] = true; } } // Left configure for(int i=0; i<n; i++) { int j; for(j=0; !p[i][j] && j<m; j++) ; for(++j; j<m; j++) l[i][j] = true; } // Right configure for(int i=n-1; i>=0; i--) { int j; for(j=m-1; j>=0 && !p[i][j]; j--) ; for(--j; j>=0; j--) r[i][j] = true; } // Up configure for(int j=0; j<m; j++) { int i; for(i=0; !p[i][j] && i<n; i++) ; for(++i; i<n; i++) u[i][j] = true; } // Down configure for(int j=m-1; j>=0; j--) { int i; for(i=n-1; i>=0 && !p[i][j]; i--) ; for(--i; i>=0; i--) d[i][j] = true; } int ans = 0; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(p[i][j] == false) { if(l[i][j]) ans++; if(r[i][j]) ans++; if(u[i][j]) ans++; if(d[i][j]) ans++; } } } out.println(ans); } } /* ************************* PUBLIC STATIC VOID MAIN *********************** */ public static void main(String args[]) { try { in = new Reader(); out = new PrintWriter(System.out, true); Solve s = new Solve(); s.main(); in.close(); out.close(); } catch(IOException e) { e.printStackTrace(); } } /***************************** READER CLASS ************************ */ static class Reader { BufferedReader br; StringTokenizer tk; Reader() { br = new BufferedReader(new InputStreamReader(System.in)); tk = new StringTokenizer(""); } String next() throws IOException { while(tk.hasMoreTokens() == false) { String str = br.readLine(); if(str == null) return null; tk = new StringTokenizer(str); } return tk.nextToken(); } String nextLine() throws IOException { String str = br.readLine(); while(str.length() == 0) { str = br.readLine(); } return str; } boolean nextBoolean() throws IOException { return Boolean.parseBoolean(next()); } char nextChar() throws IOException { return next().charAt(0); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } float nextFloat() throws IOException { return Float.parseFloat(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } void flush() throws IOException { tk = new StringTokenizer(""); } void close() throws IOException { br.close(); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
c7665c4a09a223f754a3bfd592648870
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import static java.lang.Boolean.*; import static java.lang.Byte.*; import static java.lang.Integer.*; import static java.lang.Long.*; import static java.lang.Float.*; import static java.lang.Double.*; import java.io.InputStreamReader; import java.io.BufferedReader; import java.util.StringTokenizer; import java.io.PrintWriter; import java.io.IOException; import java.lang.NullPointerException; import java.lang.ArrayIndexOutOfBoundsException; public class Test { static Reader in; static PrintWriter out; /* ******************** CLASS SOLVE ******************* */ static class Solve { void main() throws IOException { byte p[][] = new byte[1000][1000]; byte l[][] = new byte[1000][1000]; byte r[][] = new byte[1000][1000]; byte u[][] = new byte[1000][1000]; byte d[][] = new byte[1000][1000]; int n, m; n = in.nextInt(); m = in.nextInt(); for(int i= 0; i<n; i++) { for(int j=0; j<m; j++) { p[i][j] = in.nextByte(); } } // Left configure for(int i=0; i<n; i++) { int j; for(j=0; j<m && p[i][j] == 0; j++) ; for(++j; j<m; j++) l[i][j] = (byte)1; } // Right configure for(int i=n-1; i>=0; i--) { int j; for(j=m-1; j>=0 && p[i][j] == 0; j--) ; for(--j; j>=0; j--) r[i][j] = (byte)1; } // Up configure for(int j=0; j<m; j++) { int i; for(i=0; i<n && p[i][j] == 0; i++) ; for(++i; i<n; i++) u[i][j] = (byte)1; } // Down configure for(int j=m-1; j>=0; j--) { int i; for(i=n-1; i>=0 && p[i][j] == 0; i--) ; for(--i; i>=0; i--) d[i][j] = (byte)1; } int ans = 0; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(p[i][j] == 0) { if(l[i][j] == 1) ans++; if(r[i][j] == 1) ans++; if(u[i][j] == 1) ans++; if(d[i][j] == 1) ans++; } } } out.println(ans); } } /* ********************** PUBLIC STATIC VOID MAIN ********************* */ public static void main(String args[]) { try { in = new Reader(); out = new PrintWriter(System.out, true); Solve s = new Solve(); s.main(); in.close(); out.close(); } catch(IOException e) { e.printStackTrace(); } } /************************* READER CLASS ********************* */ static class Reader { BufferedReader br; StringTokenizer tk; Reader() { br = new BufferedReader(new InputStreamReader(System.in)); tk = new StringTokenizer(""); } String next() throws IOException { while(tk.hasMoreTokens() == false) { String str = br.readLine(); if(str == null) return null; tk = new StringTokenizer(str); } return tk.nextToken(); } String nextLine() throws IOException { String str = br.readLine(); while(str.length() == 0) { str = br.readLine(); } return str; } boolean nextBoolean() throws IOException { return Boolean.parseBoolean(next()); } char nextChar() throws IOException { return next().charAt(0); } byte nextByte() throws IOException { return Byte.parseByte(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } float nextFloat() throws IOException { return Float.parseFloat(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } void flush() throws IOException { tk = new StringTokenizer(""); } void close() throws IOException { br.close(); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
7384f36b43295556bfc0697ca4a200b2
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import static java.lang.Boolean.*; import static java.lang.Integer.*; import static java.lang.Long.*; import static java.lang.Float.*; import static java.lang.Double.*; import java.io.InputStreamReader; import java.io.BufferedReader; import java.util.StringTokenizer; import java.io.PrintWriter; import java.io.IOException; import java.lang.NullPointerException; import java.lang.ArrayIndexOutOfBoundsException; public class Test { static Reader in; static PrintWriter out; /* ******************** CLASS SOLVE ******************* */ static class Solve { void main() throws IOException { boolean p[][] = new boolean[1000][1000]; boolean l[][] = new boolean[1000][1000]; boolean r[][] = new boolean[1000][1000]; boolean u[][] = new boolean[1000][1000]; boolean d[][] = new boolean[1000][1000]; int n, m; n = in.nextInt(); m = in.nextInt(); for(int i= 0; i<n; i++) { for(int j=0; j<m; j++) { int k = in.nextInt(); if(k == 1) p[i][j] = true; } } // Left configure for(int i=0; i<n; i++) { int j; for(j=0; j<m && !p[i][j]; j++) ; for(++j; j<m; j++) l[i][j] = true; } // Right configure for(int i=n-1; i>=0; i--) { int j; for(j=m-1; j>=0 && !p[i][j]; j--) ; for(--j; j>=0; j--) r[i][j] = true; } // Up configure for(int j=0; j<m; j++) { int i; for(i=0; i<n && !p[i][j]; i++) ; for(++i; i<n; i++) u[i][j] = true; } // Down configure for(int j=m-1; j>=0; j--) { int i; for(i=n-1; i>=0 && !p[i][j]; i--) ; for(--i; i>=0; i--) d[i][j] = true; } int ans = 0; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(p[i][j] == false) { if(l[i][j]) ans++; if(r[i][j]) ans++; if(u[i][j]) ans++; if(d[i][j]) ans++; } } } out.println(ans); } } /* ********************** PUBLIC STATIC VOID MAIN ********************* */ public static void main(String args[]) { try { in = new Reader(); out = new PrintWriter(System.out, true); Solve s = new Solve(); s.main(); in.close(); out.close(); } catch(IOException e) { e.printStackTrace(); } } /************************* READER CLASS ********************* */ static class Reader { BufferedReader br; StringTokenizer tk; Reader() { br = new BufferedReader(new InputStreamReader(System.in)); tk = new StringTokenizer(""); } String next() throws IOException { while(tk.hasMoreTokens() == false) { String str = br.readLine(); if(str == null) return null; tk = new StringTokenizer(str); } return tk.nextToken(); } String nextLine() throws IOException { String str = br.readLine(); while(str.length() == 0) { str = br.readLine(); } return str; } boolean nextBoolean() throws IOException { return Boolean.parseBoolean(next()); } char nextChar() throws IOException { return next().charAt(0); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } float nextFloat() throws IOException { return Float.parseFloat(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } void flush() throws IOException { tk = new StringTokenizer(""); } void close() throws IOException { br.close(); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
b87e72df83f7a6cad3a439226fe02aa2
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.Reader; import java.io.OutputStreamWriter; import java.io.BufferedWriter; /** * Created by yash on 04/06/17. */ public class Spotlights { public static void main(String[] args) throws IOException { Reader reader = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(reader); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); Solver solver = new Solver(); solver.solve(br, bw); br.close(); bw.close(); } static class Solver { Utils utils; Solver() { utils = new Utils(); } void solve(BufferedReader br, BufferedWriter bw) throws IOException { int[] args = new int[2]; utils.getIntArray(br.readLine(), args); int m = args[0]; int n = args[1]; int[][] a = new int[m][n]; for (int i = 0; i < m; i++) { utils.getIntArray(br.readLine(), a[i]); } long ct = 0; int i, j, left, right; for (i = 0; i < m; i++) { left = 0; right = n - 1; while (left <= right && a[i][left] == 0) { left++; } while (right > left && a[i][right] == 0) { right--; } if (left <= right) ct += (left) + (n - right - 1); for (j = left + 1; j < right; j++) { if (a[i][j] == 0) ct += 2; } } for (i = 0; i < n; i++) { left = 0; right = m - 1; while (left <= right && a[left][i] == 0) { left++; } while (right > left && a[right][i] == 0) { right--; } if (left <= right) ct += (left) + (m - right - 1); for (j = left + 1; j < right; j++) { if (a[j][i] == 0) ct += 2; } } bw.write(ct + "\n"); } } static class Utils { void getLongArray(String s, long a[]) { String[] temp = s.split(" "); for (int i = 0; i < temp.length; i++) { a[i] = getLong(temp[i]); } } void getIntArray(String s, int a[]) { String[] temp = s.split(" "); for (int i = 0; i < temp.length; i++) { a[i] = getInt(temp[i]); } } void getIntArray(String s, int a[], String regex) { String[] temp = s.split(regex); for (int i = 0; i < temp.length; i++) { a[i] = getInt(temp[i]); } } int getInt(String s) { return Integer.parseInt(s); } long getLong(String s) { return Long.parseLong(s); } void printArray(int[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } void printArray(long[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } void printArray(float[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } void printArray(double[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } void print2DArray(int[][] a) { for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } System.out.println(); } void print2DArray(Object[][] a) { for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } System.out.println(); } Pair<Integer, Integer> minMax(int[] a) { int len = a.length; if (len < 1) { return null; } else if (len == 1) { new Pair<>(a[0], a[0]); } int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; for (int i = 0; i < len; i++) { int elem = a[i], next = elem; if (i != len - 1) { next = a[i + 1]; } if (elem <= next) { int temp = elem; elem = next; next = temp; } if (elem > max) { max = elem; } if (next < min) { min = next; } } return new Pair<>(max, min); } static void factorize(int n, int[] factors) { for (int i = 2; i <= n; i = (i == 2) ? i + 1 : i + 2) { if (isPrime(i) && n % i == 0) { int ct = 1; int temp = n / i; while (temp % i == 0) { temp = temp / i; ct++; } factors[i] = ct; } } } static boolean isPrime(long n) { // fast even test. if (n > 2 && (n & 1) == 0) return false; // only odd factors need to be tested up to n^0.5 for (int i = 3; i * i <= n; i += 2) if (n % i == 0) return false; return true; } } static class Pair<A, B> { A a; B b; Pair(A a, B b) { this.a = a; this.b = b; } @Override public String toString() { return "Pair{" + "a=" + a + ", b=" + b + '}'; } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
6b9458e8068c12c40cc307c74d01d709
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.Reader; import java.io.OutputStreamWriter; import java.io.BufferedWriter; /** * Created by yash on 04/06/17. */ public class Spotlights { public static void main(String[] args) throws IOException { Reader reader = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(reader); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); Solver solver = new Solver(); solver.solve(br, bw); br.close(); bw.close(); } static class Solver { Utils utils; Solver() { utils = new Utils(); } void solve(BufferedReader br, BufferedWriter bw) throws IOException { int[] args = new int[2]; utils.getIntArray(br.readLine(), args); int m = args[0]; int n = args[1]; int[][] a = new int[m][n]; for (int i = 0; i < m; i++) { utils.getIntArray(br.readLine(), a[i]); } long ct = 0; for (int i = 0; i < m; i++) { int left = 0; int right = n - 1; while (left <= right && a[i][left] == 0) { left++; } while (right > left && a[i][right] == 0) { right--; } if (left <= right) ct += (left) + (n - right - 1); for (int j = left + 1; j < right; j++) { if (a[i][j] == 0) ct += 2; } // bw.write(left + " " + right + " " + ct + "\n"); } for (int i = 0; i < n; i++) { int top = 0; int bottom = m - 1; while (top <= bottom && a[top][i] == 0) { top++; } while (bottom > top && a[bottom][i] == 0) { bottom--; } if (top <= bottom) ct += (top) + (m - bottom - 1); for (int j = top + 1; j < bottom; j++) { if (a[j][i] == 0) ct += 2; } // bw.write(top + " " + bottom + " " + ct + "\n"); } bw.write(ct+"\n"); } } static class Utils { void getLongArray(String s, long a[]) { String[] temp = s.split(" "); for (int i = 0; i < temp.length; i++) { a[i] = getLong(temp[i]); } } void getIntArray(String s, int a[]) { String[] temp = s.split(" "); for (int i = 0; i < temp.length; i++) { a[i] = getInt(temp[i]); } } void getIntArray(String s, int a[], String regex) { String[] temp = s.split(regex); for (int i = 0; i < temp.length; i++) { a[i] = getInt(temp[i]); } } int getInt(String s) { return Integer.parseInt(s); } long getLong(String s) { return Long.parseLong(s); } void printArray(int[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } void printArray(long[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } void printArray(float[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } void printArray(double[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } void print2DArray(int[][] a) { for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } System.out.println(); } void print2DArray(Object[][] a) { for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } System.out.println(); } Pair<Integer, Integer> minMax(int[] a) { int len = a.length; if (len < 1) { return null; } else if (len == 1) { new Pair<>(a[0], a[0]); } int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; for (int i = 0; i < len; i++) { int elem = a[i], next = elem; if (i != len - 1) { next = a[i + 1]; } if (elem <= next) { int temp = elem; elem = next; next = temp; } if (elem > max) { max = elem; } if (next < min) { min = next; } } return new Pair<>(max, min); } static void factorize(int n, int[] factors) { for (int i = 2; i <= n; i = (i == 2) ? i + 1 : i + 2) { if (isPrime(i) && n % i == 0) { int ct = 1; int temp = n / i; while (temp % i == 0) { temp = temp / i; ct++; } factors[i] = ct; } } } static boolean isPrime(long n) { // fast even test. if (n > 2 && (n & 1) == 0) return false; // only odd factors need to be tested up to n^0.5 for (int i = 3; i * i <= n; i += 2) if (n % i == 0) return false; return true; } } static class Pair<A, B> { A a; B b; Pair(A a, B b) { this.a = a; this.b = b; } @Override public String toString() { return "Pair{" + "a=" + a + ", b=" + b + '}'; } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
c32e9499d8a2600e0d618f2f6d59c37c
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class SpotLoght { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //Scanner input = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] s = br.readLine().split(" "); int n = Integer.parseInt(s[0]); int m = Integer.parseInt(s[1]); int[][] arr = new int[n][m]; for(int i=0; i<n;i++){ s = br.readLine().split(" "); for(int j=0; j<m;j++)arr[i][j] = Integer.parseInt(s[j]); } int[][] d1 = new int[n][m]; int[][] d2 = new int[n][m]; int[][] d3 = new int[n][m]; int[][] d4 = new int[n][m]; for(int i=0; i<n;i++){ for(int j=0; j<m;j++){ if(j!=0)d1[i][j] = d1[i][j-1]+arr[i][j]; else d1[i][j] = arr[i][j]; } } for(int i=0; i<n;i++){ for(int j=m-1;j>=0;j--){ if(j!=m-1)d2[i][j] = d2[i][j+1]+arr[i][j]; else d2[i][j] = arr[i][j]; } } for(int j=0; j<m;j++){ for(int i=0; i<n;i++){ if(i!=0) d3[i][j] = d3[i-1][j]+arr[i][j]; else d3[i][j] = arr[i][j]; } } for(int j=0; j<m;j++){ for(int i=n-1; i>=0;i--){ if(i!=n-1) d4[i][j] = d4[i+1][j]+arr[i][j]; else d4[i][j] = arr[i][j]; } } int counter=0; for(int i=0; i<n;i++){ for(int j=0; j<m;j++){ if(arr[i][j]==0){ if(d1[i][j]!=0)counter++; if(d2[i][j]!=0)counter++; if(d3[i][j]!=0)counter++; if(d4[i][j]!=0)counter++; // System.out.println(counter); } } } System.out.println(counter); } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
d5075ccd2312602cfa7520f95fd1534a
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class WorkFile { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String[] firstline = reader.readLine().split(" "); int n = Integer.parseInt(firstline[0]); int m = Integer.parseInt(firstline[1]); ArrayList[] lines = new ArrayList[n]; ArrayList[] columns = new ArrayList[m]; for (int i=0; i<m; i++) { columns[i] = new ArrayList<Integer>(); } for (int i=0; i<n; i++) { lines[i] = new ArrayList<Integer>(); String[] list = reader.readLine().split(" "); for (int j=0; j<m; j++) { if (list[j].equals("1")) { lines[i].add(j); columns[j].add(i); } } } int res = 0; for (ArrayList<Integer> element:lines) { int length = element.size(); if (length==0) continue; else if (length==1) res+=m-1; else { res+=(element.get(0)+m-element.get(length-1)-1); for (int i=1; i<length; i++) { res+=2*(element.get(i)-element.get(i-1)-1); } } } for (ArrayList<Integer> element:columns) { int length = element.size(); if (length==0) continue; else if (length==1) res+=n-1; else { res+=(element.get(0)+n-element.get(length-1)-1); for (int i=1; i<length; i++) { res+=2*(element.get(i)-element.get(i-1)-1); } } } System.out.println(res); } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
75b306537f232c058c9ec0536925a11f
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class Actors { public static BufferedReader sc = new BufferedReader(new InputStreamReader(System.in)); public static StringTokenizer st; public static PrintWriter pw = new PrintWriter(System.out); final static boolean debugmode = true; public static int k = 7; // for 10^9 + k mods. public static long STMOD = 1000000000 + k; // 10^9 + k public static void main(String[] args) throws IOException{ int rows = getInt(); int cols = getInt(); int[][] matr = readMatrix(rows,cols); boolean[][][] isPerson = new boolean[rows][cols][4]; for(int r = 0;r<rows;r++){ for(int c = 1;c<cols;c++){ isPerson[r][c][0] = isPerson[r][c-1][0] || (matr[r][c-1] == 1); } for(int c = cols-2;c>=0;c--){ isPerson[r][c][1] = isPerson[r][c+1][1] || (matr[r][c+1] == 1); } for(int c = 0;c<cols;c++){ if(r > 0){ isPerson[r][c][2] = isPerson[r-1][c][2] || (matr[r-1][c] == 1); } } } for(int r = rows-2;r>=0;r--){ for(int c = 0;c<cols;c++){ isPerson[r][c][3] = isPerson[r+1][c][3] || (matr[r+1][c] == 1); } } int ways = 0; for(int r = 0;r<rows;r++){ for (int c = 0;c < cols;c++){ if(matr[r][c] == 0){ if(r != 0 && isPerson[r][c][2]){ //System.out.println(r+" "+c+" 0"); ways++; } if(c != 0 && isPerson[r][c][0]){ //System.out.println(r+" "+c+" 1"); ways++; } if(r != rows-1 && isPerson[r][c][3]){ //System.out.println(r+" "+c+" 2"); ways++; } if(c != cols-1 && isPerson[r][c][1]){ //System.out.println(r+" "+c+" 3"); ways++; } } } } submit(ways,true); } public static void setInputFile(String fn) throws IOException{ sc = new BufferedReader(new FileReader(fn)); } public static void setOutputFile(String fn) throws IOException{ pw = new PrintWriter(new BufferedWriter(new FileWriter(fn))); } public static int GCD(int a, int b) { if (b==0) return a; return GCD(b,a%b); } public static double log(int k, int v){ return Math.log(k)/Math.log(v); } public static long longpower(int a,int b){ long[] vals = new long[(int) (log(b,2)+2)]; vals[0] = a; vals[1] = a*a; for(int i = 1;i<vals.length;i++){ vals[i] = vals[i-1]*vals[i-1]; } long ans = 1; int cindex = 0; while(b != 0){ if (b % 2 == 1){ ans *= vals[cindex]; } cindex += 1; b /= 2; } return ans; } public static void debug(String toPrint){ if(!debugmode) {return;} pw.println("[DEBUG]: "+toPrint); } public static void submit(int[] k,boolean close){ pw.println(Arrays.toString(k)); if(close){ pw.close(); } } public static void submit(int p,boolean close){ pw.println(Integer.toString(p)); if(close){ pw.close(); } } public static void submit(String k,boolean close){ pw.println(k); if(close){ pw.close(); } } public static void submit(double u,boolean close){ pw.println(Double.toString(u)); if(close){ pw.close(); } } public static void submit(long lng,boolean close){ pw.println(Long.toString(lng)); if(close){ pw.close(); } } public static void submit(){ pw.close(); } public static int getInt() throws IOException{ if (st != null && st.hasMoreTokens()){ return Integer.parseInt(st.nextToken()); } st = new StringTokenizer(sc.readLine()); return Integer.parseInt(st.nextToken()); } public static long getLong() throws IOException{ if (st != null && st.hasMoreTokens()){ return Long.parseLong(st.nextToken()); } st = new StringTokenizer(sc.readLine()); return Long.parseLong(st.nextToken()); } public static double getDouble()throws IOException{ if (st != null && st.hasMoreTokens()){ return Double.parseDouble(st.nextToken()); } st = new StringTokenizer(sc.readLine()); return Double.parseDouble(st.nextToken()); } public static String getString()throws IOException{ if(st != null && st.hasMoreTokens()){ return st.nextToken(); } st = new StringTokenizer(sc.readLine()); return st.nextToken(); } public static String getLine() throws IOException{ return sc.readLine(); } public static int[][] readMatrix(int lines,int cols) throws IOException{ int[][] matrr = new int[lines][cols]; for (int i = 0;i < lines;i++){ for(int j = 0;j < cols;j++){ matrr[i][j] = getInt(); } } return matrr; } public static int[] readArray(int lines) throws IOException{ int[] ar = new int[lines]; for (int i = 0;i<lines;i++) ar[i] =getInt(); return ar; } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
d225ebfa6c2517bba00e38fce13c6a81
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.util.Arrays; import java.util.InputMismatchException; import java.util.*; import java.io.*; import java.math.*; public class Main5{ static class Pair { long x; int y; public Pair(long x, int y) { this.x = x; this.y = y; } } static class Compare { static void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.x>p2.x) { return 1; } else if(p2.x>p1.x) { return -1; } else { if(p1.y>p2.y) { return 1; } else if(p1.y<p2.y) { return -1; } else return 0; } } }); } } public static long pow(long a, long b) { long result=1; while(b>0) { if (b % 2 != 0) { result=(result*a); b--; } a=(a*a); b /= 2; } return result; } public static long fact(long num) { long value=1; int i=0; for(i=2;i<num;i++) { value=((value%mod)*i%mod)%mod; } return value; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } public static long lcm(int a,int b) { return a * (b / gcd(a, b)); } public static long sum(int h) { return (h*(h+1)/2); } public static void dfs(int parent,boolean[] visited) { ArrayList<Integer> arr=new ArrayList<Integer>(); arr=graph.get(parent); visited[parent]=true; for(int i=0;i<arr.size();i++) { int num=arr.get(i); if(visited[num]==false) { dfs(num,visited); } bache[parent]+=bache[num]+1; } } static int[] bache; static ArrayList<Integer> ar3; static long mod=1000000007L; static ArrayList<ArrayList<Integer>> graph; public static void bfs(int num,boolean[] visited) { Queue<Integer> q=new LinkedList<>(); q.add(num); visited[num]=true; while(!q.isEmpty()) { int x=q.poll(); ArrayList<Integer> al=graph.get(x); for(int i=0;i<al.size();i++) { int y=al.get(i); if(visited[y]==false) { q.add(y); visited[y]=true; } } } } public static void main(String args[])throws IOException { // InputReader in=new InputReader(System.in); // OutputWriter out=new OutputWriter(System.out); // long a=pow(26,1000000005); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); ArrayList<Integer> ar=new ArrayList<>(); ArrayList<Integer> ar1=new ArrayList<>(); ArrayList<Integer> ar2=new ArrayList<>(); // ArrayList<Integer> ar3=new ArrayList<>(); ArrayList<Integer> ar4=new ArrayList<>(); TreeSet<Integer> ts1=new TreeSet<>(); TreeSet<String> pre=new TreeSet<>(); HashMap<Long,Long> hash=new HashMap<>(); //HashMap<Long,Integer> hash1=new HashMap<Long,Integer>(); HashMap<Long,Integer> hash2=new HashMap<Long,Integer>(); /* boolean[] prime=new boolean[10001]; for(int i=2;i*i<=10000;i++) { if(prime[i]==false) { for(int j=2*i;j<=10000;j+=i) { prime[j]=true; } } }*/ int n=i(); int m=i(); int[][] a=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=i(); } } int[][] up=new int[n][m]; int[][] down=new int[n][m]; int[][] left=new int[n][m]; int[][] right=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(j-1>=0) { if(left[i][j-1]==1) left[i][j]=1; else left[i][j]=left[i][j-1]+a[i][j]; } if(i-1>=0) { if(up[i-1][j]==1) up[i][j]=1; else up[i][j]=up[i-1][j]+a[i][j]; } if(j==0) { left[i][j]=a[i][j]; } if(i==0) { up[i][j]=a[i][j]; } } } for(int i=n-1;i>=0;i--) { for(int j=m-1;j>=0;j--) { if(j+1<m) { if(right[i][j+1]==1) right[i][j]=1; else right[i][j]=right[i][j+1]+a[i][j]; } if(i+1<n) { if(down[i+1][j]==1) down[i][j]=1; else down[i][j]=down[i+1][j]+a[i][j]; } if(j==m-1) { right[i][j]=a[i][j]; } if(i==n-1) { down[i][j]=a[i][j]; } } } for(int i=0;i<n;i++) { // pln(Arrays.toString(left[i])+""); } long sum=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]==0) { sum+=up[i][j]+down[i][j]+left[i][j]+right[i][j]; } } } pln(sum+""); } /**/ static InputReader in=new InputReader(System.in); static OutputWriter out=new OutputWriter(System.out); public static long l() { String s=in.String(); return Long.parseLong(s); } public static void pln(String value) { System.out.println(value); } public static int i() { return in.Int(); } public static String s() { return in.String(); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars== -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.Int(); return array; } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
6deb58ad5c36b3ff97a0c76cf9b8c7b1
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; public class TaskB { public static Reader in; public void run() throws IOException { in = new Reader(); solve(); } public static void solve() throws IOException{ int n = in.nextInt(); int m = in.nextInt(); int cntZero = 0; int cnt = 0; int scene[][] = new int[n][m]; for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < m ; j++){ scene[i][j] = in.nextInt(); if(scene[i][j] == 0) cntZero++; if(scene[i][j] == 1) { cnt+=cntZero; cntZero = 0; } } cntZero = 0; } for(int i = 0 ; i < n ; i++){ for(int j = m-1 ; j >= 0 ; j--){ if(scene[i][j] == 0) cntZero++; if(scene[i][j] == 1) { cnt+=cntZero; cntZero = 0; } } cntZero = 0; } for(int i = 0 ; i < m ; i++){ for(int j = 0 ; j < n ; j++){ if(scene[j][i] == 0) cntZero++; if(scene[j][i] == 1) { cnt+=cntZero; cntZero = 0; } } cntZero = 0; } for(int i = 0 ; i < m ; i++){ for(int j = n-1 ; j >= 0 ; j--){ if(scene[j][i] == 0) cntZero++; if(scene[j][i] == 1) { cnt+=cntZero; cntZero = 0; } } cntZero = 0; } System.out.println(cnt); } public static void main(String[] args) throws IOException{ new TaskB().run(); } 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(); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
81bac904fabd8fa0d8093c52198eac41
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.*; import java.util.*; public class Main { static BufferedReader br; static StringTokenizer st; static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static class AL<T> extends ArrayList<T> {}; public static class HM<T,Integer> extends HashMap<T,T> {}; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); int n = nxtInt(), m = nxtInt(); int[][] a = new int[n][m]; int ans = 0; boolean flg = false; for(int i=0; i<n; i++) { flg = false; st = new StringTokenizer(br.readLine()); for(int j=0; j<m; j++) { a[i][j] = nxtInt(); if(a[i][j] == 1) flg = true; else if(flg && a[i][j] == 0) ans++; } } for(int i=0; i<n; i++) { flg = false; for(int j=m-1; j>=0; j--) { if(a[i][j] == 1) flg = true; else if(flg && a[i][j] == 0) ans++; } } for(int i=0; i<m; i++) { flg = false; for(int j=0; j<n; j++) { if(a[j][i] == 1) flg = true; else if(flg && a[j][i] == 0) ans++; } } for(int i=0; i<m; i++) { flg = false; for(int j=n-1; j>=0; j--) { if(a[j][i] == 1) flg = true; else if(flg && a[j][i] == 0) ans++; } } pr.println(ans); pr.flush(); pr.close(); } static int nxtInt(){ return Integer.parseInt(nxt()); } static long nxtLong(){ return Long.parseLong(nxt()); } static double nxtDoub(){ return Double.parseDouble(nxt()); } static String nxt(){ return st.nextToken(); } static String nxtLn() throws IOException{ return br.readLine(); } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
d49eb6a3b3b29434cf2af4771f89df8d
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.*; import java.util.*; public class actors { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String s[]=br.readLine().split(" "); int n=Integer.parseInt(s[0]); int m=Integer.parseInt(s[1]); int arr[][]=new int[n+1][m+1]; int row[][]=new int[n+1][m+1]; for(int i=1;i<=n;i++) { s=br.readLine().split(" "); for(int j=1;j<=m;j++) { arr[i][j]=Integer.parseInt(s[j-1]); if(arr[i][j]>0) { row[i][j]=row[i][j-1]+1; } else row[i][j]=row[i][j-1]; } } int col[][]=new int[n+1][m+1]; for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { if(arr[j][i]==1) { col[j][i]=col[j-1][i]+1; } else col[j][i]=col[j-1][i]; } } int c=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { //System.out.print(row[i][j]+" "+col[i][j]+"\t"); if(arr[i][j]==0) { if(row[i][m]-row[i][j] > 0) c++; if(row[i][j-1]>0) c++; if(col[i-1][j] > 0) c++; if(col[n][j]-col[i][j]>0) c++; } } //System.out.println(); } System.out.println(c); } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
7f5259f2548eb1ebbde3081d4025d38a
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { public static long mod= (long) (1e9 +7); public static void main(String args[]) { InputReader s= new InputReader(System.in); OutputStream outputStream= System.out; PrintWriter out= new PrintWriter(outputStream); int n= s.nextInt(); int m= s.nextInt(); int a[][]= new int[n+1][m+1]; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ a[i][j]= s.nextInt(); } } boolean right[][]= new boolean[n+1][m+1]; boolean left[][]= new boolean[n+1][m+1]; boolean up[][]= new boolean[n+1][m+1]; boolean down[][]= new boolean[n+1][m+1]; int flag=0; for(int i=1;i<=n;i++){ for(int j=m-1;j>0;j--){ if(a[i][j+1]==1 || flag==1){ right[i][j]=true; flag=1; } } flag=0; } flag=0; for(int i=1;i<=n;i++){ for(int j=2;j<=m;j++){ if(a[i][j-1]==1 || flag==1){ left[i][j]=true; flag=1; } } flag=0; } flag=0; for(int i=1;i<=m;i++){ for(int j=2;j<=n;j++){ if(a[j-1][i]==1 || flag==1){ up[j][i]=true; flag=1; } } flag=0; } flag=0; for(int i=1;i<=m;i++){ for(int j=n-1;j>0;j--){ if(a[j+1][i]==1 || flag==1){ down[j][i]=true; flag=1; } } flag=0; } // out.println(up[1][4]+" "+right[1][4]+" "+down[1][4]+" "+left[1][4]); int count=0; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(a[i][j]==0){ if(right[i][j]) count++; if(down[i][j]) count++; if(left[i][j]) count++; if(up[i][j]) count++; } // out.println(i+" "+j+" "+count+" "); } } out.println(count); out.close(); } static long combinations(long n,long r){ // O(r) if(r==0 || r==n) return 1; r= Math.min(r, n-r); long ans=n; // nC1=n; for(int i=1;i<r;i++){ ans= ans*(n-i); ans= ans/(i+1); } return ans; } public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static long modulo(long a,long b,long c) { long x=1; long y=a; while(b > 0){ if(b%2 == 1){ x=(x*y)%c; } y = (y*y)%c; // squaring the base b /= 2; } return x%c; } static void catalan_numbers(int n) { long catalan[]= new long[n+1]; catalan[1] = 1; for (int i = 2; i <= n; i++) { for (int j = 1; j <= i - 1; j++) { catalan[i] = catalan[i] + ((catalan[j]) * catalan[i - j]); } } } static ArrayList<Integer> primeFactors(int n) // O(sqrt(n)) { // Print the number of 2s that divide n ArrayList<Integer> arr= new ArrayList<>(); while (n%2 == 0) { arr.add(2); n = n/2; } // n must be odd at this point. So we can skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i = i+2) { // While i divides n, print i and divide n while (n%i == 0) { arr.add(i); n = n/i; } } // This condition is to handle the case when n is a prime number // greater than 2 if (n > 2) arr.add(n); return arr; } public static int expo(int a, int b){ if (b==0) return 1; if (b==1) return a; if (b==2) return a*a; if (b%2==0){ return expo(expo(a,b/2),2); } else{ return a*expo(expo(a,(b-1)/2),2); } } static class Pair implements Comparable<Pair> { long f; String s; Pair(long ii, String cc) { f=ii; s=cc; } public int compareTo(Pair o) { return Long.compare(this.f, o.f); } } public static int[] sieve(int N){ // O(n*log(logn)) int arr[]= new int[N+1]; for(int i=2;i<=Math.sqrt(N);i++){ if(arr[i]==0){ for(int j= i*i;j<= N;j= j+i){ arr[j]=1; } } } return arr; // All the i for which arr[i]==0 are prime numbers. } static long gcd(long a,long b){ // O(logn) if(b==0) return a; return gcd(b,a%b); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream inputstream) { reader = new BufferedReader(new InputStreamReader(inputstream)); tokenizer = null; } public String nextLine(){ String fullLine=null; while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { fullLine=reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return fullLine; } return fullLine; } 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 long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
fd14870df3eca0600d72077792dd8c35
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.awt.Point; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; 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.List; import java.util.Map; import java.util.Scanner; import java.util.StringTokenizer; public class A { public static void main(String[] args) throws IOException { // Scanner in = new Scanner(System.in); BufferedReader scan = new BufferedReader(new InputStreamReader(System.in)); String[] ar = scan.readLine().split(" "); int n = Integer.parseInt(ar[0]); int m = Integer.parseInt(ar[1]); int a[][] = new int[n + 1][m + 1]; int d[][][] = new int[4][n + 2][m + 2]; for (int i = 1; i <= n; i++){ String[] s = scan.readLine().split(" "); for (int j = 1; j <= m; j++) a[i][j] = Integer.parseInt(s[j - 1]); } for (int i = 1;i <= n; i++) for (int j = 1; j <= m; j++) { d[0][i][j] = d[0][i][j - 1] + a[i][j]; d[1][i][m - j + 1] = d[1][i][m - j + 2] + a[i][m - j + 1]; d[2][i][j] = d[2][i - 1][j] + a[i][j]; d[3][n - i + 1][j] = d[3][n - i + 2][j] + a[n - i + 1][j]; } // for (int k = 0; k < 4; k++) { // for (int i = 1;i <= n; i++) { // for (int j = 1; j <= m; j++) // System.out.print(d[k][i][j] + " "); // System.out.println(); // } // System.out.println("-----"); // } int ans = 0; for (int i = 1;i <= n; i++) for (int j = 1; j <= m; j++) { if (a[i][j] == 0) { if (d[0][i][j] > 0) ans++; if (d[1][i][j] > 0) ans++; if (d[2][i][j] > 0) ans++; if (d[3][i][j] > 0) ans++; } } System.out.println(ans); } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
2d3c0a1fc931f23e27b4d4c54b6c49b9
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { String ZERO = "0"; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] line = in.readLine().split(" "); int n = Integer.parseInt(line[0]); int m = Integer.parseInt(line[1]); String[][] matrix = new String[n][m]; for (int i = 0; i < n; i++) { line = in.readLine().split(" "); for (int j = 0; j < m; j++) { matrix[i][j] = line[j]; } } int goodSpots = 0; int count; boolean foundOne; String temp; for (int i = 0; i < n; i++) { count = 0; foundOne = false; for (int j = 0; j < m; j++) { temp = matrix[i][j]; if (temp.equals(ZERO)) { count++; } else { if (foundOne) { count = count * 2; } else { foundOne = true; } goodSpots += count; count = 0; } } if (count != 0 && foundOne) { goodSpots += count; } } for (int i = 0; i < m; i++) { count = 0; foundOne = false; for (int j = 0; j < n; j++) { temp = matrix[j][i]; if (temp.equals(ZERO)) { count++; } else { if (foundOne) { count = count * 2; } else { foundOne = true; } goodSpots += count; count = 0; } } if (count != 0 && foundOne) { goodSpots += count; } } System.out.println(goodSpots); } } // 1524028828941
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
22eaee72933e011ad3a1bce5c0c3626d
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
/* HARSH KHATRI DA-IICT */ import java.io.*; import java.util.*; import java.lang.*; import java.util.Scanner; import java.util.Arrays; public class CF729B { public static void main(String[] args) { FasterScanner in = new FasterScanner(); int n = in.nextInt(); int m = in.nextInt(); int[][] a = new int[n][m]; boolean[][] left = new boolean[n][m], right = new boolean[n][m]; boolean[][] up = new boolean[n][m], down = new boolean[n][m]; long count = 0; //long cr = 0, cl = 0, cu = 0, cd = 0; int i, j, k; for(i=0; i<n; i++) { for(j=0; j<m; j++) { a[i][j] = in.nextInt(); if(i>0 && (up[i-1][j] || a[i-1][j]==1) && a[i][j]!=1) { up[i][j] = true; //cu++; count++; } if(j>0 && (left[i][j-1] || a[i][j-1]==1) && a[i][j]!=1) { left[i][j] = true; //cl++; count++; } } } for(i=n-1; i>=0; i--) { for(j=m-1; j>=0; j--) { if(i<n-1 && (down[i+1][j] || a[i+1][j]==1) && a[i][j]!=1) { down[i][j] = true; //cd++; count++; } if(j<m-1 && (right[i][j+1] || a[i][j+1]==1) && a[i][j]!=1) { right[i][j] = true; //cr++; count++; } } } //System.out.println("cr = "+cr+"\ncl = "+cl+"\ncu ="+cu+"\ncd = "+cd); System.out.println(count); } } class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int snumChars; public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public double nextDouble() { return Double.parseDouble(this.nextString()); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
8e53072a99907036903f7fc38cade332
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.util.Scanner; public class Spotlights { public static void main(String[] args) { try (Scanner s = new Scanner(System.in)) { int height = s.nextInt(); int width = s.nextInt(); s.nextLine(); // Store each stage cell and a boolean value representing if an actor occupies // that cell. boolean[][] stageCells = new boolean[height][width]; for (int i = 0; i < height; i++) { String input = s.nextLine(); String[] split = input.split(" "); for (int j = 0; j < width; j++) { stageCells[i][j] = split[j].equals("1"); } } long countSpotlights = 0; // Spotlights in the up direction. for (int i = 0; i < width; i++) { boolean actorFoundForColumn = false; for (int j = 1; j < height; j++) { if ((actorFoundForColumn || stageCells[j - 1][i]) && !stageCells[j][i]) { actorFoundForColumn = true; countSpotlights++; } } } // Spotlights in the down direction. for (int i = 0; i < width; i++) { boolean actorFoundForColumn = false; for (int j = height - 2; j >= 0; j--) { if ((actorFoundForColumn || stageCells[j + 1][i]) && !stageCells[j][i]) { actorFoundForColumn = true; countSpotlights++; } } } // Spotlights in the left direction. for (int i = 0; i < height; i++) { boolean actorFoundForRow = false; for (int j = 1; j < width; j++) { if ((actorFoundForRow || stageCells[i][j - 1]) && !stageCells[i][j]) { actorFoundForRow = true; countSpotlights++; } } } // Spotlights in the right direction. for (int i = 0; i < height; i++) { boolean actorFoundForRow = false; for (int j = width - 2; j >= 0; j--) { if ((actorFoundForRow || stageCells[i][j + 1]) && !stageCells[i][j]) { actorFoundForRow = true; countSpotlights++; } } } System.out.println(countSpotlights); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
b22512ef75be85d723372968d6cd8be3
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class spotlight { static Scanner w = new Scanner(System.in); public static void main(String[] args) throws NumberFormatException, IOException { spotlight s = new spotlight(); s.give(); } void give() throws NumberFormatException, IOException { int n = w.nextInt(); int m = w.nextInt(); int[][] ar = new int[n + 1][m + 1]; for (int i = 1; i < n + 1; i++) { for (int j = 1; j < m + 1; j++) { ar[i][j] = w.nextInt(); } } int[][] lr = new int[n + 2][m + 2]; int[][] ud = new int[n + 2][m + 2]; UpDownCalculate(ar, ud); leftRightCalcualte(ar, lr); int count =counter(ar, lr, ud); System.out.println(count); } void UpDownCalculate(int[][] ar, int[][] ud) { for (int i = 1; i < ar[0].length; i++) { for (int j = 1; j < ar.length; j++) { if (ar[j][i] == 1) { ud[j][i] = ud[j - 1][i] + 1; } else { ud[j][i] = ud[j - 1][i]; } } } }// end method void leftRightCalcualte(int[][] ar, int[][] lr) { for (int i = 1; i < ar.length; i++) { for (int j = 1; j < ar[0].length; j++) { if (ar[i][j] == 1) { lr[i][j] = lr[i][j - 1] + 1; } else { lr[i][j] = lr[i][j - 1]; } } } }//end method int counter(int[][]ar,int[][]lr,int [][]ud){ int count=0; for (int i = 1; i < ar.length; i++) { for (int j = 1; j < ar[0].length; j++) { if (ar[i][j] == 0) { if (lr[i][j]>0) { count++; }if (lr[i][ar[0].length-1]>lr[i][j]) { count++; } if (ud[i][j]>0) { count++; }if (ud[ar.length-1][j]>ud[i][j]) { count++; } } } } return count; } static class Scanner { BufferedReader br; StringTokenizer st; Scanner(FileReader f) { br = new BufferedReader(f); } public boolean ready() throws IOException { return br.ready(); } Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
164fd67a5fdfa08b62f5bf8c9b3c0a57
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Spotlights { public static void main(String[] args) throws NumberFormatException, IOException { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); boolean left[][] = new boolean[n][m]; boolean right[][] = new boolean[n][m]; boolean up[][] = new boolean[n][m]; boolean down[][] = new boolean[n][m]; boolean act[][] = new boolean[n][m]; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { int pres = sc.nextInt(); if(pres == 1) act[i][j] = up[i][j] = down[i][j] = left[i][j] = right[i][j] = true; } for(int i = 1; i < n; i++) for(int j = 0; j < m; j++) up[i][j] |= up[i-1][j]; for(int i = n - 2; i >= 0; i--) for(int j = 0; j < m; j++) down[i][j] |= down[i+1][j]; for(int i = 0; i < n; i++) for(int j = 1; j < m; j++) left[i][j] |= left[i][j - 1]; for(int i = 0; i < n; i++) for(int j = m - 2; j >= 0; j--) right[i][j] |= right[i][j+1]; int count = 0; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { if(act[i][j]) continue; if(up[i][j]) count++; if(down[i][j]) count++; if(left[i][j]) count++; if(right[i][j]) count++; } System.out.println(count); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner(FileReader f) { br = new BufferedReader(f); } public boolean ready() throws IOException { return br.ready(); } Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
744b6390ac15062623dc06a54dcc4b2f
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.lang.*; import java.util.*; import java.io.*; public class pblm2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[][] mat = new int[n+1][m+1]; for(int i=0;i<n;i++) { int sum=0; for(int j=0;j<m;j++) { mat[i][j]=sc.nextInt(); sum += mat[i][j]; } mat[i][m]=sum; } for(int i=0;i<m;i++) { int sum=0; for(int j=0;j<n;j++) { sum += mat[j][i]; } mat[n][i]=sum; } int[] upsum = new int[m]; int c=0; for(int i=0;i<n;i++) { int lsum=0; for(int j=0;j<m;j++) { if(mat[i][j]==1) { upsum[j]++;mat[n][j]--; lsum++;mat[i][m]--; } else { if(j-1>=0 && lsum>0)c++; if(j+1<m && mat[i][m]>0)c++; if(i-1>=0 && upsum[j]>0)c++; if(i+1<n && mat[n][j]>0)c++; } } } System.out.println(c); } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
89b6f616243593bb82dc5b35b4002524
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class cfs380B2 { public static void main(String[] args) { FastScanner sc = new FastScanner(); int N = sc.nextInt(); int M = sc.nextInt(); boolean[][] actors = new boolean[N][M]; int[][] row = new int[2][N]; int[][] col = new int[2][M]; Arrays.fill(row[0], -1); Arrays.fill(row[1], -1); Arrays.fill(col[0], -1); Arrays.fill(col[1], -1); int[] rowact = new int[N]; int[] colact = new int[M]; for (int n = 0; n < N; n++) { for (int m = 0; m < M; m++) { int next = sc.nextInt(); if (next == 1) { actors[n][m] = true; if (row [0][n] == -1) row[0][n] = m; if (m > row [1][n]) row[1][n] = m; if (col [0][m] == -1) col[0][m] = n; if (n > col [1][m]) col[1][m] = n; rowact[n]++; colact[m]++; } } } int count = 0; for (int r = 0; r < N; r++) { if (row[0][r] != -1) { count += row[0][r]; } if (row[1][r] != -1) { count += M - row[1][r] - 1; } if (row[0][r] != -1 && row[1][r] != -1) { int temp = 2 * ((row[1][r] - row[0][r] - 1) - (rowact[r] - 2)); if (temp > 0) { count += temp; } } } for (int c = 0; c < M; c++) { if (col[0][c] != -1) { count += col[0][c]; } if (col[1][c] != -1) count += N - col[1][c] - 1; if (col[0][c] != -1 && col[1][c] != -1) { int temp = 2 * ((col[1][c] - col[0][c] - 1) - (colact[c] - 2)); if (temp > 0 ) count += temp; } } System.out.println(count); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner() { this(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 readNextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readIntArray(int n) { int[] a = new int[n]; for (int idx = 0; idx < n; idx++) { a[idx] = nextInt(); } return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int idx = 0; idx < n; idx++) { a[idx] = nextLong(); } return a; } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
288032d9a9d18a028d26f638e1085797
train_003.jsonl
1479632700
Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
256 megabytes
import java.io.*; import java.util.NoSuchElementException; public class Main_729B { private static Scanner sc; private static Printer pr; private static void solve() { int n = sc.nextInt(); int m = sc.nextInt(); int[][] s = new int[n][]; for (int i = 0; i < n; i++) { s[i] = sc.nextIntArray(m); } int ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i][j] == 1) { for (int ii = i + 1; ii < n; ii++) { if (s[ii][j] == 0) { ans++; } else { break; } } for (int ii = i - 1; ii >= 0; ii--) { if (s[ii][j] == 0) { ans++; } else { break; } } for (int jj = j + 1; jj < m; jj++) { if (s[i][jj] == 0) { ans++; } else { break; } } for (int jj = j - 1; jj >= 0; jj--) { if (s[i][jj] == 0) { ans++; } else { break; } } } } } pr.println(ans); } // --------------------------------------------------- public static void main(String[] args) { sc = new Scanner(System.in); pr = new Printer(System.out); solve(); pr.close(); sc.close(); } static class Scanner { BufferedReader br; Scanner (InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } private boolean isPrintable(int ch) { return ch >= '!' && ch <= '~'; } private boolean isCRLF(int ch) { return ch == '\n' || ch == '\r' || ch == -1; } private int nextPrintable() { try { int ch; while (!isPrintable(ch = br.read())) { if (ch == -1) { throw new NoSuchElementException(); } } return ch; } catch (IOException e) { throw new NoSuchElementException(); } } String next() { try { int ch = nextPrintable(); StringBuilder sb = new StringBuilder(); do { sb.appendCodePoint(ch); } while (isPrintable(ch = br.read())); return sb.toString(); } catch (IOException e) { throw new NoSuchElementException(); } } int nextInt() { try { // parseInt from Integer.parseInt() boolean negative = false; int res = 0; int limit = -Integer.MAX_VALUE; int radix = 10; int fc = nextPrintable(); if (fc < '0') { if (fc == '-') { negative = true; limit = Integer.MIN_VALUE; } else if (fc != '+') { throw new NumberFormatException(); } fc = br.read(); } int multmin = limit / radix; int ch = fc; do { int digit = ch - '0'; if (digit < 0 || digit >= radix) { throw new NumberFormatException(); } if (res < multmin) { throw new NumberFormatException(); } res *= radix; if (res < limit + digit) { throw new NumberFormatException(); } res -= digit; } while (isPrintable(ch = br.read())); return negative ? res : -res; } catch (IOException e) { throw new NoSuchElementException(); } } long nextLong() { try { // parseLong from Long.parseLong() boolean negative = false; long res = 0; long limit = -Long.MAX_VALUE; int radix = 10; int fc = nextPrintable(); if (fc < '0') { if (fc == '-') { negative = true; limit = Long.MIN_VALUE; } else if (fc != '+') { throw new NumberFormatException(); } fc = br.read(); } long multmin = limit / radix; int ch = fc; do { int digit = ch - '0'; if (digit < 0 || digit >= radix) { throw new NumberFormatException(); } if (res < multmin) { throw new NumberFormatException(); } res *= radix; if (res < limit + digit) { throw new NumberFormatException(); } res -= digit; } while (isPrintable(ch = br.read())); return negative ? res : -res; } catch (IOException e) { throw new NoSuchElementException(); } } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { try { int ch; while (isCRLF(ch = br.read())) { if (ch == -1) { throw new NoSuchElementException(); } } StringBuilder sb = new StringBuilder(); do { sb.appendCodePoint(ch); } while (!isCRLF(ch = br.read())); return sb.toString(); } catch (IOException e) { throw new NoSuchElementException(); } } int[] nextIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = sc.nextInt(); } return ret; } long[] nextLongArray(int n) { long[] ret = new long[n]; for (int i = 0; i < n; i++) { ret[i] = sc.nextLong(); } return ret; } int[][] nextIntArrays(int m, int n) { int[][] ret = new int[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ret[j][i] = sc.nextInt(); } } return ret; } void close() { try { br.close(); } catch (IOException e) { // throw new NoSuchElementException(); } } } static class Printer extends PrintWriter { Printer(OutputStream out) { super(out); } void printInts(int... a) { StringBuilder sb = new StringBuilder(32); for (int i = 0, size = a.length; i < size; i++) { if (i > 0) { sb.append(' '); } sb.append(a[i]); } println(sb); } void printLongs(long... a) { StringBuilder sb = new StringBuilder(64); for (int i = 0, size = a.length; i < size; i++) { if (i > 0) { sb.append(' '); } sb.append(a[i]); } println(sb); } void printStrings(String... a) { StringBuilder sb = new StringBuilder(32); for (int i = 0, size = a.length; i < size; i++) { if (i > 0) { sb.append(' '); } sb.append(a[i]); } println(sb); } } }
Java
["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"]
1 second
["9", "20"]
NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
Java 8
standard input
[ "dp", "implementation" ]
c3a7d82f6c3cf8678a1c7c521e0a5f51
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
1,200
Print one integer — the number of good positions for placing the spotlight.
standard output
PASSED
bbea291e16cca225dc36ec3ddfc8abf6
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
// _________________________RATHOD_____________________________________________________________ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.math.BigInteger; import java.util.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long gcd(long a, long b) { return b == 0l ? a : gcd(b, a % b); } public static void main(String[] args) { FastReader sc=new FastReader(); int t=sc.nextInt(); while(t>0) { t--; int n=sc.nextInt(); int k=sc.nextInt(); if(k%n==0) System.out.println(0); else System.out.println(2); int ar[][]=new int[n][n]; int i,j; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(k==0) break; ar[j][(j+i)%n]=1; k--; } if(k==0) break; } for(i=0;i<n;i++) { for(j=0;j<n;j++) { System.out.print(ar[i][j]); } System.out.println(); } } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
7c29beb477ce68bb7c31df8a02c63153
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
/*** * ██████╗=====███████╗====███████╗====██████╗= * ██╔══██╗====██╔════╝====██╔════╝====██╔══██╗ * ██║==██║====█████╗======█████╗======██████╔╝ * ██║==██║====██╔══╝======██╔══╝======██╔═══╝= * ██████╔╝====███████╗====███████╗====██║===== * ╚═════╝=====╚══════╝====╚══════╝====╚═╝===== * ============================================ */ import java.io.*; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Array; import java.util.*; import java.math.*; import java.lang.*; public class AA implements Runnable { public void run() { InputReader sc = new InputReader(); PrintWriter out = new PrintWriter(System.out); int i=0,j=0,k=0; int t=0; t=sc.nextInt(); for(int testcase = 0;testcase < t; testcase++) { int n=sc.nextInt(); char mat[][]=new char[n][n]; int rr[]=new int[n]; int temp=0; int cc[]=new int[n]; k=sc.nextInt(); for(char x[]:mat) Arrays.fill(x,'0'); while(k>0) { i=0;j=temp%n; for(int z=0;z<n;z++) { mat[i][j]='1'; k--; rr[i]++; cc[j]++; if(k<=0) break; i++; j=(j+1)%n; } temp=(temp+1)%n; } int a=500; int b=0; int c=500; int d=0; for(i=0;i<n;i++) { a=Math.min(a,rr[i]); b=Math.max(b,rr[i]); c=Math.min(c,cc[i]); d=Math.max(d,cc[i]); } out.println((long)(((b-a)*(b-a))+(((d-c)*(d-c))))); for(char x[]:mat) out.println(x); } //================================================================================================================================ out.flush(); out.close(); } //================================================================================================================================ public static int[] sa(int n,InputReader sc) { int inparr[]=new int[n]; for (int i=0;i<n;i++) inparr[i]=sc.nextInt(); return inparr; } public static long gcd(long a,long b){ return (a%b==0l)?b:gcd(b,a%b); } private static long lcm(long a, long b) { return a * (b / gcd(a, b)); } public int egcd(int a, int b) { if (a == 0) return b; while (b != 0) { if (a > b) a = a - b; else b = b - a; } return a; } public int countChar(String str, char c) { int temp = 0; for(int i=0; i < str.length(); i++) { if(str.charAt(i) == c) temp++; } return temp; } static int binSearch(Integer[] inparr, int number){ int left=0,right=inparr.length-1,mid=(left+right)/2,ind=0; while(left<=right){ if(inparr[mid]<=number){ ind=mid+1; left=mid+1; } else right=mid-1; mid=(left+right)/2; } return ind; } static int binSearch(int[] inparr, int number){ int left=0,right=inparr.length-1,mid=(left+right)/2,ind=0; while(left<=right){ if(inparr[mid]<=number){ ind=mid+1; left=mid+1; } else right=mid-1; mid=(left+right)/2; } return ind; } static class Pair { int a,b; Pair(int aa,int bb) { a=aa; b=bb; } String get() { return a+" "+b; } String getrev() { return b+" "+a; } } static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } static long factorial(long n) { if (n == 0) return 1; return n*factorial(n-1); } //================================================================================================================================ static class InputReader { BufferedReader br; StringTokenizer st; public InputReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]) throws Exception { new Thread(null, new AA(),"Main",1<<27).start(); } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
fdedbd9b98908d2e660e5ef113938144
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.util.*; public class HelloWorld{ public static void main(String []args){ Scanner in=new Scanner(System.in); int t=in.nextInt(); while(t-->0) { int n=in.nextInt(); int k=in.nextInt(); if(n==1) { System.out.println(0); if(k==1) { System.out.println(1); } else { System.out.println(0); } } else { int A[][]=new int[n][n]; int col=0; int row=0; while(k>0&&row<n&&col<n) { A[row][col]=1; k--; row++; col++; } int start=1; while(k>0) { int i=0; int j=start; while(k>0&&i<n&&j<n) { A[i][j]=1; k--; i++; j++; } i=n-start; j=0; while(k>0&&i<n&&j<n) { A[i][j]=1; k--; i++; j++; } start++; } int maxR=n; int minR=n; for(int i=0;i<n;i++) { int lR=0; for(int j=0;j<n;j++) { if(A[i][j]==1) { lR++; } } if(i==0)maxR=lR; maxR=Math.max(maxR,lR); minR=Math.min(minR,lR); } long ans=(maxR-minR)*(maxR-minR); int maxC=n; int minC=n; for(int i=0;i<n;i++) { int lR=0; for(int j=0;j<n;j++) { if(A[j][i]==1) { lR++; } } if(i==0)maxC=lR; maxC=Math.max(maxC,lR); minC=Math.min(minC,lR); } ans+=(maxC-minC)*(maxC-minC); System.out.println(ans); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { System.out.print(A[i][j]); } System.out.println(); } } } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
2a69419693e85aec638b12b5f9ae38b0
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.util.Scanner; public class Solution { public static void mainA(String[] args) { final Scanner in = new Scanner(System.in); final int T = in.nextInt(); for (int t = 0; t < T; t += 1) { final int n = in.nextInt(); int answer; if ((n & 1) == 1) { answer = (n - 1) / 2 + 1; } else { answer = n / 2; } System.out.println(answer); } } public static void mainB(String[] args) { final Scanner in = new Scanner(System.in); final int T = in.nextInt(); for (int t = 0; t < T; t += 1) { final long n = in.nextLong(); final long r = in.nextLong(); long result = r >= n ? 1 : 0; long d = Math.min(r, n - 1); result += d * (d + 1) / 2; System.out.println(result); } } public static void mainC(String[] args) { final Scanner in = new Scanner(System.in); final int T = in.nextInt(); for (int t = 0; t < T; t += 1) { long v = in.nextLong(); long c = in.nextLong(); long t1 = in.nextLong(); long t2 = in.nextLong(); // as long as we have both types of cookies, nobody gets angry // if either kind of cookies runs out, second type gets angry // if there are any cookies left, first type stays happy // if we don't have enough cookies to begin with if (v + c < t1 + t2) { System.out.println("No"); continue; } while (t1 > 0 && t2 > 0 && v > 0 && c > 0) { if (v > c) { final long d = Math.min(v - c, t1); v -= d; t1 -= d; } else if (v == c) { final long m = Math.min(c, Math.min(t1, t2)); t1 -= m; t2 -= m; c -= m; v -= m; } else /* if v < c */ { final long d = Math.min(c - v, t1); c -= d; t1 -= d; } } if (t2 > 0 && Math.min(c, v) < t2) { System.out.println("No"); continue; } System.out.println("Yes"); } } public static void mainD(String[] args) { final Scanner in = new Scanner(System.in); final int T = in.nextInt(); for (int t = 0; t < T; t += 1) { final int n = in.nextInt(); final int k = in.nextInt(); final int x = k % n == 0 ? 0 : 2; final int[][] a = new int[n][n]; int r = 0; int d = -1; for (int i = 0; i < k; i += 1) { if (r == 0) d += 1; a[r][(r + d) % n] = 1; r = (r + 1) % n; } System.out.println(x); for (r = 0; r < n; r += 1) { for (int c = 0; c < n; c += 1) { System.out.print(a[r][c]); } System.out.println(); } } } public static void mainE1(String[] args) { final Scanner in = new Scanner(System.in); final int T = in.nextInt(); for (int t = 0; t < T; t += 1) { final int n = in.nextInt(); final int k = in.nextInt(); } } public static void mainE2(String[] args) { final Scanner in = new Scanner(System.in); final int T = in.nextInt(); for (int t = 0; t < T; t += 1) { final int n = in.nextInt(); final int k = in.nextInt(); } } public static void mainF(String[] args) { final Scanner in = new Scanner(System.in); final int T = in.nextInt(); for (int t = 0; t < T; t += 1) { final int n = in.nextInt(); final int k = in.nextInt(); } } public static void main(String[] args) { mainD(args); } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
baffc0ff1da31f679514e6c0bc21cbf7
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
/* ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿ ⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿ ⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿ ⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿ ⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿ ⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺ ⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘ ⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆ ⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇ ⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇ ⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇ ⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇ ⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇ ⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀ ⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀ ⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸ ⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼ ⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿ ⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿ ⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿ ⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿ */ 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 ie=0;ie<t;ie++) { int n=s.nextInt(); int k=s.nextInt(); int[][] ans=new int[n][n]; for(int i=0;i<n;i++) { if(k==0) { break; } ans[i][i]=1; k--; } int x=0;int x2=n; p1:while(k>0) { x++; x2--; for(int i=0;i<n-x;i++) { if(k==0) { break p1; } ans[i][i+x]=1; k--; } for(int i=0;i<n-x2;i++) { if(k==0) { break p1; } ans[x2+i][i]=1; k--; } } int max=Integer.MIN_VALUE; int min=Integer.MAX_VALUE; int r=0; int c=0; for(int i=0;i<n;i++) { int count=0; for(int j=0;j<n;j++) { if(ans[i][j]==1) { count++; } } max=Math.max(max, count); min=Math.min(min, count); } r=(int)Math.pow(max-min, 2); max=Integer.MIN_VALUE; min=Integer.MAX_VALUE; for(int i=0;i<n;i++) { int count=0; for(int j=0;j<n;j++) { if(ans[j][i]==1) { count++; } } max=Math.max(max, count); min=Math.min(min, count); } c=(int)Math.pow(max-min, 2); System.out.println(r+c); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { System.out.print(ans[i][j]); } System.out.println(); } } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
14ae8ed770959161fc7f6b89d195b951
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class GFG { public static void main (String[] args) throws Exception { FastScanner sc = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); int t =sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int k=sc.nextInt(); if(k%n==0){ out.println("0"); } else{ out.println("2"); } int arr[][]=new int [n][n]; int x=0,y=0; for(int i=0;i<n && k>0;i++){ x=0; y=i; for(int j=0;j<n && k>0;j++){ arr[x][y]=1; k--; x=(x+1)%(n); y=(y+1)%(n); } } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ out.print(arr[i][j]+""); } out.println(); } } out.close(); } } class FastScanner { final private int BUFFER_SIZE = 1 << 17; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public FastScanner(InputStream in) { din = new DataInputStream(in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextLine() 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(); boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; return ret; } public long nextLong() throws Exception { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; return ret; } 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++]; } public double nextDouble() throws Exception { 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 static int binarySearchPM(int[] arr, int key) { int n = arr.length; int mid = -1; int begin = 0, end = n; while(begin <= end) { mid = (begin + end) / 2; if(mid == n) { return n; } if(key < arr[mid]) { end = mid - 1; } else if(key > arr[mid]) { begin = mid + 1; } else { return mid; } } //System.out.println(begin+" "+end); return -begin; //expected Index } // function to find last index <= y static int upperIndex(int arr[], int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int lowerIndex(int arr[], int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] >= x) h = mid - 1; else l = mid + 1; } return l; } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
2e858a2b05b45b897db0ea285ceea32c
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.PrintWriter; import java.util.Scanner; /** * @author hypnos * @date 2020/11/19 */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter writer = new PrintWriter(System.out); int t = sc.nextInt(); while (t-->0){ int n = sc.nextInt(); int k = sc.nextInt(); int value, mod, did; did = k/n+1; if (n > k){ if (k == 0){ mod = 0; }else { mod = 1; } }else { mod = k%n; } if (mod==0){ value = 0; }else { value = 2; } writer.println(value); int[][] arr = new int[n][n]; while (k != 0){ for (int j = 0; j < did; j++) { for (int i = 0; i < n; i++) { arr[i][(i+j)%n] = 1; k--; if (k==0){ break; } } if (k==0){ break; } } } for (int[] ar: arr){ for (int a: ar){ writer.print(a); } writer.println(); } // writer.println(Arrays.toString(arr)); } writer.flush(); } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
f5621a2956133766a0f68a9d33dbae3d
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.util.Scanner; public class D { static Scanner in = new Scanner(System.in); static int n, m; static int[][] a; public static void main(String[] args) { int t = in.nextInt(); while (t-- > 0) { n = in.nextInt(); m = in.nextInt(); if (m % n == 0) { System.out.println(0); } else { System.out.println(2); } a = new int[n][n]; int index = 0; int x = 0, y = 0; while (m-- > 0) { a[x][y % n] = 1; x++; y++; if (x == n) { x = 0; y = ++index; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(a[i][j]); } System.out.println(); } } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
e724e4256a5aa28fc03bbd42d1abdf37
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; /** * Built using CHelper plug-in * Actual solution is at the top */ public class AA { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(in.nextInt(), in, out); out.close(); } static class TaskA { long mod = (long)(1000000007); long fact[]; int depth[]; int parentTable[][]; int degree[]; ArrayList<Integer> leaves; int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException { while(testNumber-->0){ int n = in.nextInt(); int k = in.nextInt(); int ceil = (k+n-1)/n; int a[][] = new int[n][n]; for(int i=0;i<n;i++){ int count = 0; for(int j=i;count<ceil;j = (j+1)%n){ a[i][j] = 1; count++; } } int extra = ceil*n - k; for(int i=0;i<extra;i++){ a[i][i] = 0; } int max = ceil; int min = ceil; if(extra != 0) min--; int x = (max - min)*(max - min); out.println(2*x); print2d(a , out); } } public int index(String a , int start){ int index = -1; for(int i=start;i<a.length()-1;i++) if(a.charAt(i) == '('){ index = i; break; } return index; } public int distance(ArrayList<ArrayList<Integer>> a , int u , int v){ return depth[u]+depth[v] - 2*depth[lca(a , u , v)]; } // public void dfs(ArrayList<ArrayList<Integer>> a , int index , int parent){ // parentTable[index][0] = parent; // depth[index] = depth[parent]+1; // if(a.get(index).size()==1) // leaves.add(index); // for(int i=1;i<parentTable[index].length;i++) // parentTable[index][i] = parentTable[parentTable[index][i-1]][i-1]; // for(int i=0;i<a.get(index).size();i++){ // if(a.get(index).get(i)==parent) // continue; // dfs(a , a.get(index).get(i) , index); // } // } public int lca(ArrayList<ArrayList<Integer>> a , int u , int v){ if(depth[v]<depth[u]){ int x = u; u = v; v = x; } int diff = depth[v] - depth[u]; for(int i=0;i<parentTable[v].length;i++){ // checking whether the ith bit is set in the diff variable if(((diff>>i)&1) == 1) v = parentTable[v][i]; } if(v == u) return v; for(int i=parentTable[v].length-1;i>=0;i--){ if(parentTable[u][i] != parentTable[v][i]){ v = parentTable[v][i]; u = parentTable[u][i]; } } return parentTable[u][0]; } // for the min max problems public void build(int lookup[][] , int arr[], int n) { for (int i = 0; i < n; i++) lookup[i][0] = arr[i]; for (int j = 1; (1 << j) <= n; j++) { for (int i = 0; (i + (1 << j) - 1) < n; i++) { if (lookup[i][j - 1] > lookup[i + (1 << (j - 1))][j - 1]) lookup[i][j] = lookup[i][j - 1]; else lookup[i][j] = lookup[i + (1 << (j - 1))][j - 1]; } } } public int query(int lookup[][] , int L, int R) { int j = (int)(Math.log(R - L + 1)/Math.log(2)); if (lookup[L][j] >= lookup[R - (1 << j) + 1][j]) return lookup[L][j]; else return lookup[R - (1 << j) + 1][j]; } // for printing purposes public void print1d(int a[] , PrintWriter out){ for(int i=0;i<a.length;i++) out.print(a[i] + " "); out.println(); } public void print2d(int a[][] , PrintWriter out){ for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++) out.print(a[i][j]); out.println(); } // out.println(); } public void sieve(int a[]){ a[0] = a[1] = 1; int i; for(i=2;i*i<=a.length;i++){ if(a[i] != 0) continue; a[i] = i; for(int k = (i)*(i);k<a.length;k+=i){ if(a[k] != 0) continue; a[k] = i; } } } public int [][] matrixExpo(int c[][] , int n){ int a[][] = new int[c.length][c[0].length]; int b[][] = new int[a.length][a[0].length]; for(int i=0;i<c.length;i++) for(int j=0;j<c[0].length;j++) a[i][j] = c[i][j]; for(int i=0;i<a.length;i++) b[i][i] = 1; while(n!=1){ if(n%2 == 1){ b = matrixMultiply(a , a); n--; } a = matrixMultiply(a , a); n/=2; } return matrixMultiply(a , b); } public int [][] matrixMultiply(int a[][] , int b[][]){ int r1 = a.length; int c1 = a[0].length; int c2 = b[0].length; int c[][] = new int[r1][c2]; for(int i=0;i<r1;i++){ for(int j=0;j<c2;j++){ for(int k=0;k<c1;k++) c[i][j] += a[i][k]*b[k][j]; } } return c; } public long nCrPFermet(int n , int r , long p){ if(r==0) return 1l; // long fact[] = new long[n+1]; // fact[0] = 1; // for(int i=1;i<=n;i++) // fact[i] = (i*fact[i-1])%p; long modInverseR = pow(fact[r] , p-2 , p); long modInverseNR = pow(fact[n-r] , p-2 , p); long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p; return w; } public long pow(long a, long b, long m) { a %= m; long res = 1; while (b > 0) { long x = b&1; if (x == 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } public long pow(long a, long b) { long res = 1; while (b > 0) { long x = b&1; if (x == 1) res = res * a; a = a * a; b >>= 1; } return res; } public void swap(int a[] , int p1 , int p2){ int x = a[p1]; a[p1] = a[p2]; a[p2] = x; } public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) { if (start > end) { return; } int mid = (start + end) / 2; a.add(mid); sortedArrayToBST(a, start, mid - 1); sortedArrayToBST(a, mid + 1, end); } class Combine{ int value; int delete; Combine(int val , int delete){ this.value = val; this.delete = delete; } } class Sort2 implements Comparator<Combine>{ public int compare(Combine a , Combine b){ if(a.value > b.value) return 1; else if(a.value == b.value && a.delete>b.delete) return 1; else if(a.value == b.value && a.delete == b.delete) return 0; return -1; } } public int lowerLastBound(ArrayList<Integer> a , int x){ int l = 0; int r = a.size()-1; if(a.get(l)>=x) return -1; if(a.get(r)<x) return r; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a.get(mid) == x && a.get(mid-1)<x) return mid-1; else if(a.get(mid)>=x) r = mid-1; else if(a.get(mid)<x && a.get(mid+1)>=x) return mid; else if(a.get(mid)<x && a.get(mid+1)<x) l = mid+1; } return mid; } public int upperFirstBound(ArrayList<Integer> a , Integer x){ int l = 0; int r = a.size()-1; if(a.get(l)>x) return l; if(a.get(r)<=x) return r+1; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a.get(mid) == x && a.get(mid+1)>x) return mid+1; else if(a.get(mid)<=x) l = mid+1; else if(a.get(mid)>x && a.get(mid-1)<=x) return mid; else if(a.get(mid)>x && a.get(mid-1)>x) r = mid-1; } return mid; } public int lowerLastBound(int a[] , int x){ int l = 0; int r = a.length-1; if(a[l]>=x) return -1; if(a[r]<x) return r; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a[mid] == x && a[mid-1]<x) return mid-1; else if(a[mid]>=x) r = mid-1; else if(a[mid]<x && a[mid+1]>=x) return mid; else if(a[mid]<x && a[mid+1]<x) l = mid+1; } return mid; } public int upperFirstBound(long a[] , long x){ int l = 0; int r = a.length-1; if(a[l]>x) return l; if(a[r]<=x) return r+1; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a[mid] == x && a[mid+1]>x) return mid+1; else if(a[mid]<=x) l = mid+1; else if(a[mid]>x && a[mid-1]<=x) return mid; else if(a[mid]>x && a[mid-1]>x) r = mid-1; } return mid; } public long log(float number , int base){ return (long) Math.ceil((Math.log(number) / Math.log(base)) + 1e-9); } public long gcd(long a , long b){ if(a<b){ long c = b; b = a; a = c; } while(b!=0){ long c = a; a = b; b = c%a; } return a; } public long[] gcdEx(long p, long q) { if (q == 0) return new long[] { p, 1, 0 }; long[] vals = gcdEx(q, p % q); long d = vals[0]; long a = vals[2]; long b = vals[1] - (p / q) * vals[2]; // 0->gcd 1->xValue 2->yValue return new long[] { d, a, b }; } public void sievePhi(int a[]){ a[0] = 0; a[1] = 1; for(int i=2;i<a.length;i++) a[i] = i-1; for(int i=2;i<a.length;i++) for(int j = 2*i;j<a.length;j+=i) a[j] -= a[i]; } public void lcmSum(long a[]){ int sievePhi[] = new int[(int)1e6 + 1]; sievePhi(sievePhi); a[0] = 0; for(int i=1;i<a.length;i++) for(int j = i;j<a.length;j+=i) a[j] += (long)i*sievePhi[i]; } static class AVLTree{ Node root; public AVLTree(){ this.root = null; } public int height(Node n){ return (n == null ? 0 : n.height); } public int getBalance(Node n){ return (n == null ? 0 : height(n.left) - height(n.right)); } public Node rotateRight(Node a){ Node b = a.left; Node br = b.right; a.lSum -= b.lSum; a.lCount -= b.lCount; b.rSum += a.rSum; b.rCount += a.rCount; b.right = a; a.left = br; a.height = 1 + Math.max(height(a.left) , height(a.right)); b.height = 1 + Math.max(height(b.left) , height(b.right)); return b; } public Node rotateLeft(Node a){ Node b = a.right; Node bl = b.left; a.rSum -= b.rSum; a.rCount -= b.rCount; b.lSum += a.lSum; b.lCount += a.lCount; b.left = a; a.right = bl; a.height = 1 + Math.max(height(a.left) , height(a.right)); b.height = 1 + Math.max(height(b.left) , height(b.right)); return b; } public Node insert(Node root , long value){ if(root == null){ return new Node(value); } if(value<=root.value){ root.lCount++; root.lSum += value; root.left = insert(root.left , value); } if(value>root.value){ root.rCount++; root.rSum += value; root.right = insert(root.right , value); } // updating the height of the root root.height = 1 + Math.max(height(root.left) , height(root.right)); int balance = getBalance(root); //ll if(balance>1 && value<=root.left.value) return rotateRight(root); //rr if(balance<-1 && value>root.right.value) return rotateLeft(root); //lr if(balance>1 && value>root.left.value){ root.left = rotateLeft(root.left); return rotateRight(root); } //rl if(balance<-1 && value<=root.right.value){ root.right = rotateRight(root.right); return rotateLeft(root); } return root; } public void insertElement(long value){ this.root = insert(root , value); } public int getElementLessThanK(long k){ int count = 0; Node temp = root; while(temp!=null){ if(temp.value == k){ if(temp.left == null || temp.left.value<k){ count += temp.lCount; return count-1; } else temp = temp.left; } else if(temp.value>k){ temp = temp.left; } else{ count += temp.lCount; temp = temp.right; } } return count; } public void inorder(Node root , PrintWriter out){ Node temp = root; if(temp!=null){ inorder(temp.left , out); out.println(temp.value + " " + temp.lCount + " " + temp.rCount); inorder(temp.right , out); } } } static class Node{ long value; long lCount , rCount; long lSum , rSum; Node left , right; int height; public Node(long value){ this.value = value; left = null; right = null; lCount = 1; rCount = 1; lSum = value; rSum = value; height = 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 long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
b7b6dd8039d68bc77ea241cbc37bd238
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
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 Main { public static void main (String[] args) throws java.lang.Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(bf.readLine()); while(t-->0) { String s[]=bf.readLine().split(" "); int n=Integer.parseInt(s[0]); int k=Integer.parseInt(s[1]); int z=k/n; int a[][]=new int[n][n]; int p=0; int rem=k%n; int mission=0; int ii=0; for( ii=0;ii<rem;ii++) { for(int j=0;j<z+1;j++) { if(p==n) { p=0; } // System.out.println(p+" "+n); a[ii][p]=1; p++; } // System.out.println(Arrays.toString(a[i])); } for(int i=ii;i<n;i++) { for(int j=0;j<z;j++) { if(p==n) { p=0; } // System.out.println(p+" "+n); a[i][p]=1; p++; } // System.out.println(Arrays.toString(a[i])); } // int rem=k%n; // int index=0; // ArrayList<Integer> al=new ArrayList<>(); //// for(int i=0;i<rem;i++) //// { //// for(int j=0;j<n;j++) //// { //// if(a[i][j]==0 && (!al.contains(j))) //// { //// a[i][j]=1; //// al.add(j); //// System.out.println("sangam done "+i+" "+j); //// break; //// } //// } //// } // for(index=0;index<n;index++) // { // if(a[0][index]==0) // { // break; // } // } // if(z==0) // { // int min=0; // for(int i=0;i<rem;i++) // { // for(int j=0;j<1;j++) // { // a[i][min]=1; // min++; // } // } // } // else // { // int kk=0; // for(int i=0;i<rem;i++) // { // for(int j=0;j<1;j++) // { // while(true) // { // int pp=(index+(z*kk))%n; // // System.out.println(" pp "+pp+" "+al); // if(al.contains(pp)) // { // kk++; // // System.out.println("Mishra "); // } // else // { // a[i][pp]=1; // kk++; // al.add(pp); // break; // } // } // } // } //} int maxr=Integer.MIN_VALUE,minr=Integer.MAX_VALUE,maxc=Integer.MIN_VALUE,minc=Integer.MAX_VALUE; for(int i=0;i<n;i++) { int count=0; for(int j=0;j<n;j++) { if(a[i][j]==1) { count++; } } if(count>maxr) maxr=count; if(count<minr) minr=count; } for(int i=0;i<n;i++) { int count=0; for(int j=0;j<n;j++) { if(a[j][i]==1) { count++; } } if(count>maxc) maxc=count; if(count<minc) minc=count; } int z1=Math.abs(maxr-minr); int z2=Math.abs(maxc-minc); System.out.println((z1*z1)+(z2*z2)); // int sum=0; for(int i=0;i<n;i++) { // int sum=0; for(int j=0;j<n;j++) { // sum+=a[i][j]; System.out.print(a[i][j]); } System.out.println(); } // System.out.println("sum"+ sum); // System.out.println("ArrayList "+al); // System.out.println("al size "+al.size()); } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
99a596ee5221f25724cbbae8a8f8479e
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
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 Main { public static void main (String[] args) throws java.lang.Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(bf.readLine()); while(t-->0) { String s[]=bf.readLine().split(" "); int n=Integer.parseInt(s[0]); int k=Integer.parseInt(s[1]); int z=k/n; int a[][]=new int[n][n]; int p=0; int rem=k%n; int mission=0; int ii=0; for( ii=0;ii<rem;ii++) { for(int j=0;j<z+1;j++) { if(p==n) { p=0; } a[ii][p]=1; p++; } } for(int i=ii;i<n;i++) { for(int j=0;j<z;j++) { if(p==n) { p=0; } a[i][p]=1; p++; } } if(k==0) System.out.println(0); else if(k%n==0) System.out.println(0); else System.out.println(2); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { // sum+=a[i][j]; System.out.print(a[i][j]); } System.out.println(); } } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
eafc526339fd29cc1f846e6365982e78
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
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 Main { public static void main (String[] args) throws java.lang.Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(bf.readLine()); while(t-->0) { String s[]=bf.readLine().split(" "); int n=Integer.parseInt(s[0]); int k=Integer.parseInt(s[1]); int z=k/n; int a[][]=new int[n][n]; int p=0; int rem=k%n; int mission=0; int ii=0; for( ii=0;ii<rem;ii++) { for(int j=0;j<z+1;j++) { if(p==n) { p=0; } a[ii][p]=1; p++; } } for(int i=ii;i<n;i++) { for(int j=0;j<z;j++) { if(p==n) { p=0; } a[i][p]=1; p++; } } // int maxr=Integer.MIN_VALUE,minr=Integer.MAX_VALUE,maxc=Integer.MIN_VALUE,minc=Integer.MAX_VALUE; // for(int i=0;i<n;i++) // { // int count=0; // for(int j=0;j<n;j++) // { // if(a[i][j]==1) // { // count++; // } // } // if(count>maxr) // maxr=count; // if(count<minr) // minr=count; // } // for(int i=0;i<n;i++) // { // int count=0; // for(int j=0;j<n;j++) // { // if(a[j][i]==1) // { // count++; // } // } // if(count>maxc) // maxc=count; // if(count<minc) // minc=count; // } // int z1=Math.abs(maxr-minr); // int z2=Math.abs(maxc-minc); // System.out.println((z1*z1)+(z2*z2)); if(k==0) System.out.println(0); else if(k%n==0) System.out.println(0); else System.out.println(2); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { // sum+=a[i][j]; System.out.print(a[i][j]); } System.out.println(); } } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
76d280064fd2f3032cb97b632f6071e7
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
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 Main { public static void main (String[] args) throws java.lang.Exception { // your code goes here FastScanner sc=new FastScanner(); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(),k=sc.nextInt(); if(k%n==0) System.out.println("0"); else System.out.println("2"); int a[][]=new int[n][n]; // for(int i=0;i<n;i++) // for(int j=0;j<n;j++) a[i][j]=0; int p=0,q=0; while(k-->0){ a[p][q]=1; p++;q++;q%=n; if(p==n){ p=0;q++;q%=n; } } for(int i=0;i<n;i++){ for(int j=0;j<n;j++) System.out.print(a[i][j]); System.out.println(); } } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
9bc3d3c24bbf814c8f5ec6a2a36b3642
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.util.*; public class grid { public static void main(final String args[]) { final Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int n, k; double num1, num2; /* * 1 3 6 */ while (t > 0) { --t; n = sc.nextInt(); k = sc.nextInt(); if (k == 0) { System.out.println("0"); printmatrix(0, 0, 0, n); continue; } num1 = Math.ceil((double) k / n); num2 = Math.floor((double) k / n); if (num1 == num2) System.out.println("0"); else System.out.println("2"); printmatrix(num1, num2, k, n); } } public static void printmatrix(final double num1, final double num2, final int k, final int n) { final int arr[][] = new int[n][n]; int i = 0, j = 0, a = 0; double x=(k-(n*num2))/(num1-num2); int R[]=new int[n];//3 3 2 2 for(i=0;i<n;i++) { if(i<x) R[i]=(int)num1; else R[i]=(int)num2; } while (a<n) { i=a; while (R[a] > 0) { --R[a]; if (i == n) i = 0; arr[j][i++] = 1; } j++; i--; a++; } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) System.out.print(arr[i][j]); System.out.println(); } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
ee9865daa75b54bb9fb5b344f0b951cc
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.StringTokenizer; public class Juwon { static int N, K, arr[][]; public static void main(String args0[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int TC = Integer.parseInt(br.readLine().trim()); for (int t = 1; t <= TC; t++) { StringTokenizer st = new StringTokenizer(br.readLine()); N = Integer.parseInt(st.nextToken()); K = Integer.parseInt(st.nextToken()); arr = new int[N][N]; bw.write(solve() + "\n"); bw.write(print() + ""); } bw.close(); } public static String print() { int startIdx = 0; while (K > 0) { int idx = startIdx; for (int i = 0; i < N; i++) { if (K > 0) { arr[i][idx++] = 1; idx %= N; K -= 1; } else { break; } } startIdx += 1; startIdx %= N; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { sb.append(arr[i][j]); } sb.append('\n'); } return sb.toString(); } public static int solve() { if (K == 0) { return 0; } else if (K % N == 0) { return 0; } return 2; } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
831dc46c5640f4b708956e52197a2958
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.*; import java.util.*; public class D { static Scanner scan = new Scanner(System.in); public static void solve() throws Exception { //BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //StringTokenizer tk = new StringTokenizer(in.readLine()); int n = scan.nextInt(); int k = scan.nextInt(); if(k%n==0) System.out.println(0); else System.out.println(2); int[][] a = new int[n][n]; int x = k/n; k%=n; for(int i=0;i<n;i++) { for(int j=0;j<x;j++) { a[i][(j+i)%n]=1; } } for(int i=0;i<k;i++) { for(int j=0;j<=x;j++) { if(k!=0) { a[i][(i+j)%n]=1; } } } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { System.out.print(a[i][j]); } System.out.println(); } } public static void main(String[] args) throws Exception { if(scan.hasNext()) { int t= scan.nextInt(); //int t= 1; outer:while(t-->0) { solve(); } } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
0cf36fdda7173ea4542a09c2d6a93791
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class D654{ public static void main(String args[]){ FastReader sc = new FastReader(); StringBuilder sb=new StringBuilder(); int t,i,n,j,k; t=sc.nextInt(); while(t-->0){ n=sc.nextInt(); k=sc.nextInt(); if(k%n==0) sb.append(0).append('\n'); else sb.append(2).append('\n'); int p=0,q=0; int a[][]=new int[n][n]; while(k>0){ k--; a[p++][q++]=1; q=q%n; if(p==n){ p=0; q++; q=q%n; } } for(i=0;i<n;i++){ for(j=0;j<n;j++) sb.append(a[i][j]); sb.append('\n'); } } sb.deleteCharAt(sb.length()-1); out.println(sb); } static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(System.out,true); } String next(){ while(st==null || !st.hasMoreElements()){ try{ st= new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str=br.readLine(); } catch(IOException e){ e.printStackTrace(); } return str; } } public static boolean isPrime(int n) { if(n<2) return false; for(int i=2;i<=(int)Math.sqrt(n);i++) { if(n%i==0) return false; } return true; } public static void print(int a[],int l,int r){ int i; for(i=l;i<=r;i++) out.print(a[i]+" "); out.println(); } public static long fastexpo(long x, long y, long p){ long res=1; while(y > 0){ if((y & 1)==1) res= ((res%p)*(x%p))%p; y= y >> 1; x = ((x%p)*(x%p))%p; } return res; } public static boolean[] sieve (int n) { boolean primes[]=new boolean[n+1]; Arrays.fill(primes,true); primes[0]=primes[1]=false; for(int i=2;i*i<=n;i++){ if(primes[i]){ for(int j=i*i;j<=n;j+=i) primes[j]=false; } } return primes; } public static long gcd(long a,long b){ return (BigInteger.valueOf(a).gcd(BigInteger.valueOf(b))).longValue(); } public static void merge(long a[],int l,int m,int r){ int n1,n2,i,j,k; n1=m-l+1; n2=r-m; long L[]=new long[n1]; long R[]=new long[n2]; for(i=0;i<n1;i++) L[i]=a[l+i]; for(j=0;j<n2;j++) R[j]=a[m+1+j]; i=0;j=0; k=l; while(i<n1&&j<n2){ if(L[i]<=R[j]){ a[k]=L[i]; i++; } else{ a[k]=R[j]; j++; } k++; } while(i<n1){ a[k]=L[i]; i++; k++; } while(j<n2){ a[k]=R[j]; j++; k++; } } public static void sort(long a[],int l,int r){ int m; if(l<r){ m=(l+r)/2; sort(a,l,m); sort(a,m+1,r); merge(a,l,m,r); } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
646f07089f6816404d6bc977241b08c6
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class GridzeroOne{ public static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public char nextChar() { int c = read(); while (isSpaceChar(c)) c = read(); return (char)c; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static long gcd(long a, long b){ if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a*b)/gcd(a, b); } static long func(long a[],long size,int s){ long max1=a[s]; long maxc=a[s]; for(int i=s+1;i<size;i++){ maxc=Math.max(a[i],maxc+a[i]); max1=Math.max(maxc,max1); } return max1; } public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } public int hashCode() { return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode()); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<U, V> p = (Pair<U, V>) o; return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y)); } public int compareTo(Pair<U, V> b) { int cmpU = x.compareTo(b.x); return cmpU != 0 ? cmpU : y.compareTo(b.y); } public String toString() { return String.format("(%s, %s)", x.toString(), y.toString()); } } public static void main(String args[]) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader sc = new FastReader(inputStream); PrintWriter w = new PrintWriter(outputStream); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int r = sc.nextInt(); int a[][] = new int[n][n]; int one = n*n, zero = 0; int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; if(r % n == 0) { w.println("0"); } else { w.println("2"); } int temp = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(temp < r) { a[j][(i+j)%n] = 1; temp++; } } } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { w.print(a[i][j]); } w.println(); } } w.close(); } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
c16d8e8cbf0bf7ef2c462443c19e62fd
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.*; import java.util.*; import java.util.concurrent.TimeUnit; public class d654 implements Runnable{ public static void main(String[] args) { try{ new Thread(null, new d654(), "process", 1<<26).start(); } catch(Exception e){ System.out.println(e); } } 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 out) { int N = sc.nextInt(); int K = sc.nextInt(); int[][] arr = new int[N][N]; int transpose = 0; loop: while(K > 0) { for(int i = transpose, j = 0; j < N; i++, j++) { if(i >= N) { i -= N; } arr[j][i] = 1; K--; if(K == 0) break loop; } transpose++; } int minR = Integer.MAX_VALUE, maxR = 0; int minC = Integer.MAX_VALUE, maxC = 0; for(int i = 0; i < N; i++) { int count1 = 0; int count2 = 0; for(int j = 0; j < N; j++) { if(arr[i][j] == 1) count1++; if(arr[j][i] == 1) count2++; } minR = Math.min(minR, count1); maxR = Math.max(maxR, count1); minC = Math.min(minC, count2); maxC = Math.max(maxC, count2); } out.println((maxR - minR) * (maxR - minR) + (maxC - minC) * (maxC - minC)); for(int[] each: arr) { for(int i : each) out.print(i); out.println(); } } } 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>, Comparator<tup>{ int a, b; tup(int a,int b){ this.a=a; this.b=b; } public tup() { } @Override public int compareTo(tup o){ return Integer.compare(b,o.b); } @Override public int compare(tup o1, tup o2) { return Integer.compare(o1.b, o2.b); } @Override public int hashCode() { return Objects.hash(a, b); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; tup other = (tup) obj; return a==other.a && b==other.b; } @Override public String toString() { return a + " " + 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
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
0a5a9d04039b0daff6de5abe90fb9551
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.*; import java.util.*; public class Grid00100 { /*static int Bs(int sum[], int n, int i) { int low = 0, mid , high = 2*(n-1); while(low<=high) { mid = (low+high)/2; if(sum[mid]==i&&mid%2==0) { resLow = mid;return -1; } else if(sum[mid]<i) low = mid+1; else high = mid-1; } return low; }*/ public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t =in.nextInt(); while(t-->0) { int n = in.nextInt(); int k = in.nextInt(); int f = 0; if(k%n!=0) f = 2; out.println(f); int cnt = 0; int ans[][] = new int[n+1][n+1]; int i = 0, j = 0; int r = 0; while(cnt<k) { while(i<n) { ans[i+1][j+1] = 1;cnt++; if(cnt==k) break; //out.println(i+" "+j); i++;j=(j+1)%n; } i = 0; j = r+1;r++; } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) out.print(ans[i][j]); out.println(); } } out.close(); } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output