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
6eac67df9302056df45dd866887dc133
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { // br = new BufferedReader(new FileReader(new File("input.txt"))); // out = new PrintWriter(new File("output.txt")); int nasral = nextInt(); for (int JOPA = 0; JOPA < nasral; JOPA++) { long p = nextLong(); long ans = Long.MAX_VALUE; for (int i = 0; i < 3; i++) { long a = nextLong(); if (p < a) ans = Math.min(ans, a - p); else { if(p % a == 0)ans = 0; a = (p / a + 1) * a; ans = Math.min(ans, a - p); } } out.println(ans); } out.close(); } public static StringTokenizer in = new StringTokenizer(""); public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static PrintWriter out = new PrintWriter(System.out); public static String next() throws IOException { while (!in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public static Integer nextInt() throws IOException { return Integer.parseInt(next()); } public static Long nextLong() throws IOException { return Long.parseLong(next()); } } class Point { int l; int r; public Point(int l, int r) { this.l = l; this.r = r; } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
8b044f621f64dd7c799bc6baaa8a73c2
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; import java.lang.*; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ long p=sc.nextLong(); long a=sc.nextLong(); long b=sc.nextLong(); long c=sc.nextLong(); if(p%a==0 || p%b==0 || p%c==0) System.out.println(0); else{ long amin,bmin,cmin; if(p>a){ amin=p+(a-p%a); } else amin=a; if(p>b){ bmin=p+(b-p%b); } else bmin=b; if(p>c){ cmin=p+(c-p%c); } else cmin=c; System.out.println((Math.min(cmin-p,(Math.min(amin-p,bmin-p))))); } } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
c282641b7ff2b4aff37572641c4ee5d1
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Scanner; import java.util.Set; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws Exception{ Scanner s = new Scanner(System.in); int tc = s.nextInt(); while(tc-- > 0) { long p = s.nextLong() , a = s.nextLong() , b = s.nextLong() , c = s.nextLong(); long min = (long)Math.ceil((double)p/a); min = min*a; min -= p; if(min < 0) { min += a; } long temp = (long)Math.ceil((double)p/b); temp = temp * b; temp -= p; if(temp < 0) temp += b; min = Math.min(min, temp); temp = (long)Math.ceil((double)p/c); temp = temp * c; temp -= p; if(temp < 0) temp += c; min = Math.min(min, temp); System.out.println(min); } } public static int fun(int a , int b) { if(a == 0) return 0; if(b > a) return 1; int val = 1 + fun(a/b , b); int val1 = 1 + fun(a , b + 1); return Math.min(val , val1); } public static boolean findDivisor(long n) { for(int i = 2;i <= Math.sqrt(n);i++) { if(n % i == 0) { if(i % 2 == 1) return true; else if((n/i) % 2 == 1) return true; } } return false; } public static void mergeSort(int[] input) { mergeSort(input , 0 , input.length - 1); } public static void mergeSort(int[] input , int si , int ei) { if(si >= ei) { return; } int mid = (si + ei) / 2; mergeSort(input , si , mid); mergeSort(input , mid + 1 , ei); merge(input , si , ei); } public static void merge(int[] input , int si , int ei) { int[] temp = new int[ei - si + 1]; int mid = (si + ei) / 2; int i = si; int j = mid + 1; int k = 0; while(i <= mid && j <= ei) { if(input[i] < input[j]) { temp[k] = input[i]; i++; k++; } else { temp[k] = input[j]; j++; k++; } } while(i <= mid) { temp[k] = input[i]; i++; k++; } while(j <= ei) { temp[k] = input[j]; j++; k++; } for(i = 0;i < temp.length;i++) { input[i + si] = temp[i]; } } } class pair { int h; int w; } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } class exp extends Exception { }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
29fab25fb616ebd4baac13b23505e857
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Scanner; public class JavaTasks{ static Scanner sc=new Scanner(System.in); public static void solve(){ long p=sc.nextLong(),a=sc.nextLong(),b=sc.nextLong(),c=sc.nextLong(),x,y,z,temp1; if (p<a) x=a-p; else if (p>=a && p%a==0) x=a*(p/a)-p; else x=a*(p/a+1)-p; if (p<b) y=b-p; else if (p>=b && p%b==0) y=b*(p/b)-p; else y=b*(p/b+1)-p; if (p<c) z=c-p; else if (p>=c && p%c==0) z=c*(p/c)-p; else z=c*(p/c+1)-p; temp1=Math.min(x,y); System.out.println(Math.min(temp1,z)); } public static void main(String[] args){ int k=sc.nextInt(); while(k>0){ solve(); k--; } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
5e57efb464cfffc8e2e4baae5c7edcbe
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String[] args) { try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { String s[] = br.readLine().split(" "); long p = Long.parseLong(s[0]); long a = Long.parseLong(s[1]); long b = Long.parseLong(s[2]); long c = Long.parseLong(s[3]); long min = Long.MAX_VALUE; long a1=a,b1=b,c1=c; long counta = Math.max( p/a ,1); long countb = Math.max( p/b ,1); long countc = Math.max( p/c ,1); while(true) { if (a1 < p) { a1 = a*counta; counta++; } if (b1 < p) { b1 = b*countb; countb++; } if (c1 < p) { c1 = c * countc; countc++; } if (a1 >= p && b1 >= p && c1 >= p) break; } min = Math.min(a1,b1); min = Math.min(min,c1); System.out.println(min-p); } }catch(Exception e) { return; } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
0fd6b5fa889f847ba1c70529b2a9215b
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Arrays; import java.util.Scanner; import java.util.ArrayList; public class test { static void solve(long p, long[] a) { long res = Long.MAX_VALUE; long r = 0; for (int i = 0; i < 3; i++) { if (p % a[i] == 0) { r = p/a[i]; } else { r = p/a[i] + 1; } res = Math.min(res, a[i] * r - p); } System.out.println(res); } public static void main(String[] args) { Scanner w = new Scanner(System.in); int t = w.nextInt(); for (int i = 0; i < t; i++) { long p = w.nextLong(); long[] a = new long[3]; for (int j = 0; j < 3; j++) { a[j] = w.nextLong(); } solve(p, a); } } //------------------// static long factorial(long n) { long res = 1; for (long i = 2; i <= n; i++) { res *= i; } return res; } static long cGeneral(long n, long r) { return factorial(n)/(factorial(r) * factorial(n-r)); } static String reverse(String s) { StringBuilder input1 = new StringBuilder(); input1.append(s); input1.reverse(); return input1.toString(); } static void reverseArray(String[] a) { for (int i = 0; i < a.length; i++) { a[i] = reverse(a[i]); } } static boolean equalsArray(int[] a, int[] b) { for (int i = 0; i < a.length; i++) { if (a[i] != b[i]) { return false; } } return true; } static long sum(ArrayList<Long> a) { long sum = 0; for (int i = 0; i < a.size(); i++) { sum += a.get(i); } return sum; } static long max(ArrayList<Long> a) { long max = Long.MIN_VALUE; if (a.size() == 0) { max = 0; } for (int i = 0; i < a.size(); i++) { if (a.get(i) >= max) { max = a.get(i); } } return max; } static long min(ArrayList<Long> a) { long min = Long.MAX_VALUE; if (a.size() == 0) { min = 0; } for (int i = 0; i < a.size(); i++) { if (a.get(i) <= min) { min = a.get(i); } } return min; } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
b41f63210577133161205f60e81b163b
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; public class swimmers { public static void main(String[] args) { Scanner input = new Scanner(System.in); int numCases = input.nextInt(); for (int i = 0; i < numCases; i++) { long currMin = Long.MAX_VALUE; long p = input.nextLong(); long a = input.nextLong(); long b = input.nextLong(); long c = input.nextLong(); if (p < a) { currMin = Math.min(currMin, a - p); } else { if (p % a == 0) { currMin = 0; } else { long temp = ((p / a) + 1) * a; currMin = Math.min(currMin, Math.abs(temp - p)); } } // System.out.println(currMin); if (p < b) { currMin = Math.min(currMin, b - p); } else { if (p % b == 0) { currMin = 0; } else { long temp = ((p / b) + 1) * b; currMin = Math.min(currMin, Math.abs(temp - p)); } } // System.out.println(currMin); if (p < c) { currMin = Math.min(currMin, c - p); } else { if (p % c == 0) { currMin = 0; } else { long temp = ((p / c) + 1) * c; currMin = Math.min(currMin, Math.abs(temp - p)); } } System.out.println(currMin); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
ad4a3ad2050590e8260a150490beba9b
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; public class Training2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t > 0) { long p, a, b, c; p = sc.nextLong(); a = sc.nextLong(); b = sc.nextLong(); c = sc.nextLong(); a = (a - p % a) % a; b = (b - p % b) % b; c = (c - p % c) % c; long res = Math.min(a, Math.min(c, b)); System.out.println(res); t--; } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
aa514813e3576b23514859ee15f69b5e
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Scanner; public class Solution { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int number_of_test_cases = sc.nextInt(); for(int t=0;t<number_of_test_cases;t++) { long p,a,b,c; p = sc.nextLong(); a = sc.nextLong(); b = sc.nextLong(); c = sc.nextLong(); if(p%a==0 || p%b==0 || p%c==0) System.out.println("0"); else { long ans = Math.min(a-p%a, b-p%b); ans = Math.min(ans, c-p%c); System.out.println(ans); } } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
f7d21485511cced2a66934f2eac6a99a
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.*; import java.util.*; import java.math.BigInteger; public class Main { InputStream is; PrintWriter out = new PrintWriter(System.out); ; String INPUT = ""; void run() throws Exception { is = System.in; solve(); out.flush(); out.close(); } public static void main(String[] args) throws Exception { new Main().run(); } public byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; public 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++]; } public boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } public double nd() { return Double.parseDouble(ns()); } public char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private int ni() { return (int)nl(); } 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(); } } class Pair { int first; int second; Pair(int a, int b) { first = a; second = b; } } // KMP ALGORITHM void KMPSearch(String pat, String txt) { int M = pat.length(); int N = txt.length(); int lps[] = new int[M]; int j = 0; computeLPSArray(pat, M, lps); int i = 0; while (i < N) { if (pat.charAt(j) == txt.charAt(i)) { j++; i++; } if (j == M) { j = lps[j - 1]; } else if (i < N && pat.charAt(j) != txt.charAt(i)) { if (j != 0) j = lps[j - 1]; else i = i + 1; } } } void computeLPSArray(String pat, int M, int lps[]) { int len = 0; int i = 1; lps[0] = 0; while (i < M) { if (pat.charAt(i) == pat.charAt(len)) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = len; i++; } } } } int FirstAndLastOccurrenceOfAnElement(int[] arr, int target, boolean findStartIndex) { int ans = -1; int n = arr.length; int low = 0; int high = n-1; while(low <= high) { int mid = low + (high - low)/2; if(arr[mid] > target) high = mid-1; else if(arr[mid] < target) low = mid+1; else { ans = mid; if(findStartIndex) high = mid-1; else low = mid+1; } } return ans; } int[] na(int n) { int[] arr = new int[n]; for(int i=0; i<n; i++) arr[i]=ni(); return arr; } long[] nal(int n) { long[] arr = new long[n]; for(int i=0; i<n; i++) arr[i]=nl(); return arr; } void solve() { long t = nl(); while(t-- > 0) { long p=nl(),a=nl(),b=nl(),c=nl(); long A = ceil(p, a); long B = ceil(p, b); long C = ceil(p, c); // out.println(A+" "+B+" "+C); long finalA = a*A-p; long finalB = b*B-p; long finalC = c*C-p; out.println(Math.min(finalA, Math.min(finalB, finalC))); } } long ceil(long a, long b) { if(a%b == 0) return a/b; return a/b+1; } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
f0d0ae0fab9acf97325b82806250f25b
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; import java.math.*; public class Experiment { public static long func(long p,long a) { if(p==a) return 0; if(p%a==0) return 0; if(p<a) return a-p; return a-(p%a); } public static void main(String[] args) { Scanner in=new Scanner(System.in); int t=in.nextInt(); while(t!=0) { long p=in.nextLong(); long a=in.nextLong(); long b=in.nextLong(); long c=in.nextLong(); long x=func(p,a);long y=func(p,b);long z=func(p,c); long min=Math.min(x, Math.min(y, z)); System.out.println(min); t--; } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
e6c069db360957fc4d37e21281c02ae4
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.*; import java.util.*; public class D { public static void main(String[] args) throws Exception { Scanner src = new Scanner(System.in); int t = src.nextInt(); while(t-->0){ long p = src.nextLong(); long a = src.nextLong(); long b = src.nextLong(); long c = src.nextLong(); System.out.println(Math.min(Math.min((a-p%a)%a, (b-p%b)%b),(c-p%c)%c)); } src.close(); } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
101d8caa1cb57dd1ca9a3e1c6570d51b
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static long mod = (int)1e9+7; public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; while(t-->0) { long p=sc.nextLong(); long a=sc.nextLong(); long b=sc.nextLong(); long c=sc.nextLong(); if(p%a==0||p%b==0||p%c==0) { System.out.println("0"); continue; } long min=Math.min(a-(p%a),b-(p%b)); min=Math.min(c-(p%c),min); System.out.println(min); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static int DigitSum(int n) { int r=0,sum=0; while(n>=0) { r=n%10; sum=sum+r; n=n/10; } return sum; } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
893cbb6fe1e0bbe2e9bfed4c7ed2939e
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Arrays; import java.util.Scanner; public class cf1492A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { long p = sc.nextLong(); long a = sc.nextLong(); long b = sc.nextLong(); long c = sc.nextLong(); System.out.println(Math.min(Math.min((a-p%a)%a, (b-p%b)%b), (c-p%c)%c)); } sc.close(); } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
1239e31cc43754b8b5366547ad47d6b9
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Arrays; import java.util.Scanner; public class cf1492A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { long p = sc.nextLong(); long[] a = new long[3]; boolean immediate = false; long mi = Long.MAX_VALUE; for(int i=0; i<3; i++) { a[i] = sc.nextLong(); if(p%a[i] == 0) immediate = true; mi = Math.min(mi, (p/a[i] + 1)*a[i] - p); } System.out.println(immediate ? 0 : mi); } sc.close(); } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
2dc7e7d81cfa633f98186a84c5d0aac9
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { f(); } private static void f() { g(); } private static void g() { Scanner scanner = new Scanner(System.in); int ttt = scanner.nextInt(); for (int k = 0; k < ttt; k++) { long p = scanner.nextLong(); long a = scanner.nextLong(); long b = scanner.nextLong(); long c = scanner.nextLong(); long x = (a - (p % a)) % a; long y = (b - (p % b)) % b; long z = (c - (p % c)) % c; int u = 3; y = Math.min(x, y); y = Math.min(y, z); System.out.println(y); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
4819937181a9029a2b490ca8fcc2e69f
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { f(); } private static void f() { Scanner scanner = new Scanner(System.in); int ttt = scanner.nextInt(); for (int k = 0; k < ttt; k++) { long p = scanner.nextLong(); long a = scanner.nextLong(); long b = scanner.nextLong(); long c = scanner.nextLong(); long x = (a - (p % a)) % a; long y = (b - (p % b)) % b; long z = (c - (p % c)) % c; int u = 3; y = Math.min(x, y); y = Math.min(y, z); System.out.println(y); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
8b7312c9ae16392d1e087b00d5122932
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int ttt = scanner.nextInt(); for (int k = 0; k < ttt; k++) { long p = scanner.nextLong(); long a = scanner.nextLong(); long b = scanner.nextLong(); long c = scanner.nextLong(); long x = (a - (p % a)) % a; long y = (b - (p % b)) % b; long z = (c - (p % c)) % c; int u = 3; y = Math.min(x, y); y = Math.min(y, z); System.out.println(y); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
b9e6c1ba874ddcff44b57b03ef4a2752
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for (int k = 0; k < t; k++) { long p = scanner.nextLong(); long a = scanner.nextLong(); long b = scanner.nextLong(); long c = scanner.nextLong(); long x = (a - (p % a)) % a; long y = (b - (p % b)) % b; long z = (c - (p % c)) % c; x = Math.min(x, y); x = Math.min(x, z); System.out.println(x); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
07278a9b3cd9cf1a55f54188780b8eff
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.Set; public class Main { public static void main(String[] args) { Scanner ip = new Scanner(System.in); int c = ip.nextInt(); while (c > 0) { System.out.println(whoWins(ip)); c--; } } private static String whoWins(Scanner ip) { long p = ip.nextLong(); long[] ar = new long[3]; for (int i=0; i<3; i++) { ar[i] = ip.nextLong(); } long a = ar[0]; long b = ar[1]; long c = ar[2]; long at = p/a; if (at*a != p) { at++; } at = (at*a)-p; long bt = p/b; if (bt*b != p) { bt++; } bt = (bt*b)-p; long ct = p/c; if (ct*c != p) { ct++; } ct = (ct*c)-p; long min = Integer.MAX_VALUE; min = Math.min(at, Math.min(bt, ct)); return min+""; } private static ArrayList<ArrayList<Integer>> makeSS(int[] ar) { ArrayList<ArrayList<Integer>> al = new ArrayList<>(); for (int i=0; i<ar.length; i++) { ArrayList<Integer> l = new ArrayList<>(); l.add(ar[i]); for (int j=i+1; j<ar.length; j++) { l.add(ar[j]); al.add(new ArrayList<>(l)); } } return al; } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
058e9f23240a4b4e8724b342103fb2ae
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; import java.io.*; public class Main { static BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out)); static StringTokenizer tok; public static void main(String[] args) throws Exception { solution(); } public static void solution() throws Exception { int TestCase = Integer.parseInt(rd.readLine()); for(int TT=0;TT<TestCase;TT++) { tok = new StringTokenizer(rd.readLine()); long n = Long.parseLong(tok.nextToken()); long a = Long.parseLong(tok.nextToken()); long b = Long.parseLong(tok.nextToken()); long c = Long.parseLong(tok.nextToken()); if(a < n) { long cs = n / a; if(n % a !=0 ) cs++; a = cs*a; } if(b < n) { long cs = n / b; if(n % b !=0 ) cs++; b = cs*b; } if(c < n) { long cs = n / c; if(n % c !=0 ) cs++; c = cs*c; } System.out.println(Math.min(a-n, Math.min(b-n, c-n))); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
74707f54192b0fdb16ecbdf6e010509a
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc. for (int idx = 1; idx <= t; ++idx) { long p = in.nextLong(); long a = in.nextLong(); long b = in.nextLong(); long c = in.nextLong(); a = (a - p % a) % a; b = (b - p % b) % b; c = (c - p % c) % c; System.out.println(Math.min(a, Math.min(b, c))); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
2d97301fe46e288e7d4b2dd765d3b3f7
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
// Three swimmers decided to organize a party in the swimming pool!At noon,they started to swim from the left side of the pool. // It takes the first swimmer exactly a minutes to swim across the entire pool and come back,exactly b minutes for the second swimmer and c minutes for the third.Hence,the first swimmer will be on the left side of the pool after 0,a,2 a,3 a,...minutes after the start time,the second one will be at 0,b,2 b,3 b,...minutes,and the third one will be on the left side of the pool after 0,c,2 c,3 c,...minutes. // You came to the left side of the pool exactly p minutes after they started swimming.Determine how long you have to wait before one of the swimmers arrives at the left side of the pool. // Input The first line of the input contains a single integer t(1≤t≤1000)—the number of test cases.Next t lines contains test case descriptions,one per line. // Each line contains four integers p,a,b and c(1≤p,a,b,c≤1018),time in minutes after the start,when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back. // Output For each test case,output one integer—how long you have to wait(in minutes)before one of the swimmers arrives at the left side of the pool. import java.util.Scanner; public class threeSwimmers { public static void main(String[] args) { long t, p, a, b, c, d, e, f; Scanner scan = new Scanner(System.in); t = scan.nextLong(); for (int i = 0; i < t; i++) { p = scan.nextLong(); a = scan.nextLong(); b = scan.nextLong(); c = scan.nextLong(); d = a; e = b; f = c; a = (a - p) % a; b = (b - p) % b; c = (c - p) % c; while (a < 0) a += d; while(b < 0) b += e; while (c < 0) c += f; if(a < b) if(a < c) System.out.println(a); else System.out.println(c); else if(b < c) System.out.println(b); else System.out.println(c); } scan.close(); } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
84472cafd405fe21dfc4a754c247cf7b
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.lang.reflect.Array; import java.util.*; public class t{ static Scanner sc = new Scanner(System.in); static void solve() { long a,b,c,p; p = sc.nextLong(); a = sc.nextLong(); b = sc.nextLong(); c = sc.nextLong(); long min = Integer.MAX_VALUE; if(p%a==0) min = 0; else min = ((p/a)+1)*a-p; if(p%b==0) min = 0; else if(min>((p/b)+1)*b-p) min = ((p/b)+1)*b-p; if(p%c==0) min = 0; else if(min>((p/c)+1)*c-p) min = ((p/c)+1)*c-p; System.out.println(min); } public static void main(String args[]) { int t = sc.nextInt(); for(int i=0;i<t;i++) solve(); } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
10944c8e20ed64621d68b1fdc9c99322
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; public class ThreeSwimmers { public static long mini(long a, long b, long c) { long min = 0; if ((a < b) && (a < c)) { min = a; } else if (b < c) { min = b; } else { min = c; } return min; } public static void solve(long p, long a, long b, long c) { long ans; if (p % a == 0 || p % b == 0 || p % c == 0) System.out.println(0); else { ans = mini(a - p % a, b - p % b, c - p % c); System.out.println(ans); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t > 0) { long p = in.nextLong(); long a = in.nextLong(); long b = in.nextLong(); long c = in.nextLong(); solve(p, a, b, c); t--; } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
45c5ce74701d309958055930395477b8
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; public class A { public static void main(String[] args) { Scanner in = new Scanner(System.in); long t = in.nextLong(); while(t-- >0) { long p = in.nextLong(); long a = in.nextLong(); long b = in.nextLong(); long c = in.nextLong(); long t1 = ((p-1)/a + 1)*a - p; long t2 = ((p-1)/b + 1)*b - p; long t3 = ((p-1)/c + 1)*c - p; long result = Long.MAX_VALUE; if (result > t1) result = t1; if (result > t2) result = t2; if (result > t3) result = t3; System.out.println(result); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
07fbf4d50eceebfb191294e643d05942
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; import java.io.*; public class cp{ public static void main(String[] args) { Scanner sc = new Scanner(); int t = sc.nextInt(); while(t-->0) { long p = sc.nextLong(); long a = sc.nextLong(); long b = sc.nextLong(); long c = sc.nextLong(); long fora = p%a; long forb = p%b; long forc = p%c; if(p%a!=0) fora =a-fora; if(p%b!=0) forb = b-forb; if(p%c!=0) forc = c-forc; out.println(Math.min(forc,Math.min(forb,fora))); } out.close(); } static PrintWriter out=new PrintWriter(System.out); static class Scanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String nextLine() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(nextLine()); } Double nextDouble() { return Double.parseDouble(nextLine()); } long nextLong() { return Long.parseLong(nextLine()); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
af8d3e4aab6844e92df5e2190920567c
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public class Main { public static void main (String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-->0){ long p = scn.nextLong(); long a = scn.nextLong(); long b = scn.nextLong(); long c = scn.nextLong(); long t1 = (p%a==0) ? 0 : a - p%a; long t2 = (p%b==0) ? 0 : b - p%b; long t3 = (p%c==0) ? 0 : c - p%c; long[] arr = new long[]{t1,t2,t3}; Arrays.sort(arr); System.out.println(arr[0]); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
6db92a83f5e1debeaeb7707b3d00282d
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Scanner; import java.util.ArrayList; public class ThreeSwimmer{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); ArrayList<Long> list = new ArrayList<Long>(); while(t-- > 0){ String arr [] = sc.nextLine().split("\\s+"); long p = Long.parseLong(arr[0]); long a = Long.parseLong(arr[1]); long b = Long.parseLong(arr[2]); long c = Long.parseLong(arr[3]); if(p%a==0 || p%b == 0 || p%c == 0){ list.add(0L); continue; } long aw = a - (p%a); long bw = b - (p%b); long cw = c - (p%c); list.add((aw<bw?(aw<cw?aw:cw):((bw<cw)?bw:cw))); } for(Long a: list){ System.out.println(a); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
35aea39dd5d4a793ceee9202f02756f8
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; public class Training { // public static int numOfDigits(long n) { // int c = 0; // while (n != 0) { // n /= 10; // c++; // } // return c; // } // public static long round(long p, long a){ // int n=numOfDigits(p); // while (numOfDigits(a)<n)a*=10; // while (numOfDigits(a)>n)a/=10; // while (a<p)a*=2; // while (a>p)a/=2; // return a; // } public static long min(long a, long b, long c){ if(a<=b&&a<=c)return a; else if(b<=a&&b<=c)return b; else return c; } // public static long next(long p,long a){ // long c=a; // while(c<p){ // c*=2; // } // return c-p; // } public static void main(String args[]) { Scanner in = new Scanner(System.in); int t=in.nextInt(); for (int i = 0; i < t; i++) { long p=in.nextLong(); long a=in.nextLong(); long b=in.nextLong(); long c=in.nextLong(); System.out.println(min((p+a-1)/a*a-p,(p+b-1)/b*b-p,(p+c-1)/c*c-p)); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
869cd3ce1b03aa6eb274e7a3618d95d3
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Scanner; import java.util.*; import java.lang.Math; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long q; q=sc.nextLong(); while(q!=0) { long p,a,b,c,min1,min2,min3; p=sc.nextLong(); a=sc.nextLong(); b=sc.nextLong(); c=sc.nextLong(); if(p%a==0 || p%b==0 || p%c==0) System.out.println("0"); else { if(a-p%a <= b-p%b && a-p%a <= c-p%c) System.out.println(a-p%a); else if(b-p%b <= a-p%a && b-p%b <= c-p%c) System.out.println(b-p%b); else System.out.println(c-p%c); } q--; } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
d81068cfa056a3ac995aa12363aa7d58
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.StringTokenizer; public class Main //JAVA FORMAT { final static int n=1000000; static boolean b[]=new boolean[n+2]; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader sc=new FastReader(); public static void main(String[] args) { int t=i(); while(t-->0) { Long p=l(); Long a=l(); Long b=l(); Long c=l(); Long a1=0L,b1=0L,c1=0L; a1=(p%a)==0?0:Math.max(a,p%a)-Math.min(a,p%a); b1=(p%b)==0?0:Math.max(b,p%b)-Math.min(b,p%b); c1=(p%c)==0?0:Math.max(c,p%c)-Math.min(c,p%c); System.out.println(Math.min(a1,Math.min(b1,c1))); } } public static int i() { int s=sc.nextInt(); return s; } public static Long l() { Long k=sc.nextLong(); return k; } public static Integer [] input(int n) { Integer a[]=new Integer[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); return a; } public static Long [] inpuut(int n) { Long a[]=new Long[n]; for(int i=0;i<n;i++) a[i]=sc.nextLong(); return a; } public static int sum(int a[],int n){ int s=0; for(int i=0;i<n;i++) s+=a[i]; return s; } public static Long suml(Long a[],int n){ Long s=0L; for(int i=0;i<n;i++) s+=a[i]; return s; } public static void sieve() { b[0]=true; b[1]=true; for(int i=2;i*i<=n;i++) { if(!b[i]) for(int j=i*i;j<=n;j+=i) { b[i]=true; } } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
494b886d94d39cd3e0f6dd87610799fe
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
//package com.example.helloworld; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int t = scnr.nextInt(); for(int i = 0 ; i < t ; i++){ long a , b , c , p; p = scnr.nextLong(); a = scnr.nextLong(); b = scnr.nextLong(); c = scnr.nextLong(); a = calc(a , p); b = calc(b , p); c = calc(c , p); System.out.println(Math.min(a , Math.min(b , c))); } } public static long calc(long x , long y) { if (x >= y) return x - y; long tmp = y / x; if(tmp * x == y) return 0; tmp = (tmp + 1) * x; return tmp % y; } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
620a812021aec2c144f1677b6a793c09
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; public class cf704 { public static long ceil(long a, long b){ if(a%b==0) return a/b; else return (a/b)+1; } public static void main(String[] args){ long p=0,a=0,b=0,c=0; int t=0; Scanner sc = new Scanner(System.in); t=sc.nextInt(); while(t>0){ p=sc.nextLong(); a=sc.nextLong(); b=sc.nextLong(); c=sc.nextLong(); long ans = Long.MAX_VALUE; ans = Math.min(ans, a*ceil(p,a)-p); ans = Math.min(ans, b*ceil(p,b)-p); ans = Math.min(ans, c*ceil(p,c)-p); System.out.println(ans); t--; } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
c8c0b0acaab8d25e9fb57da8c52c84e9
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.*; import java.util.*; @SuppressWarnings("unchecked") public class ThreeSwimmers implements Runnable { void solve() throws IOException { int t = read.intNext(); while (t-- > 0) { long p = Long.parseLong(read.read()), a = Long.parseLong(read.read()), b = Long.parseLong(read.read()), c = Long.parseLong(read.read()); long res = Long.MAX_VALUE; if (p > a) { if (p % a == 0) { res = min(res, 0); } else { long div = p / a; res = min(res, a * (div + 1) - p); } } else { res = min(res, a - p); } if (p > b) { if (p % b == 0) { res = min(res, 0); } else { long div = p / b; res = min(res, b * (div + 1) - p); } } else { res = min(res, b - p); } if (p > c) { if (p % c == 0) { res = min(res, 0); } else { long div = p / c; res = min(res, c * (div + 1) - p); } } else { res = min(res, c - p); } println(res); } } /************************************************************************************************************************************************/ public static void main(String[] args) throws IOException { new Thread(null, new ThreeSwimmers(), "1").start(); } static PrintWriter out = new PrintWriter(System.out); static Reader read = new Reader(); static StringBuilder sbr = new StringBuilder(); static int mod = (int) 1e9 + 7; static int dmax = Integer.MAX_VALUE; static long lmax = Long.MAX_VALUE; static int dmin = Integer.MIN_VALUE; static long lmin = Long.MIN_VALUE; @Override public void run() { try { solve(); out.close(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } static class Reader { private byte[] buf = new byte[1024]; private int index; private InputStream in; private int total; public Reader() { in = System.in; } public int scan() throws IOException { if (total < 0) throw new InputMismatchException(); if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) return -1; } return buf[index++]; } public int intNext() throws IOException { int integer = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } else throw new InputMismatchException(); } return neg * integer; } public double doubleNext() throws IOException { double doub = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n) && n != '.') { if (n >= '0' && n <= '9') { doub *= 10; doub += n - '0'; n = scan(); } else throw new InputMismatchException(); } if (n == '.') { n = scan(); double temp = 1; while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { temp /= 10; doub += (n - '0') * temp; n = scan(); } else throw new InputMismatchException(); } } return doub * neg; } public String read() throws IOException { StringBuilder sb = new StringBuilder(); int n = scan(); while (isWhiteSpace(n)) n = scan(); while (!isWhiteSpace(n)) { sb.append((char) n); n = scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; return false; } } static void shuffle(int[] aa) { int n = aa.length; Random rand = new Random(); for (int i = 1; i < n; i++) { int j = rand.nextInt(i + 1); int tmp = aa[i]; aa[i] = aa[j]; aa[j] = tmp; } } static void shuffle(int[][] aa) { int n = aa.length; Random rand = new Random(); for (int i = 1; i < n; i++) { int j = rand.nextInt(i + 1); int first = aa[i][0]; int second = aa[i][1]; aa[i][0] = aa[j][0]; aa[i][1] = aa[j][1]; aa[j][0] = first; aa[j][1] = second; } } static final class Comparators { public static final Comparator<int[]> pairIntArr = (x, y) -> x[0] == y[0] ? compare(x[1], y[1]) : compare(x[0], y[0]); private static final int compare(final int x, final int y) { return Integer.compare(x, y); } } static void print(Object object) { out.print(object); } static void println(Object object) { out.println(object); } static int[] iArr(int len) { return new int[len]; } static long[] lArr(int len) { return new long[len]; } static long min(long a, long b) { return Math.min(a, b); } static int min(int a, int b) { return Math.min(a, b); } static long max(Long a, Long b) { return Math.max(a, b); } static int max(int a, int b) { return Math.max(a, b); } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
59b836582099a08d50ee1e268df5982c
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Scanner; public class ThreeSwimmers { public static void main (String[] args) { Scanner scn = new Scanner(System.in); int test = scn.nextInt(); long[] p = new long[test]; long[]a = new long[test]; long[]b = new long[test]; long[]c= new long[test]; long[]result=new long[test]; for (int i = 0; i < test; i++) { p[i]=scn.nextLong(); a[i]=scn.nextLong(); b[i]=scn.nextLong(); c[i]=scn.nextLong(); result[i]=Math.min((a[i]-p[i]%a[i])%a[i],Math.min((b[i]-p[i]%b[i])%b[i],(c[i]-p[i]%c[i])%c[i])); } for(int i = 0 ; i <test;i++) System.out.println(result[i]); } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
f2a9562d6ee5ddfb9713cb265b8baef4
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class Solution1 { public static void main(String args[]) { Scanner ob = new Scanner(System.in); int i, t; long p, a, b, c, min; min = 0; t = ob.nextInt(); for(i = 1; i <= t; i++) { min = 0; p = ob.nextLong(); a = ob.nextLong(); b = ob.nextLong(); c = ob.nextLong(); if(a < p) { a = ((p + a - 1) / a) * a; } if(b < p) { b = ((p + b - 1) / b) * b; } if(c < p) { c = ((p + c - 1) / c) * c; } min = Math.min((a - p), Math.min((b - p), (c - p))); System.out.println(min); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
29eb0ac7fb26afb72c07688c5a700181
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; import java.lang.*; /* ******************** _/\_Siya pati Ram Chandra ki Jai_/\_ ****************** */ public class JaiShreeRam{ static Scanner in=new Scanner(); public static void main(String[] args) throws Exception{ int t=in.readInt(); while(t-->0) { int n=4; long a[]=new long[n]; for(int i=0;i<n;i++) { a[i]=in.readLong(); } for(int i=1;i<n;i++) { if(a[i]<a[0]) { long p=a[0]%a[i]==0?a[0]/a[i]:a[0]/a[i]+1; a[i]*=p; } } long min=Long.MAX_VALUE; for(int i=1;i<n;i++) { if(a[i]-a[0]<min) min=a[i]-a[0]; } System.out.println(min); } } static int[] nextIntArray(int n){ int[] arr= new int[n]; int i=0; while(i<n){ arr[i++]=in.readInt(); } return arr; } static int[] nextIntArray1(int n){ int[] arr= new int[n+1]; int i=1; while(i<=n){ arr[i++]=in.readInt(); } return arr; } static long gcd(long a, long b) { if (b==0) return a; return gcd(b, a%b); } static void ruffleSort(long[] a) { int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n); long temp=a[i]; a[i]=a[oi]; a[oi]=temp; } Arrays.sort(a); } static class Scanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String readString() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } double readDouble() { return Double.parseDouble(readString()); } int readInt() { return Integer.parseInt(readString()); } long readLong() { return Long.parseLong(readString()); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
63ec82bec8f63e753a28958377c6df05
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Main { public static void main(String[] args) throws IOException { FastScanner f = new FastScanner(); int t=1; t=f.nextInt(); PrintWriter out=new PrintWriter(System.out); while(t>0) { t--; long p=f.nextLong(); long a=f.nextLong(); long b=f.nextLong(); long c=f.nextLong(); long ans=Long.MAX_VALUE; ans=Math.min(((p+a-1)/a)*a-p, ans); ans=Math.min(((p+b-1)/b)*b-p, ans); ans=Math.min(((p+c-1)/c)*c-p, ans); // System.out.println(((a+p-1)/p)+ " "+ ((b+p-1)/p)+" "+((a+p-1)/p)); System.out.println(ans); } out.close(); } static void sort(int [] a) { ArrayList<Integer> q = new ArrayList<>(); for (int i: a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } long[] readLongArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
b6f03cbf5f46df7f0fa2c2f895405846
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Main { public static void main(String[] args) throws IOException { FastScanner f = new FastScanner(); int t=1; t=f.nextInt(); PrintWriter out=new PrintWriter(System.out); while(t>0) { t--; long p=f.nextLong(); long a=f.nextLong(); long b=f.nextLong(); long c=f.nextLong(); long ans=Long.MAX_VALUE; if(a==p || b==p || c==p) { System.out.println(0); } else { ans=Math.min(((p+a-1)/a)*a-p, ans); ans=Math.min(((p+b-1)/b)*b-p, ans); ans=Math.min(((p+c-1)/c)*c-p, ans); // System.out.println(((a+p-1)/p)+ " "+ ((b+p-1)/p)+" "+((a+p-1)/p)); System.out.println(ans); } } out.close(); } static void sort(int [] a) { ArrayList<Integer> q = new ArrayList<>(); for (int i: a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } long[] readLongArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
779c6ef1cc9b11f0df9898a10a710141
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; import java.io.*; public class ProblemD{ public static void main(String[] args){ MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t=sc.nextInt(); for(int m = 0; m < t; m++) { long p=sc.nextLong(); long a=sc.nextLong(); long b=sc.nextLong(); long c=sc.nextLong(); if (p % a == 0 || p % b == 0 || p % c == 0) System.out.println(0); else { long res = Math.min((p / a + 1) * a - p, (p / b + 1) * b - p); res = Math.min((p / c + 1) * c - p, res); System.out.println(res); } } out.close(); } } 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
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
d10c50bb8da383d8d8e3abb6ceccadd2
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
//package clipse; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; import java.io.*; import java.math.*; public class A { static StringBuilder sb; static dsu dsu; static void solve() { long p=l(),a=l(),b=l(),c=l(); long ans=-1; if(p%a==0||p%b==0||p%c==0) ans=0; else { long ans1=Long.MAX_VALUE; if(a>p) { ans1=Math.min(ans1,a-p); } else { ans1=Math.min(ans1,a-p%a); } if(b>p) { ans1=Math.min(ans1,b-p); } else { ans1=Math.min(ans1,b-p%b); } if(c>p) { ans1=Math.min(ans1,c-p); } else { ans1=Math.min(ans1,c-p%c); } ans=ans1; } System.out.println(ans); } public static void main(String[] args) { sb=new StringBuilder(); int test=i(); while(test-->0) { solve(); } System.out.println(sb); } //*********************************Disjoint set union*************************// static class dsu { int parent[]; dsu(int n) { parent=new int[n]; for(int i=0;i<n;i++) parent[i]=-1; } int find(int a) { if(parent[a]<0) return a; else { int x=find(parent[a]); parent[a]=x; return x; } } void merge(int a,int b) { a=find(a); b=find(b); if(a==b) return; parent[b]=a; } } //*******************************************PRIME FACTORIZE *****************************************************************************************************// static TreeMap<Integer,Integer> prime(long n) { TreeMap<Integer,Integer>h=new TreeMap<>(); long num=n; for(int i=2;i<=Math.sqrt(num);i++) { if(n%i==0) { int nt=0; while(n%i==0) { n=n/i; nt++; } h.put(i, nt); } } if(n!=1) h.put((int)n, 1); return h; } //*************CLASS PAIR *********************************************************************************************************************************************** static class pair implements Comparable<pair> { int x; int y; pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(pair o) { return x-o.x==0?y-o.y:x-o.x; } } //*************CLASS PAIR ***************************************************************************************************************************************************** static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static InputReader in = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); static int modulus = (int) 1e7; public static int[] sort(int[] a) { int n = a.length; ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a[i] = l.get(i); return a; } public static long pow(long x, long y) { long res = 1; while (y > 0) { if (y % 2 != 0) { res = (res * x) % modulus; y--; } x = (x * x) % modulus; y = y / 2; } return res; } //GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public static long gcd(long x, long y) { if (x == 0) return y; else return gcd(y % x, x); } //****************LOWEST COMMON MULTIPLE ************************************************************************************************************************************* public static long lcm(long x, long y) { return (x * (y / gcd(x, y))); } //INPUT PATTERN****************************************************************************************************************************************************************** public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } public static int[] readArray(int n) { int A[] = new int[n]; for (int i = 0; i < n; i++) { A[i] = i(); } return A; } public static long[] readArray(long n) { long A[] = new long[(int) n]; for (int i = 0; i < n; i++) { A[i] = l(); } return A; } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
bbd2364ce81f0a50313c63cd27bace34
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 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); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- > 0) { long p = in.nextLong(); long a = in.nextLong(); long b = in.nextLong(); long c = in.nextLong(); out.println(Math.min((a - p % a) % a, Math.min((b - p % b) % b, (c - p % c) % c))); } } } static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
e2c5f45f99f096c08d21f75101d87525
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
/*package whatever //do not write package name here */ import java.io.*; import java.util.Scanner; public class GFG { public static void main (String[] args) { //System.out.println("GfG!"); Scanner input = new Scanner(System.in); int t = input.nextInt(); while(t>0){ long p = input.nextLong(); long a = input.nextLong(); long b = input.nextLong(); long c = input.nextLong(); long x = (p/a + ((p%a==0)?0:1))*a - p; long y = (p/b + ((p%b==0)?0:1))*b - p; long z = (p/c + ((p%c==0)?0:1))*c - p; System.out.println(Math.min(Math.min(x,y),z)); t--; } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
6026c612c41a0337d70da800926b52e1
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
//@author->.....future_me......// //..............Learning.........// /*Compete against yourself*/ import java.util.*; import java.io.*; import java.lang.*; import java.math.BigInteger; public class A { static FastReader sc = new FastReader(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws Exception { // try { // int t = sc.nextInt(); while (t-- > 0) A.go(); out.flush(); // } catch (Exception e) { // return; // } } static int count = 0; static HashSet<Integer> set = new HashSet<Integer>(); //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<<<<<<<<<<< // static void go() { long p=sc.nextLong(); long a=sc.nextLong(),b=sc.nextLong(),c=sc.nextLong(); long aa=p%a, bb=p%b,cc=p%c; if(aa==0||bb==0||cc==0) { out.println(0); }else { out.println(Math.min(a-aa,Math.min(b-bb, c-cc))); } } //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Ends <<<<<<<<<<<<<<<<<<<<<<<<<<<<< // static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static int prime[] = new int[1000005]; static int N = 1000005; static void sieve() { prime[0] = 1; prime[1] = 1; for (int i = 2; i * i <= N; i++) { if (prime[i] == 0) { for (int j = i * i; j < N; j += i) { prime[j] = 1; } } } } /* * 5 16 9 8 7 7 1 */ static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } private static void sort(int[] a) { ArrayList<Integer> aa = new ArrayList<>(); for (Integer i : a) { aa.add(i); } Collections.sort(aa); for (int i = 0; i < a.length; i++) a[i] = aa.get(i); } static void sort(long[] a) { ArrayList<Long> aa = new ArrayList<>(); for (Long i : a) { aa.add(i); } Collections.sort(aa); for (int i = 0; i < a.length; i++) a[i] = aa.get(i); } static int mod = (int) 1e9 + 7; static long pow(long x, long y) { long res = 1l; while (y != 0) { if (y % 2 == 1) { res = (x * res)%10l; } y /= 2; x = (x * x)%10l; } return res; } static long pow1(long x, long y) { long res = 1l; while (y != 0) { if (y % 2 == 1) { res = (x * res)%1000l; } y /= 2; x = (x * x)%1000l; } return res; } //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< // static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
00f95ccf645490bc034353eca3ef5a0d
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
//@author->.....future_me......// //..............Learning.........// /*Compete against yourself*/ import java.util.*; import java.io.*; import java.lang.*; import java.math.BigInteger; public class C { // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<<<<<<<<<<< // static FastReader sc = new FastReader(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws Exception { try { int t = sc.nextInt(); while (t-- > 0) C.go(); out.flush(); } catch (Exception e) { return; } } static void go() { long p=sc.nextLong(); long a=sc.nextLong(),b=sc.nextLong(),c=sc.nextLong(); long aa=(p+a-1)/a,bb=(p+b-1)/b,cc=(p+c-1)/c; aa=a*aa;bb=bb*b;cc=cc*c; long aaa=Math.abs(p-aa),bbb=Math.abs(p-bb),ccc=Math.abs(p-cc); out.println(Math.min(aaa, Math.min(bbb, ccc))); } // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Ends <<<<<<<<<<<<<<<<<<<<<<<<<<<<< // static int mod = (int) 1e9 + 7; // Returns nCr % p using Fermat's // little theorem. static long fac[]; static long ncr(int n, int r, int p) { if (n < r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } private static long modInverse(long l, int p) { return pow(l,p-2); } static void sort(long[] a) { ArrayList<Long> aa = new ArrayList<>(); for (long i : a) { aa.add(i); } Collections.sort(aa); for (int i = 0; i < a.length; i++) a[i] = aa.get(i); } static long pow(long x, long y) { long res = 1l; while (y != 0) { if (y % 2 == 1) { res = x * res % mod; } y /= 2; x = (x * x) % mod; } return res; } //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< // 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; } } } //use this for double quotes inside string // " \"asdf\""
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
e7c74d423c5eecf4d4e088c14f8ece4e
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; import java.math.*; public class Three_Swimmers { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int t=in.nextInt(); for(int i=1;i<=t;i++) { BigInteger p=new BigInteger(in.next()); BigInteger a=new BigInteger(in.next()); BigInteger b=new BigInteger(in.next()); BigInteger c=new BigInteger(in.next()); BigInteger x=new BigInteger("0"); BigInteger y=new BigInteger("0"); BigInteger z=new BigInteger("0"); BigInteger min=new BigInteger("0"); if(p.mod(a).compareTo(BigInteger.valueOf(0))!=0) x=a.subtract(p.mod(a)); if(p.mod(b).compareTo(BigInteger.valueOf(0))!=0) y=b.subtract(p.mod(b)); if(p.mod(c).compareTo(BigInteger.valueOf(0))!=0) z=c.subtract(p.mod(c)); else min=BigInteger.valueOf(0); x=x.abs(); y=y.abs(); z=z.abs(); min=(x.min(y)).min(z); System.out.println(min); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
3dada5bd39f7185bf7c975a8831f081b
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; import static java.lang.System.in; public class Main { public static void main(String[] args){ FastReader fr=new FastReader(); int tc=fr.nextInt(); while(tc-->0){ long p=fr.nextLong(); long t[]=new long[3]; t[0]=fr.nextLong();t[1]=fr.nextLong();t[2]=fr.nextLong(); long f[]=new long[3]; for(int i=0;i<3;i++){ long d=p/t[i]; if(t[i]*d<p){ d++; } f[i]=t[i]*d; } Arrays.sort(f); System.out.println(Math.abs(f[0]-p)); } } static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(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()); } byte nextByte(){ return Byte.parseByte(next()); } Float nextFloat(){ return Float.parseFloat(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } short nextShort(){ return Short.parseShort(next()); } Boolean nextbol(){ return Boolean.parseBoolean(next()); } String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e){ e.printStackTrace(); } return str; } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
bbb4d175f65e9f7fef4256ac3dd39820
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.*; import java.util.*; public class reg { static long floor(long p, long i) { long s = (p/i); long s2 = i*s; if(s2-p>=0) return s2-p; else { s = s+1; s2 = i*s; return s2-p; } } static long result(long a1, long a2, long a3) { if (a1 <= a2 && a1 <= a3) return a1; else if (a2 <= a1 && a2 <= a3) return a2; else return a3; } public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; int t = Integer.parseInt(br.readLine()); while (t-- > 0) { st = new StringTokenizer(br.readLine()); Long p = Long.parseLong(st.nextToken()); Long a = Long.parseLong(st.nextToken()); Long b = Long.parseLong(st.nextToken()); Long c = Long.parseLong(st.nextToken()); Long d1 = floor(p, a); Long d2 = floor(p, b); Long d3 = floor(p, c); System.out.println(result(d1, d2, d3)); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
31abaaa0a8fd978b817601ea8678ab6b
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Scanner; public class ThreeSwimmers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); long[] arr1 = new long[m]; for (int i = 0; i < m; i++) { long p = sc.nextLong(); long a = sc.nextLong(); long b = sc.nextLong(); long c = sc.nextLong(); long aa = p%a; long bb = p%b; long cc = p%c; if (aa == 0 || bb == 0 || cc == 0) { arr1[i] = 0; } else{ arr1[i] = Math.min(Math.min(a-aa,b-bb),c-cc); } } for(int i=0;i<m;i++){ System.out.println(arr1[i]); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
8f2c01874c4d5fd67e95f068779bf0dc
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.*; import java.util.ArrayDeque; import java.util.InputMismatchException; import java.util.Queue; import static java.lang.Double.parseDouble; import static java.lang.Long.numberOfTrailingZeros; import static java.lang.Math.pow; import static java.lang.System.*; import static java.util.Arrays.copyOf; import static java.util.Arrays.deepToString; public class A { public int lenbuf = 0, ptrbuf = 0; InputStream is; FastWriter out; String INPUT = ""; private byte[] inbuf = new byte[1024]; private boolean oj = getProperty("ONLINE_JUDGE") != null; public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for (int v : a) { c0[(v & 0xff) + 1]++; c1[(v >>> 8 & 0xff) + 1]++; c2[(v >>> 16 & 0xff) + 1]++; c3[(v >>> 24 ^ 0x80) + 1]++; } for (int i = 0; i < 0xff; i++) { c0[i + 1] += c0[i]; c1[i + 1] += c1[i]; c2[i + 1] += c2[i]; c3[i + 1] += c3[i]; } int[] t = new int[n]; for (int v : a) t[c0[v & 0xff]++] = v; for (int v : t) a[c1[v >>> 8 & 0xff]++] = v; for (int v : a) t[c2[v >>> 16 & 0xff]++] = v; for (int v : t) a[c3[v >>> 24 ^ 0x80]++] = v; return a; } public static void main(String[] args) throws Exception { new A().run(); } void solve() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringBuffer res=new StringBuffer(); int t = Integer.parseInt(br.readLine()); while (t-->0) { long result = 0; String s[] = br.readLine().trim().split("\\s+"); long p = Long.parseLong(s[0]), a = Long.parseLong(s[1]); long b = Long.parseLong(s[2]), c = Long.parseLong(s[3]); if (p % a == 0 || p % b == 0 || p % c == 0) result = 0; else { long min = diff(a, p); long tempc = diff(c, p); long tempb = diff(b, p); if (min > tempb) min = tempb; if (min > tempc) min = tempc; result = min; } out.println(result); } } public static long diff(long a,long p) { long divisor = p/a; divisor++; long reach = divisor*a; long diff = reach-p; return diff; } void run() throws Exception { is = oj ? in : new ByteArrayInputStream(INPUT.getBytes()); out = new FastWriter(System.out); long s = currentTimeMillis(); solve(); out.flush(); tr(currentTimeMillis() - s + "ms"); } 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 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 : copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } 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[][] nmi(int n, int m) { int[][] map = new int[n][]; for (int i = 0; i < n; i++) map[i] = na(m); return map; } private int ni() { return (int) nl(); } 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(); } } public void trnz(int... o) { for (int i = 0; i < o.length; i++) if (o[i] != 0) System.out.print(i + ":" + o[i] + " "); System.out.println(); } // print ids which are 1 public void trt(long... o) { Queue<Integer> stands = new ArrayDeque<>(); for (int i = 0; i < o.length; i++) { for (long x = o[i]; x != 0; x &= x - 1) stands.add(i << 6 | numberOfTrailingZeros(x)); } System.out.println(stands); } public void tf(boolean... r) { for (boolean x : r) System.out.print(x ? '#' : '.'); System.out.println(); } public void tf(boolean[]... b) { for (boolean[] r : b) { for (boolean x : r) System.out.print(x ? '#' : '.'); System.out.println(); } System.out.println(); } public void tf(long[]... b) { if (INPUT.length() != 0) { for (long[] r : b) { for (long x : r) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } System.out.println(); } } public void tf(long... b) { if (INPUT.length() != 0) { for (long x : b) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } } private void tr(Object... o) { if (!oj) System.out.println(deepToString(o)); } public static class FastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter() { out = null; } public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastWriter write(char c) { return write((byte) c); } public FastWriter write(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } public FastWriter write(int x) { if (x == Integer.MIN_VALUE) { return write((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if (x < 0) { write('-'); x = -x; } x += pow(10, -precision) / 2; // if(x < 0){ x = 0; } write((long) x).write("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; write((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastWriter writeln(char c) { return write(c).writeln(); } public FastWriter writeln(int x) { return write(x).writeln(); } public FastWriter writeln(long x) { return write(x).writeln(); } public FastWriter writeln(double x, int precision) { return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for (int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for (long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte) '\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for (char[] line : map) write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c) { return writeln(c); } public FastWriter println(int x) { return writeln(x); } public FastWriter println(long x) { return writeln(x); } public FastWriter println(double x, int precision) { return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
5ab15053c179c78fec2494a26e3793cd
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class chef{ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-- >0) { long p=sc.nextLong(); long a=sc.nextLong(); long b=sc.nextLong(); long c=sc.nextLong(); deploytemp obj=new deploytemp(); long ans=obj.solve(p,a,b,c); System.out.println(ans); } } } class deploytemp { public long solve(long p,long a,long b,long c) { long ans=Long.MAX_VALUE; int flag=0; long ia=a; long ib=b; long ic=c; long rema=(p%a)==0?0:Math.abs((p%a)-a); long remb=(p%b)==0?0:Math.abs((p%b)-b); long remc=(p%c)==0?0:Math.abs((p%c)-c); // if((((p/a)&1)==0 && p/a!=0) && rema!=0) // rema+=ia; // if((((p/b)&1)==0 && p/b!=0) && remb!=0) // remb+=ib; // if((((p/c)&1)==0 && p/b!=0) && remc!=0) // remc+=ic; return Math.min(rema,Math.min(remb,remc)); } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
ec1138ff1bc80d5cf9b772ef63ec69a5
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Scanner; import java.math.BigDecimal; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); long t=sc.nextLong(); while(t--!=0) { long p=sc.nextLong(); long a=sc.nextLong(); long b=sc.nextLong(); long c=sc.nextLong(); if(p%a==0||p%b==0||p%c==0) { System.out.println(0); continue; } long A=(p/a+1)*a-p; long B=(p/b+1)*b-p; long C=(p/c+1)*c-p; long min=Math.min(A,B); System.out.println(Math.min(min, C)); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
78976329d5e751650a1f3a457c928862
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class _704_A { static int t; static long p, a, b, c; static long ans; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); t = Integer.parseInt(st.nextToken()); for (int tc = 0; tc < t; tc++) { st = new StringTokenizer(br.readLine()); p = Long.parseLong(st.nextToken()); a = Long.parseLong(st.nextToken()); b = Long.parseLong(st.nextToken()); c = Long.parseLong(st.nextToken()); ans = Long.MAX_VALUE; long modA = p % a; long modB = p % b; long modC = p % c; if (modA == 0 || modB == 0 || modC == 0) { System.out.println("0"); continue; } ans = Math.min(ans, a - modA); ans = Math.min(ans, b - modB); ans = Math.min(ans, c - modC); System.out.println(ans); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
ce25e63d53359e0923071c91d112df09
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class f { public static void main(String[]args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); while(n-->0) { long a=sc.nextLong(); long b=sc.nextLong(); long c=sc.nextLong(); long d=sc.nextLong(); long b1=(b-(a%b))%b; long c1=(c-(a%c))%c; long d1=(d-(a%d))%d; long min=b1<c1?b1:c1; long min1=min<d1?min:d1; System.out.println(min1); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
d29615bc37b545b56d54b88a01ca2acf
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Scanner; public class A { public static Scanner scan = new Scanner(System.in); public static void main(String[] args) { int t = scan.nextInt(); while (t-- > 0) { solve(); } } public static void solve() { long p = scan.nextLong(); long a = scan.nextLong(); long b = scan.nextLong(); long c = scan.nextLong(); long x = a - (p%a); long y = b - (p%b); long z = c - (p%c); if(p%a == 0) x = 0; if(p%b == 0) y = 0; if(p%c == 0) z = 0; System.out.println(Math.min(Math.min(x,y), z)); } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
717e8684603641e14e3db32436ff337c
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.Scanner; public class threeSwimmers { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int t = in.nextInt(); while(t-- > 0) { long p = in.nextLong(); long ar[] = new long[3]; ar[0] = in.nextLong(); ar[1] = in.nextLong(); ar[2] = in.nextLong(); long ans = 1000000000000000000L; for(int i = 0; i < 3; i++) if(p % ar[i] == 0) ans = 0; else ans = Math.min( ar[i] - p % ar[i], ans); System.out.println(ans); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
9b84c13c443f0faed5ef502b449f5109
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.* ; public class JavaApplication4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++){ long p=sc.nextLong(); long a=sc.nextLong(); long b=sc.nextLong(); long c=sc.nextLong(); System.out.println(Math.min((a - p % a) % a, Math.min((b - p % b) % b, (c - p % c) % c))); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
b4e343d0f8b9a7c109ad6947ba43ad27
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; public class Main { public static void main(String args[]) { Scanner s=new Scanner(System.in); int num=s.nextInt(); for(int i=0; i<num; i++){ long[] numbers1 = new long[4]; for(int i1=0; i1<=3; i1++){ numbers1[i1]=s.nextLong(); } for(int j = 1; j < numbers1.length;j++) { if(numbers1[j]!=numbers1[0] && numbers1[0]%numbers1[j]!=0){ numbers1[j]=numbers1[j]-(numbers1[0]%numbers1[j]); } else{ numbers1[j]=0; } } long out=numbers1[1]; for(int l = 1; l < numbers1.length;l++) { if(numbers1[l]<out){ out=numbers1[l]; } } System.out.println(out); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
2313fee57de53a8f34224f864c10bd81
train_109.jsonl
1614071100
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
512 megabytes
import java.util.*; import java.lang.Math; public class Solution { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++) { long p = sc.nextLong(); long a = sc.nextLong(); long b = sc.nextLong(); long c = sc.nextLong(); long x = p%a; long y = p%b; long z = p%c; long s = Math.min((b-y)%b,(c-z)%c); long ans = Math.min(s,(a-x)%a); System.out.println(ans); } } }
Java
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
1 second
["1\n4\n0\n8"]
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
Java 11
standard input
[ "math" ]
293f9b996eee9f76d4bfdeda85685baa
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
800
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
standard output
PASSED
57ae0769cffdc8bca2807b356932fa9f
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class MaximumWidth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); String s = sc.next(); String t = sc.next(); if (n == m) System.out.println(1); else System.out.println(solve(n, m, s.toCharArray(), t.toCharArray())); } private static int solve(int n, int m, char[] s, char[] t) { // TODO Auto-generated method stub int[] S = new int[m]; for (int i = 0, j = 0; i < n; i++) { if (s[i] == t[j]) S[j++] = i; if (j >= m) break; } int[] E = new int[m]; for (int i = n - 1, j = m - 1; i >= 0; i--) { if (s[i] == t[j]) E[j--] = i; if (j == 0) break; } int res = 0; for (int i = 1; i < t.length; i++) res = Math.max(res, E[i] - S[i - 1]); return res; } } //11 3 //abcamnbcjkc //abc //[0, 1, 2] //[0, 6, 10] //9
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
badee2181b6612df2d06b862d722038b
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class MaximumWidth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); String s = sc.next(); String t = sc.next(); if (n == m) System.out.println(1); else System.out.println(solve(n, m, s.toCharArray(), t.toCharArray())); } private static int solve(int n, int m, char[] s, char[] t) { // TODO Auto-generated method stub int[] S = new int[m]; for (int i = 0, j = 0; i < n; i++) { if (s[i] == t[j]) { S[j] = i; j++; } if (j >= m) break; } int[] E = new int[m]; for (int i = n - 1, j = m - 1; i >= 0; i--) { if (s[i] == t[j]) { E[j] = i; j--; } if (j == 0) break; } int res = 0; for (int i = 1; i < t.length; i++) res = Math.max(res, E[i] - S[i - 1]); return res; } } //11 3 //abcamnbcjkc //abc //[0, 1, 2] //[0, 6, 10] //9
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
795df52e0a19f9f58823414f9d44924c
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class MaximumWidth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); String s = sc.next(); String t = sc.next(); if (n == m) System.out.println(1); else System.out.println(solve(n, m, s.toCharArray(), t.toCharArray())); } private static int solve(int n, int m, char[] s, char[] t) { // TODO Auto-generated method stub int[] S = new int[n]; for (int i = 0, j = 0; i < n; i++) { if (s[i] == t[j]) { S[j] = i; j++; } if (j >= m) break; } int[] E = new int[n]; for (int i = n - 1, j = m - 1; i >= 0; i--) { if (s[i] == t[j]) { E[j] = i; j--; } if (j == 0) break; } int res = 0; for (int i = 1; i < t.length; i++) res = Math.max(res, E[i] - S[i - 1]); return res; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
53bb67bea7b5cfef66cee9639f20520c
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class Solution{ public static void main(String arg[]){ Scanner scan = new Scanner(System.in); int i, j, k = 0, n = scan.nextInt(), m = scan.nextInt(); String a = scan.next(), b = scan.next(); HashMap <Character, ArrayList<Integer>> map = new HashMap<Character, ArrayList<Integer>>(); ArrayList <Integer> arr; for (i = 0; i < n; ++i) { arr = map.getOrDefault(a.charAt(i), new ArrayList<Integer>()); arr.add(i + 1); map.put(a.charAt(i), arr); } int [] arr1 = new int[m]; int [] arr2 = new int[m]; arr = map.get(b.charAt(0)); arr1[0] = arr.get(0); for (i = 1; i < m; ++i) { arr = map.get(b.charAt(i)); j = Collections.binarySearch(arr, arr1[i - 1] + 1); if (j < 0) { j++; j *= -1; } arr1[i] = arr.get(j); } arr = map.get(b.charAt(m - 1)); arr2[m - 1] = arr.get(arr.size() - 1); for (i = m - 2; i >= 0; --i) { arr = map.get(b.charAt(i)); j = Collections.binarySearch(arr, arr2[i + 1] - 1); if (j < 0) { j++; j *= -1; arr2[i] = arr.get(j - 1); } else { arr2[i] = arr.get(j); } } for (i = 0; i < m - 1; ++i) { k = Math.max(k, arr2[i + 1] - arr1[i]); } System.out.println(k); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
58666a5bc98b594d7b44c5aca3bfc42b
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; public class C680 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextArray(int n) { int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=nextInt(); return arr; } } private static int V ; private static List<List<Integer>> adj; public C680(int V) { this.V = V; adj = new ArrayList<>(V); for (int i = 0; i < V; i++) adj.add(new LinkedList<>()); } static void sum(int nums[],int i,ArrayList<Integer> a,ArrayList<Integer> b,HashSet<String> hSet) { if(i==nums.length) { Collections.sort(a); Collections.sort(b,Collections.reverseOrder()); String string=a.toString()+b.toString(); if(hSet.contains(string)) return; else { } } } static long gcd(long a, long b) { //System.out.println(a+","+b); // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } static long findGCD(long arr[], int n) { long result = 0; for (long element: arr){ result = gcd(result, element); if(result == 1) { return 1; } } return result; } private static boolean isCyclicUtil(int i, boolean[] visited, boolean[] recStack) { // Mark the current node as visited and // part of recursion stack if (recStack[i]) return true; if (visited[i]) return false; visited[i] = true; recStack[i] = true; List<Integer> children = adj.get(i); for (Integer c: children) if (isCyclicUtil(c, visited, recStack)) return true; recStack[i] = false; return false; } // Returns true if the graph contains a // cycle, else false. // This function is a variation of DFS() in // https://www.geeksforgeeks.org/archives/18212 private static boolean isCyclic() { // Mark all the vertices as not visited and // not part of recursion stack boolean[] visited = new boolean[V]; boolean[] recStack = new boolean[V]; // Call the recursive helper function to // detect cycle in different DFS trees for (int i = 0; i < V; i++) if (isCyclicUtil(i, visited, recStack)) return true; return false; } static boolean same(int []arr) { for(int i=1;i<arr.length;i++) if(arr[i]!=arr[0]&&arr[0]!=Integer.MAX_VALUE&&arr[i]!=Integer.MAX_VALUE) return false; return true; } static int inv(int[] arr) { int tot=0; for(int i=0;i<arr.length;i++) { for(int j=i+1;j<arr.length;j++) if(arr[i]>arr[j]) tot++; } return tot; } static boolean ab(int[] a,int[] b,int i,int k,int inv,int[] p) { if(i==a.length) { if(inv(b)<=inv) return true; else return false; } for(int j=k;j>=1;j--) { int in=a.length-a.length%j-1; if(p[in]==0) { b[i]=j; p[in]=j; if(ab(a, b, i+1, k, inv, p)) return true; } else { b[i]=p[in]; } } return false; } static int prime(int n) { for(int i=n;;i++) { boolean flag=true; for(int j=2;j<i;j++) if(i%j==0) { flag=false; break; } if(flag) return i; } } static int cal(int a,int b,int x,int y,HashSet<String> hs) { int hcf=(int)gcd(a, b); a=a/hcf; b=b/hcf; String s=a+","+b; if(hs.contains(s)) return 0; if(a==x&&b==y) return 0; hs.add(s); int ans=0; if(a/b==a%b) ans=1; if(a<x) ans+=cal(a+1, b, x, y, hs); if(b<y) ans+=cal(a, b+1, x, y, hs); return ans; } static int helper(String s,String t,int i,int j,int prev,int max,HashMap<String , Integer> hm) { //System.out.println(i+","+j+","+prev); if(j-t.length()==0) { //System.out.println(pq); return max; } else if (i-s.length()==0) { return -1; } else if(s.substring(i).equals(t.substring(j))) return Math.max(max,i-prev); String key=i+","+j+","+prev; if(hm.containsKey(key)) return hm.get(key); int curr=helper(s, t, i+1, j, prev, max,hm); if(s.charAt(i)==t.charAt(j)) { if(prev!=-1) max=Math.max(max, i-prev); hm.put(key,Math.max(curr,helper(s, t, i+1, j+1,i,max,hm))); return hm.get(key); } hm.put(key, curr); return hm.get(key); } public static void main(String[] args)throws IOException { BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in)); FastReader fs=new FastReader(); //int T=fs.nextInt(); //while(T-->0) { StringBuilder sb=new StringBuilder(); int n=fs.nextInt(),k=fs.nextInt(); String s=fs.nextLine(); String t=fs.nextLine(); if(s.equals(t)) { System.out.println(1); } else { int[] arr1,arr2; arr1=new int[t.length()+1]; arr2=new int[t.length()+1]; //Arrays.fill(arr2, -1); for(int i=0,j=0;i<s.length()&&j<t.length();i++) { if(s.charAt(i)==t.charAt(j)) { arr1[j]=i; j++; } } for(int i=s.length()-1,j=t.length()-1;i>=0&&j>=0;i--) { if(s.charAt(i)==t.charAt(j)) { arr2[j]=i; j--; } } int min=0; for(int i=0;i<t.length()-1;i++) min=Math.max(min,arr2[i+1]-arr1[i]); System.out.println(min); } } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
d030d3370cafe968f199e662343a58ba
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; public class Ques3 { public static void main(String args[]) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int m=in.nextInt(); String s=in.next(); String t=in.next(); int a[]=new int[m]; int b[]=new int[m]; int i; char ch,ch1; int k=0; for(i=0;i<n;i++) { ch=s.charAt(i); ch1=t.charAt(k); if(ch==ch1) a[k++]=i; // else // k--; if(k==m) break; } k=m-1; for(i=n-1;i>=0;i--) { ch=s.charAt(i); ch1=t.charAt(k); if(ch==ch1) b[k--]=i; // else // k++; if(k==-1) break; } int diff=0,max=0; for(i=1;i<m;i++) { diff=b[i]-a[i-1]; if(diff>max) max=diff; } System.out.println(max); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
fe4ca8643ed0a06b11c5a7a1568f515e
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class CF1492_D2_C { public static void main(String[] args) { FastScanner scanner = new FastScanner(); int n = scanner.nextInt(); int m = scanner.nextInt(); String s = scanner.next(); String t = scanner.next(); int[] mins = new int[m]; int[] maxs = new int[m]; for (int i = 0, j = 0; j < m; i++) { char c1 = s.charAt(i); char c2 = t.charAt(j); if (c1 == c2) { mins[j] = i; j++; } } for (int i = n - 1, j = m - 1; j >= 0; i--) { char c1 = s.charAt(i); char c2 = t.charAt(j); if (c1 == c2) { maxs[j] = i; j--; } } int max = Integer.MIN_VALUE; for (int i = 0 ; i < m - 1; i++) { int diff = maxs[i + 1] - mins[i]; max = Math.max(max, diff); } System.out.println(max); // HashMap<Character, ArrayList<Integer>> map = new HashMap<>(); // for (int i = 0; i < n; i++) { // ArrayList<Integer> stack; // if (!map.containsKey(s.charAt(i))) { // stack = new ArrayList<>(); // } else { // stack = map.get(s.charAt(i)); // } // stack.add(i); // map.put(s.charAt(i), stack); // } // int res = Integer.MIN_VALUE; // HashMap<Character, Integer> freq = new HashMap<>(); // for (int i = 0; i < m; i++) { // freq.put(t.charAt(i), freq.getOrDefault(t.charAt(i), 0) + 1); // } // HashMap<Character, Integer> found = new HashMap<>(); // // for (int i = 0; i < m - 1; i++) { // int a = found.getOrDefault(t.charAt(i), 0); // int min = map.get(t.charAt(i)).get(a); // found.put(t.charAt(i), found.getOrDefault(t.charAt(i), 0) + 1); // int b = freq.get(t.charAt(i + 1)) - found.getOrDefault(t.charAt(i + 1), 0); // int max = map.get(t.charAt(i + 1)).get(map.get(t.charAt(i + 1)).size() - b); // res = Math.max(res, max - min); // } // System.out.println(res); // if( n == 200000){ // System.out.println(150000); // return; // } // for (int i = 0; i < n; i++) { // Stack<Integer> stack; // if (!map.containsKey(s.charAt(i))) { // stack = new Stack<>(); // } else { // stack = map.get(s.charAt(i)); // } // stack.push(i); // map.put(s.charAt(i), stack); // // } // int max = Integer.MIN_VALUE; // for (int i = m - 1; i > 0; i--) { // int diff = map.get(t.charAt(i)).pop() - map.get(t.charAt(i - 1)).pop(); // // max = Math.max(max, diff); // } // System.out.println(max); } // static class Item { // char c; // int min; // int max; // // public Item(char c, int n) { // this.c = c; // this.min = n; // this.max = n; // } // } 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 nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
81a690d229a0948ab7dd68be334e0521
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; import java.math.*; /** * * @Har_Har_Mahadev */ public class A { private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353; private static int N = 0; public static void process() throws IOException { int n = sc.nextInt(),m = sc.nextInt(); String str = sc.next(),t = sc.next(); int left[] = new int[m]; int right[] = new int[m]; int i = 0,j = 0; while(j<m) { if(str.charAt(i) == t.charAt(j)) { left[j] = i; i++;j++;continue; } i++; } i = n-1;j = m-1; while(j>=0) { if(str.charAt(i) == t.charAt(j)) { right[j] = i; i--;j--;continue; } i--; } int max = 0; for(i=0; i<m-1; i++) { max = max(max,right[i+1] - left[i]); } System.out.println(max); } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= static FastScanner sc; static PrintWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new PrintWriter(System.out); } else { sc = new FastScanner(100); out = new PrintWriter("output.txt"); } int t = 1; // t = sc.nextInt(); while (t-- > 0) { process(); } out.flush(); out.close(); } static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Integer.compare(this.x, o.x); } // @Override // public boolean equals(Object o) { // if (this == o) return true; // if (!(o instanceof Pair)) return false; // Pair key = (Pair) o; // return x == key.x && y == key.y; // } // // @Override // public int hashCode() { // int result = x; // result = 31 * result + y; // return result; // } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static void println(Object o) { out.println(o); } static void println() { out.println(); } static void print(Object o) { out.print(o); } static void pflush(Object o) { out.println(o); out.flush(); } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int abs(int x) { return Math.abs(x); } static long abs(long x) { return Math.abs(x); } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static int lower_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(System.in)); } FastScanner(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; } int[] readArray(int n) throws IOException { int[] A = new int[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextInt(); } return A; } long[] readArrayLong(int n) throws IOException { long[] A = new long[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextLong(); } return A; } } static void ruffleSort(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; } Arrays.sort(a); } static void ruffleSort(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; } Arrays.sort(a); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
69d9f42c17e83a9e7d659b6809a60a83
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*;import java.io.*;import java.math.*; public class MaxWidth { static final Random random=new Random(); static void ruffleSort(int[] a) { int n = a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } ArrayList<Integer> lst = new ArrayList<>(); for(int i : a) lst.add(i); Collections.sort(lst); for(int i = 0; i < n; i++) a[i] = lst.get(i); } static void ruffleSort(long[] a) { int n = a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } ArrayList<Long> lst = new ArrayList<>(); for(long i : a) lst.add(i); Collections.sort(lst); for(int i = 0; i < n; i++) a[i] = lst.get(i); } public static void process()throws IOException { int n = ni(), m = ni(); char[] a = nln().toCharArray(); char[] b = nln().toCharArray(); int p = 0, q = n-1; int[] first = new int[m]; int[] last = new int[m]; for(int i = 0; i < m; i++){ while(p < n && a[p] != b[i]) p++; first[i] = p; p++; } for(int i = m-1; i >= 0; i--){ while(q >= 0 && a[q] != b[i]) q--; last[i] = q; q--; } int ans = 0; for(int i = 0; i < m-1; i++) ans = Math.max(ans, last[i+1]-first[i]); pn(ans); } static class Node{ int l, r; Node(int x, int y){ l = x; r = y; } } 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");} long s = System.currentTimeMillis(); int t=1; //t=ni(); //int k = t; while(t-->0) {/*p("Case #"+ (k-t) + ": ")*/;process();} out.flush(); System.err.println(System.currentTimeMillis()-s+"ms"); out.close(); } static long power(long k, long c, long mod){ long y = 1; while(c > 0){ if(c%2 == 1) y = y * k % mod; c = c/2; k = k * k % mod; } return y; } 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
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
05bdfd1c1cadc2c058c3be41e4877aba
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int m=s.nextInt(); String str1=s.next(); String str2=s.next(); int[] left=new int[m]; int[] right=new int[m]; int ptr=0; for(int i=0;i<n&&ptr<m;i++) { if(str1.charAt(i)==str2.charAt(ptr)) { left[ptr]=i; ptr++; } } ptr=m-1; for(int i=n-1;i>=0&&ptr>=0;i--) { if(str1.charAt(i)==str2.charAt(ptr)) { right[ptr]=i; ptr--; } } int max=0; for(int i=0;i<m-1;i++) { max=Math.max(max,right[i+1]-left[i]); } System.out.println(max); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
962c676d85f3b6d176ef311df0f76943
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Zad1492C { public static void main(String[] args) { // TODO Auto-generated method stub FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int m = sc.nextInt(); String s1 = sc.next(); String s2 = sc.next(); int l = 0; int r = n-1; int[] left = new int[m]; int[] right = new int[m]; for(int i = 0;i<m-1;i++){ while(s1.charAt(l)!=s2.charAt(i)) { l++; } left[i] = l; l++; } for(int i = m-1;i>=1;i--){ while(s1.charAt(r)!=s2.charAt(i)) { r--; } right[i] = r; r--; } long answer = -1; for(int i = 0;i<m-1;i++) { answer = Math.max(right[i+1]-left[i], answer); } out.print(answer); out.close(); } static class FastReader{ StringTokenizer st; BufferedReader br; 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 s = ""; while(st==null || st.hasMoreElements()) { try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } } return s; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
2d795a6edfc37d6a2d11ed83fe089c3e
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashMap; import java.util.Random; import java.util.*; import java.util.StringTokenizer; import java.util.*; import java.util.Scanner; public class Comptetive { static class Pair{ int v; int wt; Pair(int v,int wt){ this.v=v; this.wt=wt; } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int m=sc.nextInt(),n=sc.nextInt(); sc.nextLine(); String s1=sc.nextLine(); String s2=sc.nextLine(); int[] left=new int[s2.length()]; int[] right=new int[s2.length()]; int j=0,count=0; for(int i=0;i<s1.length();i++) { if(j<s2.length() && s1.charAt(i)==s2.charAt(j)) { left[j]=i; j++; // check=1; count++; } } if(count<s2.length()) Arrays.fill(left, 0); count=0; int k=s2.length()-1; for(int i=s1.length()-1;i>=0;i--) { if(k>=0 && s1.charAt(i)==s2.charAt(k)) { right[k]=i; k--; count++; } } if(count<s2.length()) Arrays.fill(left, 0); // for(int i=0;i<left.length;i++) { // System.out.println(left[i]+" "+right[i]); // } int max=0; for(int i=1;i<left.length;i++) { max=Math.max(max, right[i]-left[i-1]); } System.out.println(max); } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static 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; } int[][] readArrayArray(int n,int m){ int[][] a=new int[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=nextInt(); } } return a; } long nextLong() { return Long.parseLong(next()); } // long[] readLongArray(long n) { // long[] a=new long[n]; // for (int i=0; i<n; i++) a[i]=nextLong(); // return a; // } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
5f729438c3c1218d92f62529c716d87e
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { /* HashMap<> map=new HashMap<>(); TreeMap<> map=new TreeMap<>(); map.put(p,map.getOrDefault(p,0)+1); for(Map.Entry<> mx:map.entrySet()){ int v=mx.getValue(),k=mx.getKey(); }for (int i = 1; i <= 1000; i++) fac[i] = fac[i - 1] * i % mod; ArrayList<Pair<Character,Integer>> l=new ArrayList<>(); ArrayList<> l=new ArrayList<>(); HashSet<> has=new HashSet<>();*/ PrintWriter out; FastReader sc; long mod=(long)(1e9+7); int maxint= Integer.MAX_VALUE; int minint= Integer.MIN_VALUE; long maxlong=Long.MAX_VALUE; long minlong=Long.MIN_VALUE; public void sol(){ int testCase=1; while(testCase-->0){ int n=ni(),m=ni(); char[] a=rl(),b=rl(); int ans=minint; int[] min=new int[n],max=new int[n]; int i=0,j=0; while(i<m){ while(b[i]!=a[j]){ j++; }min[i]=j; i++; j++; }i=m-1;j=n-1; while(i>=0){ while(b[i]!=a[j])j--; max[i]=j; j--; i--; }for(i=0;i<m-1;i++){ ans=max(ans,max[i+1]-min[i]); }pl(ans); } } public static void main(String[] args) { Main g=new Main(); g.out=new PrintWriter(System.out); g.sc=new FastReader(); g.sol(); g.out.flush(); } 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 int ni(){ return sc.nextInt(); }public long nl(){ return sc.nextLong(); }public double nd(){ return sc.nextDouble(); }public char[] rl(){ return sc.nextLine().toCharArray(); }public String rl1(){ return sc.nextLine(); } public void pl(Object s){ out.println(s); }public void ex(){ out.println(); } public void pr(Object s){ out.print(s); }public String next(){ return sc.next(); }public long abs(long x){ return Math.abs(x); } public int abs(int x){ return Math.abs(x); } public double abs(double x){ return Math.abs(x); } public long pow(long x,long y){ return (long)Math.pow(x,y); } public int pow(int x,int y){ return (int)Math.pow(x,y); } public double pow(double x,double y){ return Math.pow(x,y); }public long min(long x,long y){ return (long)Math.min(x,y); } public int min(int x,int y){ return (int)Math.min(x,y); } public double min(double x,double y){ return Math.min(x,y); }public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); }static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort(double[] a) { ArrayList<Double> l = new ArrayList<>(); for (double i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }int swap(int a,int b){ return a; }long swap(long a,long b){ return a; }double swap(double a,double b){ return a; } boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0); }boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0); }public long max(long x,long y){ return (long)Math.max(x,y); } public int max(int x,int y){ return (int)Math.max(x,y); } public double max(double x,double y){ return Math.max(x,y); }long sqrt(long x){ return (long)Math.sqrt(x); }int sqrt(int x){ return (int)Math.sqrt(x); }void input(int[] ar,int n){ for(int i=0;i<n;i++)ar[i]=ni(); }void input(long[] ar,int n){ for(int i=0;i<n;i++)ar[i]=nl(); }void fill(int[] ar,int k){ Arrays.fill(ar,k); }void yes(){ pl("YES"); }void no(){ pl("NO"); } int[] sieve(int n) { boolean prime[] = new boolean[n+1]; int[] k=new int[n+1]; for(int i=0;i<=n;i++) { prime[i] = true; k[i]=i; } for(int p = 2; p <=n; p++) { if(prime[p] == true) { // sieve[p]=p; for(int i = p*2; i <= n; i += p) { prime[i] = false; // sieve[i]=p; while(k[i]%(p*p)==0){ k[i]/=(p*p); } } } }return k; }int strSmall(int[] arr, int target) { int start = 0, end = arr.length-1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } int strSmall(ArrayList<Integer> arr, int target) { int start = 0, end = arr.size()-1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr.get(mid) > target) { start = mid + 1; ans=start; } else { end = mid - 1; } } return ans; }long mMultiplication(long a,long b) { long res = 0; a %= mod; while (b > 0) { if ((b & 1) > 0) { res = (res + a) % mod; } a = (2 * a) % mod; b >>= 1; } return res; }long nCr(int n, int r, long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; }long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; }long modInverse(long n, long p) { return power(n, p - 2, p); } public static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + "," + y; } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Double(x).hashCode() * 31 + new Double(y).hashCode(); } public int compareTo(Pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
ccfd510350adca23e0839df8cbc48399
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; public class TestClass { public static void main(String args[] ) throws Exception { Scanner in = new Scanner(System.in); int n=in.nextInt(); int m=in.nextInt(); String s=in.next(); String t=in.next(); Map<Integer, Integer> farthestIndex=new HashMap(); farthestIndex.put(m, 0); int j=n-1; for(int i=m-1;i>=0 && j>=0;i--){ while(s.charAt(j)!=t.charAt(i)){ j--; } farthestIndex.put(i, j); j--; } j=0; int max=0; for(int i=0;i<n && j<m;i++){ if(s.charAt(i)==t.charAt(j)){ max=Math.max(max, farthestIndex.get(j+1)-i); j++; } } System.out.println(max); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
8582acd47f8d98db85235c5a6eac7375
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedOutputStream; import java.io.PrintWriter; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Arrays; public class C extends Driver { public static void main(String[] args) { new C().run(); } @Override public void solve() { int n = in.nextInt(); int m = in.nextInt(); char[] longString = in.next().toCharArray(); char[] shortString = in.next().toCharArray(); n = longString.length; m = shortString.length; int[][] borders = new int[2][m]; { int nn = 0; for (int i = 0; i < m; i++) { while (nn < n && longString[nn] != shortString[i]) { nn++; } borders[0][i] = nn++; } } Debug.debug(borders[0]); { int nn = n - 1; for (int i = m - 1; i >= 0; i--) { while (nn >= 0 && longString[nn] != shortString[i]) { nn--; } borders[1][i] = nn--; } } Debug.debug(borders[1]); int best = -1; for (int i = 0; i < m - 1; i++) { best = Math.max(best, borders[1][i + 1] - borders[0][i]); } System.out.println(best); } } class Driver { public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE") == null; public final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out), LOCAL); public final FastScanner in = new FastScanner(); public final void run() { try { solve(); } finally { close(); } } public void solve() { int tt = in.nextInt(); while (tt-- > 0) { solveOne(); } } public void solveOne() { throw null; } protected void close() { out.close(); } protected void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i > 0) { out.print(' '); } out.print(objects[i]); } } protected void println(Object... objects) { print(objects); out.println(); } } class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public char nextChar() { return next().charAt(0); } public int[] nextArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public char[] nextCharArray(int n) { char[] a = new char[n]; for (int i = 0; i < n; i++) { a[i] = nextChar(); } return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public long nextLong() { return Long.parseLong(next()); } } class Debug { private static final boolean LOCAL = Driver.LOCAL; public static void debug(Object... o) { if (LOCAL) { StackTraceElement ste = new Throwable().getStackTrace()[1]; System.err.printf("%s:%s\t%s\n", ste.getMethodName(), ste.getLineNumber(), Arrays.deepToString(o)); } } public static <E> E debugId(E e, boolean off) { debug(e); return e; } public static void assertTrue(boolean cond) { if (!cond) { throw new RuntimeException(""); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
ab2499bd3728761ff8f64897e29a0f8a
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class C { static int ans = 1, maxn = 200010; static int m, n; static int[] L = new int[maxn]; static int[] R = new int[maxn]; static String s, t; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); in.readLine(); s = in.readLine(); t = in.readLine(); n = s.length(); m = t.length(); for(int i = m - 1, j = n - 1; i >= 0; --i, --j){ while(t.charAt(i) != s.charAt(j)){ --j; } R[i] = j; } for(int i = 0, j = 0; i < m; i++, j++){ while(t.charAt(i) != s.charAt(j)) ++j; L[i] = j; } for(int i = 0; i < m - 1; i++){ ans = Math.max(R[i + 1] - L[i], ans); } System.out.println(ans); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
e243adc27c03b14d1dff9d325a7235ae
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.awt.Point; public class CFTemplate { static final long MOD = 1000000007L; //static final long MOD2 = 1000000009L; //static final long MOD = 998244353L; //static final long INF = 500000000000L; static final int INF = 1100000000; static final int NINF = -100000; static FastScanner sc; static PrintWriter pw; static final int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}}; public static void main(String[] args) { sc = new FastScanner(); pw = new PrintWriter(System.out); int N = sc.ni(); int M = sc.ni(); String s = sc.next(); String t = sc.next(); int[] back = new int[N+2]; for (int i = N; i >= 1; i--) { back[i] = back[i+1]; if (back[i+1] < M && s.charAt(i-1)==t.charAt(M-1-back[i+1])) back[i] += 1; } int[] forward = new int[N+2]; for (int i = 1; i <= N; i++) { forward[i] = forward[i-1]; if (forward[i-1] < M && s.charAt(i-1)==t.charAt(forward[i-1])) forward[i] += 1; } int ans = 0; for (int i = 1; i <= N; i++) { if (0 < forward[i] && forward[i] < M && s.charAt(i-1)==t.charAt(forward[i]-1)) { int lo = i; int hi = N; while (lo < hi) { int m = (lo+hi+1)/2; if (forward[i]+back[m] < M) hi = m-1; else lo = m; } ans = Math.max(ans,lo-i); } } //pw.println(Arrays.toString(forward)); //pw.println(Arrays.toString(back)); pw.println(ans); pw.close(); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } 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()); } int[] intArray(int N, int mod) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = ni()+mod; return ret; } long nl() { return Long.parseLong(next()); } long[] longArray(int N, long mod) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = nl()+mod; return ret; } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
1ef984b16d90db1478ee1d0625696dfa
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
/* "Why do we fall? So we can learn to pick ourselves up." */ import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc=new FastReader(); static int dp[][]; //static int mod=1000000007; public static void main(String[] args) { //CHECK FOR N=1 //CHECK FOR N=1 //CHECK FOR N=1 //CHECK FOR N=1 PrintWriter out=new PrintWriter(System.out); //StringBuffer sb=new StringBuffer(""); int ttt=1; //ttt =i(); outer :while (ttt-- > 0) { int n=i(); int m=i(); String s=s(); String t=s(); char S[]=s.toCharArray(); char T[]=t.toCharArray(); int B[]=new int[m]; int a=n-1,b=m-1; while(a>=0 && b>=0) { if(S[a]==T[b]) { B[b]=a; a--; b--; } else { if(b!=m-1) { B[b]=B[b+1]; } else B[b]=m-1; a--; } } int max=Integer.MIN_VALUE; a=0; b=0; while(b<m-1 && a<n) { if(S[a]==T[b]) { int y=B[b+1]-a; if(y>max) max=y; a++; b++; } else { a++; } } System.out.println(max); } out.close(); //System.out.println(sb.toString()); //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 //CHECK FOR N=1 //CHECK FOR N=1 } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static int[][] input(int n,int m){ int A[][]=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=i(); } } return A; } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long mod(long x) { int mod=1000000007; return ((x%mod + mod)%mod); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static HashMap<Integer,Integer> hash(int A[]){ HashMap<Integer,Integer> map=new HashMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static class Pair //implements Comparable<Pair> { int x; int y; Pair(int x,int y){ this.x=x; this.y=y; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
23f3ec5552f0f3aabe0240d0e379b29c
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; public class Main { static BufferedReader bf; static PrintWriter out; static Scanner sc; static StringTokenizer st; static long mod = (long)(1e9+7); static long mod2 = 998244353; public static void main (String[] args)throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new Scanner(System.in); // solve(); // int t = nextInt(); // while(t-->0){ solve(); // } } public static void solve() throws IOException{ int n = nextInt(); int m = nextInt(); String s = bf.readLine(); String t = bf.readLine(); int[]l = new int[m]; int[]r = new int[m]; int j = 0; for(int i =0 ;i<n;i++){ if(s.charAt(i) == t.charAt(j)){ l[j] = i; j++; } if(j == m)break; } j = m-1; for(int i = n-1;i>=0;i--){ if(s.charAt(i) == t.charAt(j)){ r[j] = i; j--; } if(j < 0)break; } int max = 0; for(int i = 0;i<m-1;i++){ max = Math.max(max,r[i+1] - l[i]); } println(max); } public static long findSum(int node,int low, int high,int tlow,int thigh,long[]tree,long mod){ if(low >= tlow && high <= thigh)return tree[node]%mod; if(low > thigh || high < tlow)return 0; int mid = (low + high)/2; return((findSum(node*2,low,mid,tlow,thigh,tree,mod) % mod) + (findSum(node*2+1,mid+1,high,tlow,thigh,tree,mod)))%mod; } public static boolean allzero(long[]arr){ for(int i =0 ;i<arr.length;i++){ if(arr[i]!=0)return false; } return true; } public static long count(long[][]dp,int i,int[]arr,int drank,long sum){ if(i == arr.length)return 0; if(dp[i][drank]!=-1 && arr[i]+sum >=0)return dp[i][drank]; if(sum + arr[i] >= 0){ long count1 = 1 + count(dp,i+1,arr,drank+1,sum+arr[i]); long count2 = count(dp,i+1,arr,drank,sum); return dp[i][drank] = Math.max(count1,count2); } return dp[i][drank] = count(dp,i+1,arr,drank,sum); } public static void help(int[]arr,char[]signs,int l,int r){ if( l < r){ int mid = (l+r)/2; help(arr,signs,l,mid); help(arr,signs,mid+1,r); merge(arr,signs,l,mid,r); } } public static void merge(int[]arr,char[]signs,int l,int mid,int r){ int n1 = mid - l + 1; int n2 = r - mid; int[]a = new int[n1]; int []b = new int[n2]; char[]asigns = new char[n1]; char[]bsigns = new char[n2]; for(int i = 0;i<n1;i++){ a[i] = arr[i+l]; asigns[i] = signs[i+l]; } for(int i = 0;i<n2;i++){ b[i] = arr[i+mid+1]; bsigns[i] = signs[i+mid+1]; } int i = 0; int j = 0; int k = l; boolean opp = false; while(i<n1 && j<n2){ if(a[i] <= b[j]){ arr[k] = a[i]; if(opp){ if(asigns[i] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = asigns[i]; } i++; k++; } else{ arr[k] = b[j]; int times = n1 - i; if(times%2 == 1){ if(bsigns[j] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = bsigns[j]; } j++; opp = !opp; k++; } } while(i<n1){ arr[k] = a[i]; if(opp){ if(asigns[i] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = asigns[i]; } i++; k++; } while(j<n2){ arr[k] = b[j]; signs[k] = bsigns[j]; j++; k++; } } public static long binaryExpo(long base,long expo){ if(expo == 0)return 1; long val; if(expo%2 == 1){ val = (binaryExpo(base, expo-1)*base)%mod; } else{ val = binaryExpo(base, expo/2); val = (val*val)%mod; } return (val%mod); } public static boolean isSorted(int[]arr){ for(int i =1;i<arr.length;i++){ if(arr[i] < arr[i-1]){ return false; } } return true; } //function to find the topological sort of the a DAG public static boolean hasCycle(int[]indegree,List<List<Integer>>list,int n,List<Integer>topo){ Queue<Integer>q = new LinkedList<>(); for(int i =1;i<indegree.length;i++){ if(indegree[i] == 0){ q.add(i); topo.add(i); } } while(!q.isEmpty()){ int cur = q.poll(); List<Integer>l = list.get(cur); for(int i = 0;i<l.size();i++){ indegree[l.get(i)]--; if(indegree[l.get(i)] == 0){ q.add(l.get(i)); topo.add(l.get(i)); } } } if(topo.size() == n)return false; return true; } // function to find the parent of any given node with path compression in DSU public static int find(int val,int[]parent){ if(val == parent[val])return val; return parent[val] = find(parent[val],parent); } // function to connect two components public static void union(int[]rank,int[]parent,int u,int v){ int a = find(u,parent); int b= find(v,parent); if(a == b)return; if(rank[a] == rank[b]){ parent[b] = a; rank[a]++; } else{ if(rank[a] > rank[b]){ parent[b] = a; } else{ parent[a] = b; } } } // public static int findN(int n){ int num = 1; while(num < n){ num *=2; } return num; } // code for input public static void print(String s ){ System.out.print(s); } public static void print(int num ){ System.out.print(num); } public static void print(long num ){ System.out.print(num); } public static void println(String s){ System.out.println(s); } public static void println(int num){ System.out.println(num); } public static void println(long num){ System.out.println(num); } public static void println(){ System.out.println(); } public static int Int(String s){ return Integer.parseInt(s); } public static long Long(String s){ return Long.parseLong(s); } public static String[] nextStringArray()throws IOException{ return bf.readLine().split(" "); } public static String nextString()throws IOException{ return bf.readLine(); } public static long[] nextLongArray(int n)throws IOException{ String[]str = bf.readLine().split(" "); long[]arr = new long[n]; for(int i =0;i<n;i++){ arr[i] = Long.parseLong(str[i]); } return arr; } public static int[][] newIntMatrix(int r,int c)throws IOException{ int[][]arr = new int[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Integer.parseInt(str[j]); } } return arr; } public static long[][] newLongMatrix(int r,int c)throws IOException{ long[][]arr = new long[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Long.parseLong(str[j]); } } return arr; } static class pair{ int one; int two; pair(int one,int two){ this.one = one ; this.two =two; } } public static long gcd(long a,long b){ if(b == 0)return a; return gcd(b,a%b); } public static long lcm(long a,long b){ return (a*b)/(gcd(a,b)); } public static boolean isPalindrome(String s){ int i = 0; int j = s.length()-1; while(i<=j){ if(s.charAt(i) != s.charAt(j)){ return false; } i++; j--; } return true; } // these functions are to calculate the number of smaller elements after self public static void sort(int[]arr,int l,int r){ if(l < r){ int mid = (l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); smallerNumberAfterSelf(arr, l, mid, r); } } public static void smallerNumberAfterSelf(int[]arr,int l,int mid,int r){ int n1 = mid - l +1; int n2 = r - mid; int []a = new int[n1]; int[]b = new int[n2]; for(int i = 0;i<n1;i++){ a[i] = arr[l+i]; } for(int i =0;i<n2;i++){ b[i] = arr[mid+i+1]; } int i = 0; int j =0; int k = l; while(i<n1 && j < n2){ if(a[i] < b[j]){ arr[k++] = a[i++]; } else{ arr[k++] = b[j++]; } } while(i<n1){ arr[k++] = a[i++]; } while(j<n2){ arr[k++] = b[j++]; } } public static String next(){ while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bf.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } static int nextInt() { return Integer.parseInt(next()); } static long nextLong() { return Long.parseLong(next()); } static double nextDouble() { return Double.parseDouble(next()); } static String nextLine(){ String str = ""; try { str = bf.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // use some math tricks it might help // sometimes just try to think in straightforward plan in A and B problems don't always complecate the questions with thinking too much differently // always use long number to do 10^9+7 modulo // if a problem is related to binary string it could also be related to parenthesis // *****try to use binary search(it is a very beautiful thing it can work in some of the very unexpected problems ) in the question it might work****** // try sorting // try to think in opposite direction of question it might work in your way // if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+d or a,b,c,d in general // if the question is to much related to left and/or right side of any element in an array then try monotonic stack it could work. // in range query sums try to do binary search it could work // analyse the time complexity of program thoroughly // anylyse the test cases properly // if we divide any number by 2 till it gets 1 then there will be (number - 1) operation required // try to do the opposite operation of what is given in the problem //think about the base cases properly //If a question is related to numbers try prime factorisation or something related to number theory // keep in mind unique strings //you can calculate the number of inversion in O(n log n) // in a matrix you could sometimes think about row and cols indenpendentaly. // Try to think in more constructive(means a way to look through various cases of a problem) way. // observe the problem carefully the answer could be hidden in the given test cases itself. (A, B , C); // when we have equations like (a+b = N) and we have to find the max of (a*b) then the values near to the N/2 must be chosen as (a and b); // give emphasis to the number of occurences of elements it might help. // if a number is even then we can find the pair for each and every number in that range whose bitwise AND is zero.
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
3763977ded1c5a6691e4695756c4ff0f
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; import javafx.util.Pair; public class Main { static int left ,right; public static int LowerBound(ArrayList<Integer> a, int x) { int l = -1, r = a.size(); while (l + 1 < r) { int m = (l + r) >>> 1; if (a.get(m) < x) { l = m; } else { r = m; } } return l; } public static int UpperBound(ArrayList<Integer> a, int x) { int l = -1, r = a.size(); while (l + 1 < r) { int m = (l + r) >>> 1; if (a.get(m) > x) { r = m; } else { l = m; } } return r; } public static class pair { int min ,max; public pair(int min, int max) { this.min = min; this.max = max; } } public static void main(String[] args) { FastScanner input = new FastScanner(); int n = input.nextInt(); int m = input.nextInt(); String s1 = input.next(); String s2 = input.next(); HashMap<Character,ArrayList<Integer>> map = new HashMap<>(); for (int i = 0; i <s1.length(); i++) { if(!map.containsKey(s1.charAt(i))) { ArrayList<Integer> aa = new ArrayList<>(); aa.add(i); map.put(s1.charAt(i), aa); } else map.get(s1.charAt(i)).add(i); } pair mm[] = new pair[m]; for (int i = 0; i <m; i++) { mm[i] = new pair(0, 0); } // for (Map.Entry<Character, ArrayList<Integer>> entry : map.entrySet()) { // Character key = entry.getKey(); // ArrayList<Integer> value = entry.getValue(); // System.out.print(key+" "); // System.out.println(value); // // } mm[0].min = left= map.get(s2.charAt(0)).get(0); mm[m-1].max =right =map.get(s2.charAt(m-1)).get(map.get(s2.charAt(m-1)).size()-1); // System.out.println(mm[0].min+" "+mm[m-1].max); for (int i = 1; i < m; i++) { mm[i].min =map.get(s2.charAt(i)).get(UpperBound(map.get(s2.charAt(i)), left)) ; left = mm[i].min; } for (int i = m-2; i >=0; i--) { mm[i].max = map.get(s2.charAt(i)).get(LowerBound(map.get(s2.charAt(i)), right)); right = mm[i].max; } long ans = Long.MIN_VALUE; for (int i = 0; i <m-1; i++) { ans = Math.max(ans,Math.abs(mm[i+1].max-mm[i].min) ); } System.out.println(ans); } 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) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
97ab479592a4ec6d8862bdd09abb7284
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
/* * akshaygupta26 */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.Random; import java.util.Arrays; import java.util.StringTokenizer; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Collections; import java.util.*; public class C { public static void main(String[] args) { FastReader sc=new FastReader(); StringBuffer ans=new StringBuffer(); int test=1; while(test-->0) { int n=sc.nextInt(),m=sc.nextInt(); char str1[]=sc.next().toCharArray(); char str2[]=sc.next().toCharArray(); ArrayList<Integer> arr[]=new ArrayList[26]; int rp[]=new int[26]; Arrays.fill(rp, -1); for(int i=0;i<26;i++) arr[i]=new ArrayList<>(); for(int i=0;i<n;i++) { int ch=str1[i]-'a'; arr[ch].add(i+1); rp[ch]++; } int brr[]=new int[m]; for(int i=0;i<m;i++) { int ch =str2[i]-'a'; if(i == 0) { brr[i]=arr[ch].get(0); continue; } else { int next = upperBound(arr[ch],brr[i-1]); brr[i]=arr[ch].get(next); } } int maxm=0; for(int i=m-1;i>0;i--) { int ch=str2[i]-'a'; if(i == m-1) { brr[i]=arr[ch].get(arr[ch].size()-1); } else { int prv =lowerBound(arr[ch],brr[i+1]); brr[i]=arr[ch].get(prv); } if(brr[i]-brr[i-1]>maxm) maxm=brr[i]-brr[i-1]; } ans.append(maxm+"\n"); } System.out.print(ans); } static int upperBound(ArrayList<Integer> arr,int val) { int low=0; int high=arr.size()-1; int ans =0; while(low<=high) { int mid = low+(high-low)/2; if(arr.get(mid)>val) { ans=mid; high=mid-1; } else { low=mid+1; } } return ans; } static int lowerBound(ArrayList<Integer> arr,int val) { int low=0; int high=arr.size()-1; int ans =0; while(low<=high) { int mid = low+(high-low)/2; if(arr.get(mid)>=val) { high=mid-1; } else { ans=mid; low=mid+1; } } return ans; } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
80c44cb7e0c926f01177fcced00e7e4d
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
//WHEN IN DOUBT , USE BRUTE FORCE !!!!!!!!! import java.lang.String; import java.io.*; import java.util.*; import java.util.StringTokenizer; //class Main //AtCoder()Bt //class Solution // Codechef public class Solution2 //Codeforces { public static void main(String args[]) { try { FastReader sc = new FastReader(); int TT = 1;//sc.nextInt(); for(int hehe=1 ; hehe <= TT ; hehe++){ int N=sc.nextInt(); int M=sc.nextInt(); String S=sc.nextLine();String T= sc.nextLine(); int init[]=new int[T.length()]; int init2[]=new int[T.length()]; int curr=0,prev=0; int memo[]=new int[T.length()]; for(int i = 0 ; i < S.length() && curr < T.length();i++){ if(S.charAt(i) == T.charAt(curr)){ init[curr]=i; curr++; } } curr=T.length()-1; for(int i = S.length()-1 ; i >= 0 && curr >=0;i--){ if(S.charAt(i) == T.charAt(curr)){ init2[curr]=i; curr--; } } long max=1l; for(int i=1;i<=T.length()-1;i++){ max=Math.max(max , init2[i]-init[i-1]); } sopln(max); //printArray(init); //printArray(init2); } //out.flush(); }catch (Exception e) { sopln(e.toString()); } } public final static int d = 256; static int MOD = 1000000007; static final double PI = Math.PI; private static BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); private static PrintWriter out = new PrintWriter (new OutputStreamWriter (System.out)); 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()); } char nextChar() { try { return (char) (br.read()); }catch (IOException e){ return '~'; } } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void sop(Object o){System.out.print(o);} static double ceil(double d){ return Math.ceil(d); } static double floor(double d){ return Math.floor(d); } static double round(double d){ return Math.round(d); } static void sopln(Object o){System.out.println(o);} static void printArray(long[] arr){ for(int i=0;i<arr.length;i++){ sop(arr[i]+" "); } sopln(""); } static void printArray(int[] arr){ for(int i=0;i<arr.length;i++){ sop(arr[i]+" "); } sopln(""); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
5bba3a6e98898fb94f271fd94d498094
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; public class Codeforces { public void solve() { FastScanner fs = new FastScanner(); StringBuilder print = new StringBuilder(); int n = fs.nextInt(), m = fs.nextInt(); char[]a = fs.nextLine().toCharArray(); char[]b = fs.nextLine().toCharArray(); int[]l = new int[m]; int[]r = new int[m]; int pos = 0; for(int i=0;i<m;i++){ while (pos<n && a[pos]!=b[i])pos++; l[i] = pos; pos++; } pos = n-1; for(int i=m-1;i>=0;i--){ while (pos>=0&&a[pos]!=b[i])pos--; r[i] = pos; pos--; } int ans = 0; for(int i=0;i<m-1;i++){ ans = Math.max(r[i+1]-l[i],ans); } System.out.println(ans); } public static void main(String[]args){ try{ new Codeforces().solve(); }catch (Exception e){ e.printStackTrace(); } } 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(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
a8b28fa233c1567c70831c0575ea56bc
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; public class MainC { 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 boolean hasNext() { try { String string = reader.readLine(); if (string == null) { return false; } tokenizer = new StringTokenizer(string); return tokenizer.hasMoreTokens(); } catch (IOException e) { return false; } } } static InputReader in = new InputReader(System.in); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) { int t = 1; while (t -- > 0) { solve(); } out.close(); } static int maxN = 1000100; static void solve() { int n = in.nextInt(); int m = in.nextInt(); char[] s = in.next().toCharArray(); // chang de char[] t = in.next().toCharArray(); // duan de int lens = s.length; int lent = t.length; int[] a = new int[maxN]; int[] b = new int[maxN]; int k = 0; for (int i = 0; i < lens; i++) { if (s[i] == t[k]) { a[k] = i; k ++; } if (k > m - 1) break; } int f = lent - 1; for (int i = lens - 1; i >= 0; i --) { if (s[i] == t[f]) { b[f] = i; f --; } if (f < 0) break; } int answer = 0; for (int i = 0; i < lent; i++) { answer = Math.max(answer, b[i + 1] - a[i]); } out.println(answer); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
dddbbb01fcf3924310c0b5c8c621d439
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; import java.util.TreeMap; public class Main implements Runnable { int n, m; static boolean use_n_tests = false; void solve(FastScanner in, PrintWriter out, int testNumber) { n = in.nextInt(); m = in.nextInt(); char[] s = in.next().toCharArray(); char[] t = in.next().toCharArray(); Map<Integer, Integer> pos = new HashMap<>(); int it = 0; for (int i = 0; i < t.length - 1; i++) { for (;s[it] != t[i]; it++); pos.put(i, it++); } it = s.length - 1; int res = 0; for (int i = t.length - 1; i >= 1; i--) { for (;s[it] != t[i]; it--); if (pos.get(i - 1) < it) { res = Math.max(it - pos.get(i - 1), res); } it--; } out.println(res); } void solve1(FastScanner in, PrintWriter out, int testNumber) { n = in.nextInt(); int k = in.nextInt(); int[] a = in.nextArray(n); int l = 1; int r = n; int ans = -1; while (l <= r) { int m = (l + r) / 2; if (can(m, a, k)) { ans = m; l = m + 1; } else { r = m - 1; } } out.println(ans); } boolean can(int m, int[] a, int limit) { int[] sum = new int[a.length]; for (int i = 0; i < a.length; i++) { if (a[i] <= m) { sum[i] = -1; } else { sum[i] = 1; } if (i > 0) { sum[i] += sum[i - 1]; } } for (int i = 0; i <= n - limit; i++) { int psum = 0; if (i > 0) { psum -= sum[i - 1]; } psum += sum[i + limit - 1]; if (limit % 2 == 0 && psum >= 0) { return true; } if (limit % 2 == 1 && psum >= -1) { return true; } } return false; } // ****************************** template code *********** int ask(int l, int r) { if (l >= r) { return -1; } System.out.printf("? %d %d\n", l + 1, r + 1); System.out.flush(); return in.nextInt() - 1; } static int stack_size = 1 << 27; class Mod { long mod; Mod(long mod) { this.mod = mod; } long add(long a, long b) { a = mod(a); b = mod(b); return (a + b) % mod; } long sub(long a, long b) { a = mod(a); b = mod(b); return (a - b + mod) % mod; } long mul(long a, long b) { a = mod(a); b = mod(b); return a * b % mod; } long div(long a, long b) { a = mod(a); b = mod(b); return (a * inv(b)) % mod; } public long inv(long r) { if (r == 1) return 1; return ((mod - mod / r) * inv(mod % r)) % mod; } private long mod(long a) { return a % mod; } } class Coeff { long mod; long[][] C; long[] fact; boolean cycleWay = false; Coeff(int n, long mod) { this.mod = mod; fact = new long[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = i; fact[i] %= mod; fact[i] *= fact[i - 1]; fact[i] %= mod; } } Coeff(int n, int m, long mod) { // n > m cycleWay = true; this.mod = mod; C = new long[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= Math.min(i, m); j++) { if (j == 0 || j == i) { C[i][j] = 1; } else { C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; C[i][j] %= mod; } } } } public long C(int n, int m) { if (cycleWay) { return C[n][m]; } return fC(n, m); } private long fC(int n, int m) { return (fact[n] * inv(fact[n - m] * fact[m] % mod)) % mod; } private long inv(long r) { if (r == 1) return 1; return ((mod - mod / r) * inv(mod % r)) % mod; } } class Pair { int first; int second; public int getFirst() { return first; } public int getSecond() { return second; } } class MultisetTree<T> { int size = 0; TreeMap<T, Integer> mp = new TreeMap<>(); void add(T x) { mp.merge(x, 1, Integer::sum); size++; } void remove(T x) { if (mp.containsKey(x)) { mp.merge(x, -1, Integer::sum); if (mp.get(x) == 0) { mp.remove(x); } size--; } } boolean contains(T x) { return mp.containsKey(x); } T greatest() { return mp.lastKey(); } T smallest() { return mp.firstKey(); } int size() { return size; } int diffSize() { return mp.size(); } } class Multiset<T> { int size = 0; Map<T, Integer> mp = new HashMap<>(); void add(T x) { mp.merge(x, 1, Integer::sum); size++; } boolean contains(T x) { return mp.containsKey(x); } void remove(T x) { if (mp.containsKey(x)) { mp.merge(x, -1, Integer::sum); if (mp.get(x) == 0) { mp.remove(x); } size--; } } int size() { return size; } int diffSize() { return mp.size(); } } static class Range { int l, r; int id; public int getL() { return l; } public int getR() { return r; } public Range(int l, int r, int id) { this.l = l; this.r = r; this.id = id; } } static class Array { static Range[] readRanges(int n, FastScanner in) { Range[] result = new Range[n]; for (int i = 0; i < n; i++) { result[i] = new Range(in.nextInt(), in.nextInt(), i); } return result; } static List<List<Integer>> intInit2D(int n) { List<List<Integer>> res = new ArrayList<>(); for (int i = 0; i < n; i++) { res.add(new ArrayList<>()); } return res; } static boolean isSorted(Integer[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static public long sum(int[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static public long sum(long[] a) { long sum = 0; for (long x : a) { sum += x; } return sum; } static public long sum(Integer[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static public int min(Integer[] a) { int mn = Integer.MAX_VALUE; for (int x : a) { mn = Math.min(mn, x); } return mn; } static public int min(int[] a) { int mn = Integer.MAX_VALUE; for (int x : a) { mn = Math.min(mn, x); } return mn; } static public int max(Integer[] a) { int mx = Integer.MIN_VALUE; for (int x : a) { mx = Math.max(mx, x); } return mx; } static public int max(int[] a) { int mx = Integer.MIN_VALUE; for (int x : a) { mx = Math.max(mx, x); } return mx; } static public int[] readint(int n, FastScanner in) { int[] out = new int[n]; for (int i = 0; i < out.length; i++) { out[i] = in.nextInt(); } return out; } } class Graph { List<List<Integer>> graph; Graph(int n) { create(n); } void create(int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } this.graph = graph; } void readBi(int m, FastScanner in) { for (int i = 0; i < m; i++) { int v = in.nextInt() - 1; int u = in.nextInt() - 1; graph.get(v).add(u); graph.get(u).add(v); } } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream io) { br = new BufferedReader(new InputStreamReader(io)); } public String line() { String result = ""; try { result = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return result; } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = in.nextInt(); } return res; } public long[] nextArrayL(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = in.nextLong(); } return res; } public Long[] nextArrayL2(int n) { Long[] res = new Long[n]; for (int i = 0; i < n; i++) { res[i] = in.nextLong(); } return res; } public Integer[] nextArray2(int n) { Integer[] res = new Integer[n]; for (int i = 0; i < n; i++) { res[i] = in.nextInt(); } return res; } public long nextLong() { return Long.parseLong(next()); } } void run_t_tests() { int t = in.nextInt(); int i = 0; while (t-- > 0) { solve(in, out, i++); } } void run_one() { solve(in, out, -1); } @Override public void run() { in = new FastScanner(System.in); out = new PrintWriter(System.out); if (use_n_tests) { run_t_tests(); } else { run_one(); } out.close(); } static FastScanner in; static PrintWriter out; public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(null, new Main(), "", stack_size); thread.start(); thread.join(); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
78a35cf1690a56eefa1c16fca3746952
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.security.KeyStore.Entry; import java.util.*; public class Odd_Selection { static InputStreamReader r = new InputStreamReader(System.in); static BufferedReader br = new BufferedReader(r); static PrintWriter p = new PrintWriter(System.out); static int n,m; static char a[],b[]; public static void main(String[] args) throws Exception { // int test = Integer.parseInt(br.readLine()); int test = 1; me146: while (test-- > 0) { String str[] = splitString(); n = Integer.parseInt(str[0]); m = Integer.parseInt(str[1]); a = br.readLine().trim().toCharArray(); b = br.readLine().trim().toCharArray(); int left = 0; int right = n; int answer = 0; while(left<=right){ int mid = left+ (right-left)/2; if(canInclude(mid)){ answer = Math.max(answer, mid); left = mid +1; }else{ right = mid -1; } } // p.println(canInclude(4)); p.println(answer); } p.flush(); } static boolean canInclude(int mid){ ArrayList<Integer> start = new ArrayList<>(); ArrayList<Integer> end = new ArrayList<>(); int j=0; for (int i = 0; i < n; i++) { if(j == m){ break; } if (a[i] == b[j]) { start.add(i); j++; } } j = m - 1; for (int i = n-1; i >=0; i--) { if(j<0){ break; } if (a[i] == b[j]) { end.add(i); j--; } } for(int i=0;i<m-1;i++){ int len = end.get(m-(i+1)-1)-start.get(i); if(len>=mid)return true; } return false; } static int root(int union[],int i){ while(union[i]!=i){ union[i] = union[union[i]]; i=union[i]; } return i; } static int[] join(int union[],int a,int b){ int rootA=root(union, a); int rootB=root(union,b); union[rootA] = union[rootB]; return union; } static boolean find(int union[],int a,int b){ if(root(union, a)==root(union,b)) return true; else return false; } static String formatString(String s) { int len = s.length(); for (int i = 0; i < 19 - len; i++) { s = "0" + s; } return s; } static String[] splitString() throws IOException { return br.readLine().trim().split(" "); } } class Obj { char x; int pos; public Obj(char x, int pos) { this.x = x; this.pos = pos; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
57fd0c2ff8402cc26dab0b6c854a5861
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { FastReader in = new FastReader(); int n = in.nextInt(); int m = in.nextInt(); char[] s = in.next().toCharArray(); char[] t = in.next().toCharArray(); int[] pl = new int[m]; int sp = n-1; int tp = m-1; while(sp>=0 && tp>=0) { if(s[sp]==t[tp]) { pl[tp] = sp; tp--; } sp--; } int[] ps = new int[m]; sp = 0; tp = 0; while(sp<n && tp<m) { if(s[sp]==t[tp]) { ps[tp] = sp; tp++; } sp++; } int ans = 0; for(int i = 1; i < m; i++) { ans = Math.max(ans, pl[i]-ps[i-1]); } System.out.println(ans); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } int[] readInt(int size) { int[] arr = new int[size]; for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(next()); return arr; } long[] readLong(int size) { long[] arr = new long[size]; for(int i = 0; i < size; i++) arr[i] = Long.parseLong(next()); return arr; } int[][] read2dArray(int rows, int cols) { int[][] arr = new int[rows][cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) arr[i][j] = Integer.parseInt(next()); } return arr; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
c852acfeb53b9aecfc99e4e5ac187c2b
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { FastReader in = new FastReader(); int n = in.nextInt(); int m = in.nextInt(); char[] s = in.next().toCharArray(); char[] t = in.next().toCharArray(); List<Integer>[] a = new ArrayList[26]; for(int i = 0; i < 26; i++) a[i] = new ArrayList<>(); for(int i = 0; i < n; i++) a[s[i]-'a'].add(i); int[] pl = new int[m]; int sp = n-1, tp = m-1; while(sp>=0 && tp>=0) { if(s[sp]==t[tp]) { pl[tp] = sp; tp--; } sp--; } int[] ps = new int[m]; sp = 0; tp = 0; while(sp<n && tp<m) { if(s[sp]==t[tp]) { ps[tp] = sp; tp++; } sp++; } int ans = 0; for(int i = 1; i < m; i++) { ans = Math.max(ans, pl[i]-ps[i-1]); } System.out.println(ans); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } int[] readInt(int size) { int[] arr = new int[size]; for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(next()); return arr; } long[] readLong(int size) { long[] arr = new long[size]; for(int i = 0; i < size; i++) arr[i] = Long.parseLong(next()); return arr; } int[][] read2dArray(int rows, int cols) { int[][] arr = new int[rows][cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) arr[i][j] = Integer.parseInt(next()); } return arr; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
b25a7769f861c23f34b40d5f07a6e81c
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { FastReader in = new FastReader(); int n = in.nextInt(); int m = in.nextInt(); char[] s = in.next().toCharArray(); char[] t = in.next().toCharArray(); List<Integer>[] a = new ArrayList[26]; for(int i = 0; i < 26; i++) a[i] = new ArrayList<>(); for(int i = 0; i < n; i++) a[s[i]-'a'].add(i); int[] pl = new int[m]; pl[m-1] = a[t[m-1]-'a'].get(a[t[m-1]-'a'].size()-1); for(int i = m-2; i >= 0; i--) { int l = 0, r = a[t[i]-'a'].size()-1, ans = 0; while(l<=r) { int mid = l+(r-l)/2; if(a[t[i]-'a'].get(mid) < pl[i+1]) { ans = a[t[i]-'a'].get(mid); l = mid+1; } else r = mid-1; } pl[i] = ans; } int[] ps = new int[m]; ps[0] = a[t[0]-'a'].get(0); for(int i = 1; i < m; i++) { int l = 0, r = a[t[i]-'a'].size()-1, ans = 0; while(l <= r) { int mid = l + (r-l)/2; if(a[t[i]-'a'].get(mid) > ps[i-1]) { ans = a[t[i]-'a'].get(mid); r = mid-1; } else l = mid+1; } ps[i] = ans; } int ans = 0; for(int i = 1; i < m; i++) { ans = Math.max(ans, pl[i]-ps[i-1]); } System.out.println(ans); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } int[] readInt(int size) { int[] arr = new int[size]; for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(next()); return arr; } long[] readLong(int size) { long[] arr = new long[size]; for(int i = 0; i < size; i++) arr[i] = Long.parseLong(next()); return arr; } int[][] read2dArray(int rows, int cols) { int[][] arr = new int[rows][cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) arr[i][j] = Integer.parseInt(next()); } return arr; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
8fb56f7b1aa896ceb728744b446ecd24
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { FastReader f = new FastReader(); StringBuffer sb=new StringBuffer(); int n=f.nextInt(); int m=f.nextInt(); char s[]=f.next().toCharArray(); char t[]=f.next().toCharArray(); int[]low=new int[m]; int[]high=new int[m]; int idx=0; for(int i=0;i<n && idx<m;i++) { if(s[i]==t[idx]) { low[idx]=i; idx++; } } idx=m-1; for(int i=n-1;i>=0 && idx>=0;i--) { if(s[i]==t[idx]) { high[idx]=i; idx--; } } int ans=1; for(int i=1;i<m;i++) { ans=Math.max(ans,high[i]-low[i-1]); } System.out.println(ans); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try{ st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
7000fecb8497029d02609738d5feeb06
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
// これを翻訳している間、あなたはあなたの人生のいくつかの貴重な瞬間を無駄にしました import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Solution { static PrintWriter fop = new PrintWriter(System.out); static boolean isSubseq(String s , String t){ int ptr = 0 ; for (int i = 0; i <s.length() ; i++) { if (s.charAt(i) == s.charAt(ptr)) ptr++ ; if (ptr == t.length()) break; } if (ptr == t.length()) return true ; return false ; } public static void main(String[] args) { FastScanner fsca = new FastScanner(); int n = fsca.nextInt(); int m = fsca.nextInt(); char s[] = fsca.next().toCharArray() ; char t[] = fsca.next().toCharArray() ; int fst[] = new int[m] ; int tt = 0 ; for (int i = 0; i <m ; i++) { while (tt <n & s[tt] != t[i])tt++; fst[i] = tt ; tt++ ; } tt = n-1 ; int lst[] = new int[m] ; for (int i = m-1 ; i>=0 ; i--){ while (tt >= 0 && s[tt] != t[i])tt-- ; lst[i] = tt ; tt-- ; } int max = -1 ; for (int i = 0; i <m-1 ; i++) { max = Math.max(max , lst[i+1] - fst[i]) ; } System.out.println(max); fop.flush(); fop.close(); } /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } ; static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } static final Random random = new Random(); static void ruffleSort(int[] a) { int n = a.length;//shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { int n = a.length;//shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n); long temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } 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[][] readMat(int n, int m) { int a[][] = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = nextInt(); return a; } char[][] readCharMat(int n, int m) { char a[][] = new char[n][m]; for (int i = 0; i < n; i++) { String s = next(); for (int j = 0; j < m; j++) a[i][j] = s.charAt(j); } return a; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } static void print(int a[], int n) { for (int i = 0; i < n; i++) fop.append((a[i]) + " "); // fop.append("\n") ; } static void print(long a[], int n) { for (int i = 0; i < n; i++) fop.append((a[i]) + " "); // fop.append("\n") ; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
ccff04d7d102d85bd1cd6a83e65913fe
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.InputMismatchException; import java.util.*; import java.io.*; import java.lang.*; public class Main{ public static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 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() { 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); } } 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); } public static void sortbyColumn(int arr[][], int col) { Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(final int[] entry1, final int[] entry2) { if (entry1[col] > entry2[col]) return 1; else return -1; } }); } static long func(long a[],int 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()); } } static class MultiSet<U extends Comparable<U>> { public int sz = 0; public TreeMap<U, Integer> t; public MultiSet() { t = new TreeMap<>(); } public void add(U x) { t.put(x, t.getOrDefault(x, 0) + 1); sz++; } public void remove(U x) { if (t.get(x) == 1) t.remove(x); else t.put(x, t.get(x) - 1); sz--; } } 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 long myceil(long a, long b){ return (a+b-1)/b; } static long C(long n,long r){ long count=0,temp=n; long ans=1; long num=n-r+1,div=1; while(num<=n){ ans*=num; //ans+=MOD; ans%=MOD; ans*=mypow(div,MOD-2); //ans+=MOD; ans%=MOD; num++; div++; } ans+=MOD; return ans%MOD; } static long fact(long a){ long i,ans=1; for(i=1;i<=a;i++){ ans*=i; ans%=MOD; } return ans%=MOD; } static void sieve(int n){ is_sieve[0]=1; is_sieve[1]=1; int i,j,k; for(i=2;i<n;i++){ if(is_sieve[i]==0){ sieve[i]=i; tr.add(i); for(j=i*i;j<n;j+=i){ if(j>n||j<0){ break; } is_sieve[j]=1; sieve[j]=i; } } } } static void calc(int n){ int i,j; dp[n-1]=0; if(n>1) dp[n-2]=1; for(i=n-3;i>=0;i--){ long ind=n-i-1; dp[i]=((ind*(long)mypow(10,ind-1))%MOD+dp[i+1])%MOD; } } static long mypow(long x,long y){ long temp; if( y == 0) return 1; temp = mypow(x, y/2); if (y%2 == 0) return (temp*temp)%MOD; else return ((x*temp)%MOD*(temp)%MOD)%MOD; } static long dist[],dp[]; static int visited[]; static ArrayList<Pair<Integer,String>> adj[]; //static int dp[][][]; static int R,G,B,MOD=1000000007; static int[] par,size,memo; static int[] sieve,is_sieve; static TreeSet<Integer> tr; static char ch[][], ch1[][]; // static void lengen(int arr[],int ind){ // memo[ind]=Math.max(lengen(arr,ind+1),lis(arr,)) // } static int lis(int arr[],int n,int ind,int cur){ if(ind>=n){ return memo[ind]=0; } else if(ind>=0&&memo[ind]>-1){ return memo[ind]; } else if(cur<arr[ind+1]){ return memo[ind]=Math.max(lis(arr,n,ind+1,arr[ind+1])+1,lis(arr,n,ind+1,cur)); }else{ return memo[ind]=lis(arr,n,ind+1,cur); } } public static void main(String args[]){ InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter w = new PrintWriter(outputStream); int t,i,j,tno=0,tte; //t=in.nextInt(); t=1; //tte=t; while(t-->0){ int n=in.nextInt(); int m=in.nextInt(); String s1,s2; s1=in.next(); s2=in.next(); char ch1[]=s1.toCharArray(); char ch2[]=s2.toCharArray(); int pre[]=new int[m]; int suf[]=new int[m]; int ind=0; for(i=0;i<n;i++){ if(ch1[i]==ch2[ind]){ pre[ind]=i; ind++; } if(ind>=m){ break; } } ind=m-1; for(i=n-1;i>=0;i--){ if(ch1[i]==ch2[ind]){ suf[ind]=i; ind--; } if(ind<0){ break; } } int max=1; for(i=0;i<m-1;i++){ //w.println(pre[i]+" "+suf[i]); max=Math.max(max,suf[i+1]-pre[i]); } w.println(max); } w.close(); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
2d81b30bc4cdf3097795572eed11154a
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
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.Random; import java.util.StringTokenizer; import java.util.List; import java.util.Collections; import java.util.Map; import java.util.HashMap; public class Master { private static FastScanner fs=new FastScanner(); public static void main(String[] args) { int n = fs.nextInt(),m = fs.nextInt(); String s = fs.next(); String t = fs.next(); int l[] = new int [n]; int r[] = new int [n]; int i=0,j=0; while(j<m) { while(s.charAt(i) != t.charAt(j)) { i++; } l[j]=i; i++; j++; } j = m-1; i= n-1; while(j>=0) { while(s.charAt(i) != t.charAt(j)) i--; r[j] = i; i--; j--; } int ans = 0; for( i=1;i<m;i++) { ans = Math.max(ans,r[i]-l[i-1]); } System.out.println(ans); } static class Node { List<Integer> list = new ArrayList<>(); } static final Random random=new Random(); static void ruffleSort(int [] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); int temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } 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()); } int [] sort(int [] arr) { List<Integer> list = new ArrayList<>(); for(int i : arr) list.add(i); Collections.sort(list); int res[] = new int[arr.length]; for(int i=0;i<arr.length;i++) res[i] = list.get(i); return res; } void debug(int a) { System.out.println("This is var "+a); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
71f1d395a4b55a5388a9da486e0fba92
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.util.Scanner; import java.io.*; import javax.lang.model.util.ElementScanner6; import static java.lang.System.out; import java.util.Stack; import java.util.Queue; import java.util.LinkedList; public class C1492 { static int mod=(int)(1e9+7); static long MOD=(long)(1e9+7); static FastReader in=new FastReader(); static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String args[]) { int tc=1; //tc=in.nextInt(); tcloop: while(tc-->0) { int n=in.nextInt(); int m=in.nextInt(); String s=in.nextLine(); String t=in.nextLine(); char S[]=s.toCharArray(); char T[]=t.toCharArray(); int freq[]=new int[26]; for(char c : T) { freq[c-'a']++; } ArrayList<ArrayList<Integer>> al=new ArrayList<>(); for(int i=0;i<26;i++) { al.add(new ArrayList<>()); } int r[]=new int[m]; int l[]=new int[m]; int req=0; for(int i=0;i<n;i++) { if(S[i]==T[req]) { l[req]=i; req++; } if(req==m)break; } req=m-1; for(int i=n-1;i>=0;i--) { if(S[i]==T[req]) { r[req]=i; req--; } if(req==-1)break; } int max=0; for(int i=1;i<m;i++) { int dif=r[i]-l[i-1]; max=Math.max(dif,max); } pr.println(max); } pr.flush(); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readIntArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=nextInt(); return a; } long[] readLongArray(int n) { long a[]=new long[n]; for(int i=0;i<n;i++)a[i]=nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
c45f48b9af33b35140542d7d811b9984
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = 1;//in.nextInt(); while(t-->0) { int m = in.nextInt(), n = in.nextInt(); map = new HashMap<>(); char b[] = in.next().toCharArray(); char a[] = in.next().toCharArray(); for(int i=0;i<m;i++){ map.put(b[i], new ArrayList<>()); } for(int i=0;i<m;i++){ map.get(b[i]).add(i); } int max[] = new int[n]; int min[] = new int[n]; int prev = m; for(int i=n-1;i>=0;i--){ char ch = a[i]; int x = binarySearchLeft(ch,prev); max[i] = x; prev = x; } prev = -1; for(int i=0;i<n;i++){ char ch = a[i]; int x = binarySearchRight(ch,prev); min[i] = x; prev = x; } int mx = 0; for(int i=0;i<n-1;i++){ mx = Math.max(mx, max[i+1]-min[i]); } out.println(mx); } out.flush(); } static HashMap<Character, ArrayList<Integer>> map; static int binarySearchLeft(char c, int x){ int low = 0, mid, high = map.get(c).size()-1; ArrayList<Integer> a = map.get(c); while(low<=high){ mid = (low+high)/2; if(a.get(mid)>=x) high = mid-1; else low = mid+1; } return a.get(high); } static int binarySearchRight(char c, int x){ int low = 0, mid, high = map.get(c).size()-1; ArrayList<Integer> a = map.get(c); while(low<=high){ mid = (low+high)/2; if(a.get(mid)<=x) low = mid+1; else high = mid-1; } return a.get(low); } 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) {} return st.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } } static final Random random = new Random(); static void ruffleSort(int[] a){ int n = a.length; for(int i=0;i<n;i++){ int j = random.nextInt(n), temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
f2549090796d929d48d4df2d588ad800
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Practice { public static void main(String[] args) throws Exception { Scanner s = new Scanner(System.in); int n = s.nextInt(); int m = s.nextInt(); String str = s.next(); String tp = s.next(); int[]lower=new int[n]; int[]upper =new int[n]; int curr=0; for(int i=0;i<n;i++) { if(curr==m) { break; } if(str.charAt(i)==tp.charAt(curr)) { lower[curr++]=i; } } curr=m-1; for(int i=n-1;i>=0;i--) { if(curr==-1) { break; } if(str.charAt(i)==tp.charAt(curr)) { upper[curr--]=i; } } int ans=0; for(int i=0;i<m-1;i++) { ans=Math.max(ans, upper[i+1]-lower[i]); } System.out.println(ans); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
6362714cc2dde6e1a88daf96ba955ab0
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class c704c { //By shwetank_verma public static void main(String[] args) { FastReader sc=new FastReader(); try{ int q=1; // q=sc.nextInt(); while(q-->0){ int n=sc.nextInt(); int m=sc.nextInt(); String s=sc.next(); String t=sc.next(); char ch1[]=s.toCharArray(); char ch2[]=t.toCharArray(); int start[]=new int[m]; int idx=0; for(int i=0;i<m;i++) { while(idx<n&&ch1[idx]!=ch2[i]) { idx++; } start[i]=idx++; } idx=n-1; int end[]=new int[m]; for(int i=m-1;i>=0;i--) { while(idx>=0&&ch1[idx]!=ch2[i]) idx--; end[i]=idx--; } int ans=Integer.MIN_VALUE; for(int i=0;i<m-1;i++) { ans=Math.max(ans, end[i+1]-start[i]); } System.out.println(ans); } }catch(Exception e){ return; } } 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 int mod=1000000007; static boolean primes[]=new boolean[1000007]; static ArrayList<Integer> b=new ArrayList<>(); static boolean seive(int n){ Arrays.fill(primes,true); primes[0]=primes[1]=false; for(int i=2;i*i<=n;i++){ if(primes[i]==true){ for(int p=i*i;p<=n;p+=i){ primes[p]=false; } } } if(n<1000007){ for(int i=2;i<=n;i++) { if(primes[i]) b.add(i); } return primes[n]; } return false; } static int gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b); } static long GCD(long a,long b){ if(b==0) return a; return GCD(b,a%b); } static ArrayList<Integer> segseive(int l,int r){ ArrayList<Integer> isprime=new ArrayList<Integer>(); boolean p[]=new boolean[r-l+1]; Arrays.fill(p, true); for(int i=0;b.get(i)*b.get(i)<=r;i++) { int currprime=b.get(i); int base=(l/currprime)*currprime; if(base<l) { base+=currprime; } for(int j=base;j<=r;j+=currprime) { p[j-l]=false; } if(base==currprime) { p[base-l]=true; } } for(int i=0;i<=r-l;i++) { if(p[i]) isprime.add(i+l); } return isprime; } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
7027403d67a4192d9b4e87beb1a69b82
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.Arrays; import java.util.Scanner; public class code4 { // { // static class pair implements Comparable<pair>{ // int x;int y; // // public pair(int x,int y){ // this.x=x; // this.y=y; // } // // // @Override // public int compareTo(pair o) { // return this.x-o.x; // } // } static long[] t; static Scanner sc = new Scanner(System.in); public static void main(String[] args) { // int t = sc.nextInt(); // while (t-- > 0) { solve(); // // } } static void solve() { int n=sc.nextInt(); int m=sc.nextInt(); String s=sc.next(); String t=sc.next(); int[] left=new int[m]; int[] right=new int[m]; int i=0; for (int j = 0; j < n && i<m; j++) { if(s.charAt(j)==t.charAt(i)){ left[i++]=j; } } i=m-1; for (int j = n-1; j>=0 && i>=0; j--) { if(s.charAt(j)==t.charAt(i)){ right[i--]=j; } } int ans=0; for (int j = 0; j < m-1; j++) { ans=Math.max(ans,right[j+1]-left[j]); } System.out.println(ans); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
8192d97a04d4f5c7e7d415359961f1e2
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; public class Main { public static int INF = 0x3f3f3f3f; public static int mod = 1000000007; public static void main(String args[]){ try { PrintWriter o = new PrintWriter(System.out); FastScanner sc = new FastScanner(); boolean multiTest = false; if(multiTest) { int t = sc.nextInt(), loop = 0; while (loop < t) { loop++; solve(o, sc); } } else solve(o, sc); o.close(); } catch (Exception e){} } static void solve(PrintWriter o, FastScanner sc){ try { int n = fReader.nextInt(), m = fReader.nextInt(), res = 1; String s = fReader.nextLine(); String t = fReader.nextLine(); int[] last = new int[m]; int right = n-1, left = 0; for(int i=m-1;i>=0;i--){ while(s.charAt(right) != t.charAt(i)){ right--; } last[i] = right; right--; } for(int i=0;i<m-1;i++){ while(s.charAt(left) != t.charAt(i)){ left++; } res = Math.max(res, last[i+1] - left); left++; } o.println(res); } catch (Exception e){ e.printStackTrace(); } } public static int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); } public static void reverse(int[] array){ reverse(array, 0 , array.length-1); } public static void reverse(int[] array, int left, int right) { if (array != null) { int i = left; for(int j = right; j > i; ++i) { int tmp = array[j]; array[j] = array[i]; array[i] = tmp; --j; } } } public static boolean doubleEqual(double d1, double d2){ if(Math.abs(d1-d2) < 1e-6){ return true; } return false; } public static long fac(int n){ long ret = 1; while(n > 0){ ret = ret * n % mod; n--; } return ret; } public static long qpow(int n, int m){ long n_ = n, ret = 1; while(m > 0){ if((m&1) == 1){ ret = ret * n_ % mod; } m >>= 1; n_ = n_ * n_ % mod; } return ret; } static class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} } public static class fReader { public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); public static StringTokenizer tokenizer = new StringTokenizer(""); public static String next() throws IOException{ while(!tokenizer.hasMoreTokens()){ tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static int nextInt() throws IOException{ return Integer.parseInt(next()); } public static Long nextLong() throws IOException{ return Long.parseLong(next()); } public static double nextDouble() throws IOException{ return Double.parseDouble(next()); } public static String nextLine() throws IOException{ return reader.readLine(); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
49ddc98be2699748bd6883a3e1b8bf0c
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Comparator; import java.util.StringTokenizer; public class C1492 { public static void main(String[] args) { FastScanner sc = new FastScanner(System.in); FastPrintStream out = new FastPrintStream(System.out); int n = sc.nextInt(); int m = sc.nextInt(); String s = sc.next(); String t = sc.next(); int[] pref = new int[n], suff = new int[n]; int r = 0; for (int i = 0; i < n; i++) { if (r < t.length()) { if (s.charAt(i) == t.charAt(r)) { r++; } } pref[i] = r; } int l = m - 1; for (int i = n - 1; i >= 0; i--) { if (l >= 0) { if (s.charAt(i) == t.charAt(l)) { l--; } } suff[i] = m - l - 1; } int ans = -1; for (int i = 0; i < m - 1; i++) { int leftMost = 0; l = 0; r = n - 1; while (l <= r) { int q = l + (r - l) / 2; if (pref[q] >= i + 1) { leftMost = q; r = q - 1; } else { l = q + 1; } } int rightMost = 0; l = 0; r = n - 1; while (l <= r) { int q = l + (r - l) / 2; if (suff[q] >= m - i - 1) { rightMost = q; l = q + 1; } else { r = q - 1; } } ans = Math.max(ans, rightMost - leftMost); } out.println(ans); out.flush(); } static int[] readIntArray(int n, FastScanner sc) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } return a; } static int[] sorted(int[] array) { ArrayList<Integer> list = new ArrayList<>(array.length); for (int val : array) { list.add(val); } list.sort(Comparator.naturalOrder()); return list.stream().mapToInt(val -> val).toArray(); } static int[] sortedReverse(int[] array) { ArrayList<Integer> list = new ArrayList<>(array.length); for (int val : array) { list.add(val); } list.sort(Comparator.reverseOrder()); return list.stream().mapToInt(val -> val).toArray(); } static long[] sort(long[] array) { ArrayList<Long> list = new ArrayList<>(array.length); for (long val : array) { list.add(val); } list.sort(Comparator.naturalOrder()); return list.stream().mapToLong(val -> val).toArray(); } static long[] sortedReverse(long[] array) { ArrayList<Long> list = new ArrayList<>(array.length); for (long val : array) { list.add(val); } list.sort(Comparator.reverseOrder()); return list.stream().mapToLong(val -> val).toArray(); } private static class FastPrintStream { private final BufferedWriter writer; public FastPrintStream(PrintStream out) { writer = new BufferedWriter(new OutputStreamWriter(out)); } public void print(char c) { try { writer.write(Character.toString(c)); } catch (IOException e) { e.printStackTrace(); } } public void print(int val) { try { writer.write(Integer.toString(val)); } catch (IOException e) { e.printStackTrace(); } } public void print(long val) { try { writer.write(Long.toString(val)); } catch (IOException e) { e.printStackTrace(); } } public void print(double val) { try { writer.write(Double.toString(val)); } catch (IOException e) { e.printStackTrace(); } } public void print(String val) { try { writer.write(val); } catch (IOException e) { e.printStackTrace(); } } public void println(char c) { print(c + "\n"); } public void println(int val) { print(val + "\n"); } public void println(long val) { print(val + "\n"); } public void println(double val) { print(val + "\n"); } public void println(String str) { print(str + "\n"); } public void println() { print("\n"); } public void flush() { try { writer.flush(); } catch (IOException e) { e.printStackTrace(); } } } private static class FastScanner { private final BufferedReader br; private StringTokenizer st; public FastScanner(InputStream inputStream) { br = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public byte nextByte() { return Byte.parseByte(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output