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
8a4cabc282ae6353ec5289faa059aaeb
train_002.jsonl
1379691000
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
256 megabytes
import java.util.*; import java.util.Map.Entry; import java.io.*; import java.awt.Point; import java.math.BigInteger; import static java.lang.Math.*; public class Codeforces_Solution_C implements Runnable{ final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException{ if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException{ while(!tok.hasMoreTokens()){ try{ tok = new StringTokenizer(in.readLine()); }catch (Exception e){ return null; } } return tok.nextToken(); } int readInt() throws IOException{ return Integer.parseInt(readString()); } long readLong() throws IOException{ return Long.parseLong(readString()); } double readDouble() throws IOException{ return Double.parseDouble(readString()); } public static void main(String[] args){ new Thread(null, new Codeforces_Solution_C(), "", 128 * (1L << 20)).start(); } long timeBegin, timeEnd; void time(){ timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } long memoryTotal, memoryFree; void memory(){ memoryFree = Runtime.getRuntime().freeMemory(); System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB"); } void debug(Object... objects){ if (DEBUG){ for (Object o: objects){ System.err.println(o.toString()); } } } public void run(){ try{ timeBegin = System.currentTimeMillis(); memoryTotal = Runtime.getRuntime().freeMemory(); init(); solve(); out.close(); time(); memory(); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } boolean DEBUG = false; int gcd (int a, int b) { if (b == 0) return a; else return gcd (b, a % b); } void solve() throws IOException{ int n = readInt(); int[] a = new int[n]; for (int i=0; i<n; i++) a[i] = readInt(); Arrays.sort(a); int gcd = gcd(a[0],a[1]); for (int i=2; i<n; i++) gcd = gcd(gcd,a[i]); int max = a[n-1]; out.println((max/gcd-n) % 2 == 1? "Alice" : "Bob"); } }
Java
["2\n2 3", "2\n5 3", "3\n5 6 7"]
2 seconds
["Alice", "Alice", "Bob"]
NoteConsider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
Java 6
standard input
[ "number theory", "games", "math" ]
3185ae6b4b681a10a21d02e67f08fd19
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
1,600
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
standard output
PASSED
bd4901b128d6f3be8bfeed8dce5e5a1d
train_002.jsonl
1379691000
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
256 megabytes
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class C { public static void main(String[] args) { C solver = new C(); solver.solve(); } private void solve() { Scanner sc = new Scanner(System.in); // sc = new Scanner("2\n" + // "2 3\n"); // sc = new Scanner("2\n" + // "5 3\n"); // sc = new Scanner("3\n" + // "5 6 7\n"); // sc = new Scanner("2\n" + // "6 2\n"); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } int max = a[0]; for (int i = 1; i < n; i++) { max = Math.max(max, a[i]); } int gcd = gcd(a[0], a[1]); for (int i = 2; i < n; i++) { gcd = gcd(gcd, a[i]); } int k = max / gcd - n; System.out.println(k % 2 == 0 ? "Bob" : "Alice"); } public static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } }
Java
["2\n2 3", "2\n5 3", "3\n5 6 7"]
2 seconds
["Alice", "Alice", "Bob"]
NoteConsider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
Java 6
standard input
[ "number theory", "games", "math" ]
3185ae6b4b681a10a21d02e67f08fd19
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
1,600
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
standard output
PASSED
5f2d8d218a14eeb825b969084d3205c7
train_002.jsonl
1379691000
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
256 megabytes
/* @@@@@@@@@@@@@@@@@@@@@@@ * @@@ Doston Akhmedov @@@ * @@@@@@@@@@@@@@@@@@@@@@@ */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.HashSet; import java.util.Locale; import java.util.StringTokenizer; public class r_201_c { static StringTokenizer st; static BufferedReader br; static PrintWriter pw; public static void main(String[] args) throws IOException { Locale.setDefault(Locale.US); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter( System.out))); int n=nextInt(); int a[]=new int[n+1],max=Integer.MIN_VALUE; for (int i = 1; i <=n; i++) { a[i]=nextInt(); max=Math.max(max, a[i]); } int e=a[1]; for (int i = 2; i <=n; i++) e=gcd(e,a[i]); int ans=max/e-n; pw.print(ans%2==1?"Alice":"Bob"); pw.close(); } private static int gcd(int a, int b) { return b==0?a:gcd(b,a%b); } private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static double nextDouble() throws IOException { return Double.parseDouble(next()); } private static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } }
Java
["2\n2 3", "2\n5 3", "3\n5 6 7"]
2 seconds
["Alice", "Alice", "Bob"]
NoteConsider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
Java 6
standard input
[ "number theory", "games", "math" ]
3185ae6b4b681a10a21d02e67f08fd19
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
1,600
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
standard output
PASSED
59946c620ecdf99b70d7473e9174175b
train_002.jsonl
1379691000
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class C { static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int[] nums = new int[N]; for (int i = 0; i < N; i++) nums[i] = Integer.parseInt(st.nextToken()); int g = nums[0]; for (int i = 1; i < N; i++) g = gcd(g, nums[i]); for (int i = 0; i < N; i++) nums[i] /= g; int max = -1; for (int i = 0; i < N; i++) max = Math.max(max, nums[i]); int c = max - N; if (c % 2 == 1) System.out.println("Alice"); else System.out.println("Bob"); } }
Java
["2\n2 3", "2\n5 3", "3\n5 6 7"]
2 seconds
["Alice", "Alice", "Bob"]
NoteConsider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
Java 6
standard input
[ "number theory", "games", "math" ]
3185ae6b4b681a10a21d02e67f08fd19
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
1,600
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
standard output
PASSED
45fc299440af84429a65cd828626a296
train_002.jsonl
1379691000
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
256 megabytes
import java.util.Scanner; public class Contest201_div2_q3__Alice_and_Bob { public static void main(String ... args) throws Exception { // long time = System.currentTimeMillis(); Scanner in = new Scanner(System.in); int count = in.nextInt(); int max = 0; int gcd = 0; for(int i=0; i<count; ++i) { int a = in.nextInt(); if(a>max) max = a; gcd = gcd(a, gcd); } if((max/gcd-count)%2==0) System.out.println("Bob"); else System.out.println("Alice"); } public static int gcd(int a, int b) { if(a==b) return a; else if(a==0) return b; else if(b==0) return a; else if(a>b) return gcd(a%b, b); else return gcd(b%a, a); } }
Java
["2\n2 3", "2\n5 3", "3\n5 6 7"]
2 seconds
["Alice", "Alice", "Bob"]
NoteConsider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
Java 6
standard input
[ "number theory", "games", "math" ]
3185ae6b4b681a10a21d02e67f08fd19
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
1,600
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
standard output
PASSED
1a77c927a311d19951f8987fb8d14290
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.io.*; import java.util.*; public class Main { static BufferedReader br ; public static void main(String[] args) throws Exception{ // write your code here input input = new input(); int n = input.in(); int x=0;int y=0; x++; x--; y++; long a[] = new long[n]; long b[] = new long[n]; y--; TreeMap<Long,Long> r = new TreeMap<>(); TreeMap<Long,Long> l = new TreeMap<>(); HashMap<Long ,Long> ah = new HashMap<>(); HashMap<Long ,Long>bh = new HashMap<>(); x++; x--; y++; String read[] = input.sarrin(); for(int k =0;k<n;k++) { a[k] = input.Lin(read[k]); ah.put(a[k],(long)k); } x++; x--; y++; read = input.sarrin(); x++; x--; y++; for(int k =0;k<n;k++){ b[k] = input.Lin(read[k]); bh.put(b[k],(long)k); } x++; x--; y++; long rmax=0; long lmax=0; for(int k =0;k<n;k++){ x++; x--; y++; long val = a[k]; long bi = bh.get(val); if(bi>k){ long lshift= k+(n-bi); long rshift= bi-k; long rf = r.containsKey(rshift)?r.get(rshift):0; r.put(rshift,rf+1); long lf = l.containsKey(lshift)?l.get(lshift):0; l.put(lshift,lf+1); if(lmax>lf+1){ lmax=lf+1; } if(rmax>rf+1){ rmax=rf+1; } } else if(bi<k){ x++; y--; x++; long lshift = k-bi; long rshift = n-1-k+(bi+1); long rf = r.containsKey(rshift)?r.get(rshift):0; r.put(rshift,rf+1); long lf = l.containsKey(lshift)?l.get(lshift):0; l.put(lshift,lf+1); if(lmax>lf+1){ lmax=lf+1; } if(rmax>rf+1){ rmax=rf+1; } } else{ long lf = l.containsKey(0L)?l.get(0L):0; l.put(0L,lf+1); long rf = r.containsKey(0L)?r.get(0L):0; r.put(0L,rf+1); if(lmax>lf+1){ lmax=lf+1; } if(rmax>rf+1){ rmax=rf+1; } } } x++; x--; y++; for(Map.Entry entry:r.entrySet()){ long f = (long)entry.getValue(); rmax = Math.max(f,rmax); } x++; x--; y++; for(Map.Entry entry:l.entrySet()){ long f = (long)entry.getValue(); lmax = Math.max(f,lmax); } System.out.println(Math.max(rmax,lmax)); } } class input{ BufferedReader br ; public input(){ br = new BufferedReader(new InputStreamReader(System.in)); } int in() throws Exception{ return Integer.parseInt(br.readLine()); } long Lin() throws Exception{ return Long.parseLong(br.readLine()); } long Lin(String num) throws Exception{ return Long.parseLong(num); } String sin() throws Exception{ return br.readLine(); } String [] sarrin() throws Exception{ return br.readLine().split(" "); } String [] regexSplit() throws Exception{ return br.readLine().split(" ?(?<!\\G)((?<=[^\\p{Punct}])(?=\\p{Punct})|\\b) ?"); } int in(String num) throws Exception{ return Integer.parseInt(num); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
84d834c958abc726a434c69b6f5e43fd
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; import java.io.*; //Captain on duty! public class Main { static void compare(Main.pair a[], int n) { Arrays.sort(a, new Comparator<Main.pair>() { @Override public int compare(Main.pair p1, Main.pair p2) { return p1.f - p2.f; } }); } public static boolean checkPalindrome(String s) { // reverse the given String String reverse = new StringBuffer(s).reverse().toString(); // check whether the string is palindrome or not if (s.equals(reverse)) return true; else return false; } static class pair implements Comparable { int f; int s; pair(int fi, int se) { f = fi; s = se; } public int compareTo(Object o)//desc order { pair pr = (pair) o; if (s > pr.s) return -1; if (s == pr.s) { if (f > pr.f) return 1; else return -1; } else return 1; } public boolean equals(Object o) { pair ob = (pair) o; if (o != null) { if ((ob.f == this.f) && (ob.s == this.s)) return true; } return false; } public int hashCode() { return (this.f + " " + this.s).hashCode(); } } public static boolean palin(int l, int r, char[] c) { while (l <= r) { if (c[l] != c[r]) return false; l++; r--; } return true; } 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 long hcf(long a, long b) { long t; while (b != 0) { t = b; b = a % b; a = t; } return a; } public static boolean isPrime(long n) { if (n <= 1) return false; // Check from 2 to n-1 for (int i = 2; i <= Math.sqrt(n) + 1; i++) if (n % i == 0) return false; return true; } public static String reverse(String str) { String str1 = ""; for (int i = 0; i < str.length(); i++) { str1 = str1 + str.charAt(str.length() - i - 1); } return str1; } public static double fact(long a) { if (a == 1) return 1; else return a * fact(a - 1); } static boolean isPerfectSquare(double x) { // Find floating point value of // square root of x. double sr = Math.sqrt(x); // If square root is an integer return ((sr - Math.floor(sr)) == 0); } public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 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()); } double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } } public static void main(String[] args) { FastReader s = new FastReader(); //System.out.println("Forces Babyy!!"); int n=s.nextInt(); int[] a=new int[n]; int[] b=new int[n]; int[] pos1=new int[n]; int[] pos2=new int[n]; for(int i=0;i<n;i++) { a[i]=s.nextInt(); pos1[a[i]-1] = i+1; } for(int i=0;i<n;i++) { b[i]=s.nextInt(); pos2[b[i]-1] = (i+1); } int[] ans=new int[n]; for(int i=0;i<n;i++) { ans[i] = pos1[i]-pos2[i]; if(ans[i]<0) ans[i]+=n; } Arrays.sort(ans); /*for(int i=0;i<n;i++) System.out.print(ans[i] + " "); System.out.println();*/ int c=1; int max=1; for(int i=0;i<n-1;i++) { if(ans[i]==ans[i+1]) c++; else c=1; max = Math.max(max,c); } System.out.println(max); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
75ac490ecce79e66da4ed16f3778381b
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner s = new Scanner(System.in); int n =s.nextInt(); int map[] = new int[n]; int cnt[] = new int[n]; int [] arr = new int[n]; int [] arr1 = new int[n]; for(int i=0 ; i< n; i++){ int p=s.nextInt()-1; arr[i]=p; map[p]=i; } for(int i=0 ; i< n; i++){ int p = s.nextInt()-1; arr1[i]=p; } for(int i=0 ; i<n ; i++){ int q = map[arr1[i]] - i; if(q<0) cnt[n+q]++; else cnt[q]++; } int max = 0; for(int i=0 ; i<n ; i++) if(cnt[i]>max) max=cnt[i]; System.out.println(max); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
2835f6815aef37067131eee77a783dc0
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner s = new Scanner(System.in); int n =s.nextInt(); int map[] = new int[n]; int cnt[] = new int[n]; int [] arr = new int[n]; int [] arr1 = new int[n]; for(int i=0 ; i< n; i++){ int p=s.nextInt()-1; arr[i]=p; map[p]=i; } for(int i=0 ; i< n; i++){ int p = s.nextInt()-1; arr1[i]=p; } for(int i=0 ; i<n ; i++){ int q = map[arr1[i]] ; if(i<=q) cnt[q-i]++; else cnt[n-i+q]++; } int max = 0; for(int i=0 ; i<n ; i++) if(cnt[i]>max) max=cnt[i]; System.out.println(max); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
48cfe7ca87c6a916658dfcafc0c41bb7
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.Scanner; import java.util.HashMap; public class codeforces1365C { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n=s.nextInt(),i,x,m=1,h; int[] a = new int[n]; int[] b = new int[n]; for(i=0;i<n;i++) a[s.nextInt()-1]=i; for(i=0;i<n;i++) b[s.nextInt()-1]=i; HashMap<Integer,Integer> hm = new HashMap<>(); for(i=0;i<n;i++) { x=a[i]-b[i]; if(x<0) x=x+n; if(hm.get(x)!=null) { h=hm.get(x); hm.replace(x,h,h+1); m=Math.max(m,h+1); } else hm.put(x,1); } System.out.print(m); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
51fc55927848dd84559f5dad36fb3a87
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.Scanner; public final class RatationMatching { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } int pos[] = new int[n + 1]; for (int i = 0; i < n; i++) { int x = sc.nextInt(); pos[x] = i; } int cnt[] = new int[n]; for (int i = 0; i < n; i++) { int index = pos[a[i]]; if (index >= i) { cnt[index - i]++; } else { cnt[index + (n - i)]++; } } int max = 0; for (int i = 0; i < n; i++) { max = Math.max(max, cnt[i]); } System.out.println(max); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
5cba2fea53e00bc44cf9a8d3183e8c7c
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; int[] b = new int[n]; int[] pozA = new int[n + 1]; Map<Integer, Integer> mp = new HashMap<>(); for (int i = 0; i < n; i++) { a[i] = in.nextInt(); pozA[a[i]] = i; } int mx = 0; for (int i = 0; i < n; i++) { b[i] = in.nextInt(); int dif; if (i < pozA[b[i]]) { dif = pozA[b[i]] - i; } else { dif = pozA[b[i]] - i + n; } Integer val = 0; if (mp.containsKey(dif)) { val = mp.remove(dif); } val++; if (val > mx) mx = val; mp.put(dif, val); } System.out.println(mx); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
dfba24b9ad405791ca235c16541ea65a
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; import java.io.*; public class Sol { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int N = sc.nextInt(); int[] A = new int[N+1]; for (int i = 0; i < N; i++) A[sc.nextInt()] = i; int[] counts = new int[N]; for (int i = 0; i < N; i++) { int B = sc.nextInt(); int d = A[B] - i; if (d < 0) d += N; counts[d]++; } int ans = 0; for (int i = 0; i < N; i++) { ans = Math.max(ans, counts[i]); } // d235938f0ab655e8c5832b2e554f5aa4d979f3744b24a4619b802b64bd0f8f8f out.println(ans); sc.close(); out.close(); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
cb68fac90492268a5ecedceb3e6dec3a
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; import java.io.*; public class Sol { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int N = sc.nextInt(); int[] A = new int[N+1]; for (int i = 0; i < N; i++) A[sc.nextInt()] = i; int[] counts = new int[N]; for (int i = 0; i < N; i++) { int B = sc.nextInt(); int d = A[B] - i; if (d < 0) d += N; counts[d]++; } int ans = 0; for (int i = 0; i < N; i++) { ans = Math.max(ans, counts[i]); } out.println(ans); sc.close(); out.close(); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
e7ace43522a28b38d302fbeba8c51dab
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; import java.io.*; public class Main{ static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } 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 class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.nextInt(); return array; } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int mod = (int)(1e9+7); public static long pow(long a,long b) { long ans = 1; while(b> 0) { if((b & 1)==1){ ans = (ans*a) % mod; } a = (a*a) % mod; b = b>>1; } return ans; } static int n; public static int adjust(int i) { i = i%n; if(i<0) return i+=n; else return i; } public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); //IOUtils io = new IOUtils(); n = in.nextInt(); int[] a = in.nextIntArray(n); int[] b = in.nextIntArray(n); // System.out.println(Arrays.toString(a)); // System.out.println(Arrays.toString(b)); Map<Integer,Integer> map = new HashMap<>(); for(int i=0;i<n;i++) { map.put(b[i],i); } int res = 0; int[] arr = new int[n+1]; // System.out.println(map); for(int i=0;i<n;i++) { int offset = i - map.get(a[i]); // out.printLine(offset); if(offset<0) offset+=n; arr[offset]++; } for(int i:arr) res = Math.max(res,i); out.printLine(res); out.flush(); out.close(); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
116f3787b75612beb67f38cab44a568e
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.io.*; import java.util.*; public class Round648C { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numIndex = sc.nextInt(); int[] original = new int[numIndex]; for (int i = 0; i < numIndex; i++) { int tempI = sc.nextInt()-1; original[tempI]=i; } // System.out.println(Arrays.toString(original)); int[] score = new int[numIndex]; int max = 0; for (int i = 0; i < numIndex; i++) { int tempNum = sc.nextInt()-1; // System.out.println(original[tempNum]-i); if(original[tempNum]-i >= 0){ score[original[tempNum]-i]++; if(score[original[tempNum]-i]>max){ max = score[original[tempNum]-i]; } }else{ score[numIndex-i+original[tempNum]]++; if(score[numIndex-i+original[tempNum]] > max){ max = score[numIndex-i+original[tempNum]]; } } } // System.out.println(Arrays.toString(score)); System.out.println(max); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
839c68c6aea8dc23b959e311f2fcdf2a
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; import java.io.*; public class Main { // File file = new File("input.txt"); // Scanner in = new Scanner(file); // PrintWriter out = new PrintWriter(new FileWriter("output.txt")); public static void main(String[] args) { // Scanner in = new Scanner(System.in); FastReader in = new FastReader(); // int test_cases = in.nextInt(); // while (test_cases -- > 0){ int n = in.nextInt(); ArrayList<Integer> a = new ArrayList<>(), b = new ArrayList<>(); int[] posa = new int[n+1], posb = new int[n+1]; for(int i = 0; i<n; i++) { a.add(in.nextInt()); posa[a.get(i)] = i; } for(int i = 0; i<n; i++) { b.add(in.nextInt()); posb[b.get(i)] = i; } int[] cnt = new int[n]; for(int i = 1; i<=n; i++) { cnt[(posb[i] - posa[i] + n) % n]++; } int ans = 0; for(int e: cnt) { ans = Math.max(ans, e); } System.out.println(ans); // } } private static int gcd(int a, int b) { if(b==0) return a; return gcd(b, a%b); } 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\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
f479db4a947a520e59a6c2b0dea50516
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; import java.io.*; public class TestClass { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); ArrayList<Integer> arr1 = new ArrayList<>(n); ArrayList<Integer> arr2 = new ArrayList<>(n); StringTokenizer st1 = new StringTokenizer(br.readLine()); int ans = Integer.MIN_VALUE; HashMap<Integer,Integer> map = new HashMap<>(); for(int i = 0; i<n; i++){ int val = Integer.parseInt(st1.nextToken()); arr1.add(val); map.put(val,i); } StringTokenizer st2 = new StringTokenizer(br.readLine()); for(int i = 0; i<n; i++){ arr2.add(Integer.parseInt(st2.nextToken())); } int[] right = new int[n]; for(int i = 0; i<n; i++){ int val = arr2.get(i); int ind = map.get(val); map.remove(val); right[(ind-i+n)%n]++; } for(int i = 0; i<n; i++){ ans = Math.max(ans,right[i]); } System.out.println(ans); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
3e7441bb06e64c4934f384592e4a4a97
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; import java.io.*; public class TestClass { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); ArrayList<Integer> arr1 = new ArrayList<>(n); ArrayList<Integer> arr2 = new ArrayList<>(n); StringTokenizer st1 = new StringTokenizer(br.readLine()); int ans = Integer.MIN_VALUE; HashMap<Integer,Integer> map = new HashMap<>(); for(int i = 0; i<n; i++){ int val = Integer.parseInt(st1.nextToken()); arr1.add(val); map.put(val,i); } StringTokenizer st2 = new StringTokenizer(br.readLine()); for(int i = 0; i<n; i++){ arr2.add(Integer.parseInt(st2.nextToken())); } int[] right = new int[n]; for(int i = 0; i<n; i++){ int val = arr2.get(i); int ind = map.get(val); right[(ind-i+n)%n]++; } for(int i = 0; i<n; i++){ ans = Math.max(ans,right[i]); } System.out.println(ans); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
cd8a5adb755f9de346c5ec8ba108c095
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; import java.io.*; public class rotationmatching { public static void main(String[]args)throws Exception{ Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); ArrayList<Integer>list1 = new ArrayList<>(); ArrayList<Integer>list2 = new ArrayList<>(); int[]position = new int[n]; int[]diff = new int[n]; int max = Integer.MIN_VALUE; for(int i = 0; i < n; i++){ list1.add(sc.nextInt()); } for(int i = 0; i < n; i++){ list2.add(sc.nextInt()); } for(int i = 0; i < n; i++){ position[list1.get(i)-1] = i; } for(int i = 0; i < n; i++){ int difference = position[list2.get(i)-1] - i; if(difference<0){ difference+=n; } diff[difference]++; } for(int i = 0; i < n; i++){ max =Integer.max(diff[i], max); } out.print(max); out.close(); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
ef73ace0a2c4f9a8c4456b3ea386d0ae
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; import java.io.*; public class C{ private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main (String[] args) throws IOException{ int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); HashMap<Integer, Integer> a = new HashMap<>(); for(int i=0;i<n;i++){ a.put(Integer.parseInt(st.nextToken()), i); } int b[] = new int[n]; st = new StringTokenizer(br.readLine()); for(int i=0;i<n;i++){ b[i] = Integer.parseInt(st.nextToken()); } HashMap<Integer, Integer> shifts = new HashMap<>(); for(int i=0;i<n;i++){ int idx = a.get(b[i]); int add = 0; if(idx == i){ if(shifts.containsKey(0)){ shifts.put(0, shifts.get(0) + 1); }else{ shifts.put(0, 1); } continue; } if(idx<i){ add += i - idx; }else{ add += i; add += n - idx; } if(shifts.containsKey(add)){ shifts.put(add, shifts.get(add) + 1); }else{ shifts.put(add, 1); } } int max = 0; for(int curr : shifts.values()){ if(curr>max){ max = curr; } } bw.write(max + " "); bw.newLine(); bw.flush(); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
d3d2905125a65a4d42736c3c74a25ed3
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.Scanner; public class Problem1365c { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; int[] b = new int[n]; for(int i = 0; i < n; i++) { int x = sc.nextInt(); a[x-1] = i; } for(int i = 0; i < n; i++) { int x = sc.nextInt(); b[Math.abs(i - a[x-1] + n) % n]++; } int ans = 0; for(int i = 0; i < n; i++) { if(ans < b[i]) { ans = b[i]; } } System.out.println(ans); sc.close(); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
0e791d52ec40cc2f959e27c52a3297de
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.Arrays; import java.util.Scanner; import javax.lang.model.util.ElementScanner6; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int ans = 0; int[] a = new int[n+1], b = new int[n+1]; int[] pos = new int[n+1]; int[] offset = new int[n + 1]; for (int i = 1; i <= n; i++) { a[i] = in.nextInt(); pos[a[i]] = i; } for (int i = 1; i <= n; i++) { b[i] = in.nextInt(); } for (int i = 1; i <= n; i++) { int cur = pos[b[i]] - i; if (cur < 0) { cur += n; } offset[cur]++; } for (int i : offset) { ans = Math.max(ans, i); } System.out.println(ans); in.close(); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
0d1ae5c270b6f80d0fbc529f2d3fb736
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class code { public static void main(String[] args)throws IOException { FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int arr[] = new int[n]; HashMap<Integer, Integer> map1 = new HashMap<>(); HashMap<Integer, Integer> map2 = new HashMap<>(); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); if (!map1.containsKey(arr[i])) { map1.put(arr[i], i); } } int brr[] = new int[n]; for (int i = 0; i < n; i++) { brr[i] = sc.nextInt(); if (!map2.containsKey(brr[i])) { map2.put(brr[i], i); } } // System.out.println(map1); // System.out.println(map2); HashMap<Integer, Integer> map3 = new HashMap<>(); for (int i = 0; i < n; i++) { int index1 = map1.get(arr[i]); int index2 = map2.get(arr[i]); if (index1 <= index2) { if (!map3.containsKey(index2 - index1)) { map3.put(index2 - index1, 1); } else { map3.put(index2 - index1, map3.get(index2 - index1) + 1); } } else { int res = n - index1 + index2; if (!map3.containsKey(res)) { map3.put(res, 1); } else { map3.put(res, map3.get(res) + 1); } } } int max = 0; for (Map.Entry<Integer, Integer> m : map3.entrySet()) { if (m.getValue() > max) max = m.getValue(); } System.out.println(max); } static boolean arraySortedOrNot(int arr[], int n) { // Array has one or no element if (n == 0 || n == 1) return true; for (int i = 1; i < n; i++) // Unsorted pair found if (arr[i - 1] > arr[i]) return false; // No unsorted pair found return true; } } 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\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
88218ffc3ba75b35fa2d562a078c8e56
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.io.*; import java.util.*; public class MyClass { 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 fr = new FastReader(); int n = fr.nextInt(); int[] a = new int[n], b = new int[n]; for (int i = 0; i < n; i += 1) a[i] = fr.nextInt(); for (int i = 0; i < n; i += 1) b[i] = fr.nextInt(); int[] indexes = new int[n + 1]; for (int i = 0; i < n; i += 1) { indexes[a[i]] = i; } int[] counts = new int[n]; for (int i = 0; i < n; i += 1) { int diff =(i - indexes[b[i]] + n) % n; // System.out.println(diff); counts[diff] += 1; } int res = 0; for (int i = 0; i < n; i += 1) { res = Math.max(res, counts[i]); } System.out.println(res); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
e6b60372f8c9eb5377033ff30df60639
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] arrA = new int[n]; int[] arrB = new int[n]; HashMap<Integer,Integer> hashMap = new HashMap<>(); for(int i=0;i<n;i++){ arrA[i] = scan.nextInt(); } for(int i=0;i<n;i++){ arrB[i] = scan.nextInt(); hashMap.put(arrB[i],i); } HashMap<Integer,Integer> hashMapRight = new HashMap<>(); HashMap<Integer,Integer> hashMapLeft = new HashMap<>(); for(int i=0;i<n;i++){ int index = hashMap.get(arrA[i]); if(index>=i){ if(hashMapRight.containsKey(index - i)){ hashMapRight.put(index-i,hashMapRight.get(index-i) + 1); } else{ hashMapRight.put(index-i,1); } if(hashMapLeft.containsKey(i+n-index)){ hashMapLeft.put(i+n-index,hashMapLeft.get(i+n-index)+1); } else{ hashMapLeft.put(i+n-index,1); } }else{ if(hashMapRight.containsKey(n-i+index)){ hashMapRight.put(n-i+index,hashMapRight.get(n-i+index)+1); } else{ hashMapRight.put(n-i+index,1); } if(hashMapLeft.containsKey(i-index)){ hashMapLeft.put(i-index,hashMapLeft.get(i-index)+1); } else{ hashMapLeft.put(i-index,1); } } } int max = 0; for(Map.Entry me:hashMapRight.entrySet()){ //System.out.println(me.getKey()+" "+me.getValue()); if((int)me.getValue() > max){ max = (int)me.getValue(); } } //System.out.println("Left Side: "); for(Map.Entry me:hashMapLeft.entrySet()){ //System.out.println(me.getKey()+" "+me.getValue()); if((int)me.getValue() > max){ max = (int)me.getValue(); } } System.out.println(max); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
7d7211d2d1b1c6def4adbb986d511d92
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
// package com.ganesh.CodeForces.R648Div2; import java.util.Scanner; public class C { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; int[] b = new int[n]; int[] posA = new int[n+1]; int[] posB = new int[n+1]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); posA[a[i]] = i; } for (int i = 0; i < n; i++) { b[i] = sc.nextInt(); posB[b[i]] = i; } int[] cnt = new int[n]; int ans = 0; for (int i = 1; i <= n; i++) { cnt[(posB[i]-posA[i]+n)%n]++; } for (int i = 0; i < n; i++) { ans = Math.max(ans,cnt[i]); } System.out.println(ans); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
7d41c130117e677b73ba89a62fa5192e
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
//package cf; import java.io.*; import java.util.*; public class fs { static int p=1000000007; public static void main(String[] args) throws Exception { BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512); FastReader sc = new FastReader(); int n = sc.nextInt(); int ar[] = new int[n]; int br[] = new int[n]; HashMap<Integer,Integer> h1=new HashMap<>(); HashMap<Integer,Integer> h2=new HashMap<>(); for (int i = 0; i < n; i++) { ar[i] = sc.nextInt(); h1.put(ar[i],i); } for (int i = 0; i < n; i++) { br[i] = sc.nextInt(); h2.put(br[i],i); } HashMap<Integer,Integer> l=new HashMap<>(); HashMap<Integer,Integer> r=new HashMap<>(); int max=Integer.MIN_VALUE; for(int i=0;i<n;i++) { int lv=h2.get(ar[i])-h1.get(ar[i]); lv=lv<0?lv+n:lv; int rv=h1.get(ar[i])-h2.get(ar[i]); rv=rv<0?rv+n:rv; if(l.containsKey(lv)) { l.put(lv,l.get(lv)+1); max=Math.max(l.get(lv),max); } else { l.put(lv,1); max=Math.max(1,max); } if(r.containsKey(rv)) { r.put(rv,r.get(rv)+1); max=Math.max(r.get(rv),max); } else { r.put(rv,1); max=Math.max(1,max); } } out.write( max+ "\n"); out.flush(); } } /////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////// 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\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
4810bb8b815619680235a439919fbc0c
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Solution implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private 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); } } public static void main(String args[]) throws Exception { new Thread(null, new Solution(), "Main", 1 << 27).start(); } static class Pair { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x * 7 + (y * 3 + 5 * (y - x)); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } Pair other = (Pair) obj; if (x != other.x && y != other.y) { return false; } return true; } } static void sieveOfEratosthenes(int n) { //Prints prime nos till n boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) System.out.print(i + " "); } } public void run() { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n=in.nextInt(); int a[]=new int[n]; int b[]=new int[n]; HashMap<Integer,Integer> A=new HashMap<Integer, Integer>(); HashMap<Integer,Integer> B=new HashMap<Integer, Integer>(); for(int i=0;i<n;i++) { a[i]=in.nextInt(); A.put(a[i],i); } for(int i=0;i<n;i++) { b[i]=in.nextInt(); B.put(b[i],i); } for(int i=0;i<n;i++) { if(B.get(a[i])<A.get(a[i])) B.put(a[i], B.get(a[i])+n); } HashMap<Integer,Integer> C=new HashMap<Integer, Integer>(); for(int i=0;i<n;i++) { int x=(A.get(a[i])-B.get(a[i])); C.put(x,C.getOrDefault(x, 0)+1); } //w.println(C); Iterator I=C.entrySet().iterator(); int max=Integer.MIN_VALUE; while(I.hasNext()) { Map.Entry M=(Map.Entry)I.next(); if(max<(int)M.getValue()) max=(int)M.getValue(); } w.println(max); w.flush(); w.close(); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
31049b4f5ecf63faa83b598a8480e76e
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*;import java.io.*;import java.math.*; public class RotationMatching { public static void process()throws IOException { int n = ni(); int[] a = nai(n); int[] b = nai(n); int[] p = new int[n+1]; int[] q = new int[n+1]; ArrayList<Integer> l1 = new ArrayList<>(); ArrayList<Integer> l2 = new ArrayList<>(); int[] dp1 = new int[1000001]; int[] dp2 = new int[1000001]; int ans = 0; for(int i = 0; i < n; i++){ p[a[i]] = i; q[b[i]] = i; } for(int i = 1; i <= n; i++){ if(q[i] > p[i]){ l1.add(p[i] - 0 + n-q[i]); l2.add(q[i] - p[i]); } else{ l1.add(p[i]-q[i]); l2.add(n-p[i] + q[i]-0); } } for(int i = 0; i < l1.size(); i++){ dp1[l1.get(i)]++; dp2[l2.get(i)]++; } for(int i = 0; i < l1.size(); i++){ ans = Math.max(ans, dp1[l1.get(i)]); ans = Math.max(ans, dp2[l2.get(i)]); } pn(ans); } 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(); while(t-->0) {process();} out.flush(); System.err.println(System.currentTimeMillis()-s+"ms"); out.close(); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static void pni(Object o){out.println(o);out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;} static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
f83969d64001f2310470b0206af275ce
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*;import java.io.*;import java.math.*; public class RotationMatching { public static void process()throws IOException { int n = ni(); int[] a = nai(n); int[] b = nai(n); int[] p = new int[n+1]; int[] q = new int[n+1]; int[] dp1 = new int[n+1]; int[] dp2 = new int[n+1]; int ans = 0; for(int i = 0; i < n; i++){ p[a[i]] = i; q[b[i]] = i; } for(int i = 1; i <= n; i++){ if(q[i] > p[i]){ dp1[p[i] - 0 + n-q[i]]++; dp2[q[i] - p[i]]++; } else{ dp1[p[i]-q[i]]++; dp2[n-p[i] + q[i]-0]++; } } for(int i = 0; i <= n; i++){ ans = Math.max(ans, dp1[i]); ans = Math.max(ans, dp2[i]); } pn(ans); } 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(); while(t-->0) {process();} out.flush(); System.err.println(System.currentTimeMillis()-s+"ms"); out.close(); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static void pni(Object o){out.println(o);out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;} static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
929340c15bc6720d7d27fb3f8c3f1cda
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; public class codeforces { public static void main(String[] args) { Scanner s=new Scanner(System.in); int n=s.nextInt(); HashMap<Integer,Integer>map=new HashMap<>(); HashMap<Integer,Integer>map2=new HashMap<>(); for(int i=0;i<n;i++) map.put(s.nextInt(),i); for(int i=0;i<n;i++) { int val=map.get(s.nextInt())-i; if(val<0) val=n+val; map2.put(val,map2.getOrDefault(val,0)+1); } Set<Integer>key=map2.keySet(); int max=0; for(int i:key) { max=Math.max(max,map2.get(i)); } System.out.println(max); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
91e25e50160891d3a84ff31cec6da17c
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class Codechef { PrintWriter out; StringTokenizer st; BufferedReader br; class Pair implements Comparable<Pair> { int f; int s; Pair(int t, int r) { f = t; s = r; } public int compareTo(Pair p) { if(this.f!=p.f) return this.f-p.f; return this.s-p.s; } } // class Sort implements Comparator<String> // { // public int compare(String a, String b) // { // return (a+b).compareTo(b+a); // } // } String ns() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String nextLine() throws Exception { String str = ""; try { str = br.readLine(); } catch (IOException e) { throw new Exception(e.toString()); } return str; } int nextInt() { return Integer.parseInt(ns()); } long nextLong() { return Long.parseLong(ns()); } double nextDouble() { return Double.parseDouble(ns()); } int upperBound(long a[],long key) { int l=0,r=a.length-1; int i=-1; while(l<=r) { int mid=(l+r)/2; if(a[mid]<key) l=mid+1; else{ i=mid; r=mid-1; } } return i; } int lowerBound(ArrayList<Long> a , long key) { int l = 0, r = a.size() - 1; int i = -1; while(l<=r) { int mid=(l+r)/2; if(a.get(mid)<=key) { i=mid; l=mid+1; } else r=mid-1; } return i; } long power(long x,long y) { long ans=1; while(y!=0) { if(y%2==1) ans=(ans*x)%mod; x=(x*x)%mod; y/=2; } return ans%mod; } int mod= 1000000007; long gcd(long x ,long y) { if(y==0) return x; return gcd(y,x%y); } // ArrayList a[]; // int vis[],cnt=0; // void dfs(int ver) // { // ArrayList<Integer> l=a[ver]; // if(l.size()==1) // cnt++; // for(int v:l) // { // if(vis[v]==0){ // vis[v]=vis[ver]+1; // dfs(v); // } // } // } int countSetBits(long n) { int count = 0; while (n > 0) { n &= (n - 1); count++; } return count; } void solve() throws IOException{ int n = nextInt(); int a[] = new int[n]; int f[] = new int[n+1]; HashMap<Integer,Integer> map = new HashMap<>(); for(int i = 0; i < n; i++) a[i] = nextInt(); for(int i = 0; i < n; i++) map.put(nextInt(), i); for(int i = 0; i < n; i++) { int m = map.get(a[i]); if(m >= i) f[m - i]++; else f[n - i + m]++; } int max = 0; for(int i = 0; i < n; i++) max = Math.max(max, f[i]); out.println(max); } void run() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); out.close(); } public static void main(String args[]) throws IOException { new Codechef().run(); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
298e51de64ca2c69cbdb0923a0655039
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.PriorityQueue; import java.util.Scanner; import java.util.StringTokenizer; import java.util.TreeMap; import java.lang.Math.*; import java.util.TreeSet; public class Main { static ArrayList<Integer> adj[]; static PrintWriter out = new PrintWriter(System.out); public static long mod; static int [][]notmemo; static int k; static int[] a; static int b[]; static int m; static int c[]; static class Pair implements Comparable<Pair>{ int v,cost,energy; public Pair(int a1,int a2,int e ) { v=a1; cost=a2; energy=e; } @Override public int compareTo(Pair o) { if(o.energy==this.energy) return this.cost-o.cost; return o.energy-this.energy; } } static Pair s1[]; static long s[]; static ArrayList<Pair> adjlist[]; static int n1; static int n2; static int k1; static int k2; static int skip=0; public static void main(String args[]) throws IOException { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; int b[]=new int[n]; HashMap<Integer,Integer> map=new HashMap<>(); for (int i = 0; i < b.length; i++) { a[i]=sc.nextInt(); } for (int i = 0; i < b.length; i++) { b[i]=sc.nextInt(); map.put(b[i],i); } int left[]=new int[n]; int right[]=new int[n]; for (int i = 0; i < a.length; i++) { int idx1=map.get(a[i]); int r=idx1-i; int l=i-idx1; if(r<0) { r+=n; } if(l<0) { l+=n; } left[l]++; right[r]++; } sortarray(left); sortarray(right); int max=Math.max(left[n-1],right[n-1]); System.out.println(max); } static long cost[]; static long total; static void dfscalc(int u ,int p) { vis[u]=true; for(int v:adj[u]) { if(!vis[v]) { dfscalc(v,u); } } long mixed=Math.min(b[u],c[u]); total+=(1l*mixed*1l*cost[u]*1l*2*1l); b[u]-=mixed; c[u]-=mixed; b[p]+=b[u]; c[p]+=c[u]; } static void dfsvstobestcost(int u ,int p) { vis[u]=true; cost[u]=Math.min(cost[u],cost[p]); for(int v:adj[u]) { if(!vis[v]) { dfsvstobestcost(v,u); } } } static int backtobackdp(int i,int maxsofar,int state) { if(i==n) { if(state==0) return 0; return (32-maxsofar); } if(memo[state][maxsofar][i]!=-1) { return memo[state][maxsofar][i]; } int ans=0; if(state==0) { ans=backtobackdp(i+1,maxsofar,state); ans=Math.max(ans,backtobackdp(i+1,Math.max(maxsofar,a[i]+32),1)+a[i]); } else { ans-=(maxsofar-32); ans=Math.max(ans,backtobackdp(i+1,Math.max(maxsofar,a[i]+32),1)+a[i]); } return memo[state][maxsofar][i]=ans; } static int reali,realj; static int dx[]= {1,-1,0,0}; static int dy[]= {0,0,1,-1}; static char curchar; private static int lcm(int a2, int b2) { return (a2*b2)/gcd(a2,b2); } static boolean visit[][]; static Boolean zero[][]; static int small[]; static int idx=0; static long dpdp[][][][]; static int modd=(int) 1e8; static class Edge implements Comparable<Edge> { int node;long cost; Edge(int a, long b) { node = a; cost = b; } public int compareTo(Edge e){ return Long.compare(cost,e.cost); } } static void sieve(int N) // O(N log log N) { isComposite = new int[N+1]; isComposite[0] = isComposite[1] = 1; // 0 indicates a prime number primes = new ArrayList<Integer>(); for (int i = 2; i <= N; ++i) //can loop till i*i <= N if primes array is not needed O(N log log sqrt(N)) if (isComposite[i] == 0) //can loop in 2 and odd integers for slightly better performance { primes.add(i); if(1l * i * i <= N) for (int j = i * i; j <= N; j += i) // j = i * 2 will not affect performance too much, may alter in modified sieve isComposite[j] = 1; } } static TreeSet<Integer> factors; static ArrayList<Integer> primeFactors(int N) // O(sqrt(N) / ln sqrt(N)) { ArrayList<Integer> factors = new ArrayList<Integer>(); //take abs(N) in case of -ve integers int idx = 0, p = primes.get(idx); while(1l*p * p <= N) { while(N % p == 0) { factors.add(p); N /= p; } p = primes.get(++idx); } if(N != 1) // last prime factor may be > sqrt(N) factors.add(N); // for integers whose largest prime factor has a power of 1 return factors; } static String y; static int nomnom[]; static long fac[]; static boolean f = true; static class SegmentTree { // 1-based DS, OOP int N; // the number of elements in the array as a power of 2 (i.e. after padding) int[] array, sTree, lazy; SegmentTree(int[] in) { array = in; N = in.length - 1; sTree = new int[N << 1]; // no. of nodes = 2*N - 1, we add one to cross out index zero lazy = new int[N << 1]; //build(1, 1, N); } void build(int node, int b, int e) // O(n) { if (b == e) sTree[node] = array[b]; else { int mid = b + e >> 1; build(node << 1, b, mid); build(node << 1 | 1, mid + 1, e); sTree[node] = sTree[node << 1] + sTree[node << 1 | 1]; } } void update_point(int index, int val) // O(log n) { index += N - 1; sTree[index] = val; while(index>1) { index >>= 1; sTree[index] = Math.max(sTree[index<<1] ,sTree[index<<1|1]); } } void update_range(int i, int j, int val) // O(log n) { update_range(1, 1, N, i, j, val); } void update_range(int node, int b, int e, int i, int j, int val) { if (i > e || j < b) return; if (b >= i && e <= j) { sTree[node] += (e - b + 1) * val; lazy[node] += val; } else { int mid = b + e >> 1; propagate(node, b, mid, e); update_range(node << 1, b, mid, i, j, val); update_range(node << 1 | 1, mid + 1, e, i, j, val); sTree[node] = sTree[node << 1] + sTree[node << 1 | 1]; } } void propagate(int node, int b, int mid, int e) { lazy[node << 1] += lazy[node]; lazy[node << 1 | 1] += lazy[node]; sTree[node << 1] += (mid - b + 1) * lazy[node]; sTree[node << 1 | 1] += (e - mid) * lazy[node]; lazy[node] = 0; } int query(int i, int j) { return query(1, 1, N, i, j); } int query(int node, int b, int e, int i, int j) // O(log n) { if (i > e || j < b) return 0; if (b >= i && e <= j) return sTree[node]; int mid = b + e >> 1; // propagate(node, b, mid, e); int q1 = query(node << 1, b, mid, i, j); int q2 = query(node << 1 | 1, mid + 1, e, i, j); return Math.max(q1,q2); } } static int[][][] memo; static class UnionFind { int[] p, rank, setSize; int numSets; int max[]; public UnionFind(int N) { p = new int[numSets = N]; rank = new int[N]; setSize = new int[N]; for (int i = 0; i < N; i++) { p[i] = i; setSize[i] = 1; } } public int findSet(int i) { return p[i] == i ? i : (p[i] = findSet(p[i])); } public boolean isSameSet(int i, int j) { return findSet(i) == findSet(j); } public int chunion(int i,int j, int x2) { if (isSameSet(i, j)) return 0; numSets--; int x = findSet(i), y = findSet(j); int z=findSet(x2); p[x]=z;; p[y]=z; return x; } public void unionSet(int i, int j) { if (isSameSet(i, j)) return; numSets--; int x = findSet(i), y = findSet(j); if (rank[x] > rank[y]) { p[y] = x; setSize[x] += setSize[y]; } else { p[x] = y; setSize[y] += setSize[x]; if (rank[x] == rank[y]) rank[y]++; } } public int numDisjointSets() { return numSets; } public int sizeOfSet(int i) { return setSize[findSet(i)]; } } /** * private static void trace(int i, int time) { if(i==n) return; * * * long ans=dp(i,time); * if(time+a[i].t<a[i].burn&&(ans==dp(i+1,time+a[i].t)+a[i].cost)) { * * trace(i+1, time+a[i].t); * * l1.add(a[i].idx); return; } trace(i+1,time); * * } **/ static class incpair implements Comparable<incpair> { int a; long b; int idx; incpair(int a, long dirg, int i) { this.a = a; b = dirg; idx = i; } public int compareTo(incpair e) { return (int) (b - e.b); } } static class decpair implements Comparable<decpair> { int a; long b; int idx; decpair(int a, long dirg, int i) { this.a = a; b = dirg; idx = i; } public int compareTo(decpair e) { return (int) (e.b - b); } } static long allpowers[]; static class Quad implements Comparable<Quad> { int u; int v; char state; int turns; public Quad(int i, int j, char c, int k) { u = i; v = j; state = c; turns = k; } public int compareTo(Quad e) { return (int) (turns - e.turns); } } static long dirg[][]; static Edge[] driver; static int n; static long manhatandistance(long x, long x2, long y, long y2) { return Math.abs(x - x2) + Math.abs(y - y2); } static long fib[]; static long fib(int n) { if (n == 1 || n == 0) { return 1; } if (fib[n] != -1) { return fib[n]; } else return fib[n] = ((fib(n - 2) % mod + fib(n - 1) % mod) % mod); } static class Point implements Comparable<Point>{ long x, y; Point(long counth, long counts) { x = counth; y = counts; } @Override public int compareTo(Point p ) { return Long.compare(p.y*1l*x, p.x*1l*y); } } static long[][] comb; static class Triple implements Comparable<Triple> { int l; int r; long cost; int idx; public Triple(int a, int b, long l1, int l2) { l = a; r = b; cost = l1; idx = l2; } public int compareTo(Triple x) { if (l != x.l || idx == x.idx) return l - x.l; return -idx; } } static TreeSet<Long> primeFactors(long N) // O(sqrt(N) / ln sqrt(N)) { TreeSet<Long> factors = new TreeSet<Long>(); // take abs(N) in case of -ve integers int idx = 0, p = primes.get(idx); while (p * p <= N) { while (N % p == 0) { factors.add((long) p); N /= p; } if (primes.size() > idx + 1) p = primes.get(++idx); else break; } if (N != 1) // last prime factor may be > sqrt(N) factors.add(N); // for integers whose largest prime factor has a power of 1 return factors; } static boolean visited[]; /** * static int bfs(int s) { Queue<Integer> q = new LinkedList<Integer>(); * q.add(s); int count=0; int maxcost=0; int dist[]=new int[n]; dist[s]=0; * while(!q.isEmpty()) { * * int u = q.remove(); if(dist[u]==k) { break; } for(Pair v: adj[u]) { * maxcost=Math.max(maxcost, v.cost); * * * * if(!visited[v.v]) { * * visited[v.v]=true; q.add(v.v); dist[v.v]=dist[u]+1; maxcost=Math.max(maxcost, * v.cost); } } * * } return maxcost; } **/ public static boolean FindAllElements(int n, int k) { int sum = k; int[] A = new int[k]; Arrays.fill(A, 0, k, 1); for (int i = k - 1; i >= 0; --i) { while (sum + A[i] <= n) { sum += A[i]; A[i] *= 2; } } if (sum == n) { return true; } else return false; } static boolean[] vis2; static boolean f2 = false; static long[][] matMul(long[][] a2, long[][] b, int p, int q, int r) // C(p x r) = A(p x q) x (q x r) -- O(p x q x // r) { long[][] C = new long[p][r]; for (int i = 0; i < p; ++i) { for (int j = 0; j < r; ++j) { for (int k = 0; k < q; ++k) { C[i][j] = (C[i][j] + (a2[i][k] % mod * b[k][j] % mod)) % mod; C[i][j] %= mod; } } } return C; } public static int[] schuffle(int[] a2) { for (int i = 0; i < a2.length; i++) { int x = (int) (Math.random() * a2.length); int temp = a2[x]; a2[x] = a2[i]; a2[i] = temp; } return a2; } static int memo1[]; static boolean vis[]; static TreeSet<Integer> set = new TreeSet<Integer>(); static long modPow(long ways, long count, long mod) // O(log e) { ways %= mod; long res = 1; while (count > 0) { if ((count & 1) == 1) res = (res * ways) % mod; ways = (ways * ways) % mod; count >>= 1; } return res % mod; } static int gcd(int ans, int b) { if (b == 0) { return ans; } return gcd(b, ans % b); } static int[] isComposite; static int[] valid; static ArrayList<Integer> primes; static ArrayList<Integer> l1; static TreeSet<Integer> primus = new TreeSet<Integer>(); static void sieveLinear(int N) { int[] lp = new int[N + 1]; //lp[i] = least prime divisor of i for(int i = 2; i <= N; ++i) { if(lp[i] == 0) { primus.add(i); lp[i] = i; } int curLP = lp[i]; for(int p: primus) if(p > curLP || p * i > N) break; else lp[p * i] = i; } } public static long[] schuffle(long[] a2) { for (int i = 0; i < a2.length; i++) { int x = (int) (Math.random() * a2.length); long temp = a2[x]; a2[x] = a2[i]; a2[i] = temp; } return a2; } static int V; static long INF = (long) 1E16; static class Edge2 { int node; long cost; long next; Edge2(int a, int c, Long long1) { node = a; cost = long1; next = c; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } public int[] nxtArr(int n) throws IOException { int[] ans = new int[n]; for (int i = 0; i < n; i++) ans[i] = nextInt(); return ans; } } public static int[] sortarray(int a[]) { schuffle(a); Arrays.sort(a); return a; } public static long[] sortarray(long a[]) { schuffle(a); Arrays.sort(a); return a; } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
82cfa8fd234b9ffeba909c8aedeb1ce4
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
public class Main { public static void main(String[] args) { var s = new java.util.Scanner(System.in); int n = s.nextInt(); int[] offset = new int[n]; int[] a = new int[n]; int[] pos = new int[n]; for (int i = 0; i < n; i++) a[i] = s.nextInt(); for (int i = 0; i < n; i++) pos[s.nextInt()-1] = i; for (int i = 0; i < n; i++) { int e = a[i]; int interval = i - pos[e-1]; if (interval < 0) interval += n; offset[interval]++; } System.out.println(java.util.stream.IntStream.of(offset).max().getAsInt()); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
5e96f055deffb0953a05b5da385bf742
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; import java.io.*; public class C{ public static void main(String[] args) { FastScanner sc = new FastScanner(); int n = sc.nextInt(); int[] a = new int[n]; int[] b = new int[n]; int max = 1; HashMap<Integer, Integer> pos = new HashMap<>(); HashMap<Integer, Integer> offset = new HashMap<>(); for (int i = 0; i < n; i++){ a[i] = sc.nextInt(); pos.put(a[i], i+1); } for (int i = 0; i < n; i++) b[i] = sc.nextInt(); for (int i = 0; i < n; i++){ int curr = pos.get(b[i]) - (i+1); if(curr < 0) curr += n; if(offset.get(curr) == null){ offset.put(curr, 1); } else{ int count = offset.get(curr); offset.put(curr, ++count); if(count > max) max = count; } } System.out.println(max); } public 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; } } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
a7f15736f3582b9ef95288843cfc1e36
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.io.*; public final class Solution { public static int cost(int[] a, int[] b,int mid) { int cnt=0; for(int i=0;i<a.length;i++) { if(a[i]==b[(i+mid)%b.length]) cnt++; } return cnt; } static double PI = 3.1415926535; public static void main(String[] args) throws IOException { Scanner sc=new Scanner(System.in); PrintWriter writer = new PrintWriter(System.out); Reader re=new Reader(); int n=sc.nextInt(); int[] a=new int[n]; int[] b=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); for(int i=0;i<n;i++) b[i]=sc.nextInt(); int l=1, max=0; HashMap<Integer, Integer> h=new HashMap<>(); for(int i=0;i<n;i++) h.put(a[i], i); int[] ans=new int[n]; for(int i=0;i<n;i++) { ans[b[i]-1]=(h.get(b[i])-i+n)%n; } Arrays.sort(ans); for(int i=0;i<n-1;i++) { if(ans[i]==ans[i+1]) { l++; }else { l=1; } max=Math.max(max, l); }max=Math.max(max, l); System.out.println(max); writer.flush(); writer.close(); sc.close(); } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
a99b55d02085892374b7f8d71fc36062
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; public final class C { public static void main(String[] args) { final Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); final int n = in.nextInt(); in.nextLine(); final int[] arr = new int[n]; final int[] m1 = new int[n + 1]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); m1[arr[i]] = i; } in.nextLine(); final int[] arr2 = new int[n]; final int[] m2 = new int[n + 1]; for (int i = 0; i < n; i++) { arr2[i] = in.nextInt(); m2[arr2[i]] = i; } in.nextLine(); int[] count = new int[n + 1]; int res = 0; for (int num : arr) { int key = m1[num] - m2[num]; if (key < 0) { key += n; } res = Math.max(res, ++count[key]); } System.out.println(res); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
3e1731e0b08fc0cfe66c82400bd35c23
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.io.BufferedInputStream; import java.util.Scanner; public class ProblemC { public static void main(String[] args) { Scanner input = new Scanner(new BufferedInputStream(System.in)); int n = input.nextInt(); int[] a = new int[n]; int[] b = new int[n]; for (int i = 0; i < n; i++) { a[input.nextInt() - 1] = i; } for (int i = 0; i < n; i++) { b[input.nextInt() - 1] = i; } int[] c = new int[n]; for (int i = 0; i < n; i++) { c[i] = a[i] - b[i]; if (c[i] == n - 1) c[i] = -1; else if (c[i] == -(n - 1)) c[i] = 1; } int[] d = new int[2 * n]; for (int i : c) { if (i >= 0) { d[i]++; } else { d[i + n]++; } } int ans = 0; for (int i : d) { if (i > ans) ans = i; } System.out.println(ans); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
511daede13a7bcbf831985f5cd88aebc
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class sdsdsds { static int[] shi(int ar[]) { int n=ar.length; int a[]=new int[n]; for(int i=0;i<n-1;i++) { a[i]=ar[i+1]; } a[n-1]=ar[0]; return a; } public static void main(String[] args) { Scanner sc=new Scanner (System.in); int n=sc.nextInt(); int ar[]=new int[n]; int arr[]=new int[n]; int aw[]=new int[n]; int af[]=new int[n]; for(int i=0;i<n;i++) { ar[i]=sc.nextInt(); aw[ar[i]-1]=i; } for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); af[arr[i]-1]=i; } int max=0; int w[]=new int[n+1]; for(int i=0;i<n;i++) { int y=aw[ar[i]-1]-af[ar[i]-1]; if(y>=0) { w[y]++; } else{ w[y+n]++; } } for(int i=0;i<n;i++) { max=Math.max(w[i],max); } System.out.println(max); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
d7bbfd40dcff81b5f7ffac0d9993785f
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.io.*; import java.util.*; public class Main { static BufferedReader br ; public static void main(String[] args) throws Exception{ // write your code here input input = new input(); int n = input.in(); long a[] = new long[n]; long b[] = new long[n]; String read[] = input.sarrin(); for(int k =0;k<n;k++) { a[k] = input.Lin(read[k]); //ah.put(a[k],(long)k); } read = input.sarrin(); HashMap<Long,Long> x = new HashMap<Long,Long>(); for(int k =0;k<n;k++){ b[k] = input.Lin(read[k]); x.put(b[k],(long)k); } HashMap<Long,Long> x1 = new HashMap<Long,Long>(); for(int i=0;i<n;i++){ long id =x.get(a[i]); if(id>i){ id = (n-1)-id+i+1; }else{ id = i-id; } // System.out.println(id); x1.put(id,x1.getOrDefault(id,0l)+1l); } long max=(Long)Collections.max(x1.values()); System.out.println(max); } } class input{ BufferedReader br ; public input(){ br = new BufferedReader(new InputStreamReader(System.in)); } int in() throws Exception{ return Integer.parseInt(br.readLine()); } long Lin() throws Exception{ return Long.parseLong(br.readLine()); } long Lin(String num) throws Exception{ return Long.parseLong(num); } String sin() throws Exception{ return br.readLine(); } String [] sarrin() throws Exception{ return br.readLine().split(" "); } String [] regexSplit() throws Exception{ return br.readLine().split(" ?(?<!\\G)((?<=[^\\p{Punct}])(?=\\p{Punct})|\\b) ?"); } int in(String num) throws Exception{ return Integer.parseInt(num); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
0b2ec7bc8d88af7b5f0ab3e255de746a
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.io.*; import java.util.*; public class Main { static BufferedReader br ; public static void main(String[] args) throws Exception{ // write your code here input input = new input(); int n = input.in(); long a[] = new long[n]; long b[] = new long[n]; TreeMap<Long,Long> rs = new TreeMap<>(); TreeMap<Long,Long> ls = new TreeMap<>(); HashMap<Long ,Long> ah = new HashMap<>(); HashMap<Long ,Long>bh = new HashMap<>(); String read[] = input.sarrin(); for(int k =0;k<n;k++) { a[k] = input.Lin(read[k]); ah.put(a[k],(long)k); } read = input.sarrin(); for(int k =0;k<n;k++){ b[k] = input.Lin(read[k]); bh.put(b[k],(long)k); } long rmax=0; int l=0; long lmax=0; for(int k =0;k<n;k++){ l+=1; long val = a[k]; long bi = bh.get(val); if(bi>k){ long lshift= k+(n-bi); long rshift= bi-k; long rf = rs.containsKey(rshift)?rs.get(rshift):0; rs.put(rshift,rf+1); long lf = ls.containsKey(lshift)?ls.get(lshift):0; ls.put(lshift,lf+1); if(lmax>lf+1){ lmax=lf+1; } if(rmax>rf+1){ rmax=rf+1; } } else if(bi<k){ long lshift = k-bi; long rshift = n-1-k+(bi+1); long rf = rs.containsKey(rshift)?rs.get(rshift):0; rs.put(rshift,rf+1); long lf = ls.containsKey(lshift)?ls.get(lshift):0; ls.put(lshift,lf+1); if(lmax>lf+1){ lmax=lf+1; } if(rmax>rf+1){ rmax=rf+1; } } else{ long lf = ls.containsKey(0L)?ls.get(0L):0; ls.put(0L,lf+1); long rf = rs.containsKey(0L)?rs.get(0L):0; rs.put(0L,rf+1); if(lmax>lf+1){ lmax=lf+1; } if(rmax>rf+1){ rmax=rf+1; } } } int cnt=0; for(Map.Entry entry:rs.entrySet()){ cnt+=1; long f = (long)entry.getValue(); rmax = Math.max(f,rmax); } for(Map.Entry entry:ls.entrySet()){ cnt+=1; long f = (long)entry.getValue(); lmax = Math.max(f,lmax); } System.out.println(Math.max(rmax,lmax)); } } class input{ BufferedReader br ; public input(){ br = new BufferedReader(new InputStreamReader(System.in)); } int in() throws Exception{ return Integer.parseInt(br.readLine()); } long Lin() throws Exception{ return Long.parseLong(br.readLine()); } long Lin(String num) throws Exception{ return Long.parseLong(num); } String sin() throws Exception{ return br.readLine(); } String [] sarrin() throws Exception{ return br.readLine().split(" "); } String [] regexSplit() throws Exception{ return br.readLine().split(" ?(?<!\\G)((?<=[^\\p{Punct}])(?=\\p{Punct})|\\b) ?"); } int in(String num) throws Exception{ return Integer.parseInt(num); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
717e417fd9c7ca09ada8ae6bbf12c678
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.io.*; import java.text.*; import java.util.*; import java.math.*; public class template { public static void main(String[] args) throws Exception { new template().run(); } public void run() throws Exception { FastScanner f = new FastScanner(); PrintWriter out = new PrintWriter(System.out); /// int n= f.nextInt(); int[] map = new int[n]; for(int i = 0; i < n; i++) map[f.nextInt()-1] = i; int[] cnt = new int[n]; for(int i = 0; i < n; i++) cnt[(i-map[f.nextInt()-1]+n)%n]++; int best = 0; for(int i : cnt) best = Math.max(i, best); out.println(best); /// out.flush(); } /// static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
2aa6fbe2a30c5bf47e9dc607f0e36575
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class q1 { public static void main(String[] args) throws IOException { Reader.init(System.in); int n = Reader.nextInt(); TreeMap <Integer,Integer> atm = new TreeMap<Integer,Integer>(); TreeMap <Integer,Integer> btm = new TreeMap<Integer,Integer>(); for(int i = 0;i<n;i++) { int e = Reader.nextInt(); atm.put(e, i); } for(int i = 0;i<n;i++) { int f = Reader.nextInt(); btm.put(f, i); } int c = 0; TreeMap <Integer,Integer> res = new TreeMap<Integer,Integer>(); Iterator tm = btm.entrySet().iterator(); while(tm.hasNext()) { Map.Entry key = (Map.Entry)tm.next(); int val = (int) key.getValue(); int k = (int) key.getKey(); int w = atm.get(k); if(w<val) { int r = (n-1-val) + (w+1); if(res.containsKey(r)) { int v = res.get(r); v+=1; res.put(r, v); } else { res.put(r,1); } } else { if(res.containsKey(w-val)) { int v = res.get(w-val); v+=1; res.put(w-val, v); } else { res.put(w-val, 1); } } } int max = 0; Iterator tm2 = res.entrySet().iterator(); while(tm2.hasNext()) { Map.Entry key1 = (Map.Entry)tm2.next(); int val1 = (int) key1.getValue(); if(val1>max) { max = val1; } } System.out.println(max); // int b0 = in.nextInt(); // int b1 = in.nextInt(); // int b2 = in.nextInt(); // // int temp = Math.min(a0, b2); // a0-= temp; // b2-= temp; // // temp = Math.min(a1, b0); // a1-= temp; // b0-= temp; // // temp = Math.min(a2,b1); // a2-= temp; // b1-= temp; // // int sum =0; // sum += 2*temp; // // int r = Math.min(b2, a1); // sum+= -(r*2); // System.out.println(sum); // } // TODO Auto-generated method stub } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
5c5ba2469096065298ef4a9bade01ca5
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.Scanner; public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n+1], cnt = new int[n+1]; for (int i = 1; i <= n; i++) { int tmp = sc.nextInt(); a[tmp] = i; cnt[i] = 0; } int ans = 0; for (int i = 1; i <= n; i++) { int tmp = sc.nextInt(); cnt[((a[tmp] - i) + n) % n]++; ans = Math.max(ans, cnt[((a[tmp] - i) + n) % n]); } System.out.println(ans); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
424f1f790a2cb7289e48c425c93ebc92
train_002.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.*; public class Rotation { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int max1 = 0,max2=0; int[] B = new int[N]; HashMap<Integer,Integer> A = new HashMap<Integer,Integer>(); int[] rightShift = new int[N]; for(int i=0;i<N;i++) A.put(in.nextInt(),i); for(int i=0;i<N;i++) { int x = in.nextInt(); B[i] = x; int k = i - (int) A.get(x); if (k < 0) k += N; rightShift[k]++; } for(int i=0;i<N;i++) { max2 = Math.max(max2,rightShift[i]); } System.out.println(max2); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 11
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
5584284f6f912d01a50f5793a7049f1d
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.Scanner; public class B_26_Regular_Bracket_Sequence { public static void main(String[] args){ Scanner input = new Scanner(System.in); String s = input.nextLine().trim(); int l, res; l = res = 0; for(int i = 0; i < s.length(); ++i){ char ch = s.charAt(i); if(ch == '(') ++l; else if(l > 0) { --l; ++res; } } System.out.println(2 * res); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
3de0f39105b56dac05b1181a4afaf7dd
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; public class Myon { public static void main(String[] args) { new Myon().calc(); } void calc() { String s = new Scanner(System.in).nextLine(); int ret = s.length(); int now = 0; for (char c : s.toCharArray()) { if (c == '(') { now++; } else { now--; if (now < 0) { now++; ret--; } } } ret -= now; System.out.println(ret); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
34c0c7279d9ee2a699c78804cacc76a5
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.Scanner; public class regular { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String b =sc.nextLine(); char[] bc = b.toCharArray(); int rb=0,lb=0; for (int i = 0; i < b.length(); i++) { if (bc[i]=='(') rb++; else if (bc[i] ==')'){ lb++; if (rb!=0){ lb--; rb--; } } } int r = lb+rb; System.out.println(b.length()-r); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
e0d1282d68e929732f2eda0920fe057f
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; /** * (() ) ())) * * * */ public class B_Div2_26 { public static void main(String[]arg) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = in.readLine(); int i,n = 0; char c; boolean end = false; Stack<Character> s1 = new Stack<Character>(); for(i = 0; i < s.length() && !end; i++) { c = s.charAt(i); if(c == '(') { s1.add(c); } else { if(!s1.isEmpty()) { s1.pop(); n++; } } } System.out.println(n*2); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
9691ece673c64a2f1985a4167cc34007
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; /** * (() ) ())) * * * */ public class B_Div2_26 { public static void main(String[]arg) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = in.readLine(); int i,n = 0; char c; boolean end = false; Stack<Character> s1 = new Stack<Character>(); for(i = 0; i < s.length() && !end; i++) { c = s.charAt(i); if(c == '(') { s1.add(c); } else { if(!s1.isEmpty()) { s1.pop(); n++; } } } System.out.println(n*2); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
de60628a8eede900f495cbfa663a6df1
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; import java.io.*; public class B26 { class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public InputReader() throws FileNotFoundException { reader = new BufferedReader(new FileReader("d:/input.txt")); 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 char nextChar(){ return next().charAt(0); } } public void run(){ InputReader reader = new InputReader(System.in); String str = reader.next(); int re[] = new int[str.length()]; for(int i = 0; i < re.length ; i ++){ if(str.charAt(i) == '(') re[i] = 1; else re[i] = 0; } Stack<Integer> s = new Stack<Integer>(); int ans = 0; for(int i = 0 ; i < re.length; i ++){ if(re[i] == 0){ if(!s.empty() && s.peek() == 1){ s.pop(); ans ++; } }else{ s.add(re[i]); } } System.out.println(ans*2); } public static void main(String[] args) { new B26().run(); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
50903792cc69764243c92cc678b89c92
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String cad = br.readLine(); char[] arr = cad.toCharArray(); char c = ' '; int cont = 0; Stack<Character> par = new Stack<Character>(); for (int j = 0; j < cad.length(); j++) { try { if (arr[j] == '(') { par.push(arr[j]); } if (arr[j] == ')' ) { c = par.pop(); cont++; } if ((arr[j] == ')' && c != '(')) { cont--; } } catch (Exception e) {} } System.out.println(cont*2); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
b267d0b654c2344c4c6079ea601cbe0c
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; public class Main{ public static void main(String[] args) throws IOException { BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); String cad = b.readLine(); char[] arr = cad.toCharArray(); char c = ' '; int cont = 0; Stack<Character> par = new Stack<Character>(); for (int j = 0; j < cad.length(); j++) { try { if (arr[j] == '(') { par.push(arr[j]); } if (arr[j] == ')' ) { c = par.pop(); cont++; } if ((arr[j] == ')' && c != '(')) { cont--; } } catch (Exception e) {} } System.out.println(cont*2); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
ddf50b677514ae700b15544e9526209e
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.math.*; import java.util.*; public class q1 { public static void main(String args[]){ Scanner s= new Scanner(System.in); String x= s.next(); Stack<Character> st= new Stack(); for (int i = 0; i < x.length(); i++) { if(st.empty()){st.push(x.charAt(i));} else if(st.peek()=='(' && x.charAt(i)==')')st.pop(); else st.push(x.charAt(i)); } System.out.println(x.length()-st.size()); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
f9860c908349ef62c7c03cf3d76bb93d
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Stack; /** * Built using CHelper plug-in * Actual solution is at the top * @author Erasyl Abenov * * */ public class Main { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); try (PrintWriter out = new PrintWriter(outputStream)) { TaskB solver = new TaskB(); solver.solve(1, in, out); } } } class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException{ String s = in.nextLine(); Stack<Character> st = new Stack<>(); int cnt = 0; for(int i = 0; i < s.length(); ++i){ if(s.charAt(i) == ')'){ if(!st.isEmpty() && st.lastElement() == '('){ cnt += 2; st.pop(); } } else st.add('('); } out.println(cnt); } } class InputReader { private final BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(nextLine()); } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public BigInteger nextBigInteger(){ return new BigInteger(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
e60c541b7647d8d6e0b931b67b026349
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.Stack; public class B { public static void main(String[] args) throws Exception { int buf; Stack<Integer> stack = new Stack<Integer>(); int c = 0; while ((buf = System.in.read()) > 0) { if (buf == 40) { stack.push(0); } else if (buf == 41) { if (!stack.isEmpty()) { stack.pop(); c+=2; } } } System.out.println(c); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
b76c270ac896d0415fd8af84988e59d7
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.Scanner; import java.util.Stack; public class BrKtSeq { static int count = 0; static Stack<Character> stk = new Stack<Character>(); public static void main(String[] args) { Scanner inp = new Scanner(System.in); String seq = inp.nextLine(); for (int i = 0; i < seq.length(); i++) { if (seq.charAt(i) == '(') { stk.add(seq.charAt(i)); } else if (seq.charAt(i) == ')') { if (!stk.isEmpty()) { stk.pop(); count+=2; } } } System.out.println(count); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
3cc01900ea7b6d3085e7f9df4c821344
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
//package main; import java.util.*; import java.io.*; import java.math.*; public class Main { static StringTokenizer st; static BufferedReader scan; static PrintWriter out; public static void main(String[] args) throws IOException{ // Scanner scanf = new Scanner(new File("input.txt")); //PrintStream outf = new PrintStream(new File("output.txt")); scan = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); String s = next(); int x = 0, ans = 0; for(int i = 0; i < s.length(); i++){ if(s.charAt(i) == '('){ x++; }else{ if(x > 0){ x--; ans += 2; } } } out.println(ans); scan.close(); out.close(); //scanf.close(); outf.close(); } public static BigInteger nextBigInteger() throws IOException { return new BigInteger(next()); } public static double nextDouble() throws IOException { return Double.parseDouble(next()); } public static long nextLong() throws IOException { return Long.parseLong(next()); } public static int nextInt() throws IOException { return Integer.parseInt(next()); } public static String next() throws IOException{ while(st == null || !st.hasMoreTokens()){ st = new StringTokenizer(scan.readLine()); } return st.nextToken(); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
79ca1780126a98f445e934bd84c6a58d
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Stack; import java.util.StringTokenizer; public class Solution { static InputReader in = new InputReader(System.in); public static void main(String[] args) { String str = in.readLine(); Stack<Character> st = new Stack<>(); int count = 0; for (int i = 0; i < str.length(); i++) { if (st.size() == 0) st.add(str.charAt(i)); else if (str.charAt(i) == ')' && st.peek() == '(') { st.pop(); count++; } else st.add(str.charAt(i)); } System.out.println(count*2); } } class InputReader { private BufferedReader reader; private 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 String readLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
322d62a8f05ba19eb0911769bf2f69ae
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.Scanner; public class P26B { public P26B() { Scanner sc = new Scanner (System.in); String par = sc.next(); sc.close(); int balance = 0; int ok = 0; for (int i = 0; i < par.length(); i++){ if (par.charAt(i) == '('){ balance ++; } else { if (balance > 0){ ok ++; balance --; } } } System.out.println(2 * ok); } public static void main (String []args){ new P26B(); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
838a98abe086ba7a8f824a970bc91c6d
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.Scanner; import java.util.Stack; import java.util.Vector; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String bracket = sc.next(); Stack<Character>first = new Stack<Character>(); Stack<Character>sec = new Stack<Character>(); int count =0; for(int i = 0 ; i < bracket.length();++i) { if(bracket.charAt(i)=='(')first.push('('); if(bracket.charAt(i)==')'&&!first.isEmpty()&&first.peek()=='(') { first.pop(); count +=2; } } System.out.println(count); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
e9183741cc7cfe2b61be71992999a3f4
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.*; import java.util.*; public class bracket{ public static int atoi(String l){ return Integer.parseInt(l);} public static void main(String kvjnfjv[]) throws Exception{ BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); String l=br.readLine(); int max=0; System.out.println(l.length()-verificar(l)); } public static int verificar(String l){ Stack<Character> pila= new Stack<Character>(); int v=0; for(int i=0;i<l.length();i++){ if(l.charAt(i)=='('){ pila.push(l.charAt(i)); }else if(pila.empty()) v++; else{ pila.pop(); } } return v+pila.size(); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
4be479161ce43edae11ae739d00a3f2a
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.*; import java.util.*; public class j { public static void main(String a[])throws IOException { Scanner b=new Scanner(System.in); int i,n,m=0,p=0; String s; s=b.next(); n=s.length(); for(i=0;i<n;i++) { if(((int)(s.charAt(i)))==41) { if(m==0) p++; else m--; } else m++; } System.out.print(n-m-p); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
dde062bff4afb084b9eb3a6bf1fdafcb
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { public static StreamTokenizer in = new StreamTokenizer(System.in); public static boolean bg = true; public static void main(String[] args) throws Exception { in = null; LR in1 = new LR(); char[] l1 = in1.nx().toCharArray(); boolean[] in = new boolean[l1.length]; ArrayList<Integer> l = new ArrayList(); ArrayList<Integer> r = new ArrayList(); for (int i = 0; i < l1.length; i++){ if (l1[i] == '('){ l.add(i); } else { r.add(i); if (l.size() > 0 && r.size() > 0){ in[l.remove(l.size()-1)] = true; in[r.remove(r.size()-1)] = true; } } } int count = 0; for (boolean e: in){ if (e == true){ count++; } } System.out.println(count); } private static int ni() throws Exception { in.nextToken(); return (int) in.nval; } private static long nl() throws Exception { in.nextToken(); return (long) in.nval; } private static double nd() throws Exception { in.nextToken(); return in.nval; } private static String ns() throws Exception { in.nextToken(); return in.sval; } private static int[] intl(int n1) throws Exception { int[] fin = new int[n1]; for (int i = 0; i < n1; i++) { fin[i] = ni(); } return fin; } private static long[] longl(int n1) throws Exception { long[] fin = new long[n1]; for (int i = 0; i < n1; i++) { fin[i] = nl(); } return fin; } private static double[] doublel(int n1) throws Exception { double[] fin = new double[n1]; for (int i = 0; i < n1; i++) { fin[i] = nd(); } return fin; } private static void pn(Object o1) { System.out.println(o1); } private static void p(Object o1) { System.out.print(o1); } private static void ex() { System.exit(0); } private static BigInteger bi(long n1) { return new BigInteger(n1 + ""); } private static class LR { BufferedReader k1; public LR() throws Exception { k1 = new BufferedReader(new InputStreamReader(System.in)); } public String nx() throws Exception { return k1.readLine(); } } private static class Sorted<E extends Comparable<? super E>> { ArrayList<E> set = new ArrayList(); ArrayList<Integer> count = new ArrayList(); public Sorted(ArrayList<E> o1) { Collections.sort(o1); for (int i = 0; i < o1.size(); i++) { if (i == 0) { set.add(o1.get(i)); count.add(1); continue; } E cur = o1.get(i); if (cur.equals(o1.get(i - 1))) { count.set(count.size() - 1, count.get(count.size() - 1) + 1); } else { set.add(cur); count.add(1); } } } } private static class CS<E> { public long total = 0; public HashMap<E, Integer> m1 = new HashMap(); public void add(E k1) { add(k1, 1); } public void remove(E k1) { add(k1, -1); } public void add(E k1, int n1) { int nv = 0; if (m1.containsKey(k1)) { nv = m1.get(k1); } m1.put(k1, nv + n1); total += n1; if (nv + n1 == 0) { m1.remove(k1); } } public void set(E k1, int n1) { int nv = 0; if (m1.containsKey(k1)) { nv = m1.get(k1); } m1.put(k1, n1); total += n1 - nv; if (n1 == 0) { m1.remove(k1); } } public int get(E k1) { if (!m1.containsKey(k1)) { return 0; } else { return m1.get(k1); } } public String toString() { return m1.toString(); } } private static class DJS<E> { public HashMap<E, Integer> mapping = new HashMap(); public ArrayList<E> value = new ArrayList(); public ArrayList<Integer> p = new ArrayList(); public ArrayList<Integer> size = new ArrayList(); public int n = 0; public void add(E k1) { if (!mapping.containsKey(k1)) { mapping.put(k1, n); value.add(k1); p.add(n); size.add(1); n++; } } public int find(int x) { if (p.get(x) == x) return x; else return find(p.get(x)); } public void union(E k1, E k2) { if (!mapping.containsKey(k1)) add(k1); if (!mapping.containsKey(k2)) add(k2); int s1 = mapping.get(k1); int s2 = mapping.get(k2); int r1 = find(s1); int r2 = find(s2); if (r1 == r2) return; if (size.get(r1) >= size.get(r2)) { size.set(r1, size.get(r1) + size.get(r2)); p.set(r2, r1); } else { size.set(r2, size.get(r1) + size.get(r2)); p.set(r1, r2); } } public void print() { System.out.println("value\n" + value); System.out.println("p\n" + p); System.out.println("size\n" + size); } public HashMap<Integer, ArrayList<E>> partition() { HashMap<Integer, ArrayList<E>> temp1 = new HashMap(); for (int i = 0; i < p.size(); i++) { int classNo = find(i); if (!temp1.containsKey(classNo)) temp1.put(classNo, new ArrayList()); ArrayList curList = temp1.get(classNo); curList.add(value.get(i)); } return temp1; } } private static class Bit { public static String bin8(int n1) { String k1 = Integer.toBinaryString(n1); k1 = String.format("%8s", k1).replace(' ', '0'); return k1; } public static String bin32(int n1) { String k1 = Integer.toBinaryString(n1); k1 = String.format("%32s", k1).replace(' ', '0'); return k1; } } private static class Fenwick { long[] l1 = null; public Fenwick(int n1) { l1 = new long[n1]; } public static int[] temp = new int[100]; public static int len = -1; public long get(int n1) { int k1 = n1 + 1; int ptr = 0; for (int i = 0;; i++) { if (k1 == 0) break; if (k1 % 2 == 1) { temp[ptr++] = i; } k1 = k1 >> 1; } len = ptr; int ptr1 = -1; long fin = 0; for (int i = len - 1; i >= 0; i--) { int cur = temp[i]; ptr1 += 1 << cur; fin += l1[ptr1]; } return fin; } public void add(int id) { add(id, 1); } public void add(int id, long val) { int k1 = id; int v = 1; for (;;) { int idx = k1 * v + v - 1; if (idx >= l1.length) { break; } if (k1 % 2 == 0) l1[idx] += val; v = v << 1; k1 = k1 >> 1; } } public String toString() { long[] fin = new long[l1.length]; for (int i = 0; i < l1.length; i++) fin[i] = get(i); return Arrays.toString(fin); } } private static class CBT { public static BigInteger fact(int n1) { BigInteger fin = BigInteger.ONE; for (int i = 2; i <= n1; i++) { fin = fin.multiply(new BigInteger(n1 + "")); } return fin; } public static BigInteger choose(int n1, int n2) { BigInteger fin = fact(n1); fin = fin.divide(fact(n2)); fin = fin.divide(fact(n1 - n2)); return fin; } } private static class NT { public static ArrayList<Integer> factor(int n1) { ArrayList<Integer> l1 = new ArrayList(); for (int i = 1; i <= Math.sqrt(n1); i++) { if (n1 % i == 0) { l1.add(i); if (i != n1 / i) l1.add(n1 / i); } } return l1; } public static int[] factor2(int n1) { ArrayList<Integer> l1 = new ArrayList(); ArrayList<Integer> l2 = new ArrayList(); for (int i = 1; i <= Math.sqrt(n1); i++) { if (n1 % i == 0) { l1.add(i); if (i != n1 / i) l2.add(n1 / i); } } int[] fin = new int[l1.size() + l2.size()]; for (int i = 0; i < l1.size(); i++) { fin[i] = l1.get(i); } for (int i = l1.size(); i < l1.size() + l2.size(); i++) { fin[i] = l2.get(l2.size() - 1 - (i - l1.size())); } return fin; } public static HashMap<Integer, int[]> facts = new HashMap(); public static int[] facts(int n1) { if (facts.containsKey(n1)) return facts.get(n1); int[] fin = factor2(n1); facts.put(n1, fin); return fin; } public static ArrayList<ArrayList<Integer>> mults(int k1) { ArrayList<ArrayList<Integer>> fin = new ArrayList(); mults(k1, new ArrayList(), 1, fin); return fin; } public static void mults(int k1, ArrayList<Integer> stk, int max, ArrayList<ArrayList<Integer>> result) { if (k1 == 1) { result.add((ArrayList<Integer>) stk.clone()); return; } int[] f1 = facts(k1); for (int i = 1; i < f1.length; i++) { if (f1[i] < max) { continue; } stk.add(f1[i]); int max1 = max; if (f1[i] > max1) max1 = f1[i]; mults(k1 / f1[i], stk, max1, result); stk.remove(stk.size() - 1); } } public static int gcd(int n1, int n2) { int k1 = n1 > n2 ? n1 : n2; int k2 = k1 == n1 ? n2 : n1; int rm = k1 % k2; while (rm != 0) { k1 = k2; k2 = rm; rm = k1 % k2; } return k2; } public static long xgcd(long n1, long n2) { long k1 = n1; long k2 = n2; long[] l1 = { 1, 0 }; long[] l2 = { 0, 1 }; for (;;) { long f1 = k1 / k2; long f2 = k1 % k2; if (f2 == 0) break; long[] l3 = { 0, 0 }; l3[0] = l1[0] - f1 * l2[0]; l3[1] = l1[1] - f1 * l2[1]; l1 = l2; l2 = l3; k1 = k2; k2 = f2; } long fin = l2[1] % n1; if (fin < 0) { fin += n1; } return fin; } public static ArrayList<Integer> sieve(int n1) { ArrayList<Integer> fin = new ArrayList(); boolean[] l1 = new boolean[n1 + 1]; for (int m = 2; m <= Math.sqrt(n1); m++) { if (!l1[m]) { fin.add(m); for (int k = m * m; k <= n1; k += m) { l1[k] = true; } } } for (int m = (int) Math.sqrt(n1) + 1; m <= n1; m++) { if (!l1[m]) fin.add(m); } return fin; } public static BigInteger lcm(BigInteger n1, BigInteger n2) { BigInteger mul = n1.multiply(n2); return mul.divide(n1.gcd(n2)); } } private static class MOD { public long mod = -1; public MOD(long k1) { mod = k1; } public long a(long n1, long n2) { long k1 = (n1 + n2) % mod; if (k1 < 0) k1 += mod; return k1; } public long s(long n1, long n2) { long k1 = (n1 - n2) % mod; if (k1 < 0) k1 += mod; return k1; } public long t(long n1, long n2) { long k1 = (n1 * n2) % mod; if (k1 < 0) k1 += mod; return k1; } public long d(long n1, long n2) { return (t(n1, xgcd(mod, n2))) % mod; } public static long xgcd(long n1, long n2) { long k1 = n1; long k2 = n2; long[] l1 = { 1, 0 }; long[] l2 = { 0, 1 }; for (;;) { long f1 = k1 / k2; long f2 = k1 % k2; if (f2 == 0) break; long[] l3 = { 0, 0 }; l3[0] = l1[0] - f1 * l2[0]; l3[1] = l1[1] - f1 * l2[1]; l1 = l2; l2 = l3; k1 = k2; k2 = f2; } long fin = l2[1] % n1; if (fin < 0) { fin += n1; } return fin; } public long pow(long n1, long pow) { if (pow == 0) return 1; else if (pow == 1) return t(1l, n1); else if (pow % 2 == 0) { long half = pow(n1, pow / 2); return t(half, half); } else { long half = pow(n1, pow / 2); return t(half, t(half, n1)); } } } private static class Geometry { public static double dist1(double x1, double y1, double x2, double y2) { return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } public static double dist2(double k1, double k2) { return Math.sqrt(k1 * k1 + k2 * k2); } public static double sarea(double a, double b, double coseta) { return 0.5 * a * b * Math.sin(coseta); } public static double lcos(double a, double b, double coseta) { double k1 = a * a + b * b - 2 * a * b * Math.cos(coseta); return Math.sqrt(k1); } } private static class Palin { public static int[] map(String s1) { char[] l1 = s1.toCharArray(); int[] l2 = new int[l1.length * 2 - 1]; int ptr = 0; int k1 = 1; for (;;) { for (;;) { int right = ptr / 2 + k1 / 2 + 1; int left = ptr / 2 - k1 / 2 - 1; if (ptr % 2 == 1) left++; if (right > l1.length - 1) break; if (left < 0) break; if (l1[left] == l1[right]) k1 += 2; else break; } if (ptr > l2.length - 1) break; l2[ptr] = k1; boolean found = false; for (int i = 0; i < k1 - 1; i++) { int temp = l2[ptr - i - 1]; if (temp + i + 1 == k1) { ptr = ptr + i + 1; k1 = temp; found = true; break; } else { l2[ptr + i + 1] = l2[ptr - i - 1]; } } if (!found) { ptr += 1; k1 = 0; if (ptr % 2 == 0) k1 = 1; } } return l2; } public static int[] idx(int id, int leng) { int left = (id) / 2 - leng / 2; if (id % 2 == 1) left += 1; int right = (id) / 2 + leng / 2; return new int[] { left, right }; } } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
02577645f07ddf5f63615b45a61a1813
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.Stack; public class RegularBracketSequence26B { public static void main(String [] args){ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader flujoE = new BufferedReader(isr); Stack<Character> pila = new Stack<Character>(); String palabra=""; try { palabra = flujoE.readLine(); } catch (IOException e) { } int size=palabra.length(); int cant1=0, cant2=0,cant3=0; for(int i=0;i<size;i++){ if(palabra.charAt(i)=='('){ cant1++; pila.add('('); }else if(palabra.charAt(i)==')'){ cant1++; if(pila.isEmpty()){ cant2++; }else{ pila.pop(); } } } if(!pila.isEmpty()){ cant3=pila.size(); } System.out.println((cant1-cant2-cant3)); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
34f2cac7161f81a23b109a7ca01998d9
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Stack; public class Main { public static void main(String[] args) throws Exception { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); //int size = Integer.parseInt(input.readLine()); int count=0; Stack<Character> stack = new Stack<Character>(); String str = input.readLine(); for(int i=0;i<str.length();++i){ if(((str.charAt(i)==')') && stack.isEmpty())){continue;} if((str.charAt(i)==')' && stack.peek()!='(')){continue;} if((str.charAt(i)==')' && stack.peek()=='(')){stack.pop();count+=2;} if(str.charAt(i)=='(')stack.push(str.charAt(i)); } out.println(count); out.flush(); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
a828294ff6993809630a78033097f526
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.EmptyStackException; import java.util.Stack; /** * * @author Mbt */ public class D { public static void main(String[] args) throws IOException { new Solver().solve(); } } class Solver{ void solve() throws IOException{ BufferedReader reader= new BufferedReader(new InputStreamReader(System.in)); char[] expression= reader.readLine().toCharArray(); Stack<Character> stack= new Stack<>(); int removeCount=0; for (int i=0; i<expression.length; i++){ if (expression[i]=='(') stack.push(expression[i]); else{ try{ stack.pop(); }catch(EmptyStackException ex){ removeCount++; } } } removeCount += stack.size(); System.out.println(expression.length-removeCount); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
4ceeb0200846a60e245dfbdf64cccdf7
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; public class A { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line = ""; StringBuilder out = new StringBuilder(); while ((line = in.readLine()) != null && line.length() != 0) { char[] l = line.trim().toCharArray(); int s = 0, ans = 0; for (int i = 0; i < l.length; i++) { if (l[i] == '(') { s++; } else if (s > 0) { ans += 2; s--; } } out.append(ans + "\n"); } System.out.print(out); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
5e73cb9e36ca83ad55dd8f7c0b292f2e
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Zyflair Griffane */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; PandaScanner in = new PandaScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); RegularBracketSequence solver = new RegularBracketSequence(); solver.solve(1, in, out); out.close(); } } class RegularBracketSequence { public void solve(int testNumber, PandaScanner in, PrintWriter out) { char[] str = in.next().toCharArray(); int balance = 0; int taken = 0; for (char c: str) { if (c == '(') { balance++; } if (c == ')' && balance > 0) { balance--; taken++; } } out.println(taken << 1); } } class PandaScanner { public BufferedReader br; public StringTokenizer st; public InputStream in; public PandaScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(this.in = in)); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { return null; } } public String next() { if (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine().trim()); return next(); } return st.nextToken(); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
c824a07e14991c95bfe9b84f60f188d9
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.Stack; public class RegularBracketSequence { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); OutputStream out = new BufferedOutputStream(System.out); Stack<Character> st = new Stack<Character>(); String s = br.readLine(); int count = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (st.isEmpty()) st.add(c); else if (st.peek() == '(' && c == ')') { st.pop(); count++; } else st.add(c); } out.write(((count * 2) + "\n").getBytes()); out.flush(); br.close(); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
3c747aa4ec53b66674aa7a38259da055
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; import java.io.*; public class Main { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static StringBuilder out = new StringBuilder(); public static void main(String[] args){ solve(); return; } // the followings are methods to take care of inputs. static int nextInt(){ return Integer.parseInt(nextLine()); } static long nextLong(){ return Long.parseLong(nextLine()); } static int[] nextIntArray(){ String[] inp = nextLine().split("\\s+"); int[] ary = new int[inp.length]; for (int i = 0; i < ary.length; i++){ ary[i] = Integer.parseInt(inp[i]); } return ary; } static int[] nextIntArrayFrom1(){ String[] inp = nextLine().split("\\s+"); int[] ary = new int[inp.length + 1]; for (int i = 0; i < inp.length; i++){ ary[i+1] = Integer.parseInt(inp[i]); } return ary; } static long[] nextLongArray(){ String[] inp = nextLine().split("\\s+"); long[] ary = new long[inp.length]; for (int i = 0; i < inp.length; i++){ ary[i] = Long.parseLong(inp[i]); } return ary; } static long[] nextLongArrayFrom1(){ String[] inp = nextLine().split("\\s+"); long[] ary = new long[inp.length + 1]; for (int i = 0; i < inp.length; i++){ ary[i+1] = Long.parseLong(inp[i]); } return ary; } static String nextLine(){ try { return reader.readLine().trim(); } catch (Exception e){} return null; } static void solve(){ String str = nextLine(); int s=0; int count=0; for(int i=0;i<str.length();i++){ char c = str.charAt(i); String bar=""; if(c=='(') s++; else{ if(s==0){ bar+='\n'; } else{ count+=2; s--; } } } System.out.println(count); return; } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
10a0b7315ee4c577d4606f33fdda41ff
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; public class SS8 { public static void main(String [] args){ Scanner in = new Scanner(System.in); String str = in.next(); int sum=0; int answer=0; for(int i=0;i<str.length();i++){ if(str.charAt(i)=='(') sum++; if(str.charAt(i)==')') sum--; if(sum>=0) answer++; if(sum<=0) sum=0; } System.out.println(answer-sum); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
5592a13e04332cac4ddec868360ed895
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.Scanner; import java.util.Stack; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * @author Computragy */ public class RegularBracketSequence { public static void main(String[] args) { Scanner cs1 = new Scanner(System.in); String word = cs1.next(); Stack<Character> stack = new Stack<Character>(); int counter = 0; for (int i = 0; i < word.length(); i++) { if (word.charAt(i) == '(') { stack.push('('); } else if (word.charAt(i) == ')') { if (!stack.isEmpty()) { stack.pop(); counter+=2; } } } System.out.println(counter); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
3c0d20a1c5984c5456a084fbb4f2c70f
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.*; import java.util.Stack; import java.util.StringTokenizer; /** * * @author Prateep */ public class JavaApplication1 { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { // TODO code application logic here InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB{ public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException{ char[] base=in.next().toCharArray(); Stack<Character> stack=new Stack<>(); int count=0; for(int i=0;i<base.length;i++){ if(base[i]=='(')stack.add(base[i]); else if(stack.size()!=0){ stack.pop(); count++; } } out.println(count*2); } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; private BufferedReader br; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextWhole() throws IOException{ br=new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } public String nextLine(){ try{ return reader.readLine(); }catch (IOException e){ throw new RuntimeException(e); } } } class Pair<A, B> { private A first; private B second; public Pair() { super(); } public Pair(A first, B second) { super(); this.first = first; this.second = second; } @Override public int hashCode() { int hashFirst = first != null ? first.hashCode() : 0; int hashSecond = second != null ? second.hashCode() : 0; return (hashFirst + hashSecond) * hashSecond + hashFirst; } @Override public boolean equals(Object other) { if (other instanceof Pair) { Pair otherPair = (Pair) other; return (( this.first == otherPair.first || ( this.first != null && otherPair.first != null && this.first.equals(otherPair.first))) && ( this.second == otherPair.second || ( this.second != null && otherPair.second != null && this.second.equals(otherPair.second))) ); } return false; } @Override public String toString() { return "("+first+","+second+")"; } public A getFirst() { return first; } public void setFirst(A first){ this.first=first; } public B getSecond(){ return second; } public void setSecond(B second){ this.second=second; } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
183f31b6d7bdcb34eff076e4911091eb
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.*; import java.util.Stack; import java.util.StringTokenizer; /** * * @author Prateep */ public class JavaApplication1 { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { // TODO code application logic here InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB{ public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException{ char[] base=in.next().toCharArray(); StringBuilder str=new StringBuilder(); int count=0; for(int i=0;i<base.length;i++){ if(base[i]=='(')str.append(base[i]); else if(str.length()!=0){ str.deleteCharAt(str.length()-1); count++; } } out.println(count*2); } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; private BufferedReader br; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextWhole() throws IOException{ br=new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } public String nextLine(){ try{ return reader.readLine(); }catch (IOException e){ throw new RuntimeException(e); } } } class Pair<A, B> { private A first; private B second; public Pair() { super(); } public Pair(A first, B second) { super(); this.first = first; this.second = second; } @Override public int hashCode() { int hashFirst = first != null ? first.hashCode() : 0; int hashSecond = second != null ? second.hashCode() : 0; return (hashFirst + hashSecond) * hashSecond + hashFirst; } @Override public boolean equals(Object other) { if (other instanceof Pair) { Pair otherPair = (Pair) other; return (( this.first == otherPair.first || ( this.first != null && otherPair.first != null && this.first.equals(otherPair.first))) && ( this.second == otherPair.second || ( this.second != null && otherPair.second != null && this.second.equals(otherPair.second))) ); } return false; } @Override public String toString() { return "("+first+","+second+")"; } public A getFirst() { return first; } public void setFirst(A first){ this.first=first; } public B getSecond(){ return second; } public void setSecond(B second){ this.second=second; } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
06052653f6b66bcf15d575b6eb535023
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.util.Scanner; import java.util.Stack; /** * * @author Lotus */ public class B26 { public static void main(String[]args) { Scanner input = new Scanner(System.in); String s = input.next(); Stack a = new Stack(); int counter=0; for(int i=0; i<s.length();i++) { if(s.charAt(i)=='(') { a.push('('); } else if(a.size()!=0) { a.pop(); } else { counter++; } } counter+=a.size(); System.out.println(s.length()-counter); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
1cd602ae5ec3da8e50c8eabe52c9d759
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); int count = 0; ArrayDeque<Character> stack = new ArrayDeque<Character>(); for (int i = 0; i < s.length(); i++) { if(s.charAt(i)==')' && stack.size()==0){ count++; continue; } else if(s.charAt(i)==')' && stack.peek()=='('){ stack.pop(); } else stack.push(s.charAt(i)); } System.out.println(s.length()-(count+stack.size())); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
630c8a434184fda91366985f31a9cce8
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.Scanner; import java.util.Stack; public class main{ public static void main(String[] args){ Scanner s=new Scanner(System.in); Stack <Character> st =new Stack(); int c=0; String str = s.next(); for(int i=0;i<str.length();i++) { if((str.charAt(i)+"").equalsIgnoreCase(")")&&st.size()==0) continue; else if((str.charAt(i)+"").equalsIgnoreCase(")")) { st.pop(); c++; } else st.push(str.charAt(i)); } System.out.println(c*2); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
ee5366e526cc9863a48ca42dc55ac90f
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; public class RegularBracketSequence { public static void main(String[] args) { Scanner in = new Scanner(System.in); Stack<Character> stack = new Stack<Character>(); String s = in.next(); int ans = 0; for(int i = 0; i < s.length(); i ++) { if(!stack.isEmpty() && stack.peek() == '(' && s.charAt(i) == ')') { stack.pop(); ans++; } else stack.push(s.charAt(i)); } System.out.println(ans*2); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
2b60f050c83b322e1688d210f0950af9
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.awt.List; import java.util.Scanner; /** * * @author mina */ public class B { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); char[] permutation = new char[1000000]; int start=0,finish=0; permutation = sc.nextLine().toCharArray(); for (int i = 0; i < permutation.length; i++) { if(permutation[i]=='('){ start++; } if(permutation[i]==')' && start > 0){ finish++; start--; } } System.out.print(finish * 2); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
dfda884792c9124b677afcf334fb794f
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Scanner; public class B { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); String s = in.next(); int leftCnt = 0; int pairs = 0; for (int i=0; i<s.length(); i++) { if (s.charAt(i) == '(') { leftCnt++; }else if (leftCnt > 0){ leftCnt--; pairs++; } } pw.println(2*pairs); pw.close(); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
e4e02fcbb2249707ed063c153921034d
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.nio.charset.Charset; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int cont=0; String ent= sc.nextLine(); Stack<Integer> pila= new Stack<Integer>(); int i=0; while(i<ent.length()) { if(ent.charAt(i)=='(') { pila.add(1); } else { if(!pila.isEmpty()) { if(ent.charAt(i)==')') { pila.pop(); cont+=2; } } } i++; } System.out.println(cont); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
ea04db1eb9e580a4a871ee99ab479e59
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.Scanner; import java.util.Stack; /* * @author zezo * date : Oct 26, 2014 */ public class RegularBracketSequence { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); char array[] = scanner.next().toCharArray(); Stack<Character> S = new Stack<Character>(); int count = 0; for (int i = 0; i < array.length; i++) { if (array[i] == '(') { S.add('('); } else if (array[i] == ')' && !S.isEmpty()) { count++; S.pop(); } } System.out.println(count * 2); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
58e225b5d3db00ea3e1010963c53ba31
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Stack<String> data = new Stack<>(); String s = in.next(); data.push(s.charAt(0) + ""); int i = 1; int count=0; while (i < s.length()) { if (data.isEmpty() && s.charAt(i) == ')') { } else { if (s.charAt(i) == ')'&& data.pop().equals("(")) { count+=2; } else { data.push(s.charAt(i) + ""); } } i++; } System.out.println(count); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
8c5b9f3c29684f9bab1ef36620a26b75
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Main1 { public static void main(String[] args) throws IOException { BufferedReader in; StringBuilder out = new StringBuilder(); File file = new File("in"); if (file.exists()) in = new BufferedReader(new FileReader(file)); else in = new BufferedReader(new InputStreamReader(System.in)); String line, lines[]; char arr[] = in.readLine().toCharArray(); int a = 0, size = arr.length, count = 0; for ( int i = 0; i < size; i++ ) { if ( arr[i] == '(' ) { a++; } else { if ( a > 0 ) { a--; count += 2; } } } System.out.print(count); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
2ec1d23481fbcdb9ab27ac98ea1735d1
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class B26 { public static void main(String args[]){ Scanner in = new Scanner( System .in); String tmp = in.next(); char a[] = tmp.toCharArray(); int ans = 0; Queue<Character> q = new LinkedList<Character>(); for (int i = 0 ; i < a.length;i++){ if(a[i]=='(') { q.add(a[i]); } else if(a[i]==')'){ if(!q.isEmpty()) if(q.peek()=='(') { q.poll(); ans+=2; } else q.add(a[i]); } } System.out.println(ans); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
78ee405d0aa8f8e1cd416fc463e70c6a
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.*; import java.util.*; public class RegularBracketSequence { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); String str = f.readLine(); int count = 0; int sub = 0; for (int i = 0; i < str.length(); i++) if (str.charAt(i) == '(') count++; else { count--; if (count < 0) { count = 0; sub++; } } System.out.println(str.length()-sub-count); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
9a518f83b40e48b4ef0bf66197611623
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; public class RegularBracketSequence { public static void main(String []args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Stack<Character>p=new Stack<Character>(); String secuencia=br.readLine(); int j=0,cont=0; while(j<secuencia.length()) { char c=secuencia.charAt(j); if(c==')') { if(!p.empty()){ p.pop(); cont++; } } else p.push(c); j++; } System.out.print(2*cont); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
d2fc2beca0213058ff7245318df9b421
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; public class Main{ public static void main(String[] args) throws IOException { BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); String cad = b.readLine(); char[] arr = cad.toCharArray(); char c = ' '; int cont = 0; Stack<Character> par = new Stack<Character>(); for (int j = 0; j < cad.length(); j++) { try { if (arr[j] == '(') { par.push(arr[j]); } if (arr[j] == ')' ) { c = par.pop(); cont++; } if ((arr[j] == ')' && c != '(')) { cont--; } } catch (Exception e) {} } System.out.println(cont*2); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
254a5441836803c8d83abee291bbdb0e
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; import com.sun.org.apache.xml.internal.utils.CharKey; public class Regular { public static void main(String[] args){ Stack<String>x = new Stack<String>(); Scanner sc = new Scanner(System.in); String s = sc.next(); int cont = 0; for(int i = 0; i < s.length(); i++){ if(s.charAt(i) == '('){ x.push("("); }else if(!x.empty()){ x.pop(); cont += 2; } } System.out.println(cont); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
4f7f3c0603a64525594639f14c5367d1
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.Scanner; import java.util.Stack; public class RegularBracketSequence { public static void main(String[] args){ int count=0; Scanner input =new Scanner(System.in); Stack<Character> stack=new Stack<Character>(); String s=input.next(); for(int i=0;i<s.length();i++){ if(s.charAt(i)=='('){ stack.push('('); } else if (!stack.isEmpty()){ stack.pop(); count+=2; } }//for System.out.println(count); input.close(); }//main }//class
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
82c88c08807ad0f99446b2f62c883f10
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; import java.io.*; public class Codeforces { static boolean graph[][]; public static void main(String[] args) throws IOException { Reader.init(System.in); char arr[] = Reader.next().toCharArray(); int n = arr.length, count = 0, x = 0; for(int i = arr.length - 1; i > -1; i--){ if(arr[i] == '('){ if(x == 0) count++; else x--; } else x++; } System.out.println(n - (count+x)); } } class Pair implements Comparable<Pair>{ long a; int b; @Override public int compareTo(Pair t) { long x = this.a - t.a; if(x == 0) return this.b - t.b; return (int)x; } public Pair(long a, int b){ this.a = a; this.b = b; } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** * call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** * get next word */ static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static byte nextByte() throws IOException { return Byte.parseByte(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
91160cff74b90e7e91b4cdbab84baf9c
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; import java.io.*; public class Codeforces { static boolean graph[][]; public static void main(String[] args) throws IOException { Reader.init(System.in); // char arr[] = Reader.next().toCharArray(); String line = Reader.next(); int n = line.length(), count = 0, x = 0; for(int i = line.length() - 1; i > -1; i--){ if(line.charAt(i) == '('){ if(x == 0) count++; else x--; } else x++; } System.out.println(n - (count+x)); } } class Pair implements Comparable<Pair>{ long a; int b; @Override public int compareTo(Pair t) { long x = this.a - t.a; if(x == 0) return this.b - t.b; return (int)x; } public Pair(long a, int b){ this.a = a; this.b = b; } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** * call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** * get next word */ static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static byte nextByte() throws IOException { return Byte.parseByte(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
c5d88e7228b7f7f19dc9cf0f4233d12e
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; import java.io.*; public class Codeforces { static boolean graph[][]; public static void main(String[] args) throws IOException { Reader.init(System.in); char arr[] = Reader.next().toCharArray(); int n = arr.length, count = 0; Stack<Character> s = new Stack<>(); for(int i = arr.length - 1; i > -1; i--){ if(arr[i] == '('){ if(s.empty()) count++; else s.pop(); } else s.push(arr[i]); } System.out.println(n - (count+s.size())); } } class Pair implements Comparable<Pair>{ long a; int b; @Override public int compareTo(Pair t) { long x = this.a - t.a; if(x == 0) return this.b - t.b; return (int)x; } public Pair(long a, int b){ this.a = a; this.b = b; } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** * call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** * get next word */ static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static byte nextByte() throws IOException { return Byte.parseByte(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
430d38a72a4adbc4a4eb0b08d4bc9573
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; /** * Created by omar on 3/24/16. */ public class main { public static void main(String[] args) { String input = new Scanner(System.in).nextLine(); int open = 0; int close = 0; for(int i=0; i<input.length(); i++) { if(input.charAt(i) == '(') { open++; continue; } if(open == 0) { close++; } else { open--; } } System.out.println(input.length() - open - close); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
9777e3ea7f5c9481c9af73b36fc2d70f
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.Scanner; public class RegualrBracket { /** * @param args */ public static void main(String[] args) { Scanner scan = new Scanner(System.in); String n = scan.next(); int countL = 0; int countR = 0; boolean found = false; for (int i = 0; i < n.length(); i++) { if (n.charAt(i) == '(') { countL++; found = true; } else { if (found) { countR++; if (countR >= countL) { found = false; } } } } System.out.println(Math.min(countL, countR)*2); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
d70c4a5dc187a06591fa56863a2cca6b
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; public class BRegularBracketSequence { private static class Stack { private int top, capacity; private char[] a; Stack(int n) { capacity = n; top = -1; a = new char[capacity]; } boolean push(char ch) { top++; if (top >= capacity) return false; a[top] = ch; return true; } char pop() { top--; return a[top + 1]; } boolean isEmpty() { if (top == -1) return true; return false; } int size() { return top + 1; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String string = sc.next(); int n =string.length(); int ans = 0; char[] a = new char[n]; a = string.toCharArray(); Stack stack = new Stack(n); for(int i= 0; i<n; i++){ if(a[i]=='('){ stack.push(a[i]); continue; } if(!stack.isEmpty()) stack.pop(); else ans++; } if(!stack.isEmpty()) ans+=stack.size(); System.out.println(n-ans); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
9907f6bfe11a92b0b1bd64ed1ceadfcf
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
//package codeforces; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.StreamTokenizer; import java.util.Scanner; public class CodeForces { public void run() throws IOException { Scanner sc = new Scanner(System.in); String s = sc.next(); int op = 0, match = 0; int n = s.length(); for (int i = 0; i < n; i++) { if (s.charAt(i) == '(') { op++; } else { if (op > 0) { op--; match++; } } } System.out.println(2 * match); } public static void main(String[] args) throws FileNotFoundException, IOException { CodeForces cf = new CodeForces(); cf.run(); } } class MyScanner { private StreamTokenizer st; public MyScanner(InputStream is) { st = new StreamTokenizer(is); } public MyScanner(File f) throws FileNotFoundException { BufferedReader in = new BufferedReader(new FileReader(f)); st = new StreamTokenizer(in); } public int nextInt() throws IOException { st.nextToken(); return ((int) st.nval); } public double nextDouble() throws IOException { st.nextToken(); return (st.nval); } public String nextString() throws IOException { st.nextToken(); return (st.sval); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
59dbaff07d16772609cfcea8242a38ec
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.*; import java.util.*; public final class regular_br_sq { static Scanner sc=new Scanner(System.in); static PrintWriter out=new PrintWriter(System.out); public static void main(String args[]) throws Exception { char[] a=sc.next().toCharArray(); List<Integer> list=new ArrayList<Integer>(); int cnt=0; for(int i=0;i<a.length;i++) { if(a[i]=='(') { list.add(i); } else if(list.size()>0) { list.remove(list.size()-1); cnt+=2; } } out.println(cnt); out.close(); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 7
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output