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
b5776a223d0c77c1971b27779904359b
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
//package codeforces; import java.util.Scanner; public class Codechef { public static Scanner scan = new Scanner(System.in); public static void main(String[] args) { int testCases = scan.nextInt(); for (int vedant = 0; vedant < testCases; vedant++) { solve(); } } private static void solve() { // Solution goes over here. String str = scan.next(); boolean flag = str.charAt(0) == 'A' && str.charAt(str.length() - 1) == 'B'; int aCount = 0; int bCount = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == 'A') { aCount++; } else { if (aCount > bCount) { bCount++; } else { flag = false; break; } } } if (flag && aCount >= bCount) { System.out.println("YES"); } else { System.out.println("NO"); } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
872a0c231956359ae9be8c017a9a9e19
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
//Some of the methods are copied from GeeksforGeeks Website import java.util.*; import java.lang.*; import java.io.*; public class Main { //static Scanner sc=new Scanner(System.in); //static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static long mod = (long)(1e9)+ 7; static int max_num=(int)1e5+5; static int count(String a, String b) { int m = a.length(); int n = b.length(); // Create a table to store // results of sub-problems int lookup[][] = new int[m + 1][n + 1]; // If first string is empty for (int i = 0; i <= n; ++i) lookup[0][i] = 0; // If second string is empty for (int i = 0; i <= m; ++i) lookup[i][0] = 1; // Fill lookup[][] in // bottom up manner for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { // If last characters are // same, we have two options - // 1. consider last characters // of both strings in solution // 2. ignore last character // of first string if (a.charAt(i - 1) == b.charAt(j - 1)) lookup[i][j] = lookup[i - 1][j - 1] + lookup[i - 1][j]; else // If last character are // different, ignore last // character of first string lookup[i][j] = lookup[i - 1][j]; } } return lookup[m][n]; } public static void main (String[] args) throws java.lang.Exception { try{ /* Collections.sort(al,(a,b)->a.x-b.x); Collections.sort(al,Collections.reverseOrder()); long n=sc.nextLong(); String s=sc.next(); char a[]=s.toCharArray(); StringBuilder sb=new StringBuilder(); map.put(a[i],map.getOrDefault(a[i],0)+1); out.println("Case #"+tt+": "+ans ); */ int t = sc.nextInt(); for(int tt=1;tt<=t;tt++) { String s=sc.next(); char a[]=s.toCharArray(); int n=a.length; int b=0; boolean f=true; for(char c : a) { if(c=='B') b++; } if(b==0) f=false; if(a[0]!='A') f=false; if(a[n-1]!='B') f=false; if(b>n-b) f=false; int x=0,y=0; for(int i=0;i<n;i++) { if(a[i]=='A') x++; else y++; if(y>x) f=false; } flag(f); } out.flush(); out.close(); } catch(Exception e) {} } /* ...SOLUTION ENDS HERE...........SOLUTION ENDS HERE... */ static void flag(boolean flag) { out.println(flag ? "YES" : "NO"); out.flush(); } /* Map<Long,Long> map=new HashMap<>(); for(int i=0;i<n;i++) { if(!map.containsKey(a[i])) map.put(a[i],1); else map.replace(a[i],map.get(a[i])+1); } Set<Map.Entry<Long,Long>> hmap=map.entrySet(); for(Map.Entry<Long,Long> data : hmap) { } Iterator<Integer> itr = set.iterator(); while(itr.hasNext()) { int val=itr.next(); } */ // static class Pair // { // int x,y; // Pair(int x,int y) // { // this.x=x; // this.y=y; // } // } // Arrays.sort(p, new Comparator<Pair>() // { // @Override // public int compare(Pair o1,Pair o2) // { // if(o1.x>o2.x) return 1; // else if(o1.x==o2.x) // { // if(o1.y>o2.y) return 1; // else return -1; // } // else return -1; // }}); static void print(int a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print(long a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print_int(ArrayList<Integer> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void print_long(ArrayList<Long> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static void DFS(ArrayList<Integer> graph[],boolean[] visited, int u) { visited[u]=true; int v=0; for(int i=0;i<graph[u].size();i++) { v=graph[u].get(i); if(!visited[v]) DFS(graph,visited,v); } } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static long nCr(long a,long b,long mod) { return (((fact[(int)a] * modInverse(fact[(int)b],mod))%mod * modInverse(fact[(int)(a - b)],mod))%mod + mod)%mod; } static long fact[]=new long[max_num]; static void fact_fill() { fact[0]=1l; for(int i=1;i<max_num;i++) { fact[i]=(fact[i-1]*(long)i); if(fact[i]>=mod) fact[i]%=mod; } } static long modInverse(long a, long m) { return power(a, m - 2, m); } static long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (long)((p * (long)p) % m); if (y % 2 == 0) return p; else return (long)((x * (long)p) % m); } static long sum_array(int a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static long sum_array(long a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void reverse_array(int a[]) { int n=a.length; int i,t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static void reverse_array(long a[]) { int n=a.length; int i; long t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static PrintWriter out=new PrintWriter(System.out); static int int_max=Integer.MAX_VALUE; static int int_min=Integer.MIN_VALUE; static long long_max=Long.MAX_VALUE; static long long_min=Long.MIN_VALUE; } // Thank You !
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
f992af8ad9c1a238dc22c06c69508ae3
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t>0) { String s=sc.next(); int l=s.length(); int flag=0; if(l>1 && s.charAt(0)=='A' && s.charAt(l-1)=='B') { int counta=1, countb=0; for(int i=1; i<l-1 && flag==0 ;i++) { if(s.charAt(i)=='A') counta++; if(s.charAt(i)=='B') countb++; if(countb>counta ) flag=1; } if(countb+1>counta) flag=1; } else flag=1; if(flag==1) System.out.println("no"); else System.out.println("yes"); t--; } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
1abce4ba7f0220856d69319e9d22c4dc
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class IloveAAAB { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { String s = br.readLine(); if(s.length() >= 2) { if(s.charAt(0) == 'A' && s.charAt(s.length()-1) == 'B') { int a = 0; int b = 0; Boolean correct = true; for (int i = 0; i < s.length(); i++) { if(s.charAt(i) == 'A') a++; else b++; if(b>a) { out.println("NO"); correct = false; break; } } if(correct) out.println("YES"); } else { out.println("NO"); } } else { out.println("NO"); } } out.flush(); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
6672838f6e7d2fac46bf3fa0ae99503e
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class IloveAAAB { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { String s = br.readLine(); if(s.length() >= 2) { if(s.charAt(0) == 'A' && s.charAt(s.length()-1) == 'B') { int a = 0; Boolean correct = true; for (int i = 0; i < s.length(); i++) { if(s.charAt(i) == 'A') a++; else a--; if(a == -1) { out.println("NO"); correct = false; break; } } if(correct) out.println("YES"); } else { out.println("NO"); } } else { out.println("NO"); } } out.flush(); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
205b660f59008fa893f02e1b9c4b1c2b
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; public class B { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { String str=sc.next(); boolean flag=true; if(str.length()==1 || str.charAt(0)=='B' || str.charAt(str.length()-1)=='A') flag=false; int count=1; for(int i=1;i<str.length();i++) { if(str.charAt(i)=='A') count++; else count--; if(count<0) { flag=false; break; } } System.out.println(flag?"YES":"NO"); } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
a22daba449262ee9618757f9b1fcaad1
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; import java.io.*; public class Main { // when can't think of anything -->> // 1. In sorting questions try to think about all possibilities like starting from start, end, middle. // 2. Two pointers, brute force. // 3. In graph query questions try to solve it reversely or try to process all the queries in a single parse. // 4. If order does not matter then just sort the data if constraints allow. It'll never harm. // 5. In greedy problems if you are just overwhelmed by the possibilities and stuck, try to code whatever you come up with. // 6. Try to solve it from back or reversely. // 7. When can't prove something take help of contradiction/pigeon hole principle. public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter writer = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { String s = sc.next(); int count = 0; if(s.length() < 2 || s.charAt(s.length() - 1) != 'B')writer.println("NO"); else { for(char c : s.toCharArray()) { if(c=='A')count++; else count--; if(count < 0)break; } if(count < 0)writer.println("NO"); else writer.println("YES"); } } writer.flush(); writer.close(); } private static boolean palindrome(String ss) { int i = 0; int j = ss.length() - 1; while(i<j) { if(ss.charAt(i) == ss.charAt(j)) { i++; j--; }else return false; } return true; } private static long power (long a, long n, long p) { long res = 1; while(n!=0) { if(n%2==1) { res=(res*a)%p; n--; }else { a= (a*a)%p; n/=2; } } return res; } private static boolean isPrime(int c) { for (int i = 2; i*i <= c; i++) { if(c%i==0)return false; } return true; } private static int find(int a , int arr[]) { if(arr[a] != a) return arr[a] = find(arr[a], arr); return arr[a]; } private static void union(int a, int b, int arr[]) { int aa = find(a,arr); int bb = find(b, arr); arr[aa] = bb; } private static int gcd(int a, int b) { if(a==0)return b; return gcd(b%a, a); } public static int[] readIntArray(int size, FastReader s) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = s.nextInt(); } return array; } 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; } } } class Pair implements Comparable<Pair>{ int a; int b; Pair(int a, int b){ this.a = a; this.b = b; } @Override public boolean equals(Object obj) { if(this == obj) return true; if(obj == null || obj.getClass()!= this.getClass()) return false; Pair pair = (Pair) obj; return (pair.a == this.a && pair.b == this.b); } @Override public int hashCode() { return Objects.hash(a,b); } @Override public int compareTo(Pair o) { if(this.a == o.a) { return Integer.compare(this.b, o.b); }else { return Integer.compare(this.a, o.a); } } @Override public String toString() { return this.a + " " + this.b; } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
92b0a737b10e66ce907d1416e0443519
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; import java.io.*; public class AAAB { public static void main(String[] args) throws IOException { BufferedReader buff = new BufferedReader(new InputStreamReader(System.in)); int B = Integer.parseInt(buff.readLine()); for (int N = 0; N < B; N++) { String str = buff.readLine(); boolean works = true; boolean b = false; int ACount = 0; for (int i = 0; i < str.length(); i++) { if(str.substring(i, i + 1).equals("B")) { b = true; } if (str.substring(i, i + 1).equals("A")) { ACount++; } if(str.substring(i, i+1).equals("B")) { ACount--; } if(ACount<0) { works = false; } } if(works&&b&&str.charAt(str.length()-1)=='B') { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
473e4131a4ccffa302335626ae465ec6
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.Scanner; // https://codeforces.com/problemset/problem/1672/B public class CF1672B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { String str = sc.next(); if(str.length()==1 || str.charAt(0)=='B' || str.charAt(str.length()-1)=='A') System.out.println("NO"); else { int a = 0, b = 0; byte humt = 1; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == 'A') a++; else { b++; if (b > a) { humt = 0; break; } } } System.out.println(humt == 1 ? "YES" : "NO"); } } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
89182927791cc24af80abe671995816d
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main{ static void solve() { String line=in.next(); if(line.charAt(line.length()-1)=='A'){out.println("NO");return;} int a=0,b=0; for(int i=0;i<line.length();i++){ if(line.charAt(i)=='A'){ a++; }else{ if(a==0){out.println("NO"); return;} b++; a--; } } if(b==0){out.println("NO"); return;} out.println("YES"); } public static void main(String[] args) throws Exception { int cnt=in.nextInt(); while (cnt-->0){ solve(); } out.flush(); } static class FastReader{ StringTokenizer st; BufferedReader br; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st==null||!st.hasMoreElements()){ try { st=new StringTokenizer(br.readLine()); }catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
6d46f1409f96aeba375fe4f3d285bfae
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
/*----------------------------------------- Created By : Piyush On 29-04-2022 at 14:52 --------------------------------------------*/ import java.util.*; import java.io.*; import java.math.*; public class AAAB { static final int MOD = (int) 1e9 + 7; static FastScanner fs = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int t = fs.nextInt(); while (t-- > 0) solve(); out.close(); } private static void solve() { String s = fs.next(); int a = 0 , b = 0; for (int i = 0; i <s.length(); i++) { if(s.charAt(i) == 'A') a++; else b++; if(b > a){ out.println("NO"); return; } } if(a == 0 && b != 0 || b == 0 && a != 0 || s.endsWith("A")) out.println("NO"); else out.println("YES"); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
d2c67e11b184f8259d4a81ef75c95568
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; import javax.swing.plaf.synth.SynthDesktopIconUI; import java.lang.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); for(int i=0;i<n;i++){ String s = sc.nextLine(); int cnt=0; int c = 0; char ch[] = new char[s.length()]; for(int j=0;j<s.length();j++){ ch[j] = s.charAt(j); if(ch[j]=='A'){ cnt++; } else{ cnt--; } if(cnt<0){ c=1; } } if(c==0 && ch[s.length()-1]=='B'){ System.out.println("YES"); } else{ System.out.println("NO"); } } } public static String Ssort(String s) { char tempArray[] = s.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } public static int smallest(int x, int y,int z ) { return Math.min(Math.min(x, y), z); } public static int largest(int x,int y, int z) { return Math.max(Math.max(x, y), z); } public static int average(int x,int y,int z) { return(x+y+z)/3; } public static int sumdigit( long x) { int result = 0; while(x>0){ result+=x%10; x/=10; } return result; } public static String isPowerOfTwo(long n) { if (n == 0) return ("Yes"); while (n != 1) { if (n % 2 != 0) return ("Yes"); n = n / 2; } return ("No"); } public static char[] swap(String s) { char ch[] = s.toCharArray(); char temp = ch[0]; ch[0] = ch[s.length()-1]; ch[s.length()-1] = temp; return ch; } public static boolean perfectsquare(int x) { if (x >= 0) { int sr = (int)Math.sqrt(x); return ((sr * sr) == x); } return false; } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
97048cce98e6b2f76508ba768f0035e1
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; import java.util.Stack; /** * iloveaaab */ public class iloveaaab { static String tolove(String str) { int sum = 0; if (str.charAt(str.length() - 1) != 'B') return "No"; for (char ch : str.toCharArray()) { if (ch == 'A') sum++; if (ch == 'B') sum--; if (sum < 0) return "No"; } if (sum < 0) return "No"; else return "yes"; } // static String tolove(String str) { // if (str.length() == 1) // return "NO"; // if (str.charAt(str.length() - 1) != 'B' || str.charAt(0) == 'B') // return "NO"; // for (int i = 1; i < str.length(); i++) { // if (str.charAt(i - 1) == 'B' && str.charAt(i) == 'B') { // return "NO"; // } // } // return "YES"; // } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testcase = sc.nextInt(); sc.nextLine(); List<String> res = new ArrayList<>(); while (testcase-- > 0) { String str = sc.nextLine(); res.add(tolove(str)); } Iterator<String> it = res.iterator(); while (it.hasNext()) { System.out.println(it.next()); } sc.close(); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
bbade857273e98d4fee42a198d61528c
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; import java.io.*; public class Main { public static Scanner sc = new Scanner(System.in); public static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws Exception { int t = sc.nextInt(); while( t > 0 ) { solve(); t--; } pw.flush(); } static void solve(){ String S = sc.next(); char[] s = S.toCharArray(); int N = S.length(); int[] cnt = {0, 0}; for( char c : s ) { cnt[c-'A']++; if( cnt[0] < cnt[1] ) { pw.println("NO"); return; } } if( s[0] == 'A' && s[N-1] == 'B' && cnt[0] >= cnt[1] ) pw.println("YES"); else pw.println("NO"); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
a6d2a79b3011ab991b62af2c9bc830eb
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); // System.out.println("------------"); for(int i = 0; i < n; i++) { String input = sc.nextLine(); String[] inputArr = input.trim().split(""); if(inputArr.length == 1) { System.out.println("NO"); }else if(!inputArr[inputArr.length - 1].equals("B")) { System.out.println("NO"); }else { System.out.println(stackChecker(inputArr)); } // if(input.contains("BB")) { // System.out.println("NO"); // }else if(input.length() == 1) { // System.out.println("NO"); // }else if(!inputArr[inputArr.length - 1].equals("B")) { // System.out.println("NO"); // } // else if(inputArr[0].equals("B")) { // System.out.println("NO"); // } // else { // System.out.println("YES"); // } } } public static String stackChecker(String[] arr) { String[] stack = new String[arr.length]; int sp = -1; for(int i = 0; i < arr.length; i++) { if(arr[i].equals("A")) { sp += 1; stack[sp] = arr[i]; }else { if(sp == -1) { return "NO"; }else { sp--; } } } return "YES"; } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
95c1b69ee4bdbd842d409fa8a7b7a1e7
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
// 1672B import java.util.*; public class ILoveAAAB { public static void main (String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); in.nextLine(); for (int i = 0; i < t; i++) { String s = in.nextLine(); if (s.length() < 2 || s.charAt(0) == 'B' || s.charAt(s.length() - 1) != 'B') { System.out.println("NO"); continue; } int c = 1; for (int j = 1; j < s.length(); j++) { if (s.charAt(j) == 'B') { c--; } else if (s.charAt(j) == 'A') { c++; } if (c < 0) { break; } } if (c >= 0) System.out.println("YES"); else System.out.println("NO"); } in.close(); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
26bc26ce4207e3f7b269e701ec8f617a
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class B_I_love_AAAB { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); long t = Long.parseLong(br.readLine()); while (t-- > 0) { boolean can = true; String s = br.readLine(); long ca = 0, cb = 0; if (s.charAt(0) == 'B' || s.charAt(s.length()-1)=='A') { can = false; } if(can) for (int i = 0; i < s.length()-1; i++) { if(s.charAt(i)=='A'){ ca++; } if(s.charAt(i+1)!='A'){ cb++; }else{ if(cb > ca){ can = false; break; } } } if(can && cb <= ca){ System.out.println("YES"); }else System.out.println("NO"); } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
22e446f15f1dcbc91041e1bc2fc073e8
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class B_I_love_AAAB { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); long t = Long.parseLong(br.readLine()); while (t-- > 0) { boolean can = true; String s = br.readLine(); long ca = 0, cb = 0; if (s.charAt(0) == 'B' || s.charAt(s.length()-1)=='A') { can = false; } if(can) for (int i = 0; i < s.length()-1; i++) { if(s.charAt(i)=='A'){ ca++; } if(s.charAt(i+1)!='A'){ cb++; }else{ if(cb > ca){ can = false; break; }else{ continue; } } } if(can && cb <= ca && cb!=0){ System.out.println("YES"); }else System.out.println("NO"); } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
254caf970b189cfe4d3b7d58a5b6f2b0
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; import java.io.*; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.math.BigInteger; public final class Main{ static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(new FileWriter(problemName + ".out")); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } } static Kattio sc = new Kattio(); static long mod = 998244353l; static PrintWriter out =new PrintWriter(System.out); //Heapify function to maintain heap property. public static void swap(int i,int j,int arr[]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static void swap(int i,int j,long arr[]) { long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static void swap(int i,int j,char arr[]) { char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static String endl = "\n" , gap = " "; public static void main(String[] args)throws IOException { int t =ri(); // int MAK = 2500 * 2500; // long seive[] = new long[MAK + 1]; // for (int i = 1; i <= MAK; i++) { // for (int j = i; j <= MAK; j+=i) { // seive[j]++; // } // } // for (int i = 1; i <= MAK; i++) { // seive[i] += seive[i - 1]; // } int test_case = 1; while(t-->0) { // out.write("Case "+(test_case++)+":" + endl); // System.out.println(); solve(); } out.close(); } public static void solve()throws IOException { char s[] = rac(); boolean ans = true; int n = s.length; if(s[n-1] == 'A') { System.out.println("NO"); return; } int a = 0 , b =0; for(int i =0;i<n;i++) { if(s[i] == 'A') a++; else b++; if(b > a) { System.out.println("NO"); return; } } System.out.println("YES"); } public static boolean valid(String s) { System.out.println(s); if(s.length() == 1) return false; if(s.length() == 0) return true; if(s.charAt(s.length()-1) != 'B') return false; for(int i =0;i<s.length()-1;i++) { if(s.charAt(i) != 'A') return false; } return true; } public static boolean collinearr(long a[] , long b[] , long c[]) { return (b[1]-a[1])*(b[0]-c[0]) == (b[0]-a[0])*(b[1]-c[1]); } public static boolean callfun(int index , int n,int neg , int pos , String s) { if(neg < 0 || pos < 0) return false; if(index >= n) return true; if(s.charAt(0) == 'P') { if(neg <= 0) return false; return callfun(index + 1,n , neg-1 , pos , s.charAt(1) + "N"); } else { if(pos <= 0) return false; return callfun(index + 1 , n , neg , pos-1 , s.charAt(1) + "P"); } } public static void getPerm(int n , char arr[] , String s ,List<String>list) { if(n == 0) { list.add(s); return; } for(char ch : arr) { getPerm(n-1 , arr , s+ ch,list); } } public static int getLen(int i ,int j , char s[]) { while(i >= 0 && j < s.length && s[i] == s[j]) { i--; j++; } i++; j--; if(i>j) return 0; return j-i + 1; } public static int getMaxCount(String x) { char s[] = x.toCharArray(); int max = 0; int n = s.length; for(int i =0;i<n;i++) { max = Math.max(max,Math.max(getLen(i , i,s) , getLen(i ,i+1,s))); } return max; } public static double getDis(int arr[][] , int x, int y) { double ans = 0.0; for(int a[] : arr) { int x1 = a[0] , y1 = a[1]; ans += Math.sqrt((x-x1)*(x-x1) + (y-y1)*(y-y1)); } return ans; } public static long callfun(int day , int k, int limit,int n) { if(k > limit) return 0; if(day > n) return 1; long ans = callfun(day + 1 , k , limit, n)*k + callfun(day + 1,k+1,limit,n)*(k+1); return ans; } public static void primeDivisor(HashMap<Long , Long >cnt , long num) { for(long i = 2;i*i<=num;i++) { while(num%i == 0) { cnt.put(i ,(cnt.getOrDefault(i,0l) + 1)); num /= i; } } if(num > 2) { cnt.put(num ,(cnt.getOrDefault(num,0l) + 1)); } } public static int callfun(int arr[],int move , int index , Integer memo[][]) { // System.out.println("AT " + index); if(index >= arr.length-1) return 0; if(memo[index][move] != null) return memo[index][move]; if(move == 0) { int next = getNext(arr[index] , arr , index + 1); int dif = getNextDIF(arr[index] ,arr , index + 1); int ans = Integer.MAX_VALUE; if(next != -1) { int curans = callfun(arr,move ,next , memo); if(curans != Integer.MAX_VALUE) ans = Math.min(ans , curans + 1); } if(dif != -1){ int curans = callfun(arr , move + 1 , dif ,memo); if(curans != Integer.MAX_VALUE) ans = Math.min(ans , curans + 1); } return memo[index][move] = ans; } else { int next = getNext(arr[index] , arr , index + 1); int ans = Integer.MAX_VALUE; if(next != -1) { ans = callfun(arr,move , next , memo); if(ans != Integer.MAX_VALUE) ans++; } return memo[index][move] = ans; } } public static int getNext(int same , int arr[],int index) { for(int i = index;i<arr.length;i++) { if(arr[i]%2 == same%2) return i; } return -1; } public static int getNextDIF(int same , int arr[],int index) { for(int i = index;i<arr.length;i++) { if(arr[i]%2 != same%2) return i; } return -1; } public static boolean hasAll(String a , String b) { for(int i =0;i<b.length();i++) { boolean cur = false; for(int j =0;j<a.length();j++) { if(b.charAt(i) == a.charAt(j)) cur = true; } if(cur == false) return false; } return true; } public static String getAbsent(String s) { char arr[] = {'a' , 'e' , 'i' , 'o' ,'u'}; StringBuilder res = new StringBuilder(); for(int i =0;i<5;i++) { boolean has = false; for(int j =0;j<s.length();j++) { if(s.charAt(j) == arr[i]) has = true; } if(has == false)res.append(arr[i]); } return res.toString(); } public static String getCompressed(String x) { char s[] = x.toCharArray(); Arrays.sort(s); StringBuilder res = new StringBuilder(); HashSet<Character> set = new HashSet<>(); for(char ch : s) { if(set.contains(ch)) continue; set.add(ch); res.append(ch); } return res.toString(); } public static boolean callfun(char s[] , char t[] , int f1[] , int f2[]) { for(int i = 0;i<26;i++) { if(f1[i] > 0 && f2[i] == 0) { f1[i]--; return !callfun(s , t,f2 , f1); } } return false; } public static void primeFactors(long x ,HashMap<Long, Integer> map ) { int cnt= 0; while(x%2 == 0) { cnt++; x /=2; } if(cnt > 0) map.put(2l , cnt ); cnt = 0; for(long i =3;i*i<=x;i+=2) { cnt = 0; while(x%i == 0) { cnt++; x /= i; } if(cnt > 0) { map.put(i ,cnt); } cnt = 0; } if(x > 2) map.put(x , 1); } public static long callfun(long x,long c,HashMap<Long,Long> set , HashSet<Long> vis) { if(x == 1) return 1; long ans = x; long i = 1; while (pow(i ,c) <= pow(10l , 3l) * x) { long gc = gcd(pow(i,c), x); ans = Math.min(ans, pow(i,c)*x/(gc*gc)); i += 1; } return ans; // for(long i =1;i<=x;i++) { // long val = pow(i , c); // if(val > 10000000000l) { // return ans; // } // long _gcd = gcd(val , x); // long _lcm = val/_gcd*x; // long newx = _lcm/_gcd; // ans = Math.min(ans , newx); // } // return ans; } public static long callfun(int cursum , int n , int k , int m ,int index ,Long memo[][]) { if(n == 0 ) { if(cursum > k) return 0; return 1; } if(memo[index][cursum] != null) return memo[index][cursum]; long mod = (long)998244353; long ans = 0; for(int i =1;i<=m;i++) { long curans = callfun(cursum + i , n-1 ,k , m,index + 1,memo )%mod; ans = (ans + curans)%mod; } return memo[index][cursum] = ans; } public static boolean isSubsequene(char a[], char b[] ) { int i =0 , j = 0; while(i < a.length && j <b.length) { if(a[i] == b[j]) { j++; } i++; } return j >= b.length; } public static long fib(int n ,long M) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else { long[][] mat = {{1, 1}, {1, 0}}; mat = pow(mat, n-1 , M); return mat[0][0]; } } public static long[][] pow(long[][] mat, int n ,long M) { if (n == 1) return mat; else if (n % 2 == 0) return pow(mul(mat, mat , M), n/2 , M); else return mul(pow(mul(mat, mat,M), n/2,M), mat , M); } static long[][] mul(long[][] p, long[][] q,long M) { long a = (p[0][0]*q[0][0] + p[0][1]*q[1][0])%M; long b = (p[0][0]*q[0][1] + p[0][1]*q[1][1])%M; long c = (p[1][0]*q[0][0] + p[1][1]*q[1][0])%M; long d = (p[1][0]*q[0][1] + p[1][1]*q[1][1])%M; return new long[][] {{a, b}, {c, d}}; } public static long[] kdane(long arr[]) { int n = arr.length; long dp[] = new long[n]; dp[0] = arr[0]; long ans = dp[0]; for(int i = 1;i<n;i++) { dp[i] = Math.max(dp[i-1] + arr[i] , arr[i]); ans = Math.max(ans , dp[i]); } return dp; } public static void update(int low , int high , int l , int r, int val , int treeIndex ,int tree[]) { if(low > r || high < l || high < low) return; if(l <= low && high <= r) { System.out.println("At " +low + " and " + high + " ans ttreeIndex " + treeIndex); tree[treeIndex] += val; return; } int mid = low + (high - low)/2; update(low , mid , l , r , val , treeIndex*2 + 1, tree); update(mid + 1 , high , l , r , val , treeIndex*2 + 2 , tree); } static int colx[] = {1 ,-1, 0,0 , 1,1,-1,-1}; static int coly[] = {0 ,0, 1,-1,1,-1,1,-1}; public static void reverse(char arr[]) { int i =0 , j = arr.length-1; while(i < j) { swap(i , j , arr); i++; j--; } } public static long[] reverse(long arr[]) { long newans[] = arr.clone(); int i =0 , j = arr.length-1; while(i < j) { swap(i , j , newans); i++; j--; } return newans; } public static long inverse(long x , long mod) { return pow(x , mod -2 , mod); } public static int maxArray(int arr[]) { int ans = arr[0] , n = arr.length; for(int i =1;i<n;i++) { ans = Math.max(ans , arr[i]); } return ans; } public static long maxArray(long arr[]) { long ans = arr[0]; int n = arr.length; for(int i =1;i<n;i++) { ans = Math.max(ans , arr[i]); } return ans; } public static int minArray(int arr[]) { int ans = arr[0] , n = arr.length; for(int i =0;i<n;i++ ) { ans = Math.min(ans ,arr[i]); } return ans; } public static long minArray(long arr[]) { long ans = arr[0]; int n = arr.length; for(int i =0;i<n;i++ ) { ans = Math.min(ans ,arr[i]); } return ans; } public static int sumArray(int arr[]) { int ans = 0; for(int x : arr) { ans += x; } return ans; } public static long sumArray(long arr[]) { long ans = 0; for(long x : arr) { ans += x; } return ans; } public static long rl() { return sc.nextLong(); } public static char[] rac() { return sc.next().toCharArray(); } public static String rs() { return sc.next(); } public static char rc() { return sc.next().charAt(0); } public static int [] rai(int n) { int ans[] = new int[n]; for(int i =0;i<n;i++) { ans[i] = sc.nextInt(); } return ans; } public static long [] ral(int n) { long ans[] = new long[n]; for(int i =0;i<n;i++) { ans[i] = sc.nextLong(); } return ans; } public static int ri() { return sc.nextInt(); } public static int getValue(int num ) { int ans = 0; while(num > 0) { ans++; num = num&(num-1); } return ans; } public static boolean isValid(int x ,int y , int n,char arr[][],boolean visited[][][][]) { return x>=0 && x<n && y>=0 && y <n && !(arr[x][y] == '#'); } // public static Pair join(Pair a , Pair b) { // Pair res = new Pair(Math.min(a.min , b.min) , Math.max(a.max , b.max) , a.count + b.count); // return res; // } // segment tree query over range // public static int query(int node,int l , int r,int a,int b ,Pair tree[] ) { // if(tree[node].max < a || tree[node].min > b) return 0; // if(l > r) return 0; // if(tree[node].min >= a && tree[node].max <= b) { // return tree[node].count; // } // int mid = l + (r-l)/2; // int ans = query(node*2 ,l , mid ,a , b , tree) + query(node*2 +1,mid + 1, r , a , b, tree); // return ans; // } // // segment tree update over range // public static void update(int node, int i , int j ,int l , int r,long value, long arr[] ) { // if(l >= i && j >= r) { // arr[node] += value; // return; // } // if(j < l|| r < i) return; // int mid = l + (r-l)/2; // update(node*2 ,i ,j ,l,mid,value, arr); // update(node*2 +1,i ,j ,mid + 1,r, value , arr); // } public static long pow(long a , long b , long mod) { if(b == 1) return a; if(b == 0) return 1; long ans = pow(a , b/2 , mod)%mod; if(b%2 == 0) { return (ans*ans)%mod; } else { return ((ans*ans)%mod*a)%mod; } } public static long pow(long a , long b ) { if(b == 1) return a; if(b == 0) return 1; long ans = pow(a , b/2); if(b%2 == 0) { return (ans*ans); } else { return ((ans*ans)*a); } } public static boolean isVowel(char ch) { if(ch == 'a' || ch == 'e'||ch == 'i' || ch == 'o' || ch == 'u') return true; return false; } public static int getFactor(int num) { if(num==1) return 1; int ans = 2; int k = num/2; for(int i = 2;i<=k;i++) { if(num%i==0) ans++; } return Math.abs(ans); } public static int[] readarr()throws IOException { int n = sc.nextInt(); int arr[] = new int[n]; for(int i =0;i<n;i++) { arr[i] = sc.nextInt(); } return arr; } public static boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0); } public static boolean isPrime(long num) { if(num==1) return false; if(num<=3) return true; if(num%2==0||num%3==0) return false; for(long i =5;i*i<=num;i+=6) { if(num%i==0 || num%(i+2) == 0) return false; } return true; } public static boolean isPrime(int num) { // System.out.println("At pr " + num); if(num==1) return false; if(num<=3) return true; if(num%2==0||num%3==0) return false; for(int i =5;i*i<=num;i+=6) { if(num%i==0 || num%(i+2) == 0) return false; } return true; } // public static boolean isPrime(long num) { // if(num==1) return false; // if(num<=3) return true; // if(num%2==0||num%3==0) return false; // for(int i =5;i*i<=num;i+=6) { // if(num%i==0) return false; // } // return true; // } public static long gcd(long a , long b) { if (b == 0) return a; return gcd(b, a % b); } public static int gcd(int a , int b) { if (b == 0) return a; return gcd(b, a % b); } public static int get_gcd(int a , int b) { if (b == 0) return a; return gcd(b, a % b); } public static long get_gcd(long a , long b) { if (b == 0) return a; return gcd(b, a % b); } // public static long fac(long num) { // long ans = 1; // int mod = (int)1e9+7; // for(long i = 2;i<=num;i++) { // ans = (ans*i)%mod; // } // return ans; // } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
67b08a9133cb3be30670c692d010bf37
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
/*LoudSilence*/ import java.io.*; import java.util.*; import static java.lang.Math.*; public class Solution { /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ static FastScanner s = new FastScanner(); static FastWriter out = new FastWriter(); final static int mod = (int)1e9 + 7; final static int INT_MAX = Integer.MAX_VALUE; final static int INT_MIN = Integer.MIN_VALUE; final static long LONG_MAX = Long.MAX_VALUE; final static long LONG_MIN = Long.MIN_VALUE; final static double DOUBLE_MAX = Double.MAX_VALUE; final static double DOUBLE_MIN = Double.MIN_VALUE; final static float FLOAT_MAX = Float.MAX_VALUE; final static float FLOAT_MIN = Float.MIN_VALUE; /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ static class FastScanner{BufferedReader br;StringTokenizer st; public FastScanner() {if(System.getProperty("ONLINE_JUDGE") == null){try {br = new BufferedReader(new FileReader("E:\\Competitive Coding\\input.txt"));} catch (FileNotFoundException e) {br = new BufferedReader(new InputStreamReader(System.in));}}else{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());} List<Integer> readIntList(int n){List<Integer> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextInt()); return arr;} List<Long> readLongList(int n){List<Long> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextLong()); return arr;} int[] readIntArr(int n){int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = s.nextInt(); return arr;} long[] readLongArr(int n){long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = s.nextLong(); return arr;} String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}} static class FastWriter{private BufferedWriter bw;public FastWriter(){if(System.getProperty("ONLINE_JUDGE") == null){try {this.bw = new BufferedWriter(new FileWriter("E:\\Competitive Coding\\output.txt"));} catch (IOException e) {this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}}else{this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}} public void print(Object object) throws IOException{bw.append(""+ object);} public void println(Object object) throws IOException{print(object);bw.append("\n");} public void debug(int object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");} public void debug(long object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");} public void close() throws IOException{bw.close();}} public static void println(Object str) throws IOException{out.println(""+str);} public static void println(Object str, int nextLine) throws IOException{out.print(""+str);} /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public static ArrayList<Integer> seive(int n){ArrayList<Integer> list = new ArrayList<>();int arr[] = new int[n+1];for(long i = 2; i <= n; i++) {if(arr[(int)i] == 1) {continue;}else {list.add((int)i);for(long j = i*i; j <= n; j = j + i) {arr[(int)j] = 1;}}}return list;} public static long gcd(long a, long b){if(a > b) {a = (a+b)-(b=a);}if(a == 0L){return b;}return gcd(b%a, a);} public static void swap(int[] arr, int i, int j) {arr[i] = arr[i] ^ arr[j]; arr[j] = arr[j] ^ arr[i]; arr[i] = arr[i] ^ arr[j];} public static boolean isPrime(long n){if(n < 2){return false;}if(n == 2 || n == 3){return true;}if(n%2 == 0 || n%3 == 0){return false;}long sqrtN = (long)Math.sqrt(n)+1;for(long i = 6L; i <= sqrtN; i += 6) {if(n%(i-1) == 0 || n%(i+1) == 0) return false;}return true;} public static long mod_add(long a, long b){ return (a%mod + b%mod)%mod;} public static long mod_sub(long a, long b){ return (a%mod - b%mod + mod)%mod;} public static long mod_mul(long a, long b){ return (a%mod * b%mod)%mod;} public static long modInv(long a, long b){ return expo(a, b-2)%b;} public static long mod_div(long a, long b){return mod_mul(a, modInv(b, mod));} public static long expo (long a, long n){if(n == 0){return 1;}long recAns = expo(mod_mul(a,a), n/2);if(n % 2 == 0){return recAns;}else{return mod_mul(a, recAns);}} /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // Pair class public static class Pair<X extends Comparable<X>,Y extends Comparable<Y>> implements Comparable<Pair<X, Y>>{ X first; Y second; public Pair(X first, Y second){ this.first = first; this.second = second; } public String toString(){ return "( " + first+" , "+second+" )"; } @Override public int compareTo(Pair<X, Y> o) { int t = first.compareTo(o.first); if(t == 0) return second.compareTo(o.second); return t; } } /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // Code begins public static void solve() throws IOException { String str = s.nextLine(); int n = str.length(); if(n == 1){ println("NO"); return; } if(str.charAt(0) == 'B' || str.charAt(n-1) == 'A'){ println("NO"); return; } int a = 0; for(int i = 0; i < n; i++){ if(str.charAt(i) == 'A') a++; else{ if(a < 1){ println("NO"); return; } a--; } } println("YES"); } /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public static void main(String[] args) throws IOException { int test = s.nextInt(); for(int t = 1; t <= test; t++) { solve(); } out.close(); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
06b227e3f366b2169eb00e4f7bf7333e
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; import java.io.*; import java.lang.Math; public class aaab { public static void main(String[] args) throws Exception{ Scanner in = new Scanner(System.in); int t = in.nextInt(); while(t-- > 0){ String s = in.next(); boolean b = false; int n = 0; if(s.length() < 2) System.out.println("NO"); else if(s.charAt(0) == 'B' || s.charAt(s.length() - 1) == 'A') System.out.println("NO"); else{ for(int i = 0; i < s.length(); i++){ if(s.charAt(i) == 'A') n++; else { n--; if(n < 0){ System.out.println("NO"); b = true; break; } }; } if(!b && n < 0) System.out.println("NO"); else if(!b && n >= 0) System.out.println("YES"); } } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
f81a05ba71d589d22fd27255b06127b5
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st; while (t-- > 0) { // int n = Integer.parseInt(br.readLine()); char arr[] = br.readLine().toCharArray(); int n = arr.length; int af = 0; int bf = 0; for(int i = 0; i < n; i++) { if(arr[i] == 'A') af = 1; else bf = 1; } if((af+bf)!=2 || (arr[n-1]!='B')) { output.write("NO\n"); continue; } int ac = 0; int bc = 0; // af = 0; // bf = 0; boolean flag = true; if(arr[0] == 'A') ac++; else ac--; for(int i = 1; i < n; i++) { if(ac < 0) flag = false; if(arr[i] == 'A') ac++; else ac--; } if(ac < 0) flag = false; if(flag) output.write("YES\n"); else output.write("NO\n"); } output.flush(); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
e6cec5bf07d45409b9bc6d148a4f3022
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.Scanner; public class I_love_AAAB { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); while(t-- > 0) { String str = sc.nextLine(); boolean is_possible = true; int len = str.length(); if(str.charAt(len-1) != 'B') { is_possible = false; } else { int count_of_A = 0, count_of_B = 0; for(int i = 0 ; i < len ; i++) { if(str.charAt(i) == 'B') { count_of_B++; } else { count_of_A++; } if(count_of_A < count_of_B) { is_possible = false; break; } } } if(is_possible) { System.out.println("YES"); } else { System.out.println("NO"); } } sc.close(); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
4e4c108086d3bc7b0339986d2e563aa4
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.Scanner; public class Codeforces { public static void main(String[] args){ Scanner sc = new Scanner (System.in); int t = sc.nextInt(); while(t-- > 0){ String s = sc.next(); boolean b = true; if(s.length()==1){ b = false; } else{ if(s.charAt(s.length()-1)=='A'){ b = false; } if(s.charAt(0)=='B'){ b = false; } else{int k =0; int m =0; for(int i =0;i<s.length();i++){ if(s.charAt(i)=='A'){ k++; } else{m++; if(m>k){ b = false; } } } } } if(b){ System.out.println("Yes"); } else{ System.out.println("No"); } } }}
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
571f6f00d4630192dd428614e1d6399c
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; import java.io.*; public class Solution{ static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); static StringTokenizer st; public static void main(String[] YDSV) throws IOException{ //int t=1; int t=Integer.parseInt(br.readLine()); while(t-->0) Solve(); bw.flush(); } public static void Solve() throws IOException{ // st=new StringTokenizer(br.readLine()); // int n=Integer.parseInt(st.nextToken()); // st=new StringTokenizer(br.readLine()); // int[] ar=new int[n]; // for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken()); char[] ch=br.readLine().toCharArray(); int n=ch.length; if(ch[n-1]=='A' || ch[0]=='B'){ bw.write("NO\n"); return ; } int cb=0,ca=0; for(int i=0;i<n;i++){ if(ch[i]=='B') cb++; if(ch[i]=='A') ca++; if(ca<cb){ bw.write("NO\n"); return; } } bw.write("YES\n"); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
82640436216b862e6fd11ce603a62233
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; public class AAAB { public static void main(String[] args) { Scanner sn=new Scanner(System.in); int t=Integer.parseInt(sn.nextLine()); while(t-->0){ String s=sn.nextLine(); int len=s.length(); if(check(s, len)) System.out.println("YES"); else System.out.println("NO"); } } public static boolean check(String s,int len) { int na=0; int nb=0; if(len==1)return false; if(s.charAt(len-1)=='A')return false; if(s.charAt(0)=='B')return false; for(int i=0;i<len;i++) { if(s.charAt(i)=='A'){ na++; if(na<nb)return false; } else{ nb++; if(nb>na)return false; } } if(nb>na)return false; else return true; } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
d6c376b93033ce3c78100de30e6f3221
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static long mod = (int)1e9+7; static PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; O : while(t-->0) { char a[]=sc.next().toCharArray(); int acount=0,bcount=0; boolean flag=true; for(int i=0;i<a.length;i++){ if(a[i]=='A')acount++; else bcount++; if(bcount>acount)flag=false; } if(a[a.length-1]!='B')printN(); else if(!flag)printN(); else printY(); } out.flush(); } static void printN() { System.out.println("NO"); } static void printY() { System.out.println("YES"); } static int findfrequencies(int a[],int n) { int count=0; for(int i=0;i<a.length;i++) { if(a[i]==n) { count++; } } return count; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } static int[] EvenOddArragement(int a[]) { ArrayList<Integer> list=new ArrayList<>(); for(int i=0;i<a.length;i++) { if(a[i]%2==0) { list.add(a[i]); } } for(int i=0;i<a.length;i++) { if(a[i]%2!=0) { list.add(a[i]); } } for(int i=0;i<a.length;i++) { a[i]=list.get(i); } return a; } static int gcd(int a, int b) { while (b != 0) { int t = a; a = b; b = t % b; } return a; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static int DigitSum(int n) { int r=0,sum=0; while(n>=0) { r=n%10; sum=sum+r; n=n/10; } return sum; } static boolean checkPerfectSquare(int number) { double sqrt=Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static boolean isPowerOfTwo(int n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static boolean isPrime2(int n) { if (n <= 1) { return false; } if (n == 2) { return true; } if (n % 2 == 0) { return false; } for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2) { if (n % i == 0) { return false; } } return true; } static String minLexRotation(String str) { int n = str.length(); String arr[] = new String[n]; String concat = str + str; for(int i=0;i<n;i++) { arr[i] = concat.substring(i, i + n); } Arrays.sort(arr); return arr[0]; } static String maxLexRotation(String str) { int n = str.length(); String arr[] = new String[n]; String concat = str + str; for (int i = 0; i < n; i++) { arr[i] = concat.substring(i, i + n); } Arrays.sort(arr); return arr[arr.length-1]; } static class P implements Comparable<P> { int i, j; public P(int i, int j) { this.i=i; this.j=j; } public int compareTo(P o) { return Integer.compare(i, o.i); } } static class pair{ int i,j; pair(int x,int y){ i=x; j=y; } } static int binary_search(int a[],int value) { int start=0; int end=a.length-1; int mid=start+(end-start)/2; while(start<=end) { if(a[mid]==value) { return mid; } if(a[mid]>value) { end=mid-1; } else { start=mid+1; } mid=start+(end-start)/2; } return -1; } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
267f3e9a9acdb0e95c6aa5be4acf95c5
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { 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') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static boolean check(int arr[] , int x) { for(int i = 0 ; i < arr.length ; i++) { if(Math.abs(x+i-arr[i]) > 1) return false; } return true; } public static void main(String []args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); //int t = sc.nextInt(); while(t-- > 0) { String s = sc.nextLine(); int n = s.length(); if(s.length() < 2 || s.charAt(0) == 'B' || s.charAt(n-1) == 'A') System.out.println("NO"); else { int a = 0; boolean bl = true; for(int i = 0 ; i < n ; i++) { if(s.charAt(i) == 'A') a++; else a--; if(a < 0) { bl = false; break; } } if(bl) System.out.println("YES"); else System.out.println("NO"); } } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
c229fc23756aa1fd850dfc2786e5848d
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; import java.io.*; public class Practice { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new PrintWriter(System.out))); StringBuilder sb = new StringBuilder(); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); while (t --> 0) { st = new StringTokenizer(br.readLine()); String s = st.nextToken(); int len = s.length(); if (len == 1) sb.append("No"); else if (len == 2) sb.append(s.equals("AB") ? "Yes" : "No"); else if (len == 3) sb.append(s.equals("AAB") ? "Yes" : "No"); else { if (s.charAt(0) == 'B' || s.charAt(len - 1) == 'A') sb.append("No"); else { boolean flag = false; int a = 0; for (int i = 0; i < len; i++) { if (s.charAt(i) == 'A') a++; else { if (i == len - 1 && a == 0) flag = true; else { if (a == 0) flag = true; else { a--; } } } } sb.append(flag ? "No" : "Yes"); } } sb.append("\n"); } pw.println(sb.toString().trim()); pw.close(); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
650950ccae89184f2777471bd9a926f3
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; import java.io.*; public class A { 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 t = Integer.parseInt(br.readLine()); while (t-- > 0) { String s = br.readLine(); int n = s.length(); if (n == 1) { bw.write("NO\n"); continue; } boolean can = true; int b = 0, a = 0; for (int i = 0; i < n; i++) { if (s.charAt(i) == 'B') { if (a == 0) { can = false; break; } b++; a--; if (a < 0) { can = false; break; } } else { b = 0; a++; } } if (b == 0) { can = false; } if (can) { bw.write("YES\n"); } else { bw.write("NO\n"); } } bw.flush(); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
e62452cc80ef74c72f714b4fae664403
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class JaiShreeRam{ static Scanner in=new Scanner(); static long systemTime; static long mod = 1000000007; static ArrayList<ArrayList<Integer>> adj; static int seive[]=new int[1000001]; static long C[][]; static ArrayList<Integer> ans=new ArrayList<>(); public static void main(String[] args) throws Exception{ int z=in.readInt(); for(int test=1;test<=z;test++) { //setTime(); solve(); //printTime(); //printMemory(); } } static void solve() { char c[]=in.readString().toCharArray(); int n=c.length; if(c[n-1]=='A'||c[0]=='B') { print("NO"); return; } int a=0,b=0; for(int i=0;i<n;i++) { if(c[i]=='B') { b++; if(a<b) { print("NO"); return; } } else { a++; } } if(a<b) { print("NO"); return; } print("YES");/* int n=in.readInt(); int a[]=nia(n); int cnt=0; for(int i=1;i<n;i++) { if(a[i]==a[i-1]) { cnt++; a[i]=-1; } } print(cnt);*/ } static long pow(long n, long m) { if(m==0) return 1; else if(m==1) return n; else { long r=pow(n,m/2); if(m%2==0) return (r*r)%mod; else return (r*r*n)%mod; } } static long maxsumsub(ArrayList<Long> al) { long max=0; long sum=0; for(int i=0;i<al.size();i++) { sum+=al.get(i); if(sum<0) { sum=0; } max=Math.max(max,sum); } return max; } static long abs(long a) { return Math.abs(a); } static void ncr(int n, int k){ C= new long[n + 1][k + 1]; int i, j; for (i = 0; i <= n; i++) { for (j = 0; j <= Math.min(i, k); j++) { if (j == 0 || j == i) C[i][j] = 1; else C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; } } } static boolean isPalin(String s) { int i=0,j=s.length()-1; while(i<=j) { if(s.charAt(i)!=s.charAt(j)) { return false; } i++; j--; } return true; } static int knapsack(int W, int wt[],int val[], int n){ int []dp = new int[W + 1]; for (int i = 1; i < n + 1; i++) { for (int w = W; w >= 0; w--) { if (wt[i - 1] <= w) { dp[w] = Math.max(dp[w],dp[w - wt[i - 1]] + val[i - 1]); } } } return dp[W]; } static void seive() { Arrays.fill(seive, 1); seive[0]=0; seive[1]=0; for(int i=2;i*i<1000001;i++) { if(seive[i]==1) { for(int j=i*i;j<1000001;j+=i) { if(seive[j]==1) { seive[j]=0; } } } } } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static int[] nia(int n){ int[] arr= new int[n]; int i=0; while(i<n){ arr[i++]=in.readInt(); } return arr; } static long[] nla(int n){ long[] arr= new long[n]; int i=0; while(i<n){ arr[i++]=in.readLong(); } return arr; } static int[] nia1(int n){ int[] arr= new int[n+1]; int i=1; while(i<=n){ arr[i++]=in.readInt(); } return arr; } static Integer[] nIa(int n){ Integer[] arr= new Integer[n]; int i=0; while(i<n){ arr[i++]=in.readInt(); } return arr; } static Long[] nLa(int n){ Long[] arr= new Long[n]; int i=0; while(i<n){ arr[i++]=in.readLong(); } return arr; } static long gcd(long a, long b) { if (b==0) return a; return gcd(b, a%b); } static void print(long i) { System.out.println(i); } static void print(Object o) { System.out.println(o); } static void print(int a[]) { for(int i:a) { System.out.print(i+" "); } System.out.println(); } static void print(long a[]) { for(long i:a) { System.out.print(i+" "); } System.out.println(); } static void print(ArrayList<Long> a) { for(long i:a) { System.out.print(i+" "); } System.out.println(); } static void print(Object a[]) { for(Object i:a) { System.out.print(i+" "); } System.out.println(); } static void setTime() { systemTime = System.currentTimeMillis(); } static void printTime() { System.err.println("Time consumed: " + (System.currentTimeMillis() - systemTime)); } static void printMemory() { System.err.println("Memory consumed: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 + "kb"); } static class Scanner{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String readString() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } double readDouble() { return Double.parseDouble(readString()); } int readInt() { return Integer.parseInt(readString()); } long readLong() { return Long.parseLong(readString()); } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
c0e7dc6718dcf2d718df86d1a73bdf82
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.io.*; import java.util.*; public class Main { static long nod(long a, long b) { while (Math.min(a, b) != 0) { if (a > b) { a = a % b; } else b = b % a; } return a + b; } public static void main(String[] args) throws IOException { // br = new BufferedReader(new FileReader("pencils.in")); // out = new PrintWriter("pencils.out"); int t = nextInt(); for (int i = 0; i < t; i++) { char[] a = nextToken().toCharArray(); String ans = "YES"; if (a[0] == 'B' || a[a.length - 1] == 'A') ans = "NO"; int l=0; for (int j = 0; j < a.length && ans.equals("YES"); j++) { if(a[j]=='A'){ l++; } else { l--; if(l<0)ans="NO"; } } if(l<0)ans="NO"; out.println(ans); } out.close(); } static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer in = new StringTokenizer(""); public static boolean hasNext() throws IOException { if (in.hasMoreTokens()) return true; String s; while ((s = br.readLine()) != null) { in = new StringTokenizer(s); if (in.hasMoreTokens()) return true; } return false; } public static String nextToken() throws IOException { while (!in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public static long nextLong() throws IOException { return Long.parseLong(nextToken()); } public static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } static class Maincraft implements Comparable<Maincraft> { int x; int y; int z; int type; public Maincraft(int x, int y, int z, int type) { this.x = x; this.y = y; this.z = z; this.type = type; } @Override public int compareTo(Maincraft o) { if (x != o.x) return Integer.compare(x, o.x); else if (y != o.y) return -Integer.compare(y, o.y); else return -Integer.compare(z, o.z); } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
b683791605f64054ab2b5c5030834a17
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
// Generated by Code Flattener. // https://plugins.jetbrains.com/plugin/9979-idea-code-flattener import java.io.*; import java.util.StringTokenizer; public class Main { public void solve(FastReader in, FastWriter out) { String str = in.nextLine(); int n = str.length(); if (str.charAt(n - 1) == 'A') { out.println("NO"); return; } int count = 0; for (int i = 0; i < n; i++) { char ch = str.charAt(i); if (ch == 'A') { count++; } else { count--; if (count < 0) { out.println("NO"); return; } } } out.println("YES"); } public static void main(String[] args) { Main main = new Main(); boolean onlineJudge = System.getProperty("ONLINE_JUDGE") != null; Problem.Builder builder = Problem .builder() .solution(main::solve) .multiTest(true); if (onlineJudge) { builder.io(StreamPair.standard()); } else { builder.io(StreamPair.debug(main.getClass())); } builder.build().execute(); } private static class Problem { private final Executable solution; private final Executable init; private final boolean multiTest; private final InputStream inputStream; private final OutputStream outputStream; public Problem(Executable solution, Executable init, boolean multiTest, InputStream inputStream, OutputStream outputStream) { this.solution = solution; this.init = init; this.multiTest = multiTest; this.inputStream = inputStream; this.outputStream = outputStream; } public void execute() { if (solution == null) { throw new RuntimeException("please, set solution"); } FastReader in = new FastReader(inputStream); FastWriter out = new FastWriter(outputStream); if (init != null) { init.run(in, out); } if (!multiTest) { solution.run(in, out); } else { int times = in.nextInt(); for (int i = 0; i < times; i++) { solution.run(in, out); } } out.close(); } public static Builder builder() { return new Builder(); } public static class Builder { private Executable solution; private Executable init; private boolean multiTest; private InputStream inputStream; private OutputStream outputStream; private Builder() { } public Builder solution(Executable solution) { this.solution = solution; return Builder.this; } public Builder init(Executable init) { this.init = init; return Builder.this; } public Builder multiTest(boolean multiTest) { this.multiTest = multiTest; return Builder.this; } public Builder io(StreamPair pair) { this.inputStream = pair.getInputStream(); this.outputStream = pair.getOutputStream(); return Builder.this; } public Problem build() { return new Problem(solution, init, multiTest, inputStream, outputStream); } } } private static class FastReader { private final BufferedReader br; private StringTokenizer st; public FastReader(InputStream inputStream) { br = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public String nextLine() { try { String line = br.readLine(); if (line == null) { throw new RuntimeException("empty line"); } st = null; return line; } catch (IOException e) { throw new RuntimeException(e); } } } private static class FastWriter { final private PrintWriter printWriter; public FastWriter(OutputStream outputStream) { printWriter = new PrintWriter(new OutputStreamWriter(outputStream)); } public void println(String s) { printWriter.println(s); } public void close() { printWriter.close(); } } private static class StreamPair { private final InputStream inputStream; private final OutputStream outputStream; public StreamPair(InputStream inputStream, OutputStream outputStream) { this.inputStream = inputStream; this.outputStream = outputStream; } public static StreamPair standard() { return new StreamPair(System.in, System.out); } public static StreamPair debug(Class resourceClass) { try { File file = new File(resourceClass.getClassLoader().getResource("input.txt").getFile()); return new StreamPair(new FileInputStream(file), System.out); } catch (FileNotFoundException e) { throw new RuntimeException(e); } } public InputStream getInputStream() { return inputStream; } public OutputStream getOutputStream() { return outputStream; } } private interface Executable { void run(FastReader in, FastWriter out); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
9ddebc2b6e0c8bd534ae024dd4de6043
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; import java.io.*; public class B { static class Scan { private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in=System.in; } public int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(int arr[],int l1,int r1,int l2,int r2) { int tmp[]=new int[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(long arr[],int l1,int r1,int l2,int r2) { long tmp[]=new long[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void main(String args[]) throws IOException { Scan input=new Scan(); StringBuilder ans=new StringBuilder(""); int test=input.scanInt(); for(int tt=1;tt<=test;tt++) { String str=input.scanString(); if(str.length()==1) { ans.append("NO\n"); continue; } if(str.charAt(0)=='B') { ans.append("NO\n"); continue; } if(!str.contains("B")) { ans.append("NO\n"); continue; } if(str.charAt(str.length()-1)!='B') { ans.append("NO\n"); continue; } boolean is_pos=true; int a=0,b=0; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='A') { a++; } else { b++; } if(b>a) { is_pos=false; } } if(b>a) { is_pos=false; } if(is_pos) { ans.append("YES\n"); } else { ans.append("NO\n"); } } System.out.println(ans); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
93555dfd6e20c249e4ba1d2ecc6173cf
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; import java.lang.*; import java.math.BigInteger; import java.io.*; public class Main implements Runnable { public static void main(String[] args) { new Thread(null, new Main(), "whatever", 1 << 26).start(); } private FastScanner sc; private PrintWriter pw; public void run() { try { boolean isSumitting = true; // isSumitting = false; if (isSumitting) { pw = new PrintWriter(System.out); sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); } else { pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); sc = new FastScanner(new BufferedReader(new FileReader("input.txt"))); } } catch (Exception e) { throw new RuntimeException(); } int t = sc.nextInt(); // int t = 1; while (t-- > 0) { // sc.nextLine(); // System.out.println("for t=" + t); solve(); } pw.close(); } public long mod = 1_000_000_007; private class Pair { int first; long second; Pair(int first, long second) { this.first = first; this.second = second; } } private void solve() { String s = sc.next(); int n = s.length(); if (s.charAt(n - 1) != 'B' || s.charAt(0) == 'B') { pw.println("NO"); return; } int countA = 0; int countB = 0; for (int i = 0; i < n; i++) { if (s.charAt(i) == 'A') countA++; if (s.charAt(i) == 'B') { countB++; if (countA < countB) { pw.println("NO"); return; } } } pw.println("YES"); } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(BufferedReader bf) { reader = bf; tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String[] nextStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } } private static class Sorter { public static <T extends Comparable<? super T>> void sort(T[] arr) { Arrays.sort(arr); } public static <T> void sort(T[] arr, Comparator<T> c) { Arrays.sort(arr, c); } public static <T> void sort(T[][] arr, Comparator<T[]> c) { Arrays.sort(arr, c); } public static <T extends Comparable<? super T>> void sort(ArrayList<T> arr) { Collections.sort(arr); } public static <T> void sort(ArrayList<T> arr, Comparator<T> c) { Collections.sort(arr, c); } public static void normalSort(int[] arr) { Arrays.sort(arr); } public static void normalSort(long[] arr) { Arrays.sort(arr); } public static void sort(int[] arr) { timSort(arr); } public static void sort(int[] arr, Comparator<Integer> c) { timSort(arr, c); } public static void sort(int[][] arr, Comparator<Integer[]> c) { timSort(arr, c); } public static void sort(long[] arr) { timSort(arr); } public static void sort(long[] arr, Comparator<Long> c) { timSort(arr, c); } public static void sort(long[][] arr, Comparator<Long[]> c) { timSort(arr, c); } private static void timSort(int[] arr) { Integer[] temp = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(int[] arr, Comparator<Integer> c) { Integer[] temp = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(int[][] arr, Comparator<Integer[]> c) { Integer[][] temp = new Integer[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; } private static void timSort(long[] arr) { Long[] temp = new Long[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(long[] arr, Comparator<Long> c) { Long[] temp = new Long[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(long[][] arr, Comparator<Long[]> c) { Long[][] temp = new Long[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; } } public long fastPow(long x, long y, long mod) { if (y == 0) return 1; if (y == 1) return x % mod; long temp = fastPow(x, y / 2, mod); long ans = (temp * temp) % mod; return (y % 2 == 1) ? (ans * (x % mod)) % mod : ans; } public long fastPow(long x, long y) { if (y == 0) return 1; if (y == 1) return x; long temp = fastPow(x, y / 2); long ans = (temp * temp); return (y % 2 == 1) ? (ans * x) : ans; } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
54091ca300b9ec7ab01ae03707b89aec
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
/*LoudSilence*/ import java.io.*; import java.util.*; import static java.lang.Math.*; public class Solution { /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ static FastScanner s = new FastScanner(); static FastWriter out = new FastWriter(); final static int mod = (int)1e9 + 7; final static int INT_MAX = Integer.MAX_VALUE; final static int INT_MIN = Integer.MIN_VALUE; final static long LONG_MAX = Long.MAX_VALUE; final static long LONG_MIN = Long.MIN_VALUE; final static double DOUBLE_MAX = Double.MAX_VALUE; final static double DOUBLE_MIN = Double.MIN_VALUE; final static float FLOAT_MAX = Float.MAX_VALUE; final static float FLOAT_MIN = Float.MIN_VALUE; /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ static class FastScanner{BufferedReader br;StringTokenizer st; public FastScanner() {if(System.getProperty("ONLINE_JUDGE") == null){try {br = new BufferedReader(new FileReader("E:\\Competitive Coding\\input.txt"));} catch (FileNotFoundException e) {br = new BufferedReader(new InputStreamReader(System.in));}}else{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());} List<Integer> readIntList(int n){List<Integer> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextInt()); return arr;} List<Long> readLongList(int n){List<Long> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextLong()); return arr;} int[] readIntArr(int n){int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = s.nextInt(); return arr;} long[] readLongArr(int n){long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = s.nextLong(); return arr;} String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}} static class FastWriter{private BufferedWriter bw;public FastWriter(){if(System.getProperty("ONLINE_JUDGE") == null){try {this.bw = new BufferedWriter(new FileWriter("E:\\Competitive Coding\\output.txt"));} catch (IOException e) {this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}}else{this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}} public void print(Object object) throws IOException{bw.append(""+ object);} public void println(Object object) throws IOException{print(object);bw.append("\n");} public void debug(int object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");} public void debug(long object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");} public void close() throws IOException{bw.close();}} public static void println(Object str) throws IOException{out.println(""+str);} public static void println(Object str, int nextLine) throws IOException{out.print(""+str);} /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public static ArrayList<Integer> seive(int n){ArrayList<Integer> list = new ArrayList<>();int arr[] = new int[n+1];for(long i = 2; i <= n; i++) {if(arr[(int)i] == 1) {continue;}else {list.add((int)i);for(long j = i*i; j <= n; j = j + i) {arr[(int)j] = 1;}}}return list;} public static long gcd(long a, long b){if(a > b) {a = (a+b)-(b=a);}if(a == 0L){return b;}return gcd(b%a, a);} public static void swap(int[] arr, int i, int j) {arr[i] = arr[i] ^ arr[j]; arr[j] = arr[j] ^ arr[i]; arr[i] = arr[i] ^ arr[j];} public static boolean isPrime(long n){if(n < 2){return false;}if(n == 2 || n == 3){return true;}if(n%2 == 0 || n%3 == 0){return false;}long sqrtN = (long)Math.sqrt(n)+1;for(long i = 6L; i <= sqrtN; i += 6) {if(n%(i-1) == 0 || n%(i+1) == 0) return false;}return true;} public static long mod_add(long a, long b){ return (a%mod + b%mod)%mod;} public static long mod_sub(long a, long b){ return (a%mod - b%mod + mod)%mod;} public static long mod_mul(long a, long b){ return (a%mod * b%mod)%mod;} public static long modInv(long a, long b){ return expo(a, b-2)%b;} public static long mod_div(long a, long b){return mod_mul(a, modInv(b, mod));} public static long expo (long a, long n){if(n == 0){return 1;}long recAns = expo(mod_mul(a,a), n/2);if(n % 2 == 0){return recAns;}else{return mod_mul(a, recAns);}} /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // Pair class static class Pair implements Comparable<Pair>{ long f; long s; public Pair(long f, long s){ this.f = f; this.s = s; } public String toString(){ return "("+f+", "+s+")"; } @Override public int compareTo(Pair o) { if(f == o.f) return s-o.s < 0 ? -1 : 1; else return f-o.f < 0 ? -1 : 1; } } static class Triplet{ long f; long s; long t; public Triplet(long f, long s, long t){ this.f = f; this.s = s; this.t = t; } public String toString(){ return "("+f+", "+s+", "+t+")"; } } // public static class Pair<X extends Comparable<X>,Y extends Comparable<Y>> implements Comparable<Pair<X,Y>>{ // X first; // Y second; // public Pair(X first, Y second){ // this.first = first; // this.second = second; // } // public String toString(){ // return "( " + first+" , "+second+" )"; // } // @Override // public int compareTo(Pair<X, Y> o) { // int t = first.compareTo(o.first); // if(t == 0) return second.compareTo(o.second); // return t; // } // } /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // Code begins public static void solve() throws IOException { char arr[] = s.next().toCharArray(); if(arr.length==1){ println("NO"); return; } int a = 0; int b = 0; for(char ch : arr){ if(ch == 'A') a++; else b++; if(b > a){ println("NO"); return; } } if(arr[arr.length-1] == 'B'){ println("YES"); }else{ println("NO"); } } /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public static void main(String[] args) throws IOException { int test = s.nextInt(); for(int t = 1; t <= test; t++) { solve(); } out.close(); } } /*public static boolean allsame(int arr[]){ for(int i = 0; i < arr.length-1; i++){ if(arr[i] != arr[i+1]) return false; } return true; } public static List<List<Integer>> permutation(int arr[], int i){ if(i == 0){ List<List<Integer>> ans = new ArrayList<>(); List<Integer> li = new ArrayList<>(); li.add(arr[i]); ans.add(li); return ans; } int temp = arr[i]; List<List<Integer>> rec = permutation(arr, i-1); List<List<Integer>> ans = new ArrayList<>(); for(List<Integer> li : rec){ for(int j = 0; j <= li.size(); j++){ List<Integer> list = new ArrayList<>(); for(int ele: li){ list.add(ele); } list.add(j, temp); ans.add(list); } } return ans; }*/
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
b2044c7fe2597005b1260f6a72b045f1
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc=new Scanner(System.in); // System.out.println(sc.nextByte()); // foodanimal(sc); goodstrings(sc); } private static void goodstrings(Scanner scanner){ int testcase=scanner.nextInt(); for(int i=0;i<testcase;i++){ String s=scanner.next(); if(s.charAt(0)=='B'||s.charAt(s.length()-1)=='A')System.out.println("NO"); else{ boolean possbile=true; int acount=0; int bcount=0; for(int j=1;j<s.length();j++){ char f=s.charAt(j-1); char sec=s.charAt(j); if(f=='A')acount++; if(sec=='B')bcount++; if(f=='B'&&sec=='B' && acount<bcount){ possbile=false; System.out.println("NO"); break; } } if(possbile)System.out.println("YES"); } } }}
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
f5be7df1f6c718963531894007f9ce70
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; public class B_I_love_AAAB { static long mod = Long.MAX_VALUE; public static void main(String[] args) { OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); FastReader f = new FastReader(); int t = f.nextInt(); while(t-- > 0){ solve(f, out); } out.close(); } public static void solve(FastReader f, PrintWriter out) { String s2 = f.nextLine(); int n = s2.length(); if(s2.charAt(n-1) != 'B') { out.println("NO"); return; } int i = 0; int flag = 0; while(i < n) { if(s2.charAt(i) == 'A') { flag++; } else { flag--; } if(flag < 0) { out.println("NO"); return; } i++; } out.println("YES"); } // Sort an array public static void sort(int arr[]) { ArrayList<Integer> al = new ArrayList<>(); for(int i: arr) { al.add(i); } Collections.sort(al); for(int i = 0; i < arr.length; i++) { arr[i] = al.get(i); } } // Find all divisors of n public static void allDivisors(int n) { for(int i = 1; i*i <= n; i++) { if(n%i == 0) { System.out.println(i + " "); if(i != n/i) { System.out.println(n/i + " "); } } } } // Check if n is prime or not public static boolean isPrime(int n) { if(n < 1) return false; if(n == 2 || n == 3) return true; if(n % 2 == 0 || n % 3 == 0) return false; for(int i = 5; i*i <= n; i += 6) { if(n % i == 0 || n % (i+2) == 0) { return false; } } return true; } // Find gcd of a and b public static long gcd(long a, long b) { long dividend = a > b ? a : b; long divisor = a < b ? a : b; while(divisor > 0) { long reminder = dividend % divisor; dividend = divisor; divisor = reminder; } return dividend; } // Find lcm of a and b public static long lcm(long a, long b) { long lcm = gcd(a, b); long hcf = (a * b) / lcm; return hcf; } // Find factorial in O(n) time public static long fact(int n) { long res = 1; for(int i = 2; i <= n; i++) { res *= res * i; } return res; } // Find power in O(logb) time public static long power(long a, long b) { long res = 1; while(b > 0) { if((b&1) == 1) { res = (res * a)%mod; } a = (a * a)%mod; b >>= 1; } return res; } // Find nCr public static long nCr(int n, int r) { if(r < 0 || r > n) { return 0; } long ans = fact(n) / (fact(r) * fact(n-r)); return ans; } // Find nPr public static long nPr(int n, int r) { if(r < 0 || r > n) { return 0; } long ans = fact(n) / fact(r); return ans; } // sort all characters of a string public static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } // User defined class for fast I/O 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(); } boolean hasNext() { if (st != null && st.hasMoreTokens()) { return true; } String tmp; try { br.mark(1000); tmp = br.readLine(); if (tmp == null) { return false; } br.reset(); } catch (IOException e) { return false; } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } boolean nextBoolean() { return Boolean.parseBoolean(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextArray(int n) { int[] a = new int[n]; for(int i=0; i<n; i++) { a[i] = nextInt(); } return a; } } } /** Dec Char Dec Char Dec Char Dec Char --------- --------- --------- ---------- 0 NUL (null) 32 SPACE 64 @ 96 ` 1 SOH (start of heading) 33 ! 65 A 97 a 2 STX (start of text) 34 " 66 B 98 b 3 ETX (end of text) 35 # 67 C 99 c 4 EOT (end of transmission) 36 $ 68 D 100 d 5 ENQ (enquiry) 37 % 69 E 101 e 6 ACK (acknowledge) 38 & 70 F 102 f 7 BEL (bell) 39 ' 71 G 103 g 8 BS (backspace) 40 ( 72 H 104 h 9 TAB (horizontal tab) 41 ) 73 I 105 i 10 LF (NL line feed, new line) 42 * 74 J 106 j 11 VT (vertical tab) 43 + 75 K 107 k 12 FF (NP form feed, new page) 44 , 76 L 108 l 13 CR (carriage return) 45 - 77 M 109 m 14 SO (shift out) 46 . 78 N 110 n 15 SI (shift in) 47 / 79 O 111 o 16 DLE (data link escape) 48 0 80 P 112 p 17 DC1 (device control 1) 49 1 81 Q 113 q 18 DC2 (device control 2) 50 2 82 R 114 r 19 DC3 (device control 3) 51 3 83 S 115 s 20 DC4 (device control 4) 52 4 84 T 116 t 21 NAK (negative acknowledge) 53 5 85 U 117 u 22 SYN (synchronous idle) 54 6 86 V 118 v 23 ETB (end of trans. block) 55 7 87 W 119 w 24 CAN (cancel) 56 8 88 X 120 x 25 EM (end of medium) 57 9 89 Y 121 y 26 SUB (substitute) 58 : 90 Z 122 z 27 ESC (escape) 59 ; 91 [ 123 { 28 FS (file separator) 60 < 92 \ 124 | 29 GS (group separator) 61 = 93 ] 125 } 30 RS (record separator) 62 > 94 ^ 126 ~ 31 US (unit separator) 63 ? 95 _ 127 DEL */ // (a/b)%mod == (a * moduloInverse(b)) % mod; // moduloInverse(b) = power(b, mod-2);
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
9a745d6240b1c9e3e94d9095155aaea4
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Reader reader = new Reader(); int t = reader.nextInt(); while(t-- > 0){ String s = reader.next(); solve(s); } } private static void solve(String s) { var n = s.length(); if(n == 1 || s.charAt(0) == 'B' || s.charAt(n - 1) == 'A'){ System.out.println("No"); }else{ int a = 0; int b = 0; for(int i = 0;i<n;++i) { var c = s.charAt(i); a += c == 'A' ? 1 : 0; b += c == 'B' ? 1 : 0; if(b > a) { System.out.println("no"); return; } } System.out.println(b > a ? "no" : "yes"); } } static class Reader{ BufferedReader bufferedReader; StringTokenizer stringTokenizer; Reader(){ bufferedReader = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(stringTokenizer == null || !stringTokenizer.hasMoreElements()){ try{ stringTokenizer = new StringTokenizer(bufferedReader.readLine()); }catch (Exception e){ System.out.println(e.getMessage()); } } return stringTokenizer.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String s = ""; try{ s = bufferedReader.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
8023441caeeea344958a9692f242b86f
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; public class codeFr6 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-->0) { String st = s.next(); boolean b=(st.charAt(st.length()-1)=='B'); int sum=0; for (int i = 0;i<st.length(); i++){ if (st.charAt(i)=='A') sum++; else sum--; if (sum<0) b=false; } if (b==true) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
9be41f5caee535f00debaa68029e9b90
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; import java.io.*; import java.io.*; import java.util.*; import java.math.*; import static java.lang.Math.sqrt; import static java.lang.Math.floor; public class Solution { static class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; } public static int max(int []nums, int s, int e) { int max = Integer.MIN_VALUE; for(int i = s; i <= e; i++) { max = Math.max(max, nums[i]); } return max; } static int depth(TreeNode root) { if(root == null)return 0; int a = 1+ depth(root.left); int b = 1+ depth(root.right); return Math.max(a,b); } static HashSet<Integer>set = new HashSet<>(); static void deepestLeaves(TreeNode root, int cur_depth, int depth) { if(root == null)return; if(cur_depth == depth)set.add(root.val); deepestLeaves(root.left,cur_depth+1,depth); deepestLeaves(root.right,cur_depth+1,depth); } public static void print(TreeNode root) { if(root == null)return; System.out.print(root.val+" "); System.out.println("er"); print(root.left); print(root.right); } public static HashSet<Integer>original(TreeNode root){ int d = depth(root); deepestLeaves(root,0,d); return set; } static HashSet<Integer>set1 = new HashSet<>(); static void leaves(TreeNode root) { if(root == null)return; if(root.left == null && root.right == null)set1.add(root.val); leaves(root.left); leaves(root.right); } public static boolean check(HashSet<Integer>s, HashSet<Integer>s1) { if(s.size() != s1.size())return false; for(int a : s) { if(!s1.contains(a))return false; } return true; } static TreeNode subTree; public static void smallest_subTree(TreeNode root) { if(root == null)return; smallest_subTree(root.left); smallest_subTree(root.right); set1 = new HashSet<>(); leaves(root); boolean smallest = check(set,set1); if(smallest) { subTree = root; return; } } public static TreeNode answer(TreeNode root) { smallest_subTree(root); return subTree; } } static class Key<K1, K2> { public K1 key1; public K2 key2; public Key(K1 key1, K2 key2) { this.key1 = key1; this.key2 = key2; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Key key = (Key) o; if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) { return false; } if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) { return false; } return true; } @Override public int hashCode() { int result = key1 != null ? key1.hashCode() : 0; result = 31 * result + (key2 != null ? key2.hashCode() : 0); return result; } @Override public String toString() { return "[" + key1 + ", " + key2 + "]"; } } public static int sumOfDigits (long n) { int sum = 0; while(n > 0) { sum += n%10; n /= 10; } return sum; } public static void swap(int []ar, int i, int j) { for(int k= j; k >= i; k--) { int temp = ar[k]; ar[k] = ar[k+1]; ar[k+1] = temp; } } public static int findOr(int[]bits){ int or=0; for(int i=0;i<32;i++){ or=or<<1; if(bits[i]>0) or=or+1; } return or; } public static boolean[] getSieve(int n) { boolean[] isPrime = new boolean[n+1]; for (int i = 2; i <= n; i++) isPrime[i] = true; for (int i = 2; i*i <= n; i++) if (isPrime[i]) for (int j = i; i*j <= n; j++) isPrime[i*j] = false; return isPrime; } public static Long gcd(Long a, Long b) { if (a == 0) return b; return gcd(b%a, a); } static long nextPrime(long n) { boolean found = false; long prime = n; while(!found) { prime++; if(isPrime(prime)) found = true; } return prime; } // method to return LCM of two numbers static Long lcm(Long a, Long b) { return (a / gcd(a, b)) * b; } static boolean isPrime(long n) { if(n <= 1)return false; if(n <= 3)return true; if(n%2 == 0 || n%3 == 0)return false; for(int i = 5; i*i <= n; i+= 6) { if(n%i == 0 || n%(i+2) == 0) return false; } return true; } static int countGreater(int arr[], int n, int k){ int l = 0; int r = n - 1; // Stores the index of the left most element // from the array which is greater than k int leftGreater = n; // Finds number of elements greater than k while (l <= r) { int m = l + (r - l) / 2; // If mid element is greater than // k update leftGreater and r if (arr[m] > k) { leftGreater = m; r = m - 1; } // If mid element is less than // or equal to k update l else l = m + 1; } // Return the count of elements greater than k return (n - leftGreater); } static ArrayList<Integer>printDivisors(int n){ ArrayList<Integer>list = new ArrayList<>(); for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { // If divisors are equal, print only one if (n/i == i) list.add(i); else // Otherwise print both list.add(i); list.add(n/i); } } return list; } static void leftRotate(int l, int r,int arr[], int d) { for (int i = 0; i < d; i++) leftRotatebyOne(l,r,arr); } static void leftRotatebyOne(int l, int r,int arr[]) { int i, temp; temp = arr[l]; for (i = l; i < r; i++) arr[i] = arr[i + 1]; arr[r] = temp; } static class pairInPq<F extends Comparable<F>, S extends Comparable<S>> implements Comparable<pairInPq<F, S>> { private F first; private S second; public pairInPq(F first, S second){ this.first = first; this.second = second; } public F getFirst(){return first;} public S getSecond(){return second;} // All the code you already have is fine @Override public int compareTo(pairInPq<F, S> o) { int retVal = getSecond().compareTo(o.getSecond()); if (retVal != 0) { return retVal; } return getFirst().compareTo(o.getFirst()); } } static long modInverse(long a, long m) { long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient long q = a / m; long t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static TreeNode buildTree(TreeNode root,int []ar, int l, int r){ if(l > r)return null; int len = l+r; if(len%2 != 0)len++; int mid = (len)/2; int v = ar[mid]; TreeNode temp = new TreeNode(v); root = temp; root.left = buildTree(root.left,ar,l,mid-1); root.right = buildTree(root.right,ar,mid+1,r); return root; } static int LIS(int arr[], int n) { int lis[] = new int[n]; int i, j, max = 0; /* Initialize LIS values for all indexes */ for (i = 0; i < n; i++) lis[i] = 1; /* Compute optimized LIS values in bottom up manner */ for (i = 1; i < n; i++) for (j = 0; j < i; j++) if (arr[i] >= arr[j] && lis[i] < lis[j] + 1) lis[i] = lis[j] + 1; /* Pick maximum of all LIS values */ for (i = 0; i < n; i++) if (max < lis[i]) max = lis[i]; return max; } static void permuteString(String s , String answer) { if (s.length() == 0) { System.out.print(answer + " "); return; } for(int i = 0 ;i < s.length(); i++) { char ch = s.charAt(i); String left_substr = s.substring(0, i); String right_substr = s.substring(i + 1); String rest = left_substr + right_substr; permuteString(rest, answer + ch); } } static boolean isPowerOfTwo(long n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static class Pair1{ long x; long y; public Pair1(long x, long y) { this.x = x; this.y = y; } } static class Pair { int x; int y; // Constructor public Pair(int x, int y) { this.x = x; this.y = y; } } static class Sorting implements Comparator<Pair>{ public int compare(Pair p1, Pair p2){ if(p1.x==p2.x){ return p1.y-p2.y; } return p1.x - p2.x; } } static class Compare1{ static void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return p1.x - p2.x; } }); } } static int min; static int max; static LinkedList<Integer>[]adj; static int n; static void insertlist(int n) { for(int i = 0; i <= n; i++) { adj[i] = new LinkedList<>(); } } static int []ar; static boolean []vis; static HashMap<Key,Integer>map; static ArrayList<Integer>list; static int dfs(int parent, int child,LinkedList<Integer>[]adj) { list.add(parent); for(int a : adj[parent]) { if(vis[a] == false) { vis[a] = true; return 1+dfs(a,parent,adj); } } return 0; } static StringTokenizer st; static BufferedReader ob; static int [] readarrayInt( int n)throws IOException { st = new StringTokenizer(ob.readLine()); int []ar = new int[n]; for(int i = 0; i < n; i++) { ar[i] = Integer.parseInt(st.nextToken()); } return ar; } static long [] readarrayLong( int n)throws IOException { st = new StringTokenizer(ob.readLine()); long []ar = new long[n]; for(int i = 0; i < n; i++) { ar[i] = Long.parseLong(st.nextToken()); } return ar; } static int readInt()throws IOException { return Integer.parseInt(ob.readLine()); } static long readLong()throws IOException { return Long.parseLong(ob.readLine()); } static int nextTokenInt() { return Integer.parseInt(st.nextToken()); } static long nextTokenLong() { return Long.parseLong(st.nextToken()); } static int root(int u) { if(ar[u] == -1)return -1; if(u == ar[u]) { return u; } return root(ar[u]); } static Pair []pairar; static int numberOfDiv(long num) { int c = 0; for(int i = 1; i <= Math.sqrt(num ); i++) { if(num%i == 0) { long d = num/i; if( d == i)c++; else c += 2; } } return c; } static long ans; static int count; static boolean []computed; static void NoOfWays(int n) { if(n == 0 ) { count++; } if(n <= 0)return; for(int i = 1; i <= n; i++) { if(n+i <= n) NoOfWays(n-i); } } static boolean binarylistsearch( List<Integer>list, int l, int r, int s, int e) { if(s > e)return false; int mid = (s+e)/2; if(list.get(mid) >= l && list.get(mid) < r) { return true; }else if(list.get(mid) > r) { return binarylistsearch(list,l,r,s,mid-1); }else if(list.get(mid) < l) { return binarylistsearch(list,l,r,mid+1,e); } return false; } static int [][] readmatrix(int r, int c)throws IOException{ int [][]mat = new int[r][c]; for(int i = 0; i < r; i++) { st = new StringTokenizer(ob.readLine()); for(int j = 0; j < c; j++) { mat[i][j] = nextTokenInt(); } } return mat; } static HashSet<Integer>set1; static boolean possible; static int c = 0; static void isbeautiful(HashSet<Integer>s,int num, List<Integer>good, int i, int x, int count) { if(c == 2) { possible = true; return; } if(num >x || i == good.size())return; if(num > x)return; if(i >= good.size())return; for(int j = i; j < good.size(); j++) { if(!map.containsKey(new Key(num,good.get(j))) && !map.containsKey(new Key(good.get(j),num))){ if(s.contains(num) && s.contains(good.get(j))) map.put(new Key (num,good.get(j)),1); isbeautiful(s,num*good.get(j),good,i,x,count); } } } static long sum; static long mod; static void recur(HashSet<Integer>set,HashMap<Integer,HashSet<Integer>>map, int n, int j) { if(j > n)return; int v = 0; for(int a : set) { if(map.get(j).contains(a)) { v++; } } long d = map.get(j).size()-v; sum = (sum*d)%mod; HashSet<Integer> temp = map.get(j); for(int num : temp) { if(!set.contains(num)) { set.add(num); recur(set,map,n,j+1); set.remove(num); } } } static int key1; static int key2; static HashSet<Integer>set; public static TreeNode lowestCommonAncestor(TreeNode root) { if(root == null)return null; TreeNode left = lowestCommonAncestor(root.left); TreeNode right =lowestCommonAncestor(root.right); if(left == null && right != null) { System.out.println(right.val); return right; }else if(right == null && left != null) { System.out.println(left.val); return left; }else if(left == null && right == null) { return null; }else { System.out.println(root.val); return root; } } static ArrayList<Integer>res; static boolean poss = false; public static void recur1 (char []ar, int i) { if(i >= ar.length) { boolean isPalindrome = false; for(int k = 0; k < ar.length; k++) { for(int j = 0; j < ar.length; j++) { if(j-k+1 >= 5) { int x = k; int y = j; while(x < y && ar[x] == ar[y]) { x++; y--; } if(x == y || x > y) { isPalindrome = true; } } } } if(!isPalindrome) { poss = true; } } if(i < ar.length && ar[i] == '?' ) { ar[i] = '0'; recur1(ar,i+1); ar[i] = '?'; } if(i < ar.length && ar[i] == '?') { ar[i] = '1'; recur1(ar,i+1); ar[i] = '?'; } if(i < ar.length && ar[i] != '?') { recur1(ar,i+1); } } static int []theArray; static int rotate(int []ar, int element, int i) { int count = 0; while(ar[i] != element) { int last = ar[i]; count++; int prev = ar[1]; for(int j = 2; j <= i; j++) { int temp = ar[j]; ar[j] = prev; prev = temp; } ar[1] = prev; } return count; } static int []A; static long nth(long max, long min, int n){ long mod = 1000000007; if(max%min == 0){ System.out.println(min*n); return (min*n)%mod; }else{ long d = max/min; d++; long e = n/d; long ans = (max*e)%mod; long r = n%d; long div = (ans/min); long temp = (div*min); long f = (temp*r)%mod; ans = f; if(temp == ans){ ans += min; } System.out.println(ans); return ans; } } public static int index(int k, int l, int r) { if(r-l == 1) { if(ar[r] >= k && ar[l] < k) { return l; } }else if(l >= r){ return l; } int mid = (l+r)/2; if(ar[mid] >= k) { return index(k,l,mid-1); }else { return index(k,mid+1,r); } } public static int remSnakes(int start, int end, int k) { int rem = k-ar[end]; if(start+rem > end ) { return 0; }else { return 1+ remSnakes(start+rem,end-1,k); } } static int N; static int []tree = new int[2*N]; static void build(int []arr,boolean odd) { for(int i = 0; i < N; i++) { tree[N+i] = arr[i]; } for(int i = N-1; i > 0; --i) { int left = i << 1; int right = i << 1|1; if(odd) tree[i] = tree[i<<1]|tree[i<<1|1]; else tree[i] = tree[i<<1]^tree[i<<1|1]; } } // function to update a tree node static void updateTreeNode(int p, int value, boolean odd) { // set value at position p tree[p + N] = value; p = p + N; // move upward and update parents for (int i = p; i > 1; i >>= 1) if(odd) tree[i >> 1] = tree[i] | tree[i^1]; else tree[i >> 1] = tree[i]^tree[i^1]; } static int query(int l ,int r) { int res = 0; //loop to build the sum in the range for(l += N, r += N; l < r; l >>= 1, r >>= 1) { if((l&1) > 0) { res += tree[--r]; } if((r&1) > 0) { res += tree[l++]; } } return res; } static int sum1; static void min_line(int u, int min) { for(int i : adj[u]) { min_line(i,min); } System.out.println(u); } static class Key1 { public final int X; public final int Y; public Key1(final int X, final int Y) { this.X = X; this.Y = Y; } public boolean equals (final Object O) { if (!(O instanceof Key1)) return false; if (((Key1) O).X != X) return false; if (((Key1) O).Y != Y) return false; return true; } public int hashCode() { return (X << 16) + Y; } } static void solve() { int []points = new int[26]; int []top_pos = new int[26]; String []ar = {"ABC"}; Arrays.fill(top_pos, Integer.MAX_VALUE); for(int i = 0; i < ar.length; i++) { String s = ar[i]; for(int j = 0; j < s.length(); j++) { int pos = 97-(s.charAt(j)-'0'); points[pos] += (j); if(j < top_pos[pos]) { top_pos[pos] = j; } } } int i = 0; boolean []vis = new boolean[26]; StringBuilder sb = new StringBuilder(); while(true) { int min = Integer.MAX_VALUE; for(int j = 0; j < 26; j++) { if(vis[j] == false) { min = Math.min(min, points[j]); } } if(min == Integer.MAX_VALUE)break; PriorityQueue<pairInPq>pq = new PriorityQueue<>(); for(int j = 0; j < 26; j++) { if(points[j] == min) { vis[j] = true; char c = (char)(j + 'a'); pq.add(new pairInPq(c,top_pos[j])); } } while(pq.size() > 0) { pairInPq p = pq.poll(); char c = (char)p.first; sb.append(c); } } String res = sb.toString(); res = res.toUpperCase(); System.out.println(res); } static boolean al_sub(String s, String a) { int k = 0; for(int i = 0; i < s.length(); i++) { if(k == a.length())break; if(s.charAt(i) == a.charAt(k)) { k++; } } return k == a.length(); } static String result(String s, String a) { char []s_ar = s.toCharArray(); char []a_ar = a.toCharArray(); StringBuilder sb = new StringBuilder(); if(s.length() < a.length()) { for(int i = 0; i < s.length(); i++) { if(s_ar[i] == '?')s_ar[i] = 'a'; } }else { if(al_sub(s,a)) { return "-1"; }else { int k = 0; char element = 'z'; for(char i = 'a'; i <= 'e'; i++) { char []temp = s.toCharArray(); boolean pos = true;; for(int j = 0; j < s.length(); j++) { if(temp[j] == '?') { temp[j] = i; } } boolean sub = false; k = 0; for(int j = 0; j < s.length(); j++) { if(k == a.length()) { pos = false; break; } if(a_ar[k] == temp[j]) { k++; } } if(pos && k < a.length()) { element = i; break; } } if(element == 'z')return "-1"; for(int i = 0; i < s.length(); i++) { if(s_ar[i] == '?') { s_ar[i] = element; } sb.append(s_ar[i]); } return sb.toString(); } } return "-1"; } static void addNodes(ListNode l, int k) { if(k > 10 )return; ListNode temp = new ListNode(k); l.next = temp; addNodes(l.next,k+1); } static ListNode head; static int listnode_recur(ListNode tail) { if(tail == null )return 0; int n = listnode_recur(tail.next); max = Math.max(max, tail.val+head.val); head = head.next; return max; } static void recursion(int []fill, int len, int []valid, int k) { if(k == len) { int num = 0; for(int i = 0; i < fill.length; i++) { num = (num*10)+fill[i]; } System.out.println(num); if(!set.contains(num)) { set.add(num); } return; } for(int i = 0; i < valid.length; i++) { if(fill[k] == 0) { fill[k] = valid[i]; recursion(fill,len,valid,k+1); fill[k] = 0; }else { recursion(fill,len,valid,k+1); } } } static long minans(Long []ar,Long max) { Long one = 0l; Long two = 0l; for(int i = 0; i < ar.length; i++) { Long d = (max-ar[i])/2; two += d; one += (max-ar[i])%2; } Long ans = 0l; if(one >= two) { ans += (two*2); // System.out.println(one+" "+two+" "+ans); one -= two; if(one > 0) ans += (one*2)-1; }else { ans += (one*2); if(two > 0) { two -= one; Long d = two/3; Long temp = d*3; Long r = two%3; ans += temp; ans += d; if(r == 2) { ans += 3; }else if(r == 1){ ans += 2; } } } if(ans < 0)ans = 0l; // System.out.println(one+" "+two+" "+ans); return ans; } static int binarySearch(long []dif,long left, int s, int e) { if(s > e)return s; int mid = (s+e)/2; if(dif[mid] > left) { return binarySearch(dif,left,s,mid-1); }else { return binarySearch(dif,left,mid+1,e); } } public static ListNode addTwoNumbers(ListNode l1, ListNode l2) { int size1 = getLength(l1); int size2 = getLength(l2); ListNode head = new ListNode(1); // make sure length l1 >= length2 head.next = size1 < size2?helper(l2,l1,size2-size1):helper(l1,l2,size1-size2); if(head.next.val > 9) { head.next.val = head.next.val%10; return head; } return head.next; } public static int getLength(ListNode l1) { int count = 0; while(l1 != null) { l1 = l1.next; count++; } return count; } public static ListNode helper(ListNode l1, ListNode l2, int offset) { if(l1 == null)return null; ListNode result = offset == 0?new ListNode(l1.val+l2.val):new ListNode(l1.val); System.out.println(l1.val+" "+l2.val); ListNode post = offset == 0?helper(l1.next,l2.next,0):helper(l1.next,l2,offset-1); if(post != null && post.val > 9) { result.val += 1; post.val = post.val%10; } result.next = post; return result; } public static ListNode arrayToLinkedList(int []ar, ListNode head) { head = new ListNode(0); head.next = null; ListNode h = head; for(int i = 0; i < ar.length; i++) { ListNode cur = new ListNode(ar[i]); if(h == null) { h = cur; }else{ h.next = cur; h = h.next; } } return head.next; } static Long numLessThanN(Long n, int s, int e,Long []powerful) { if(s >= e) { if(powerful[s] > n)return powerful[s-1]; return powerful[s]; } int mid = (s+e)/2; if(powerful[mid] == n) { return powerful[mid]; }else if(powerful[mid] > n && powerful[mid-1] < n) { return powerful[mid-1]; }else if(powerful[mid] > n){ return numLessThanN(n,0,mid-1,powerful); }else { return numLessThanN(n,mid+1,e,powerful); } } static Long minimumX; static void minX(Long x, Long c) { Long p = 2l; Long a = x; if(x > minimumX)return; if(x == 1)return; while(p*p <= x) { int count = 0; Long num = (long)Math.pow(p, c); Long minx = lcm(num,x); minx = minx/(gcd(num,x)); minimumX = Math.min(minimumX, minx); // System.out.println(minimumX+" "+x+" "+p+" "+num); //System.out.println(minx+" "+"df"+" "+p); if(minx < x) { minX(minx,c); } p++; } } static boolean isConsecutive (List<Integer>list) { for(int i : list) { if(i > 3)return false; } int x = 0; int y = 0; for(int i = 0; i < list.size(); i++) { if(list.get(i) == 2) { x++; }else if(list.get(i) == 3)y++; } if(y >= 2 || x >= 3)return false; if(x > 0 && y > 0)return false; return true; } static int []ismean(int []ar, int one, int minusone, int []temp){ int n = ar.length; for(int i = 1; i < n-1; i++) { if(temp[i] == 1 && temp[i-1] == -1 && one > 0) { one--; temp[i+1] = 1; }else if(temp[i-1] == -1 && temp[i] == -1 && one > 0) { temp[i+1] = 1; one--; }else if(temp[i] == -1 && temp[i-1] == 1 && minusone > 0) { minusone--; temp[i+1] = -1; }else if(temp[i] == 1 && temp[i-1] == 1 && minusone > 0) { minusone--; temp[i+1] = -1; }else { return temp; } } return temp; } static boolean isnottwo(int []ar, int one, int minusone,int temp[]) { int n = ar.length; int []ans = ismean(ar,one,minusone,temp); if(ans[n-1] != -2)return true; return false; } public static long findClosest(long arr[], long target) { int n = arr.length; // Corner cases if (target <= arr[0]) return arr[0]; if (target >= arr[n - 1]) return arr[n - 1]; // Doing binary search int i = 0, j = n, mid = 0; while (i < j) { mid = (i + j) / 2; if (arr[mid] == target) return arr[mid]; /* If target is less than array element, then search in left */ if (target < arr[mid]) { // If target is greater than previous // to mid, return closest of two if (mid > 0 && target > arr[mid - 1]) return getClosest(arr[mid - 1], arr[mid], target); /* Repeat for left half */ j = mid; } // If target is greater than mid else { if (mid < n-1 && target < arr[mid + 1]) return getClosest(arr[mid], arr[mid + 1], target); i = mid + 1; // update i } } // Only single element left after search return arr[mid]; } // Method to compare which one is the more close // We find the closest by taking the difference // between the target and both values. It assumes // that val2 is greater than val1 and target lies // between these two. public static long getClosest(long val1, long val2, long target) { if (target - val1 >= val2 - target) return val2; else return val1; } static int gain(int i) { if(i == 0 || i == n) { return 0; } if(ar[i] > ar[i-1] && ar[i] > ar[i+1] || ar[i] < ar[i-1] && ar[i] < ar[i+1])return 1; return 0; } static int number_of_theif(String s ) { int c = 0; int n = s.length(); int zero = 0; for(char i : s.toCharArray()) { if(i == '?')c++; if(i == '0')zero++; } if(c == n)return n; if(zero == n)return 1; int index = -1; for(int i = 0; i < n; i++) { if(s.charAt(i) == '1') { index = i; } } if(index != -1) { int zero_index = -1; for(int j = index+1; j < n; j++) { if(s.charAt(j) == '0') { zero_index = j; break; } } if(zero_index == -1) { return n-index; }else { return zero_index-index+1; } }else { for(int i = 0; i < n; i++) { if(s.charAt(i) == '0') { return i+1; } } } return 1; } static List<List<Integer>>res0; static int minpath; static void recursion(List<Integer>l, int parent, LinkedList<Integer>[]adj) { if(adj[parent].size() == 0) { l.add(parent); res0.add(new ArrayList(l)); minpath++; return; }else { l.add(parent); recursion(l,adj[parent].get(0),adj); for(int i = 1; i < adj[parent].size(); i++) { recursion(new ArrayList<>(), adj[parent].get(i),adj); } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();} } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try {str = br.readLine();} catch (IOException e) {e.printStackTrace();} return str; } } static HashMap<Integer,Integer>Amap; static HashMap<Integer,Integer>Bmap; static void numberOfPerm(int index, int []A, int []B, int []C, long cur_ans, int n) { if(index == n) { sum += cur_ans%mod; //System.out.println(cur_ans); return; } Amap.remove(A[index]); Bmap.remove(B[index]); // System.out.println(set+" "+index); if(C[index] != 0) { numberOfPerm(index+1,A,B,C,cur_ans,n); // System.out.println(index); }else if(!set.contains(Amap.get(A[index])) && !set.contains(Bmap.get(A[index])) && !set.contains(Amap.get(B[index])) && !set.contains(Bmap.get(B[index])) && !set.contains(A[index]) && !set.contains(B[index])){ set.add(A[index]); set.add(B[index]); numberOfPerm(index+1,A,B,C,(cur_ans*2)%mod,n); }else { if((!set.contains(Amap.get(A[index])) && !set.contains(Bmap.get(A[index])) && !set.contains(A[index]))) { set.add(A[index]); numberOfPerm(index+1,A,B,C,cur_ans,n); }else if (!set.contains(Amap.get(B[index])) && !set.contains(Bmap.get(A[index])) && !set.contains(B[index])) { set.add(B[index]); // System.out.println("vampire"); numberOfPerm(index+1,A,B,C,cur_ans,n); }else { numberOfPerm(index+1,A,B,C,cur_ans,n); } } } static int [][]mat; static int k; public static void last_tree_down( int n, int m) { for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(mat[i][j] == k-1) { if(i-1 >= 0 && mat[i-1][j] == 0) { mat[i-1][j] = k; } if(i+1 < n && mat[i+1][j] == 0) { mat[i+1][j] = k; } if(j-1 >= 0 && mat[i][j-1] == 0) { mat[i][j-1] = k; } if(j+1 < n && mat[i][j+1] == 0) { mat[i][j+1] = k; } } } } boolean allTreesDown = true; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(mat[i][j] == 0) { allTreesDown = false; } } } if(!allTreesDown) { k++; last_tree_down(n,m); } } public static void main(String args[])throws IOException { ob = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); BufferedOutputStream out = new BufferedOutputStream(System.out); FastReader sc = new FastReader(); FileOutputStream out1 = new FileOutputStream("output.txt"); // BufferedReader ob1 = new BufferedReader(new FileReader("input.txt")); PrintWriter writer = new PrintWriter("output.txt", "UTF-8"); int t = sc.nextInt(); while(t --> 0) { String s = sc.next(); boolean pos = true; boolean B = false; int countA = 0; int countB = 0; for(int i = 0; i < s.length(); i++) { if(s.charAt(i) == 'B') { countB++; }else { countA++; } if(countB > countA) { pos = false; break; } } if(countB == 0 || !pos || s.charAt(s.length()-1) == 'A') { System.out.println("NO"); }else { System.out.println("YES"); } } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
fb66b286a6af25515bfa9116f24c8c64
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.Scanner; public class aa { public static void main(String[] args) { var in = new Scanner(System.in); int n = in.nextInt(); for(int i = 0; i < n; i++) { int nA = 0; int nB = 0; var word = in.next(); int m = word.length(); int j = 0; if('B' != word.charAt(m -1)){ System.out.println("NO"); continue; } while(j < m){ if(word.charAt(j) != 'A') { nB++; if(nB > nA) { System.out.println("NO"); break; } } else nA++; j++; } if(j == m) System.out.println("YES"); } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
0e3514def25a539f766cb6a228abaf24
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.security.cert.X509CRL; import java.util.*; import java.lang.*; import java.util.stream.Collector; import java.util.stream.Collectors; @SuppressWarnings("unused") public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static String OUTPUT = ""; //global //private final static long BASE = 998244353L; private final static int BASE = 1000000007; private final static int INF_I = 1001001001; private final static long INF_L = 1001001001001001001L; private final static int MAXN = 100100; private static List<Integer> palin = new ArrayList<>(); static void solve() { int ntest = readInt(); for (int test=0;test<ntest;test++) { char[] S = readString().toCharArray(); int cntA=0; boolean isOk = true, hasB = false; for (int i=0;i<S.length;i++) { if (S[i]=='A') cntA+=1; else { hasB |= (i==S.length-1); isOk &= (cntA>=1); cntA-=1; } } if (isOk && hasB) out.println("yes"); else out.println("no"); } } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); if (INPUT=="") { is = System.in; } else { File file = new File(INPUT); is = new FileInputStream(file); } if (OUTPUT == "") out = new PrintWriter(System.out); else out = new PrintWriter(OUTPUT); solve(); out.flush(); long G = System.currentTimeMillis(); } private static class Point<T extends Number & Comparable<T>> implements Comparable<Point<T>> { private T x; private T y; public Point(T x, T y) { this.x = x; this.y = y; } public T getX() {return x;} public T getY() {return y;} @Override public int compareTo(Point<T> o) { int cmp = x.compareTo(o.getX()); if (cmp==0) return y.compareTo(o.getY()); return cmp; } } private static class ClassComparator<T extends Comparable<T>> implements Comparator<T> { public ClassComparator() {} @Override public int compare(T a, T b) { return a.compareTo(b); } } private static class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> { public ListComparator() {} @Override public int compare(List<T> o1, List<T> o2) { for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) { int c = o1.get(i).compareTo(o2.get(i)); if (c != 0) { return c; } } return Integer.compare(o1.size(), o2.size()); } } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double readDouble() { return Double.parseDouble(readString()); } private static char readChar() { return (char)skip(); } private static String readString() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] readChar(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] readTable(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = readChar(m); return map; } private static int[] readIntArray(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = readInt(); return a; } private static long[] readLongArray(int n) { long[] a = new long[n]; for (int i=0;i<n;i++) a[i] = readLong(); return a; } private static int readInt() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long readLong() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
4e3a84b3fefee03d15ff7794f14283ba
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
/* Enjoy the code :> Modular Arithematic 1. (a + b)%c = ((a%c)+(b%c))%c 2. (a - c)%c = ((a%c)-(b%c) + c)%c 3. (a * c)%c = ((a%c)*(b*c))%c 4. (a / b)%c = ((a%c)*(b-1%c))%c Modular Exponetion --> time complexity O(logN) */ import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.io.*; import java.util.*; import java.math.*; public class Main { static int N = 1000000; static int M = 1000000007; public static void main(String[] args) throws Exception{ fileConnect(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); while(t-- > 0){ st = new StringTokenizer(br.readLine()); String s = st.nextToken(); boolean ans = s.endsWith("B"); boolean res = true; int sum = 0; for(int i=0; i<s.length(); i++){ if(s.charAt(i) == 'A') sum++; else sum--; if(sum < 0) { res = false; break; } } if(res && ans) System.out.println("Yes"); else System.out.println("No"); } } static int stringcost(String s){ int cost = 0; for(int i=0; i<s.length(); i++){ cost += (s.charAt(i) - '`'); } return cost; } // read array static int[] readArr(int N, BufferedReader br, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(br.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } // print array static void printArr(int[] arr, int n){ for(int i=0; i<n; i++){ System.out.print(arr[i] + " "); } System.out.println(); } // gcd static long gcd(long a, long b){ if(b == 0) return a; return gcd(b, a%b); } // lcm public static long lcm(long a, long b){ return (a * b)/gcd(a, b); } // binary exponentiation static long binaryExp(long x, long n){ // calcute x ^ n; if(n == 0){ return 1; } else if(n%2 == 0){ return binaryExp(x*x, n/2); }else { return x * binaryExp(x*x, (n-1)/2); } } // modular exponentiation static long modExp(long x, long n, long m){ // calcute (x ^ n)%m; if(n == 0){ return 1; } else if(n%2 == 0){ long temp = (x * x)%m; return modExp(temp, n/2, m)%m; }else { long temp = (x * x)%m; return (x * modExp(temp, (n-1)/2, m))%m; } } // number is prime of not static boolean isPrime(int n){ if(n == 0 || n == 1) return false; if(n == 2 || n == 3) return true; for(int i=2; i*i<=n; i++){ if(n%i == 0){ return false; } } return true; } /* seive of erotheneses - mark all prime between 1 to maxN */ static void seiveOfEroth(){ int maxN = 1000000; boolean[] prime = new boolean[maxN + 1]; // false for prime and true for non-prime prime[0] = prime[1] = true; for(int i=2; i<=maxN; i++){ if(!prime[i]){ for(int j=i*i; j<=maxN; j+=i){ prime[j] = true; } } } } /* prime factorisation optimise solution time complexity = O(sqrt(N)) */ static void primeFactor(int n){ if(n == 1) System.out.print(n); for(int i=2; i*i<=n; i++){ if(n == 1) return; int cnt = 0; if(n%i == 0){ while(n%i == 0){ cnt++; n = n/i; } if(n != 1) System.out.print(i + " ^ " + cnt + " + "); else { System.out.print(i + " ^ " + cnt); } } } if(n > 1){ System.out.print(n + " ^ 1"); } } /* prime factorisation using seive -- time complexity - O(logN) -- use when multiple testcases */ static void primeFactor2(int n){ if(n == 1) System.out.print(1); int[] seive = new int[N+1]; seive[0] = 0; seive[1] = 1; // this loop mark all the position with first prime number divisor. for(int i=2; i<=N; i++){ if(seive[i] == 0){ for(int j=i; j<=N; j+=i){ if(seive[j] == 0){ seive[j] = i; } } } } // end of for loop while(n != 1){ if(n/seive[n] != 1) System.out.print(seive[n] + "*"); else System.out.print(seive[n]); n = n/seive[n]; }// end of while } /* Calculate the power of matrix */ static long[][] powerMatix(long[][] matrix, int dim, int n){ // create identity matrix long[][] identity = new long[dim][dim]; for(int i=0; i<dim; i++){ identity[i][i] = 1; } // printMatrix(identity, dim); if(n==0) return identity; if(n==1) return matrix; // time complexity O(n) // overtime O(n*( n^3)) // for(int i=1; i<n; i++){ // matrix = matmat(matrix, matrix, dim); // } // now use binary exponenition concept // new time complexity O(logN*(N^3)) long[][] temp = identity; int cnt = 0; while(n > 1){ if(n%2 == 1){ temp = multimat(matrix, temp, dim); n--; }else { matrix = multimat(matrix, matrix, dim); n = n/2; } } matrix = multimat(matrix, temp, dim); return matrix; } // print the matrix static void printMatrix(long[][] matrix, int dim){ for(int i=0; i<dim; i++){ for(int j=0; j<dim; j++){ System.out.print(matrix[i][j] + " "); } System.out.println(); } } // matrix multiplication // time complexity O(n^3) // this is for square matrix. static long[][] multimat(long[][] matrix, long[][] matrix2, int dim){ long[][] result = new long[dim][dim]; for(int i=0; i<dim; i++){ for(int j=0; j<dim; j++){ for(int k=0; k<dim; k++){ result[i][j] = (result[i][j] + ((matrix[i][k] % M)* (matrix2[k][j]%M))%M)%M; } } } // for(int i=0; i<dim; i++){ // for(int j=0; j<dim; j++){ // matrix[i][j] = result[i][j]; // } // } // printMatrix(result, dim); return result; } // FileInputOutput static void fileConnect(){ try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
0a12e5051099b09b0733fbe8bc82772d
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.Scanner; public class Main { public static boolean function(String leitura){ boolean result = false; int size = leitura.length(); int geral = 0; int contador_A = 0; int contador_B = 0; for (int i = 0; i < size; i++) { char letra = leitura.charAt(i); if( letra == 'A'){ contador_A++; } else if(letra == 'B' ){ contador_B++; } if (contador_B > contador_A){ return false; } } if (leitura.charAt(0) != 'B' && leitura.charAt(size-1) !='A'){ result = true; } return result; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { String leitura = sc.next(); boolean check = function(leitura); if(check){ System.out.println("YES"); } else{ System.out.println("NO"); } } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
67fae901eea648475f00c3fd355cc4ad
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.io.*; import java.util.*; public class Solution { static int M = 1_000_000_007; static Random rng = new Random(); private static boolean testCase(String s) { int numA = 0; if (s.charAt(s.length() - 1) == 'A') return false; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'A') { numA++; } else { if (numA == 0) { return false; } else { numA--; } } } return true; } public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc. //in.nextLine(); for (int tt = 1; tt <= t; ++tt) { String s = in.next(); out.println(testCase(s) ? "YES" : "NO"); } out.close(); } private static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } boolean hasNext() { return st.hasMoreTokens(); } char[] readCharArray(int n) { char[] arr = new char[n]; try { br.read(arr); br.readLine(); } catch (IOException e) { e.printStackTrace(); } return arr; } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } private static void sort(int[] arr) { int temp, idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } private static void sort(long[] arr) { long temp; int idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } private static <T> void sort(T[] arr) { T temp; int idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } private static <T> void sort(T[] arr, Comparator<? super T> cmp) { T temp; int idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr, cmp); } private static <T extends Comparable<? super T>> void sort(List<T> list) { T temp; int idx; for (int i = list.size() - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = list.get(i); list.set(i, list.get(idx)); list.set(idx, temp); } Collections.sort(list); } private static <T> void sort(List<T> list, Comparator<? super T> cmp) { T temp; int idx; for (int i = list.size() - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = list.get(i); list.set(i, list.get(idx)); list.set(idx, temp); } Collections.sort(list, cmp); } static class DSU { int[] parent, rank; public DSU(int n) { parent = new int[n]; rank = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; } } public int find(int i) { if (parent[i] == i) { return i; } else { int res = find(parent[i]); parent[i] = res; return res; } } public boolean isSameSet(int i, int j) { return find(i) == find(j); } public void union(int i, int j) { int iParent = find(i), jParent = find(j); if (iParent != jParent) { if (rank[iParent] > rank[jParent]) { parent[jParent] = iParent; } else { parent[iParent] = jParent; if (rank[iParent] == rank[jParent]) { rank[jParent]++; } } } } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
68616a208eed49521709556e58dfb969
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; public class HelloWorld{ public static void main(String []args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ String str=sc.next(); if(str.length()<2){ System.out.println("No"); continue; } if(str.charAt(str.length()-1)!='B'){ System.out.println("No"); continue; } if(str.charAt(0)!='A'){ System.out.println("No"); continue; } int A=0; boolean flag=true; for(int i=0;i<str.length();i++){ char ch=str.charAt(i); if(ch=='A'){ A++; }else{ A--; } if(A<0){ flag=false; break; } } if(flag){ System.out.println("Yes"); }else{ System.out.println("No"); } } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
b6f07579d8e452f79db164dbe5dc961b
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; //import java.util.*; public class Solution { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-->0) { String s = br.readLine(); System.out.println(findWays(s)); } } private static String findWays(String s) { if(s.charAt(s.length()-1)=='A') return "NO"; if(s.charAt(0)=='B') return "NO"; int aCount = 0; int bCount = 0; for(int i=0;i<s.length();i++){ if(s.charAt(i)=='A') aCount++; else bCount++; if(bCount> aCount) return "NO"; } return "YES"; } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
0e084fe365012c2d746a2e9da227fd21
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Arrays; public class Main { public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); //solve (in, out); int tests = in.nextInt(); for (int i = 0;i < tests;i++) { solve (in, out); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } // main code static void solve (FastScanner in, PrintWriter out) { String s = in.next(); int count = 0; boolean ans = s.length() < 2 || s.charAt(s.length()-1) == 'A' ? false : true; for (int i = 0;i < s.length();++i) { count += s.charAt(i) == 'A' ? 1 : -1; if (count < 0) { ans = false; break; } } out.println(ans ? "YES" : "NO"); out.flush(); } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
2953b69e21287d2b1066f4cd84d6f790
train_108.jsonl
1650722700
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
256 megabytes
import java.util.*; public class Code1672B { //I love AAAB public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T=sc.nextInt(); for(int t=0; t<T; t++) { String str = sc.next(); int countA = 0, countB = 0; boolean res = true; if(str.length() < 2 || str.charAt(0) == 'B' || str.charAt(str.length()-1) == 'A') { res = false; } else { for(int i=0; i<str.length(); i++) { if(str.charAt(i) == 'A') countA++; else countB++; if(countA < countB) { res = false; break; } } } if(res) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Java 11
standard input
[ "constructive algorithms", "implementation" ]
0b9be2f076cfa13cdc76c489bf1ea416
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
1324c875137cf5e5eef375df0948d1ef
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.util.*; import java.io.*; public class Main { static ContestScanner sc = new ContestScanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); static long mod = (long) 1e9 + 7; public static void main(String[] args) throws Exception { //int T = sc.nextInt(); //for(int i = 0; i < T; i++)solve(); solve(); pw.flush(); } static int ask(int i){ if (i==0) return 0; System.out.println("? " + i); int temp = sc.nextInt(); if (temp==-1) System.exit(0); return temp; } public static void solve() { int n = sc.nextInt(); int left = -2; int right = (int)5e6; while (right - left > 1){ int mid = (right + left)/2; if(ask(mid) == 1){ right = mid; }else{ left = mid; } } int ans = (int)1e9; for(int i = 1; i <= n; i++){ int temp = ask(right / i); if(temp >= 1){ ans = Math.min(ans,temp*(right/i)); } } System.out.println("! " + ans); } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } /** * refercence : https://github.com/NASU41/AtCoderLibraryForJava/blob/master/ContestIO/ContestScanner.java */ class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width){ long[][] mat = new long[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width){ int[][] mat = new int[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width){ double[][] mat = new double[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width){ char[][] mat = new char[height][width]; for(int h=0; h<height; h++){ String s = this.next(); for(int w=0; w<width; w++){ mat[h][w] = s.charAt(w); } } return mat; } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
4ec8898e0adc2f3e4faa2168c47be542
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.io.BufferedReader; import java.io.Closeable; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.time.Clock; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.Random; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.function.Supplier; public class Solution { private void solve() throws IOException { int n = nextInt(); int maxWidth = (n + 1) * 2000 - 1; int res; { Pii t = findWidth(1, maxWidth); res = t.x * t.y; maxWidth = t.x; } for (int i = 2; i <= n; i++) { int w = maxWidth / i; if (w > 0) { int h = ask(w); if (h <= n) { res = Math.min(res, w*h); } } } result(res); } private Pii findWidth(int height, int maxWidth) throws IOException { int left = 0; int right = maxWidth; while (left < right) { int mid = (left + right) / 2; if (ask(mid) > height) { left = mid + 1; } else { right = mid; } } return new Pii(left, ask(left)); } private static class Pii implements Comparable<Pii> { // not today, evil hacker private static final int hashMultiplier = BigInteger.valueOf(new Random().nextInt(100000) + 100).nextProbablePrime().intValue(); private int x; private int y; public Pii(int x, int y) { this.x = x; this.y = y; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pii pii = (Pii) o; if (x != pii.x) return false; return y == pii.y; } public int hashCode() { return hashMultiplier * x + y; } public String toString() { return "(" + x + ", " + y + ")"; } public int compareTo(Pii o) { int c = Integer.compare(x, o.x); return c != 0 ? c : Integer.compare(y, o.y); } } private int ask(int w) throws IOException { if (w < 1) return Integer.MAX_VALUE-13; out.println("? " + w); out.flush(); int res = nextInt(); return res > 0 ? res: Integer.MAX_VALUE-13; } private void result(int sq) { out.println("! " + sq); out.flush(); } private static final boolean runNTestsInProd = false; private static final boolean printCaseNumber = false; private static final boolean assertInProd = false; private static final boolean logToFile = false; private static final boolean readFromConsoleInDebug = true; private static final boolean writeToConsoleInDebug = true; private static final boolean testTimer = false; private static Boolean isDebug = null; private BufferedReader in; private StringTokenizer line; private PrintWriter out; public static void main(String[] args) throws Exception { isDebug = Arrays.asList(args).contains("DEBUG_MODE"); if (isDebug) { log = logToFile ? new PrintWriter("logs/j_solution_" + System.currentTimeMillis() + ".log") : new PrintWriter(System.out); clock = Clock.systemDefaultZone(); } new Solution().run(); } private void run() throws Exception { in = new BufferedReader(new InputStreamReader(!isDebug || readFromConsoleInDebug ? System.in : new FileInputStream("input.txt"))); out = !isDebug || writeToConsoleInDebug ? new PrintWriter(System.out) : new PrintWriter("output.txt"); try (Timer totalTimer = new Timer("total")) { int t = 1;//runNTestsInProd || isDebug ? nextInt() : 1; for (int i = 0; i < t; i++) { if (printCaseNumber) { out.print("Case #" + (i + 1) + ": "); } if (testTimer) { try (Timer testTimer = new Timer("test #" + (i + 1))) { solve(); } } else { solve(); } if (isDebug) { out.flush(); } } } in.close(); out.flush(); out.close(); } private void println(Object... objects) { boolean isFirst = true; for (Object o : objects) { if (!isFirst) { out.print(" "); } else { isFirst = false; } out.print(o.toString()); } out.println(); } private int[] nextIntArray(int n) throws IOException { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } private long[] nextLongArray(int n) throws IOException { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } private char[] nextTokenChars() throws IOException { return nextToken().toCharArray(); } private String nextToken() throws IOException { while (line == null || !line.hasMoreTokens()) { line = new StringTokenizer(in.readLine()); } return line.nextToken(); } private static void assertPredicate(boolean p) { if ((isDebug || assertInProd) && !p) { throw new RuntimeException(); } } private static void assertPredicate(boolean p, String message) { if ((isDebug || assertInProd) && !p) { throw new RuntimeException(message); } } private static <T> void assertNotEqual(T unexpected, T actual) { if ((isDebug || assertInProd) && Objects.equals(actual, unexpected)) { throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual); } } private static <T> void assertEqual(T expected, T actual) { if ((isDebug || assertInProd) && !Objects.equals(actual, expected)) { throw new RuntimeException("assertEqual: " + expected + " != " + actual); } } private static PrintWriter log = null; private static Clock clock = null; private static void log(Object... objects) { log(true, objects); } private static void logNoDelimiter(Object... objects) { log(false, objects); } private static void log(boolean printDelimiter, Object[] objects) { if (isDebug) { StringBuilder sb = new StringBuilder(); sb.append(LocalDateTime.now(clock)).append(" - "); boolean isFirst = true; for (Object o : objects) { if (!isFirst && printDelimiter) { sb.append(" "); } else { isFirst = false; } sb.append(o.toString()); } log.println(sb); log.flush(); } } private static class Timer implements Closeable { private final String label; private final long startTime = isDebug ? System.nanoTime() : 0; public Timer(String label) { this.label = label; } @Override public void close() throws IOException { if (isDebug) { long executionTime = System.nanoTime() - startTime; String fraction = Long.toString(executionTime / 1000 % 1_000_000); logNoDelimiter("Timer[", label, "]: ", executionTime / 1_000_000_000, '.', "00000".substring(0, 6 - fraction.length()), fraction, 's'); } } } private static <T> T timer(String label, Supplier<T> f) throws Exception { if (isDebug) { try (Timer timer = new Timer(label)) { return f.get(); } } else { return f.get(); } } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
c068da0ac227ccdd66ffe523edb2133e
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.io.BufferedReader; import java.io.Closeable; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.time.Clock; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.Random; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.function.Supplier; public class Solution { private void solve() throws IOException { int n = nextInt(); int maxWidth = (n + 1) * 2000 - 1; int res; { Pii t = findWidth(1, maxWidth); res = t.x * t.y; maxWidth = t.x; } for (int i = 2; i <= n; i++) { int w = (maxWidth - 1) / i; if (w > 0) { int h = ask(w); if (h <= n) { res = Math.min(res, w*h); } } } result(res); } private Pii findWidth(int height, int maxWidth) throws IOException { int left = 0; int right = maxWidth; while (left < right) { int mid = (left + right) / 2; if (ask(mid) > height) { left = mid + 1; } else { right = mid; } } return new Pii(left, ask(left)); } private static class Pii implements Comparable<Pii> { // not today, evil hacker private static final int hashMultiplier = BigInteger.valueOf(new Random().nextInt(100000) + 100).nextProbablePrime().intValue(); private int x; private int y; public Pii(int x, int y) { this.x = x; this.y = y; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pii pii = (Pii) o; if (x != pii.x) return false; return y == pii.y; } public int hashCode() { return hashMultiplier * x + y; } public String toString() { return "(" + x + ", " + y + ")"; } public int compareTo(Pii o) { int c = Integer.compare(x, o.x); return c != 0 ? c : Integer.compare(y, o.y); } } private int ask(int w) throws IOException { if (w < 1) return Integer.MAX_VALUE-13; out.println("? " + w); out.flush(); int res = nextInt(); return res > 0 ? res: Integer.MAX_VALUE-13; } private void result(int sq) { out.println("! " + sq); out.flush(); } private static final boolean runNTestsInProd = false; private static final boolean printCaseNumber = false; private static final boolean assertInProd = false; private static final boolean logToFile = false; private static final boolean readFromConsoleInDebug = true; private static final boolean writeToConsoleInDebug = true; private static final boolean testTimer = false; private static Boolean isDebug = null; private BufferedReader in; private StringTokenizer line; private PrintWriter out; public static void main(String[] args) throws Exception { isDebug = Arrays.asList(args).contains("DEBUG_MODE"); if (isDebug) { log = logToFile ? new PrintWriter("logs/j_solution_" + System.currentTimeMillis() + ".log") : new PrintWriter(System.out); clock = Clock.systemDefaultZone(); } new Solution().run(); } private void run() throws Exception { in = new BufferedReader(new InputStreamReader(!isDebug || readFromConsoleInDebug ? System.in : new FileInputStream("input.txt"))); out = !isDebug || writeToConsoleInDebug ? new PrintWriter(System.out) : new PrintWriter("output.txt"); try (Timer totalTimer = new Timer("total")) { int t = 1;//runNTestsInProd || isDebug ? nextInt() : 1; for (int i = 0; i < t; i++) { if (printCaseNumber) { out.print("Case #" + (i + 1) + ": "); } if (testTimer) { try (Timer testTimer = new Timer("test #" + (i + 1))) { solve(); } } else { solve(); } if (isDebug) { out.flush(); } } } in.close(); out.flush(); out.close(); } private void println(Object... objects) { boolean isFirst = true; for (Object o : objects) { if (!isFirst) { out.print(" "); } else { isFirst = false; } out.print(o.toString()); } out.println(); } private int[] nextIntArray(int n) throws IOException { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } private long[] nextLongArray(int n) throws IOException { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } private char[] nextTokenChars() throws IOException { return nextToken().toCharArray(); } private String nextToken() throws IOException { while (line == null || !line.hasMoreTokens()) { line = new StringTokenizer(in.readLine()); } return line.nextToken(); } private static void assertPredicate(boolean p) { if ((isDebug || assertInProd) && !p) { throw new RuntimeException(); } } private static void assertPredicate(boolean p, String message) { if ((isDebug || assertInProd) && !p) { throw new RuntimeException(message); } } private static <T> void assertNotEqual(T unexpected, T actual) { if ((isDebug || assertInProd) && Objects.equals(actual, unexpected)) { throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual); } } private static <T> void assertEqual(T expected, T actual) { if ((isDebug || assertInProd) && !Objects.equals(actual, expected)) { throw new RuntimeException("assertEqual: " + expected + " != " + actual); } } private static PrintWriter log = null; private static Clock clock = null; private static void log(Object... objects) { log(true, objects); } private static void logNoDelimiter(Object... objects) { log(false, objects); } private static void log(boolean printDelimiter, Object[] objects) { if (isDebug) { StringBuilder sb = new StringBuilder(); sb.append(LocalDateTime.now(clock)).append(" - "); boolean isFirst = true; for (Object o : objects) { if (!isFirst && printDelimiter) { sb.append(" "); } else { isFirst = false; } sb.append(o.toString()); } log.println(sb); log.flush(); } } private static class Timer implements Closeable { private final String label; private final long startTime = isDebug ? System.nanoTime() : 0; public Timer(String label) { this.label = label; } @Override public void close() throws IOException { if (isDebug) { long executionTime = System.nanoTime() - startTime; String fraction = Long.toString(executionTime / 1000 % 1_000_000); logNoDelimiter("Timer[", label, "]: ", executionTime / 1_000_000_000, '.', "00000".substring(0, 6 - fraction.length()), fraction, 's'); } } } private static <T> T timer(String label, Supplier<T> f) throws Exception { if (isDebug) { try (Timer timer = new Timer(label)) { return f.get(); } } else { return f.get(); } } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
286b73d989e88fea46646f3732767095
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
// https://codeforces.com/blog/entry/102172 import java.io.*; import java.util.*; public class CF1672E extends PrintWriter { CF1672E() { super(System.out, true); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1672E o = new CF1672E(); o.main(); o.flush(); } int query(int w) { println("? " + w); return sc.nextInt(); } void main() { int n = sc.nextInt(); int lower = n * 1 + n - 1 - 1; int upper = n * 2000 + n - 1; while (upper - lower > 1) { int w = (lower + upper) / 2; int h = query(w); if (h == 1) upper = w; else lower = w; } long ans = upper; for (int h = 2; h <= n; h++) { int w = upper / h; int h_ = query(w); if (h_ > 0) ans = Math.min(ans, (long) w * h_); } println("! " + ans); } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
218a054b5f8a7e95e4156bf051bce885
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
//Utilities import java.io.*; import java.util.*; public class a { static int t; static int n; static int s; static int minArea; public static void main(String[] args) throws IOException { t = 1; while (t-- > 0) { n = in.iscan(); minArea = Integer.MAX_VALUE; int l = 1, r = 2000 * n + n - 1, mid; s = 0; while (l <= r) { mid = (l+r)/2; int h = query(mid); if (h == 0 || h > 1) { l = mid+1; } else { // h == 1 s = mid; r = mid-1; } } minArea = s; for (int h = 2; h <= n; h++) { int w = s / h; int newH = query(s / h); if (newH != 0) { minArea = Math.min(minArea, w * newH); } } System.out.println("! " + minArea); System.out.flush(); } out.close(); } static int query(int w) throws IOException { System.out.println("? " + w); System.out.flush(); return in.iscan(); } static INPUT in = new INPUT(System.in); static PrintWriter out = new PrintWriter(System.out); private static class INPUT { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public INPUT (InputStream stream) { this.stream = stream; } public INPUT (String file) throws IOException { this.stream = new FileInputStream (file); } public int cscan () throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read (buf); } if (numChars == -1) return numChars; return buf[curChar++]; } public int iscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } int res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public String sscan () throws IOException { int c = cscan (); while (space (c)) c = cscan (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = cscan (); } while (!space (c)); return res.toString (); } public double dscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } double res = 0; while (!space (c) && c != '.') { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); res *= 10; res += c - '0'; c = cscan (); } if (c == '.') { c = cscan (); double m = 1; while (!space (c)) { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); m /= 10; res += (c - '0') * m; c = cscan (); } } return res * sgn; } public long lscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } long res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public boolean space (int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } public static class UTILITIES { static final double EPS = 10e-6; public static void sort(int[] a, boolean increasing) { ArrayList<Integer> arr = new ArrayList<Integer>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(long[] a, boolean increasing) { ArrayList<Long> arr = new ArrayList<Long>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(double[] a, boolean increasing) { ArrayList<Double> arr = new ArrayList<Double>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static int lower_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } public static void updateMap(HashMap<Integer, Integer> map, int key, int v) { if (!map.containsKey(key)) { map.put(key, v); } else { map.put(key, map.get(key) + v); } if (map.get(key) == 0) { map.remove(key); } } public static long gcd (long a, long b) { return b == 0 ? a : gcd (b, a % b); } public static long lcm (long a, long b) { return a * b / gcd (a, b); } public static long fast_pow_mod (long b, long x, int mod) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod; return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod; } public static long fast_pow (long b, long x) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow (b * b, x / 2); return b * fast_pow (b * b, x / 2); } public static long choose (long n, long k) { k = Math.min (k, n - k); long val = 1; for (int i = 0; i < k; ++i) val = val * (n - i) / (i + 1); return val; } public static long permute (int n, int k) { if (n < k) return 0; long val = 1; for (int i = 0; i < k; ++i) val = (val * (n - i)); return val; } // start of permutation and lower/upper bound template public static void nextPermutation(int[] nums) { //find first decreasing digit int mark = -1; for (int i = nums.length - 1; i > 0; i--) { if (nums[i] > nums[i - 1]) { mark = i - 1; break; } } if (mark == -1) { reverse(nums, 0, nums.length - 1); return; } int idx = nums.length-1; for (int i = nums.length-1; i >= mark+1; i--) { if (nums[i] > nums[mark]) { idx = i; break; } } swap(nums, mark, idx); reverse(nums, mark + 1, nums.length - 1); } public static void swap(int[] nums, int i, int j) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } public static void reverse(int[] nums, int i, int j) { while (i < j) { swap(nums, i, j); i++; j--; } } static int lower_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= cmp) high = mid; else low = mid + 1; } return low; } static int upper_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > cmp) high = mid; else low = mid + 1; } return low; } // end of permutation and lower/upper bound template } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
b7a143042043e2f9f2505274de713464
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); Grader grader = new Grader() { @Override public int test(int w) { System.out.println("? " + w); return scanner.nextInt(); } @Override public int getN() { return n; } @Override public void checkAns(int s) { System.out.println("! " + s); } }; Solver solver = new Solver(grader); solver.solve(); } public interface Grader { int test(int w); int getN(); void checkAns(int s); } public static class Solver { private static final int MAX_LEN = 2000; private final Grader grader; public Solver(Grader grader) { this.grader = grader; } public void solve() { int n = grader.getN(); int lo = 2 * n - 1, hi = n * (MAX_LEN + 1) - 1; // possible length of the longest string int currentMaxLengthMin = 1, currentMaxLengthMax = MAX_LEN; Map<Integer, Integer> answers = new TreeMap<>(); int minArea = hi; while (lo < hi) { int w = (lo + hi) / 2; int h = grader.test(w); if (h != 1) lo = w + 1; else hi = w; if (h != 0) { answers.put(w, h); currentMaxLengthMax = Math.min(currentMaxLengthMax, w); minArea = Math.min(minArea, w * h); } else currentMaxLengthMin = Math.max(currentMaxLengthMin, w + 1); } int[] heights = new int[n]; for (Map.Entry<Integer, Integer> entry : answers.entrySet()) { int w = entry.getKey(); int h = entry.getValue(); if (heights[h - 1] == 0) heights[h - 1] = w; else heights[h - 1] = Math.min(heights[h - 1], w); } int s = lo; heights[0] = s; for (int h = 2; h <= n; h++) { if (heights[h - 1] == 0) heights[h - 1] = heights[h - 2]; else heights[h - 1] = Math.min(heights[h - 1], heights[h - 2]); int length = s / h; length = Math.min(length, heights[h - 2] - 1); length = Math.max(length, currentMaxLengthMin); if (heights[h - 1] != 0 && heights[h - 1] <= length) continue; if (length * h >= minArea) continue; int actualH; if (answers.containsKey(length)) actualH = answers.get(length); else actualH = grader.test(length); if (actualH != 0) { minArea = Math.min(minArea, length * actualH); answers.put(length, actualH); currentMaxLengthMax = Math.min(currentMaxLengthMax, length); if (actualH == h) { heights[h - 1] = length; } else { if (heights[actualH - 1] == 0 || heights[actualH - 1] > length) heights[actualH - 1] = length; } } else currentMaxLengthMin = Math.max(currentMaxLengthMin, length + 1); } grader.checkAns(minArea); } } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
a47f3ae09f641e028e91f3bbd57a5515
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.io.*; import java.util.*; public class A { static PrintWriter pw; static Scanner sc; static int ask(int mid) throws IOException { pw.println("? " + mid); pw.flush(); return sc.nextInt(); } public static void main(String[] args) throws IOException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int n = sc.nextInt(); int start = 1, end = (int) 5e6; while (start <= end) { int mid = start + end >> 1; int v = ask(Math.max(1, mid)); if (v == 1) { end = mid - 1; } else { start = mid + 1; } } int ans = start; for (int i = 1; i < n; i++) { int cur = (start ) / (i + 1); int v = ask(Math.max(1, cur)); if (v != 0) { ans = Math.min(ans, v * cur); } } pw.println("! " + ans); pw.flush(); pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
fff6684b82b9322ed3603d35fbca3187
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.FileNotFoundException; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); EBloknotexe solver = new EBloknotexe(); solver.solve(1, in, out); out.close(); } static class EBloknotexe { FastScanner in; PrintWriter out; int n; int qCount = 0; public void solve(int testNumber, FastScanner in, PrintWriter out) { this.in = in; this.out = out; n = in.nextInt(); int left = (1 + 1) * n - 2, right = (2000 + 1) * n - 1; while (left < right - 1) { int mid = (left + right) >>> 1; if (ask(mid) == 1) { right = mid; } else { left = mid; } } int sumL = right - (n - 1); long ans = right; for (int h = 1; h <= n; h++) { int w = (sumL + n - h + h - 1) / h; ans = Math.min(ans, (long) w * ask(w)); } out.println("! " + ans); out.flush(); } int ask(int w) { qCount++; out.println("? " + w); out.flush(); int h = in.nextInt(); if (h == 0) { h = Integer.MAX_VALUE; } return h; } } static class FastScanner { public BufferedReader br; public StringTokenizer st; public FastScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } public FastScanner(String fileName) { try { br = new BufferedReader(new FileReader(fileName)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public int nextInt() { return Integer.parseInt(next()); } public String next() { while (st == null || !st.hasMoreElements()) { String line = null; try { line = br.readLine(); } catch (IOException e) { throw new UnknownError(); } if (line == null) { throw new UnknownError(); } st = new StringTokenizer(line); } return st.nextToken(); } } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
8e9d70f43849dc8ac39cc744e3649527
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.util.*; import java.io.*; import java.text.*; public class CF_1672_E{ //SOLUTION BEGIN void pre() throws Exception{} void solve(int TC) throws Exception{ int N = ni(); int lo = 1, hi = 4000000+N; while (lo < hi){ int mid = lo + (hi-lo)/2; int h = query(mid); if(h == 1)hi = mid; else lo = mid+1; } int ans = hi; for(int h = 2; h <= N; h++){ int w = hi/h, H = query(w); ans = Math.min(ans, w*H); } pni("!", ans); } final int INF = 1000000; int query(int w) throws Exception{ pni("?", w); int h = ni(); if(h == 0)return INF; return h; } //SOLUTION END void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");} void exit(boolean b){if(!b)System.exit(0);} static void dbg(Object... o){System.err.println(Arrays.deepToString(o));} DecimalFormat df = new DecimalFormat("0.00000000000"); double PI = 3.141592653589793238462643383279502884197169399; static boolean multipleTC = false, memory = true, fileIO = false; FastReader in;PrintWriter out; void run() throws Exception{ long ct = System.currentTimeMillis(); if (fileIO) { in = new FastReader(""); out = new PrintWriter(""); } else { in = new FastReader(); out = new PrintWriter(System.out); } //Solution Credits: Taranpreet Singh int T = multipleTC? ni():1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); System.err.println("Runtime: "+(System.currentTimeMillis() - ct)); } public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new CF_1672_E().run();}catch(Exception e){e.printStackTrace();System.exit(1);}}}, "1", 1 << 28).start(); else new CF_1672_E().run(); } int[][] make(int n, int e, int[] from, int[] to, boolean f){ int[][] g = new int[n][];int[]cnt = new int[n]; for(int i = 0; i< e; i++){ cnt[from[i]]++; if(f)cnt[to[i]]++; } for(int i = 0; i< n; i++)g[i] = new int[cnt[i]]; for(int i = 0; i< e; i++){ g[from[i]][--cnt[from[i]]] = to[i]; if(f)g[to[i]][--cnt[to[i]]] = from[i]; } return g; } int[][][] makeS(int n, int e, int[] from, int[] to, boolean f){ int[][][] g = new int[n][][];int[]cnt = new int[n]; for(int i = 0; i< e; i++){ cnt[from[i]]++; if(f)cnt[to[i]]++; } for(int i = 0; i< n; i++)g[i] = new int[cnt[i]][]; for(int i = 0; i< e; i++){ g[from[i]][--cnt[from[i]]] = new int[]{to[i], i, 0}; if(f)g[to[i]][--cnt[to[i]]] = new int[]{from[i], i, 1}; } return g; } int find(int[] set, int u){return set[u] = (set[u] == u?u:find(set, set[u]));} int digit(long s){int ans = 0;while(s>0){s/=10;ans++;}return ans;} long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));} void p(Object... o){for(Object oo:o)out.print(oo+" ");} void pn(Object... o){for(int i = 0; i< o.length; i++)out.print(o[i]+(i+1 < o.length?" ":"\n"));} void pni(Object... o){for(Object oo:o)out.print(oo+" ");out.println();out.flush();} String n()throws Exception{return in.next();} String nln()throws Exception{return in.nextLine();} int ni()throws Exception{return Integer.parseInt(in.next());} long nl()throws Exception{return Long.parseLong(in.next());} double nd()throws Exception{return Double.parseDouble(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next() throws Exception{ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception{ String str; try{ str = br.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } return str; } } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
26ddf639a33fd096424bbecfa0b3f4a2
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.util.*; import java.io.*; // you can compare with output.txt and expected out public class RoundGlobal20E { MyPrintWriter out; MyScanner in; // final static long FIXED_RANDOM; // static { // FIXED_RANDOM = System.currentTimeMillis(); // } final static String IMPOSSIBLE = "IMPOSSIBLE"; final static String POSSIBLE = "POSSIBLE"; final static String YES = "YES"; final static String NO = "NO"; private void initIO(boolean isFileIO) { if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) { try{ in = new MyScanner(new FileInputStream("input.txt")); out = new MyPrintWriter(new FileOutputStream("output.txt")); } catch(FileNotFoundException e){ e.printStackTrace(); } } else{ in = new MyScanner(System.in); out = new MyPrintWriter(new BufferedOutputStream(System.out)); } } public static void main(String[] args){ // Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); RoundGlobal20E sol = new RoundGlobal20E(); sol.run(); } private void run() { boolean isDebug = false; boolean isFileIO = false; boolean hasMultipleTests = false; initIO(isFileIO); solve(in, out); in.close(); out.close(); } public void solve(MyScanner in, MyPrintWriter out) { n = in.nextInt(); // takes lg(1998n) queries // 22 queries at n = 2000 int l = findSumLengths(); // line i: [ai... a(i+1)) // sum of length <= w // lg(2000n) + lg(1000n) + lg(500n) + .... // n lgn + // n = 1 // easy // n = 2 // need to find max(ai) or min(ai), or search for h=2 // lg(4000) + lg(2000) < 30 // n = 3 // search for h = 1, 2, 3 // n = 5 -- have 35 queries // h = 1 -> log(10000) // h = 2 -> log(6000) // h = 3 -> log(4000) // h = 4 -> log(4000) // h = 5 -> log(2000) // unless ai's have very even structure, we are unlikely to save a lot from high h // at h=k, the saving is at most k-1 // h = 1 -> w = l+n-1 // for h=2 to be better than that, // w must be (l+n-2)/2 // maintain the best area // h = k, ask for w = (area-1)/h // w*h <= area-1 -> w <= (area-1)/h int area = l+n-1; for(int h=2; h<=n; h++) { int w = (area-1)/h; if(w == 0) continue; int ans = askQuery(w); if(ans != 0) area = Math.min(area, w*ans); } giveAnswer(area); } int n; private int findSumLengths() { int left = n+n-1; int right = 2000*n; while(left < right) { int mid = (left+right)/2; int h = askQuery(mid); if(h == 1) { // mid is incl. right = mid; } else { // mid is not incl. left = mid+1; } } return left-(n-1); } private void giveAnswer(int area) { out.println("! " + area); out.flush(); } private int askQuery(int width) { out.println("? " + width); out.flush(); return in.nextInt(); } public static class MyScanner { BufferedReader br; StringTokenizer st; // 32768? public MyScanner(InputStream is, int bufferSize) { br = new BufferedReader(new InputStreamReader(is), bufferSize); } public MyScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); // br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt"))); } public void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[][] nextTreeEdges(int n, int offset){ int[][] e = new int[n-1][2]; for(int i=0; i<n-1; i++){ e[i][0] = nextInt()+offset; e[i][1] = nextInt()+offset; } return e; } int[][] nextMatrix(int n, int m) { return nextMatrix(n, m, 0); } int[][] nextMatrix(int n, int m, int offset) { int[][] mat = new int[n][m]; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { mat[i][j] = nextInt()+offset; } } return mat; } int[][] nextPairs(int n){ return nextPairs(n, 0); } int[][] nextPairs(int n, int offset) { int[][] xy = new int[2][n]; for(int i=0; i<n; i++) { xy[0][i] = nextInt() + offset; xy[1][i] = nextInt() + offset; } return xy; } int[][] nextGraphEdges(){ return nextGraphEdges(0); } int[][] nextGraphEdges(int offset) { int m = nextInt(); int[][] e = new int[m][2]; for(int i=0; i<m; i++){ e[i][0] = nextInt()+offset; e[i][1] = nextInt()+offset; } return e; } int[] nextIntArray(int len) { return nextIntArray(len, 0); } int[] nextIntArray(int len, int offset){ int[] a = new int[len]; for(int j=0; j<len; j++) a[j] = nextInt()+offset; return a; } long[] nextLongArray(int len) { return nextLongArray(len, 0); } long[] nextLongArray(int len, int offset){ long[] a = new long[len]; for(int j=0; j<len; j++) a[j] = nextLong()+offset; return a; } } public static class MyPrintWriter extends PrintWriter{ public MyPrintWriter(OutputStream os) { super(os); } public void printlnAns(boolean ans) { if(ans) println(YES); else println(NO); } public void printAns(long[] arr){ if(arr != null && arr.length > 0){ print(arr[0]); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]); } } } public void printlnAns(long[] arr){ printAns(arr); println(); } public void printAns(int[] arr){ if(arr != null && arr.length > 0){ print(arr[0]); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]); } } } public void printlnAns(int[] arr){ printAns(arr); println(); } public <T> void printAns(ArrayList<T> arr){ if(arr != null && arr.size() > 0){ print(arr.get(0)); for(int i=1; i<arr.size(); i++){ print(" "); print(arr.get(i)); } } } public <T> void printlnAns(ArrayList<T> arr){ printAns(arr); println(); } public void printAns(int[] arr, int add){ if(arr != null && arr.length > 0){ print(arr[0]+add); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]+add); } } } public void printlnAns(int[] arr, int add){ printAns(arr, add); println(); } public void printAns(ArrayList<Integer> arr, int add) { if(arr != null && arr.size() > 0){ print(arr.get(0)+add); for(int i=1; i<arr.size(); i++){ print(" "); print(arr.get(i)+add); } } } public void printlnAns(ArrayList<Integer> arr, int add){ printAns(arr, add); println(); } public void printlnAnsSplit(long[] arr, int split){ if(arr != null){ for(int i=0; i<arr.length; i+=split){ print(arr[i]); for(int j=i+1; j<i+split; j++){ print(" "); print(arr[j]); } println(); } } } public void printlnAnsSplit(int[] arr, int split){ if(arr != null){ for(int i=0; i<arr.length; i+=split){ print(arr[i]); for(int j=i+1; j<i+split; j++){ print(" "); print(arr[j]); } println(); } } } public <T> void printlnAnsSplit(ArrayList<T> arr, int split){ if(arr != null && !arr.isEmpty()){ for(int i=0; i<arr.size(); i+=split){ print(arr.get(i)); for(int j=i+1; j<i+split; j++){ print(" "); print(arr.get(j)); } println(); } } } } static private void permutateAndSort(int[] a) { int n = a.length; Random R = new Random(System.currentTimeMillis()); for(int i=0; i<n; i++) { int t = R.nextInt(n-i); int temp = a[n-1-i]; a[n-1-i] = a[t]; a[t] = temp; } Arrays.sort(a); } static private int[][] constructChildren(int n, int[] parent, int parentRoot){ int[][] childrens = new int[n][]; int[] numChildren = new int[n]; for(int i=0; i<parent.length; i++) { if(parent[i] != parentRoot) numChildren[parent[i]]++; } for(int i=0; i<n; i++) { childrens[i] = new int[numChildren[i]]; } int[] idx = new int[n]; for(int i=0; i<parent.length; i++) { if(parent[i] != parentRoot) childrens[parent[i]][idx[parent[i]]++] = i; } return childrens; } static private int[][][] constructDirectedNeighborhood(int n, int[][] e){ int[] inDegree = new int[n]; int[] outDegree = new int[n]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; outDegree[u]++; inDegree[v]++; } int[][] inNeighbors = new int[n][]; int[][] outNeighbors = new int[n][]; for(int i=0; i<n; i++) { inNeighbors[i] = new int[inDegree[i]]; outNeighbors[i] = new int[outDegree[i]]; } int[] inIdx = new int[n]; int[] outIdx = new int[n]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; outNeighbors[u][outIdx[u]++] = v; inNeighbors[v][inIdx[v]++] = u; } return new int[][][] {inNeighbors, outNeighbors}; } static private int[][] constructNeighborhood(int n, int[][] e) { int[] degree = new int[n]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; degree[u]++; degree[v]++; } int[][] neighbors = new int[n][]; for(int i=0; i<n; i++) neighbors[i] = new int[degree[i]]; int[] idx = new int[n]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; neighbors[u][idx[u]++] = v; neighbors[v][idx[v]++] = u; } return neighbors; } static private void makeDotUndirected(int[][] e) { MyPrintWriter out2 = null; try { out2 = new MyPrintWriter(new FileOutputStream("graph.dot")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } out2.println("strict graph {"); for(int i=0; i<e.length; i++){ out2.println(e[i][0] + "--" + e[i][1] + ";"); } out2.println("}"); out2.close(); } static private void makeDotDirected(int[][] e) { MyPrintWriter out2 = null; try { out2 = new MyPrintWriter(new FileOutputStream("graph.dot")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } out2.println("strict digraph {"); for(int i=0; i<e.length; i++){ out2.println(e[i][0] + "->" + e[i][1] + ";"); } out2.println("}"); out2.close(); } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
70b1cfef6c45a00b21d05ba9aad3ec4d
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA import java.util.*; import java.util.Map.Entry; import java.util.stream.*; import java.lang.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.io.*; public class CodeForces { static private final String INPUT = "input.txt"; static private final String OUTPUT = "output.txt"; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); static DecimalFormat df = new DecimalFormat("0.00000"); final static int mod = (int) (1e9 + 7); final static int MAX = Integer.MAX_VALUE; final static int MIN = Integer.MIN_VALUE; final static long INF = Long.MAX_VALUE; final static long NEG_INF = Long.MIN_VALUE; static Random rand = new Random(); // ======================= MAIN ================================== public static void main(String[] args) throws IOException { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; // ==== start ==== input(); preprocess(); int t = 1; // t = readInt(); while (t-- > 0) { solve(); } out.flush(); // ==== end ==== if (!oj) System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } private static void solve() throws IOException { int n = readInt(); int lo = n + n - 1, hi = 2000 * n + n - 1, maxL = hi; while (lo <= hi) { int mid = (lo + hi) / 2; out.println("? " + mid); out.flush(); int x = readInt(); if (x == 1) { maxL = mid; hi = mid - 1; } else lo = mid + 1; } int ans = maxL; for (int i = 1; i <= n; i++) { int w = maxL / i; out.println("? " + w); out.flush(); int l = readInt(); if (l != 0) ans = Math.min(ans, l * w); } out.println("! " + ans); } private static void preprocess() throws IOException { } // cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces // javac CodeForces.java // java CodeForces // javac CodeForces.java && java CodeForces // ==================== CUSTOM CLASSES ================================ static class Pair { int first, second; Pair(int f, int s) { first = f; second = s; } public int compareTo(Pair o) { if (this.first == o.first) return this.second - o.second; return this.first - o.first; } @Override public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (this.getClass() != obj.getClass()) return false; Pair other = (Pair) (obj); if (this.first != other.first) return false; if (this.second != other.second) return false; return true; } @Override public int hashCode() { return this.first ^ this.second; } @Override public String toString() { return this.first + " " + this.second; } } static class DequeNode { DequeNode prev, next; int val; DequeNode(int val) { this.val = val; } DequeNode(int val, DequeNode prev, DequeNode next) { this.val = val; this.prev = prev; this.next = next; } } // ======================= FOR INPUT ================================== private static void input() { FileInputStream instream = null; PrintStream outstream = null; try { instream = new FileInputStream(INPUT); outstream = new PrintStream(new FileOutputStream(OUTPUT)); System.setIn(instream); System.setOut(outstream); } catch (Exception e) { System.err.println("Error Occurred."); } br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(readLine()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readString() throws IOException { return next(); } static String readLine() throws IOException { return br.readLine().trim(); } static int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = readInt(); return arr; } static int[][] read2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = readIntArray(m); return arr; } static List<Integer> readIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readInt()); return list; } static long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = readLong(); return arr; } static long[][] read2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = readLongArray(m); return arr; } static List<Long> readLongList(int n) throws IOException { List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readLong()); return list; } static char[] readCharArray(int n) throws IOException { return readString().toCharArray(); } static char[][] readMatrix(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = readCharArray(m); return mat; } // ========================= FOR OUTPUT ================================== private static void printIList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIArray(arr[i]); } private static void printLArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLArray(arr[i]); } // ====================== TO CHECK IF STRING IS NUMBER ======================== private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // ==================== FASTER SORT ================================ private static void sort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void sort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // ==================== MATHEMATICAL FUNCTIONS =========================== private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static int mod_pow(int a, int b, int mod) { if (b == 0) return 1; int temp = mod_pow(a, b >> 1, mod); temp %= mod; temp = (int) ((1L * temp * temp) % mod); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % mod); return temp; } private static int multiply(int a, int b) { return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod); } private static int divide(int a, int b) { return multiply(a, mod_pow(b, mod - 2, mod)); } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } private static long nCr(long n, long r) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans *= i; for (long i = 2; i <= n - r; i++) ans /= i; return ans; } private static List<Integer> factors(int n) { List<Integer> list = new ArrayList<>(); for (int i = 1; 1L * i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } private static List<Long> factors(long n) { List<Long> list = new ArrayList<>(); for (long i = 1; i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } // ==================== Primes using Seive ===================== private static List<Integer> getPrimes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); for (int i = 2; 1L * i * i <= n; i++) if (prime[i]) for (int j = i * i; j <= n; j += i) prime[j] = false; // return prime; List<Integer> list = new ArrayList<>(); for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } private static int[] SeivePrime(int n) { int[] primes = new int[n]; for (int i = 0; i < n; i++) primes[i] = i; for (int i = 2; 1L * i * i < n; i++) { if (primes[i] != i) continue; for (int j = i * i; j < n; j += i) if (primes[j] == j) primes[j] = i; } return primes; } // ==================== STRING FUNCTIONS ================================ private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } // check if a is subsequence of b private static boolean isSubsequence(String a, String b) { int idx = 0; for (int i = 0; i < b.length() && idx < a.length(); i++) if (a.charAt(idx) == b.charAt(i)) idx++; return idx == a.length(); } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // ==================== LIS & LNDS ================================ private static int LIS(int arr[], int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find1(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // =============== Lower Bound & Upper Bound =========== // less than or equal private static int lower_bound(List<Integer> list, int val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(List<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(List<Long> list, long val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== UNION FIND ===================== private static int find(int x, int[] parent) { if (parent[x] == x) return x; return parent[x] = find(parent[x], parent); } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) return false; if (rank[lx] > rank[ly]) parent[ly] = lx; else if (rank[lx] < rank[ly]) parent[lx] = ly; else { parent[lx] = ly; rank[ly]++; } return true; } // ================== SEGMENT TREE (RANGE SUM & RANGE UPDATE) ================== public static class SegmentTree { int n; long[] arr, tree, lazy; SegmentTree(long arr[]) { this.arr = arr; this.n = arr.length; this.tree = new long[(n << 2)]; this.lazy = new long[(n << 2)]; build(1, 0, n - 1); } void build(int id, int start, int end) { if (start == end) tree[id] = arr[start]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; build(left, start, mid); build(right, mid + 1, end); tree[id] = tree[left] + tree[right]; } } void update(int l, int r, long val) { update(1, 0, n - 1, l, r, val); } void update(int id, int start, int end, int l, int r, long val) { distribute(id, start, end); if (end < l || r < start) return; if (start == end) tree[id] += val; else if (l <= start && end <= r) { lazy[id] += val; distribute(id, start, end); } else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; update(left, start, mid, l, r, val); update(right, mid + 1, end, l, r, val); tree[id] = tree[left] + tree[right]; } } long query(int l, int r) { return query(1, 0, n - 1, l, r); } long query(int id, int start, int end, int l, int r) { if (end < l || r < start) return 0L; distribute(id, start, end); if (start == end) return tree[id]; else if (l <= start && end <= r) return tree[id]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r); } } void distribute(int id, int start, int end) { if (start == end) tree[id] += lazy[id]; else { tree[id] += lazy[id] * (end - start + 1); lazy[(id << 1)] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; } lazy[id] = 0; } } // ==================== TRIE ================================ static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } void insert(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) curr.children[ch - 'a'] = new Node(); curr = curr.children[ch - 'a']; } curr.isEnd = true; } boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) return false; curr = curr.children[ch - 'a']; } return curr.isEnd; } } // ==================== FENWICK TREE ================================ static class FT { long[] tree; int n; FT(int[] arr, int n) { this.n = n; this.tree = new long[n + 1]; for (int i = 1; i <= n; i++) { update(i, arr[i - 1]); } } void update(int idx, int val) { while (idx <= n) { tree[idx] += val; idx += idx & -idx; } } long query(int l, int r) { return getSum(r) - getSum(l - 1); } long getSum(int idx) { long ans = 0L; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } } // ==================== BINARY INDEX TREE ================================ static class BIT { long[][] tree; int n, m; BIT(int[][] mat, int n, int m) { this.n = n; this.m = m; tree = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { update(i, j, mat[i - 1][j - 1]); } } } void update(int x, int y, int val) { while (x <= n) { int t = y; while (t <= m) { tree[x][t] += val; t += t & -t; } x += x & -x; } } long query(int x1, int y1, int x2, int y2) { return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1); } long getSum(int x, int y) { long ans = 0L; while (x > 0) { int t = y; while (t > 0) { ans += tree[x][t]; t -= t & -t; } x -= x & -x; } return ans; } } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
6df4c99e02704e263dd235a3080f33db
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.io.*; import java.util.*; public class codeforces_G20_E { private static void solve(FastIOAdapter in, PrintWriter out) { int n = in.nextInt(); int l = 1, r = 5_000_000; while (l != r) { int mid = (l + r) / 2; int ans = query(mid, in, out); if (ans == 1) r = mid; else l = mid + 1; } int ans = Integer.MAX_VALUE; for (int i = 1; i <= n; i++) { int resp = query(l / i, in, out); if (resp != 0) ans = Math.min(ans, resp * (l / i)); } out.println("! " + ans); out.flush(); } private static int query(int width, FastIOAdapter in, PrintWriter out) { out.println("? " + width); out.flush(); return in.nextInt(); } public static void main(String[] args) throws Exception { try (FastIOAdapter ioAdapter = new FastIOAdapter()) { int count = 1; //count = ioAdapter.nextInt(); while (count-- > 0) { solve(ioAdapter, ioAdapter.out); } } } static void ruffleSort(int[] arr) { int n = arr.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } Arrays.sort(arr); } static class FastIOAdapter implements AutoCloseable { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out)))); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } String nextLine() { try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); return null; } } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } @Override public void close() throws Exception { out.flush(); out.close(); br.close(); } } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
8b37a4eca09ae96f4b605df7f249e93a
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.util.*; public class E { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub solve(0); } private static void solve(int t) { int n = sc.nextInt(); int space = n - 1; int low = n + space; int high = 2000*n + space + 100; int maxArea = findMinWidth(low, high); int widthAtHeightOne = maxArea; int target, height, width; for (int i = 2; i <= n; ++i) { target = maxArea / i; target *= i; if (target == maxArea) target -= i; width = target / i; if (width > 0) { height = query(width); if (height != 0 && height*width < maxArea) { maxArea = height*width; } } } System.out.println("! " + maxArea); System.out.flush(); } private static int findMinWidth(int l, int h) { if (query(l) == 1) return l; int mid; while (h - l > 1) { mid = (h + l)/2; if (query(mid) == 1) h = mid; else l = mid; } return h; } private static int query (int val) { System.out.println("? " + val); System.out.flush(); int height = sc.nextInt(); return height; } public static void print(int test, long result) { System.out.println("Case #" + test + ": " + result); } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
fc9fba582946718606ddaf8bada1c8bb
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.util.*; public class Q1672E { public static void main(String args[]) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int l = 2*n - 1, r = 2001*n - 1; while(l!=r) { int m = (l+r)/2; System.out.println("? " + m); System.out.flush(); int h = s.nextInt(); if(h==1) r = m; else l = m + 1; } int ans = l; for(int i=2; i<=n; i++) { int w = l/i; System.out.println("? " + w); System.out.flush(); int h = s.nextInt(); if(h!=0&&w*h<ans) ans = w*h; } System.out.println("! " + ans); System.out.flush(); } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
aab0b7f6163d2297c58ab75a7a64065e
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author lucasr */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; MyScanner in = new MyScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); ENotepadexe solver = new ENotepadexe(); solver.solve(1, in, out); out.close(); } static class ENotepadexe { public static MyScanner sc; public static PrintWriter out; public void solve(int testNumber, MyScanner sc, PrintWriter out) { ENotepadexe.sc = sc; ENotepadexe.out = out; int n = sc.nextInt(); int left = 2 * n - 2, right = 2001 * n - 1; while (left + 1 < right) { int med = (left + right) / 2; if (query(med) == 1) { right = med; } else { left = med; } } int bestArea = right; for (int i = 2; i <= n; i++) { if (query(bestArea / i) == i) { bestArea = (bestArea / i) * i; } } guess(bestArea); } int query(int w) { out.println("? " + w); out.flush(); return sc.nextInt(); } void guess(int area) { out.println("! " + area); out.flush(); } } static class MyScanner { private BufferedReader br; private StringTokenizer tokenizer; public MyScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
4440c21c49464b764be894d243549412
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Math.min; import static java.lang.System.exit; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class E { static void solve() throws Exception { int n = scanInt(); int l = 2 * n - 1, r = 2001 * n - 1; while (l < r) { int m = (l + r) >> 1; out.println("? " + m); out.flush(); if (scanInt() == 1) { r = m; } else { l = m + 1; } } int ans = l; for (int i = 2; i <= n; i++) { if (ans >= i + 1) { int w = (ans - 1) / i; out.println("? " + w); out.flush(); int h = scanInt(); if (h == 0) { break; } ans = min(ans, w * h); } } out.println("! " + ans); } static int scanInt() throws IOException { return parseInt(scanString()); } static long scanLong() throws IOException { return parseLong(scanString()); } static String scanString() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; public static void main(String[] args) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 11
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
03ebdee51c6ed6141b43a88d3b5648e2
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.io.*; import java.net.InetSocketAddress; import java.util.*; public class Main { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os, true); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { // ADMIN_MODE(); //int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() { int n = sc.ni(); // min width for ht = 1 int l = 0; // ht(l) > 1 int r = (2000*n) + (n-1); // ht(r) <= 1 while(l < r-1) { int mid = (r+l)/2; w.p("? "+mid); int ht = sc.ni(); if(ht == 0) { l = mid; continue; } if(ht > 1) l = mid; else { r = mid; } } int minWidthForHeight1 = r; long minArea = minWidthForHeight1; int nextHeight = 2; while(nextHeight <= n) { long minAreaForNextHeight = minWidthForHeight1 - nextHeight + 1; long maxAreaForNextHeight = minWidthForHeight1; for(long i = minAreaForNextHeight; i < maxAreaForNextHeight; i++) { if(i%nextHeight==0) { long width = i/nextHeight; w.p("? "+width); int ht = sc.ni(); if(ht == 0) break; long area = ht*width; if(area < minArea) minArea = area; break; } } nextHeight++; } w.p("! "+minArea); } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 8
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
3fdcd2d90c319fe6e185d22168fd474d
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.io.*; import java.util.*; public class NotepadExe { private static final int W_MAX = 40022222; public static void solve(FastIO io) { final int N = io.nextInt(); int allTextWidth = BinarySearch.firstThat(1, W_MAX, new BinarySearch.IntCheck() { @Override public boolean valid(int value) { int h = query(io, value); return h == 1; } }); int best = allTextWidth; for (int h = 2; h <= N; ++h) { int w = allTextWidth / h; int hActual = query(io, w); if (hActual > 0) { best = Math.min(best, w * hActual); } } io.println("! " + best); } private static int query(FastIO io, int w) { io.println("? " + w); io.flush(); final int H = io.nextInt(); return H; } public static class BinarySearch { // Finds the left-most value that satisfies the IntCheck in the range [L, R). // It will return R if the nothing in the range satisfies the check. public static int firstThat(int L, int R, IntCheck check) { while (L < R) { int M = (L >> 1) + (R >> 1) + (L & R & 1); if (check.valid(M)) { R = M; } else { L = M + 1; } } return L; } // Finds the right-most value that satisfies the IntCheck in the range [L, R). // It will return L - 1 if nothing in the range satisfies the check. public static int lastThat(int L, int R, IntCheck check) { int firstValue = firstThat(L, R, new IntCheck() { @Override public boolean valid(int value) { return !check.valid(value); } }); return firstValue - 1; } // Finds the left-most value that satisfies the LongCheck in the range [L, R). public static long firstThat(long L, long R, LongCheck check) { while (L < R) { long M = (L >> 1) + (R >> 1) + (L & R & 1); if (check.valid(M)) { R = M; } else { L = M + 1; } } return L; } // Finds the right-most value that satisfies the IntCheck in the range [L, R). // It will return L - 1 if nothing in the range satisfies the check. public static long lastThat(long L, long R, LongCheck check) { long firstValue = firstThat(L, R, new LongCheck() { @Override public boolean valid(long value) { return !check.valid(value); } }); return firstValue - 1; } public static interface LongCheck { public boolean valid(long value); } public static interface IntCheck { public boolean valid(int value); } } public static class FastIO { private InputStream reader; private PrintWriter writer; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastIO(InputStream r, OutputStream w) { reader = r; writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w))); } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = reader.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } // TODO: read this byte-by-byte like the other read functions. public double nextDouble() { return Double.parseDouble(nextString()); } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printlnArray(int[] arr) { printArray(arr); writer.println(); } public void printlnArray(long[] arr) { printArray(arr); writer.println(); } public void printf(String format, Object... args) { print(String.format(format, args)); } public void flush() { writer.flush(); } } public static void main(String[] args) { FastIO io = new FastIO(System.in, System.out); solve(io); io.flush(); } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 8
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
e5726b450d3a12dacf6cde5bf3e5567e
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.io.*; import java.util.*; public class NotepadExe { private static final int W_MAX = 40022222; public static void solve(FastIO io) { final int N = io.nextInt(); int allTextWidth = BinarySearch.firstThat(1, W_MAX, new BinarySearch.IntCheck() { @Override public boolean valid(int value) { int h = query(io, value); return h == 1; } }); int best = allTextWidth; for (int h = 2; h <= N; ++h) { int w = best / h; int hActual = query(io, w); if (hActual > 0) { best = Math.min(best, w * hActual); } } io.println("! " + best); } private static int query(FastIO io, int w) { io.println("? " + w); io.flush(); final int H = io.nextInt(); return H; } public static class BinarySearch { // Finds the left-most value that satisfies the IntCheck in the range [L, R). // It will return R if the nothing in the range satisfies the check. public static int firstThat(int L, int R, IntCheck check) { while (L < R) { int M = (L >> 1) + (R >> 1) + (L & R & 1); if (check.valid(M)) { R = M; } else { L = M + 1; } } return L; } // Finds the right-most value that satisfies the IntCheck in the range [L, R). // It will return L - 1 if nothing in the range satisfies the check. public static int lastThat(int L, int R, IntCheck check) { int firstValue = firstThat(L, R, new IntCheck() { @Override public boolean valid(int value) { return !check.valid(value); } }); return firstValue - 1; } // Finds the left-most value that satisfies the LongCheck in the range [L, R). public static long firstThat(long L, long R, LongCheck check) { while (L < R) { long M = (L >> 1) + (R >> 1) + (L & R & 1); if (check.valid(M)) { R = M; } else { L = M + 1; } } return L; } // Finds the right-most value that satisfies the IntCheck in the range [L, R). // It will return L - 1 if nothing in the range satisfies the check. public static long lastThat(long L, long R, LongCheck check) { long firstValue = firstThat(L, R, new LongCheck() { @Override public boolean valid(long value) { return !check.valid(value); } }); return firstValue - 1; } public static interface LongCheck { public boolean valid(long value); } public static interface IntCheck { public boolean valid(int value); } } public static class FastIO { private InputStream reader; private PrintWriter writer; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastIO(InputStream r, OutputStream w) { reader = r; writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w))); } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = reader.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } // TODO: read this byte-by-byte like the other read functions. public double nextDouble() { return Double.parseDouble(nextString()); } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printlnArray(int[] arr) { printArray(arr); writer.println(); } public void printlnArray(long[] arr) { printArray(arr); writer.println(); } public void printf(String format, Object... args) { print(String.format(format, args)); } public void flush() { writer.flush(); } } public static void main(String[] args) { FastIO io = new FastIO(System.in, System.out); solve(io); io.flush(); } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 8
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
ccb42f05bde155a0dcf97e6fc3cb0cfd
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
//make sure to make new file! import java.io.*; import java.util.*; public class EG20{ public static BufferedReader f; public static PrintWriter out; public static void main(String[] args)throws IOException{ f = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n = Integer.parseInt(f.readLine()); int l = 1; int r = 4003000; int ans = -1; while(l <= r){ int mid = l + (r-l)/2; int response = query(mid); if(response != 1){ l = mid+1; } else { r = mid-1; ans = mid; } } long answer = (long)ans; ArrayList<Integer> alist = new ArrayList<Integer>(); for(int sub = 2; sub <= n; sub++){ int i = ans-sub+1; for(int k = sub; k <= n; k++){ if(i%k == 0){ alist.add(i/k); } } } for(int i : alist){ int res = query(i); if(res == 0) continue; answer = Math.min(answer,(long)i*(long)res); } out.println("! " + answer); out.close(); } public static int query(int x) throws IOException{ out.println("? " + x); out.flush(); return Integer.parseInt(f.readLine()); } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 8
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
5286a33016178e444984a32438f44a08
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
/* I am dead inside Do you like NCT, sKz, BTS? 5 4 3 2 1 Moonwalk Imma knock it down like domino Is this what you want? Is this what you want? Let's ttalkbocky about that */ import static java.lang.Math.*; import java.util.*; import java.io.*; public class NotepadExe { public static void main(String omkar[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int low = 1; int high = 1<<22; while(low != high) { int mid = (low+high)>>1; if(query(mid, infile) == 1) high = mid; else low = mid+1; } int total = low; int res = total; for(int h=2; h <= N; h++) { int actualHeight = query(total/h, infile); if(actualHeight != 0) res = min(res, actualHeight*(total/h)); } System.out.println("! "+res); } public static int query(int k, BufferedReader infile) throws Exception { System.out.println("? "+k); return Integer.parseInt(infile.readLine()); } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 8
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
2b861ebed35914fea73a33efe8039484
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.io.*; import java.util.*; public class problemE { public static void main(String[] args)throws IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(br.readLine()); int joyce_qu_so_cute = 1; int r = 2001*n; while(joyce_qu_so_cute<r) { int m = (joyce_qu_so_cute+r)/2; System.out.println("? "+m); if(Integer.parseInt(br.readLine())==1) r = m; else joyce_qu_so_cute = m+1; } int ans = joyce_qu_so_cute; for(int i=2; i<=n; i++) { double val = joyce_qu_so_cute/i; if(Math.ceil(val)-val<0.000001)val = Math.ceil(val); else val = Math.floor(val); System.out.println("? "+(int)val); int thing = Integer.parseInt(br.readLine()); if(thing!=0) ans = Math.min(ans,thing*(int)val); } System.out.println("! "+ans); } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 8
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
b4373c54c54ab9500652a66365144644
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.io.*; import java.util.*; public class problemE { public static void main(String[] args)throws IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(br.readLine()); int joyce_qu_so_cute = 1; int r = 2001*n; while(joyce_qu_so_cute<r) { int m = (joyce_qu_so_cute+r)/2; System.out.println("? "+m); if(Integer.parseInt(br.readLine())==1) r = m; else joyce_qu_so_cute = m+1; } int len = joyce_qu_so_cute; int ans = len; for(int i=2; i<=n; i++) { double val = len/i; if(Math.ceil(val)-val<0.000001)val = Math.ceil(val); else val = Math.floor(val); System.out.println("? "+(int)val); int thing = Integer.parseInt(br.readLine()); if(thing!=0) ans = Math.min(ans,thing*(int)val); } System.out.println("! "+ans); } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 8
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
7aaca86e423d80dd77626f8c620e01f4
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) {new Main().run();} FastReader in = new FastReader(false); PrintWriter out = new PrintWriter(System.out); void run() { work(); out.flush(); } long mod=998244353; long gcd(long a,long b) { return a==0?b:gcd(b%a,a); } final long inf=Long.MAX_VALUE/3; void work(){ int n=ni(); int l=1,r=1000000000; while(l<r){ int m=(l+r)/2; System.out.println("? "+m); int h=ni(); if(h!=1){ l=m+1; }else{ r=m; } } long ret=l; for(int i=2;i<=n;i++){ int w=l/i; System.out.println("? "+w); long h=nl(); if(h>0){ ret=Math.min(ret,h*w); } } out.println("! "+ret); } @SuppressWarnings("unused") private ArrayList<Integer>[] ng(int n, int m) { ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n]; for(int i=0;i<n;i++) { graph[i]=new ArrayList<>(); } for(int i=1;i<=m;i++) { int s=in.nextInt()-1,e=in.nextInt()-1; graph[s].add(e); graph[e].add(s); } return graph; } private ArrayList<long[]>[] ngw(int n, int m) { ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n]; for(int i=0;i<n;i++) { graph[i]=new ArrayList<>(); } for(int i=1;i<=m;i++) { long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong(); graph[(int)s].add(new long[] {e,w}); graph[(int)e].add(new long[] {s,w}); } return graph; } private int ni() { return in.nextInt(); } private long nl() { return in.nextLong(); } private double nd() { return in.nextDouble(); } private String ns() { return in.next(); } private long[] na(int n) { long[] A=new long[n]; for(int i=0;i<n;i++) { A[i]=in.nextLong(); } return A; } private int[] nia(int n) { int[] A=new int[n]; for(int i=0;i<n;i++) { A[i]=in.nextInt(); } return A; } } class FastReader { BufferedReader br; StringTokenizer st; InputStreamReader input;//no buffer public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } public FastReader(boolean isBuffer) { if(!isBuffer){ input=new InputStreamReader(System.in); }else{ br=new BufferedReader(new InputStreamReader(System.in)); } } public boolean hasNext(){ try{ String s=br.readLine(); if(s==null){ return false; } st=new StringTokenizer(s); }catch(IOException e){ e.printStackTrace(); } return true; } public String next() { if(input!=null){ try { StringBuilder sb=new StringBuilder(); int ch=input.read(); while(ch=='\n'||ch=='\r'||ch==32){ ch=input.read(); } while(ch!='\n'&&ch!='\r'&&ch!=32){ sb.append((char)ch); ch=input.read(); } return sb.toString(); }catch (Exception e){ e.printStackTrace(); } } while(st==null || !st.hasMoreElements())//回车,空行情况 { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return (int)nextLong(); } public long nextLong() { try { if(input!=null){ long ret=0; int b=input.read(); while(b<'0'||b>'9'){ b=input.read(); } while(b>='0'&&b<='9'){ ret=ret*10+(b-'0'); b=input.read(); } return ret; } }catch (Exception e){ e.printStackTrace(); } return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 8
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
0f3b254b49d905ad137a07e58cb317fa
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.Locale; import java.util.StringTokenizer; public class Solution implements Runnable { private static final int MAXL = 2000; private PrintStream out; private BufferedReader in; private StringTokenizer st; public void solve() throws IOException { long time0 = System.currentTimeMillis(); int n = nextInt(); int l = 0; int r = n * (MAXL + 1); while (l + 1 < r) { int m = (l + r) / 2; int res = query(m); if (res != 1) { l = m; } else { r = m; } } int answer = r; for (int i = 2; i <= n; i++) { int opt_w = (r - 1) / i; int opt_h = query(opt_w); if (opt_h > 0 && answer > opt_w * opt_h) { answer = opt_w * opt_h; } } out.println("! " + answer); System.err.println("time: " + (System.currentTimeMillis() - time0)); } private int query(int w) throws IOException { out.println("? " + w); out.flush(); int result = nextInt(); return result; } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } @Override public void run() { try { solve(); out.close(); } catch (Throwable e) { throw new RuntimeException(e); } } public Solution(String name) throws IOException { Locale.setDefault(Locale.US); if (name == null) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintStream(new BufferedOutputStream(System.out)); } else { in = new BufferedReader(new InputStreamReader(new FileInputStream(name + ".in"))); out = new PrintStream(new BufferedOutputStream(new FileOutputStream(name + ".out"))); } st = new StringTokenizer(""); } public static void main(String[] args) throws IOException { new Thread(new Solution(null)).start(); } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 8
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
0235a5eced308e3c42e95592c28329a8
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Random; import java.util.Scanner; import java.util.StringTokenizer; /* */ public class E { static Scanner fs=new Scanner(System.in); public static void main(String[] args) { int n=fs.nextInt(); int minTotal=1, maxTotal=2001*2001; while (minTotal!=maxTotal) { int mid=(minTotal+maxTotal)/2; if (query(mid)==1) { maxTotal=mid; } else { minTotal=mid+1; } } // int totalNonSpace=minTotal-(n-1); long ans=minTotal; for (int toSave=1; toSave<n; toSave++) { int divideInto=(toSave+1); int remaining=minTotal-toSave; int perLine=(divideInto-1+remaining)/divideInto; long curHeight=query(perLine); ans=Math.min(ans, curHeight*perLine); } System.out.println("! "+ans); } static final Random random=new Random(); static final int mod=1_000_000_007; static int query(int w) { System.out.println("? "+w); int returned = fs.nextInt(); if (returned ==0) { return Integer.MAX_VALUE/2; } return returned; } static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static long add(long a, long b) { return (a+b)%mod; } static long sub(long a, long b) { return ((a-b)%mod+mod)%mod; } static long mul(long a, long b) { return (a*b)%mod; } static long exp(long base, long exp) { if (exp==0) return 1; long half=exp(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials=new long[2_000_001]; static long[] invFactorials=new long[2_000_001]; static void precompFacts() { factorials[0]=invFactorials[0]=1; for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i); invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2); for (int i=invFactorials.length-2; i>=0; i--) invFactorials[i]=mul(invFactorials[i+1], i+1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k])); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 8
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
feb439f8148b08587c4f1d1d68d38867
train_108.jsonl
1650722700
This is an interactive problem.There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 \leq l_i \leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 &lt; a_2 &lt; \ldots &lt; a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 \leq i \leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays.Note that if $$$w &lt; \max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead.You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$.Find the minimum area of the text editor, which is the minimum value of $$$w \cdot h_w$$$ over all $$$w$$$ for which $$$h_w \neq 0$$$.The lengths are fixed in advance. In other words, the interactor is not adaptive.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class E { public static void main(String[] args) throws IOException { /**/ Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); /*/ Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(new FileInputStream("src/e.in")))); /**/ int n = sc.nextInt(); int min = n; int max = n*2001-1; while (min<max) { int w = (min+max)/2; System.out.println("? "+w); System.out.flush(); int h = sc.nextInt(); if (h==1) max = w; else min = w+1; } min = max-(n-1); for (int i = 2; i <= n; ++i) { while ((max-1)/i*i>=min) { int w = (max-1)/i; System.out.println("? "+w); System.out.flush(); int h = sc.nextInt(); if (h==i) max = w*h; else break; } } System.out.println("! "+max); System.out.flush(); } }
Java
["6\n\n0\n\n4\n\n2"]
1 second
["? 1\n\n? 9\n\n? 16\n\n! 32"]
NoteIn the first test case, the words are $$$\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$$$, so $$$l=\{5,2,7,3,5,6\}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$.If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory__to}$$$ $$$\texttt{ukraine__}$$$ $$$\texttt{and_anton}$$$ $$$\texttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$.If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$\texttt{glory_to_ukraine}$$$ $$$\texttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$.We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
Java 8
standard input
[ "binary search", "constructive algorithms", "greedy", "interactive" ]
8eb4ce3fb9f6220ab6dbc12819680c1e
The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$)  — the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 \leq l_i \leq 2000$$$.
2,200
null
standard output
PASSED
edd368e7659eacd5d2ab0669aa296fef
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.util.Arrays; import java.util.Collections; import java.util.Scanner; import java.util.TreeMap; public class sol2 { public static void main (String[] args) throws Exception { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int []f=new int [n]; for(int i=0;i<n;i++) f[i]=sc.nextInt(); int min=-1,max=-1; for(int i=1;i<n;i++) { if(f[i-1]==f[i]) { if(min==-1) min=i; max=i; } } // System.out.println("max="+max+" min="+min); if(min==max)System.out.println(0); else System.out.println(Math.max(1,max-min-1)); } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
75a171685b614ce7378296cd2b3b6423
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class Solution { 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 { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } static int gcd(int a, int b) { if (a == 0) return b; if (b == 0) return a; if (a == b) return a; if (a > b) return gcd(a-b, b); return gcd(a, b-a); } static void print(long []arr) { for(long a : arr) System.out.print(a + " "); System.out.println(); } static void print(int []arr) { for(int a : arr) System.out.print(a + " "); System.out.println(); } static void print(long a) { System.out.print(a); } static void printl(long a) { System.out.println(a); } static void printl(char ch) { System.out.println(ch); } static void print(char ch) { System.out.print(ch); } static void printl(String s) { System.out.println(s); } static void print(String s) { System.out.print(s); } static int[] intArray(int n) { int arr[] = new int[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextInt(); return arr; } static long[] longArray(int n) { long arr[] = new long[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextLong(); return arr; } static double[] doubleArray(int n) { double arr[] = new double[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextDouble(); return arr; } static int[][] intMatrix(int n, int m) { int mat[][] = new int[n][m]; for(int i = 0; i < mat.length; i++) for(int j = 0; j < mat[0].length; j++) mat[i][j] = sc.nextInt(); return mat; } static long[][] longMatrix(int n, int m) { long mat[][] = new long[n][m]; for(int i = 0; i < mat.length; i++) for(int j = 0; j < mat[0].length; j++) mat[i][j] = sc.nextLong(); return mat; } static class IntPair { int v1, v2; IntPair(int v1, int v2) { this.v1 = v1; this.v2 = v2; } } static class LongPair { long v1, v2; LongPair(long v1, long v2) { this.v1 = v1; this.v2 = v2; } } static FastReader sc = new FastReader(); public static void main(String[] args) { int tc = sc.nextInt(); while(tc-- > 0) { int n = sc.nextInt(); long a[] = longArray(n); int left = -1, right = -1; for(int i = 0; i < n; i++) { if(i + 1 < n && a[i] == a[i + 1]) { right = i; if(left == -1) left = i; } } if(left == -1 || right == -1 || left == right) printl(0); else printl(Math.max(1, right - left - 1)); } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
ae4b0a7949b71b479f9162272748d49d
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.util.*; import java.util.spi.AbstractResourceBundleProvider; import static java.lang.Integer.MAX_VALUE; public class codeforces { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int a[] = new int[n]; for(int i=0;i<n;i++){ a[i] = sc.nextInt(); } int min = -1; int max =-1; for(int i=1;i<n;i++){ if(a[i]==a[i-1]){ if(min==-1){ min = i; } max=i; } } if(max==min) System.out.println(0); else System.out.println(Math.max(1,max-min-1)); } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
8667553dc5c8542dcd5069dc66ffeb9d
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.io.*; import java.util.*; public class Solution { static int mod=(int)998244353; public static void main(String[] args) { Copied io = new Copied(System.in, System.out); // int k=1; int t = 1; t = io.nextInt(); for (int i = 0; i < t; i++) { // io.print("Case #" + k + ": "); solve(io); // k++; } io.close(); } public static void solve(Copied io) { int n = io.nextInt(); int a[] = io.readArray(n); int st = -1, en = -1; int ans = 0; for(int i = 0; i < n - 1; i++) { if(a[i] == a[i + 1]) { if(st == -1) { st = i; }else { en = i + 1; } } } // io.println(st + " " + en); if(en != -1) { ans = en - st + 1 == 3 ? 1 : en - st - 2; } io.println(ans); } static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } static void binaryOutput(boolean ans, Copied io){ String a=new String("YES"), b=new String("NO"); if(ans){ io.println(a); } else{ io.println(b); } } static int power(int x, int y, int p) { int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void printArr(int[] arr,Copied io) { for (int x : arr) io.print(x + " "); io.println(); } static void printArr(long[] arr,Copied io) { for (long x : arr) io.print(x + " "); io.println(); } static int[] listToInt(ArrayList<Integer> a){ int n=a.size(); int arr[]=new int[n]; for(int i=0;i<n;i++){ arr[i]=a.get(i); } return arr; } static int mod_mul(int a, int b){ a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod;} static int mod_add(int a, int b){ a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod;} static int mod_sub(int a, int b){ a = a % mod; b = b % mod; return (((a - b) % mod) + mod) % mod;} } class Pair implements Cloneable, Comparable<Pair> { int x,y; Pair(int a,int b) { this.x=a; this.y=b; } @Override public boolean equals(Object obj) { if(obj instanceof Pair) { Pair p=(Pair)obj; return p.x==this.x && p.y==this.y; } return false; } @Override public int hashCode() { return Math.abs(x)+101*Math.abs(y); } @Override public String toString() { return "("+x+" "+y+")"; } @Override protected Pair clone() throws CloneNotSupportedException { return new Pair(this.x,this.y); } @Override public int compareTo(Pair a) { int t= this.x-a.x; if(t!=0) return t; else return this.y-a.y; } } class byY implements Comparator<Pair>{ // @Override public int compare(Pair o1,Pair o2){ // -1 if want to swap and (1,0) otherwise. int t= o1.y-o2.y; if(t!=0) return t; else return o1.x-o2.x; } } class byX implements Comparator<Pair>{ // @Override public int compare(Pair o1,Pair o2){ // -1 if want to swap and (1,0) otherwise. int t= o1.x-o2.x; if(t!=0) return t; else return o1.y-o2.y; } } class DescendingOrder<T> implements Comparator<T>{ @Override public int compare(Object o1,Object o2){ // -1 if want to swap and (1,0) otherwise. int addingNumber=(Integer) o1,existingNumber=(Integer) o2; if(addingNumber>existingNumber) return -1; else if(addingNumber<existingNumber) return 1; else return 0; } } class Copied extends PrintWriter { public Copied(InputStream i) { super(new BufferedOutputStream(System.out)); r = new BufferedReader(new InputStreamReader(i)); } public Copied(InputStream i, OutputStream o) { super(new BufferedOutputStream(o)); r = new BufferedReader(new InputStreamReader(i)); } public boolean hasMoreTokens() { return peekToken() != null; } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public String next() { return nextToken(); } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } private BufferedReader r; private String line; private StringTokenizer st; private String token; private String peekToken() { if (token == null) try { while (st == null || !st.hasMoreTokens()) { line = r.readLine(); if (line == null) return null; st = new StringTokenizer(line); } token = st.nextToken(); } catch (IOException e) { } return token; } private String nextToken() { String ans = peekToken(); token = null; return ans; } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
8a00cfee5702b29cb165b7ec0688ec33
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.util.*; public class text1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int cs = sc.nextInt(); for(int dz = 0;dz < cs;dz ++) { int length = sc.nextInt(); long[] number = new long[length]; Set<Integer> set = new TreeSet<>(); for(int dz1 = 0;dz1 < length;dz1 ++){ number[dz1] = sc.nextLong(); if(dz1 > 0 && number[dz1 - 1] == number[dz1]) set.add(dz1 - 1); } if(set.size() <= 1) System.out.println(0); else System.out.println(find(set,0)); } return ; } public static int find(Set<Integer> set, int time){ for(int a : set){ set.remove(a); set.add(a + 1); if(set.contains(a + 2)) set.remove(a + 2); time ++; break; } if(set.size() == 1) return time; else return find(set,time); } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
7aba9c1af7389998257bb03fcdd5ac8b
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class UnequalArray { public static void main(String[] args) { Kattio in = new Kattio(); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); int left = 0, right = 0; for (int i = 0; i < n - 1; i++) if (a[i] == a[i + 1]) { left = i + 1; break; } for (int i = n - 1; i >= 1; i--) { if (a[i] == a[i - 1]) { right = i; break; } } if (a[right] == a[left]&&right-left==1) System.out.println(1); else System.out.println(Math.max(right - left-1, 0)); } } } class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // 标准 IO public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // 文件 IO public Kattio(String intput, String output) throws IOException { super(output); r = new BufferedReader(new FileReader(intput)); } // 在没有其他输入时返回 null public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
0a8277f7518aef293ecf6392353e326e
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; import java.util.stream.Collectors; public class coding { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st == null || !st.hasMoreElements()) { try { String str = br.readLine(); if(str == null) throw new InputMismatchException(); st = new StringTokenizer(str); } 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(); } if(str == null) throw new InputMismatchException(); return str; } public BigInteger nextBigInteger() { // TODO Auto-generated method stub return null; } public void close() { // TODO Auto-generated method stub } } public static <K, V> K getKey(Map<K, V> map, V value) { for (Map.Entry<K, V> entry: map.entrySet()) { if (value.equals(entry.getValue())) { return entry.getKey(); } } return null; } public static int median(ArrayList<Integer> x) { int p=0; if(x.size()==1) { p=x.get(0); } else { p=x.get((x.size()-1)/2 ); } return p; } // public static boolean palindrome(long x) // { // String s="",s1=""; // while(x!=BigInteger.valueOf(0)) // { // s=s+x%10; // x=x/10; // } // for(int i=0;i<s.length();i++) // { // s1=s1+s.charAt(s.length()-i-1); // } // if(s1.contentEquals(s)) // { // return true; // } // else // { // return false; // } // } static void reverse(String a[], int n) { String k="", t=""; for (int i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static int fact(int n) { int fact=1; int i=1; while(i<=n) { fact=fact*i; i++; } return fact; } static int gcd(int y, int x) { while(x!=y) { if(x>y) { x=x-y; } else { y=y-x; } } return y; } public static String generatePermutation(String str, int start, int end,ArrayList<String> x) { if (start == end-1) System.out.println(x); else { for (int i = start; i < end; i++) { str = swapString(str,start,i); generatePermutation(str,start+1,end,x); String s1=generatePermutation(str,start+1,end,x); x.add(s1); str = swapString(str,start,i); } } return str; } public static String swapString(String a, int i, int j) { char[] b =a.toCharArray(); char ch; ch = b[i]; b[i] = b[j]; b[j] = ch; return String.valueOf(b); } public static List<String> findPermutations(String str) { // base case if (str == null || str.length() == 0) { return null; } // create an empty ArrayList to store (partial) permutations List<String> partial = new ArrayList<>(); // initialize the list with the first character of the string partial.add(String.valueOf(str.charAt(0))); // do for every character of the specified string for (int i = 1; i < str.length(); i++) { // consider previously constructed partial permutation one by one // (iterate backward to avoid ConcurrentModificationException) for (int j = partial.size() - 1; j >= 0 ; j--) { // remove current partial permutation from the ArrayList String s = partial.remove(j); // Insert the next character of the specified string at all // possible positions of current partial permutation. Then // insert each of these newly constructed strings in the list for (int k = 0; k <= s.length(); k++) { // Advice: use StringBuilder for concatenation if(!partial.contains(s.substring(0, k) + str.charAt(i) + s.substring(k))) { partial.add(s.substring(0, k) + str.charAt(i) + s.substring(k)); } } } } return partial; } public static void main(String args[] ) throws Exception { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); StringBuilder sb = new StringBuilder(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); } int max=-1,min=Integer.MAX_VALUE; for(int i=0;i<n-1;i++) { if(arr[i]==arr[i+1]) { min=Math.min(min, i); max=Math.max(max, i); } } if(min==max || max==-1) { System.out.println(0); } else { if(max-min==1) { System.out.println(max-min); } else { System.out.println(max-min-1); } } } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
1f6d596748ae40a9ea5120a4a846e319
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.*; import java.lang.reflect.Array; import java.util.*; import java.io.*; import java.math.*; public class Main { public static void main(String[] args) { FastReader sc = new FastReader(new BufferedInputStream(System.in)); /* For Multiple Test Cases */ int times = sc.nextInt(); // int times = 1; while (times-- > 0){ solve(sc); } } public static void solve(FastReader sc) { int n = sc.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); } int min = -1; int max = -1; for (int i = 1; i < n; i++) { if (arr[i]==arr[i-1]){ if (min==-1){ min = i; } max = i; } } if (min==max){ out.println(0); } else { out.println(max(1,(max-min-1))); } } /*--------------------------------------------------------------------------------------- Template Credit - MagentaCobra --------------------------------------------------------------------------------------- */ public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static void sort(long[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Long> ls = new ArrayList<Long>(); for(long x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static void print(int[] arr) { //for debugging only for(int x: arr) out.print(x+" "); out.println(); } static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } /*----------------------------------------------------------------------------------------------- End for MagentaCobra's Template ------------------------------------------------------------------------------------------------ */ static long maxSubArraySum(Deque<Long> a) { long size = a.size(); long tempMax = Integer.MIN_VALUE, maxEnd = 0; for (int i = 0; i < size; i++) { maxEnd = maxEnd + a.pollFirst(); if (tempMax < maxEnd) tempMax = maxEnd; if (maxEnd < 0) maxEnd = 0; } return tempMax; } /*----------------------------------------------------------------------------------------------- Class For Fast I/O ------------------------------------------------------------------------------------------------ */ private static class FastReader { public BufferedReader br; public StringTokenizer st; public FastReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
9c3804f69ea053996e04d7b8e3b4cd0d
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.*; import java.lang.reflect.Array; import java.util.*; import java.io.*; import java.math.*; public class Main { public static void main(String[] args) { FastReader sc = new FastReader(new BufferedInputStream(System.in)); /* For Multiple Test Cases */ int times = sc.nextInt(); // For single Test Case // int times = 1; while (times-- > 0){ solve(sc); } } public static void solve(FastReader sc) { int n = sc.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); } int min = -1; int max = -1; for (int i = 1; i < n; i++) { if (arr[i]==arr[i-1]){ if (min==-1){ min = i; } max = i; } } if (min==max){ out.println(0); } else { out.println(max(1,(max-min-1))); } } /*--------------------------------------------------------------------------------------- Template Credit - MagentaCobra --------------------------------------------------------------------------------------- */ public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static void sort(long[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Long> ls = new ArrayList<Long>(); for(long x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static void print(int[] arr) { //for debugging only for(int x: arr) out.print(x+" "); out.println(); } static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } /*----------------------------------------------------------------------------------------------- End for MagentaCobra's Template ------------------------------------------------------------------------------------------------ */ static long maxSubArraySum(Deque<Long> a) { long size = a.size(); long tempMax = Integer.MIN_VALUE, maxEnd = 0; for (int i = 0; i < size; i++) { maxEnd = maxEnd + a.pollFirst(); if (tempMax < maxEnd) tempMax = maxEnd; if (maxEnd < 0) maxEnd = 0; } return tempMax; } /*----------------------------------------------------------------------------------------------- Class For Fast I/O ------------------------------------------------------------------------------------------------ */ private static class FastReader { public BufferedReader br; public StringTokenizer st; public FastReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
6453ceaab32d151e49af8df823cd1b14
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; public class practice { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new PrintWriter(System.out))); StringBuilder sb = new StringBuilder(); int t = Integer.parseInt(br.readLine()); while (t --> 0) { int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = Integer.parseInt(st.nextToken()); int l = -1; int r = -1; for (int i = 0; i < n - 1 && l == -1; i++) { if (arr[i] == arr[i + 1]) l = i; } for (int i = n - 2; i >= 0 && r == -1 && l != -1; i--) { if (arr[i] == arr[i + 1]) r = i; } int ans = 0; if (l != r) ans = Math.max(1, r - l - 1); sb.append(ans).append("\n"); } pw.println(sb.toString().trim()); pw.close(); br.close(); } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
deae83bf65a020086b2f44eaff769475
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class ComdeFormces { public static int cc2; public static pair pr; public static long sum; public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub // Reader.init(System.in); FastReader sc=new FastReader(); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); // OutputStream out = new BufferedOutputStream ( System.out ); int t=sc.nextInt(); int tc=1; while(t--!=0) { int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } int cnt=0; int vl=0; int s=-1; int e=-1; for(int i=0;i<n-1;i++) { if(a[i]==a[i+1]) { if(s==-1)s=i; cnt++; } else { cnt++; if(cnt>1) { e=i; vl++; } cnt=0; } } cnt++; if(cnt>1) { e=n-1; vl++; } if(vl==0)log.write("0\n"); else if(vl==1) { int sz=e-s+1; if(sz>2) { if(sz%2==0) { if(sz==4)log.write("1\n"); else log.write((sz-3)+"\n"); } else { if(sz==3)log.write("1\n"); else log.write((sz-3)+"\n"); } } else log.write("0\n"); } else log.write((e-s-2)+"\n"); log.flush(); } } static long evv(int a[],int mx,int mn,int n,long ans) { int ind=-1; int min=Integer.MAX_VALUE; int min2=Integer.MAX_VALUE; for(int i=0;i<n;i++) { if(i==0 || i==n-1) { if(mx-a[i]<min) { min=mx-a[i]; ind=i; } if(ind==i) { if(min2>mx-1) { min2=mx-1; } } else if(ind!=i) { if(min2>a[i]-1)min2=a[i]-1; } } else { if(Math.abs(mx-a[i])+Math.abs(mx-a[i+1])<min) { min=Math.abs(mx-a[i])+Math.abs(mx-a[i+1]); ind=i; } if(ind==i) { if(min2>mx+a[i]-2) { min2=mx+a[i]-2; } } else if(ind!=i) { if(min2>a[i]+a[i+1]-2)min2=a[i]+a[i+1]-2; } } // upd[i]=min; } return ans+=min+min2; } static long eval(ArrayList<ArrayList<Integer>> ar,int src,long f[], boolean vis[]) { long mn=Integer.MAX_VALUE; vis[src]=true; for(int p:ar.get(src)) { if(!vis[p]) { long s=eval(ar,p,f,vis); mn=Math.min(mn,s); sum+=s; } } if(src==0)return 0; if(mn==Integer.MAX_VALUE)return f[src]; sum-=mn; return Math.max(f[src], mn); } public static void radixSort(int a[]) { int n=a.length; int res[]=new int[n]; int p=1; for(int i=0;i<=8;i++) { int cnt[]=new int[10]; for(int j=0;j<n;j++) { a[j]=res[j]; cnt[(a[j]/p)%10]++; } for(int j=1;j<=9;j++) { cnt[j]+=cnt[j-1]; } for(int j=n-1;j>=0;j--) { res[cnt[(a[j]/p)%10]-1]=a[j]; cnt[(a[j]/p)%10]--; } p*=10; } } static int bits(long n) { int ans=0; while(n!=0) { if((n&1)==1)ans++; n>>=1; } return ans; } static long flor(ArrayList<Long> ar,long el) { int s=0; int e=ar.size()-1; while(s<=e) { int m=s+(e-s)/2; if(ar.get(m)==el)return ar.get(m); else if(ar.get(m)<el)s=m+1; else e=m-1; } return e>=0?e:-1; } public static int kadane(int a[]) { int sum=0,mx=Integer.MIN_VALUE; for(int i=0;i<a.length;i++) { sum+=a[i]; mx=Math.max(mx, sum); if(sum<0) sum=0; } return mx; } public static int m=1000000007; public static int mul(int a, int b) { return ((a%m)*(b%m))%m; } public static long mul(long a, long b) { return ((a%m)*(b%m))%m; } public static int add(int a, int b) { return ((a%m)+(b%m))%m; } public static long add(long a, long b) { return ((a%m)+(b%m))%m; } //debug public static <E> void p(E[][] a,String s) { System.out.println(s); for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } public static void p(int[] a,String s) { System.out.print(s+"="); for(int i=0;i<a.length;i++)System.out.print(a[i]+" "); System.out.println(); } public static void p(long[] a,String s) { System.out.print(s+"="); for(int i=0;i<a.length;i++)System.out.print(a[i]+" "); System.out.println(); } public static <E> void p(E a,String s){ System.out.println(s+"="+a); } public static <E> void p(ArrayList<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(LinkedList<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(HashSet<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(Stack<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(Queue<E> a,String s){ System.out.println(s+"="+a); } //utils static ArrayList<Integer> divisors(int n){ ArrayList<Integer> ar=new ArrayList<>(); for (int i=2; i<=Math.sqrt(n); i++){ if (n%i == 0){ if (n/i == i) { ar.add(i); } else { ar.add(i); ar.add(n/i); } } } return ar; } static ArrayList<Integer> prime(int n){ ArrayList<Integer> ar=new ArrayList<>(); int cnt=0; boolean pr=false; while(n%2==0) { ar.add(2); n/=2; } for(int i=3;i*i<=n;i+=2) { pr=false; while(n%i==0) { n/=i; ar.add(i); pr=true; } } if(n>2) ar.add(n); return ar; } static long gcd(long a,long b) { if(b==0)return a; else return gcd(b,a%b); } static int gcd(int a,int b) { if(b==0)return a; else return gcd(b,a%b); } static long factmod(long n,long mod,long img) { if(n==0)return 1; long ans=1; long temp=1; while(n--!=0) { if(temp!=img) { ans=((ans%mod)*((temp)%mod))%mod; } temp++; } return ans%mod; } static int ncr(int n, int r){ if(r>n-r)r=n-r; int ans=1; for(int i=0;i<r;i++){ ans*=(n-i); ans/=(i+1); } return ans; } public static class trip{ int a,b; int c; public trip(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } public int compareTo(trip q) { return this.b-q.b; } } static void mergesort(long[] a,int start,int end) { if(start>=end)return ; int mid=start+(end-start)/2; mergesort(a,start,mid); mergesort(a,mid+1,end); merge(a,start,mid,end); } static void merge(long[] a, int start,int mid,int end) { int ptr1=start; int ptr2=mid+1; long b[]=new long[end-start+1]; int i=0; while(ptr1<=mid && ptr2<=end) { if(a[ptr1]<=a[ptr2]) { b[i]=a[ptr1]; ptr1++; i++; } else { b[i]=a[ptr2]; ptr2++; i++; } } while(ptr1<=mid) { b[i]=a[ptr1]; ptr1++; i++; } while(ptr2<=end) { b[i]=a[ptr2]; ptr2++; i++; } for(int j=start;j<=end;j++) { a[j]=b[j-start]; } } public static class FastReader { BufferedReader b; StringTokenizer s; public FastReader() { b=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(s==null ||!s.hasMoreElements()) { try { s=new StringTokenizer(b.readLine()); } catch(IOException e) { e.printStackTrace(); } } return s.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=b.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } boolean hasNext() { if (s != null && s.hasMoreTokens()) { return true; } String tmp; try { b.mark(1000); tmp = b.readLine(); if (tmp == null) { return false; } b.reset(); } catch (IOException e) { return false; } return true; } } public static class pair{ int a; int b; public pair(int a,int b) { this.a=a; this.b=b; } public int compareTo(pair b) { return this.a-b.a; } // public int compareToo(pair b) { // return this.b-b.b; // } @Override public String toString() { return "{"+this.a+" "+this.b+"}"; } } static long pow(long a, long pw) { long temp; if(pw==0)return 1; temp=pow(a,pw/2); if(pw%2==0)return temp*temp; return a*temp*temp; } static int pow(int a, int pw) { int temp; if(pw==0)return 1; temp=pow(a,pw/2); if(pw%2==0)return temp*temp; return a*temp*temp; } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
5f882ef76d69fe871d29ef06e310657d
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.util.Scanner; /** * * @author Ahmad1423 */ public class trial { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner read = new Scanner(System.in); int t = read.nextInt(); for (int i = 0; i < t; i++) { Colorful_Stamp(read); } } public static void Colorful_Stamp(Scanner read) { int arr[] = new int[read.nextInt()]; for (int i = 0; i < arr.length; i++) { arr[i] = read.nextInt(); } int firstPair = -1; for (int i = 0; i < arr.length - 1; i++) { if(arr[i] == arr[i + 1]){ firstPair = i + 1; break; } } if(firstPair == -1){ System.out.println(0); return; } int secondPair = -1; for (int i = arr.length - 1; i > firstPair; i--) { if (arr[i] == arr[i - 1]){ secondPair = i - 1; break; } } if(secondPair == -1){ System.out.println(0); return; } if (secondPair - firstPair == 0){ System.out.println(1); return; } // System.out.println(" first pair index = " + firstPair); // System.out.println("second pair index = " + secondPair); if (secondPair - firstPair == 1){ System.out.println(1); return; } if ((secondPair - firstPair) % 2 == 0){ System.out.println(secondPair - firstPair ); return; } else { System.out.println(secondPair - firstPair ); } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
86d902868b58e91e5a8f67381ef9022b
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
// import static java.lang.System.out; import static java.lang.Math.*; import static java.lang.System.*; import java.lang.reflect.Array; import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc; static long mod = ((long) 1e9) + 7; static long lmax=Long.MAX_VALUE; static long lmin=Long.MIN_VALUE; static int imax=Integer.MAX_VALUE; static int imin=Integer.MIN_VALUE; // static FastWriter out; static FastWriter out; public static void main(String hi[]) throws IOException { initializeIO(); out = new FastWriter(); sc = new FastReader(); long startTimeProg = System.currentTimeMillis(); long endTimeProg = Long.MAX_VALUE; int t = sc.nextInt(); // int t=1; // boolean[] seave=sieveOfEratosthenes((int)(1e5)); // int[] seave2=sieveOfEratosthenesInt((long)sqrt(1e9)); // debug(seave2); while (t-- != 0) { int n=sc.nextInt(); int[] arr=readIntArray(n); int i=1,j=n-2; int c=0; debug(arr); while(i<n&&arr[i]!=arr[i-1])i++; if(i>=n){ print(0); continue; } while (j>=0&&arr[j]!=arr[j+1])j--; if(i==j){ c=1; }else if(i<j){ c=j-i; } debug(i+" "+j); print(c); } endTimeProg = System.currentTimeMillis(); debug("[finished : " + (endTimeProg - startTimeProg) + ".ms ]"); // System.out.println(String.format("%.9f", max)); } private static long findchanges(int[] arr, int ind, long avg) { long v=0; long changes=0; int n=arr.length; for(int i=0;i<n;i++){ if(ind==i)continue; v+=avg-arr[i]+v; changes+=abs(avg-arr[i]+v); } return changes; } static int c=0; private static int find(int[] arr, int i, int j) { List<Integer> li=new ArrayList<>(); while (i<=j)li.add(arr[i++]); c=0; return chk(li); } private static int chk(List<Integer> li) { if(li.size()<2)return c; List<Integer> res=new ArrayList<>(); int n=li.size(); int v=0; for(int i=0;i<n-1;i=i+2){ int l=li.get(i); int r=li.get(i+1); if(l+r>=10)c++; res.add((l+r)%10); } return chk(res); } private static boolean isPeak(int[] arr,int i,int l,int r){ if(i==l||i==r)return false; return (arr[i+1]<arr[i]&&arr[i]>arr[i-1]); } private static boolean isPeak(long[] arr,int i,int l,int r){ if(i==l||i==r)return false; return (arr[i+1]<arr[i]&&arr[i]>arr[i-1]); } private static long kadens(List<Long> li, int l, int r) { long max = Long.MIN_VALUE; long ans = 0; for (int i = l; i <= r; i++) { ans += li.get(i); max = max(ans, max); if (ans < 0) ans = 0; } return max; } public static boolean isNumeric(String strNum) { if (strNum == null) { return false; } try { double d = Double.parseDouble(strNum); } catch (NumberFormatException nfe) { return false; } return true; } private static boolean isSorted(List<Integer> li) { int n = li.size(); if (n <= 1) return true; for (int i = 0; i < n - 1; i++) { if (li.get(i) > li.get(i + 1)) return false; } return true; } static boolean isPowerOfTwo(long x) { return x != 0 && ((x & (x - 1)) == 0); } private static boolean ispallindromeList(List<Integer> res) { int l = 0, r = res.size() - 1; while (l < r) { if (res.get(l) != res.get(r)) return false; l++; r--; } return true; } private static class Pair { int first = 0; int sec = 0; int[] arr; char ch; String s; Map<Integer, Integer> map; Pair(int first, int sec) { this.first = first; this.sec = sec; } Pair(int[] arr) { this.map = new HashMap<>(); for (int x : arr) this.map.put(x, map.getOrDefault(x, 0) + 1); this.arr = arr; } Pair(char ch, int first) { this.ch = ch; this.first = first; } Pair(String s, int first) { this.s = s; this.first = first; } } private static int sizeOfSubstring(int st, int e) { int s = e - st + 1; return (s * (s + 1)) / 2; } private static Set<Long> factors(long n) { Set<Long> res = new HashSet<>(); // res.add(n); for (long i = 1; i * i <= (n); i++) { if (n % i == 0) { res.add(i); if (n / i != i) { res.add(n / i); } } } return res; } private static long fact(long n) { if (n <= 2) return n; return n * fact(n - 1); } private static long ncr(long n, long r) { return fact(n) / (fact(r) * fact(n - r)); } private static int lessThen(long[] nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = 0; while (l <= r) { int mid = l + ((r - l) / 2); if (nums[mid] <= val) { i = mid; l = mid + 1; } else { r = mid - 1; } } return i; } private static int lessThen(List<Long> nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = 0; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) <= val) { i = mid; l = mid + 1; } else { r = mid - 1; } } return i; } private static int lessThen(List<Integer> nums, int val, boolean work, int l, int r) { int i = -1; if (work) i = 0; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) <= val) { i = mid; l = mid + 1; } else { r = mid - 1; } } return i; } private static int greaterThen(List<Long> nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = r; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) >= val) { i = mid; r = mid - 1; } else { l = mid + 1; } } return i; } private static int greaterThen(List<Integer> nums, int val, boolean work) { int i = -1, l = 0, r = nums.size() - 1; if (work) i = r; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) >= val) { i = mid; r = mid - 1; } else { l = mid + 1; } } return i; } private static int greaterThen(long[] nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = r; while (l <= r) { int mid = l + ((r - l) / 2); if (nums[mid] >= val) { i = mid; r = mid - 1; } else { l = mid + 1; } } return i; } private static long gcd(long[] arr) { long ans = 0; for (long x : arr) { ans = gcd(x, ans); } return ans; } private static int gcd(int[] arr) { int ans = 0; for (int x : arr) { ans = gcd(x, ans); } return ans; } private static long sumOfAp(long a, long n, long d) { long val = (n * (2 * a + ((n - 1) * d))); return val / 2; } //geometrics private static double areaOftriangle(double x1, double y1, double x2, double y2, double x3, double y3) { double[] mid_point = midOfaLine(x1, y1, x2, y2); // debug(Arrays.toString(mid_point)+" "+x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+" "+y3); double height = distanceBetweenPoints(mid_point[0], mid_point[1], x3, y3); double wight = distanceBetweenPoints(x1, y1, x2, y2); // debug(height+" "+wight); return (height * wight) / 2; } private static double distanceBetweenPoints(double x1, double y1, double x2, double y2) { double x = x2 - x1; double y = y2 - y1; return (Math.pow(x, 2) + Math.pow(y, 2)); } public static boolean isPerfectSquareByUsingSqrt(long n) { if (n <= 0) { return false; } double squareRoot = Math.sqrt(n); long tst = (long) (squareRoot + 0.5); return tst * tst == n; } private static double[] midOfaLine(double x1, double y1, double x2, double y2) { double[] mid = new double[2]; mid[0] = (x1 + x2) / 2; mid[1] = (y1 + y2) / 2; return mid; } private static long sumOfN(long n) { return (n * (n + 1)) / 2; } private static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } /* Function to calculate x raised to the power y in O(logn)*/ static long power(long x, long y) { long temp; if (y == 0) return 1l; temp = power(x, y / 2); if (y % 2 == 0) return (temp * temp); else return (x * temp * temp); } private static StringBuilder reverseString(String s) { StringBuilder sb = new StringBuilder(s); int l = 0, r = sb.length() - 1; while (l <= r) { char ch = sb.charAt(l); sb.setCharAt(l, sb.charAt(r)); sb.setCharAt(r, ch); l++; r--; } return sb; } private static void swap(List<Integer> li, int i, int j) { int t = li.get(i); li.set(i, li.get(j)); li.set(j, t); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static String decimalToString(int x) { return Integer.toBinaryString(x); } private static String decimalToString(long x) { return Long.toBinaryString(x); } private static boolean isPallindrome(String s, int l, int r) { while (l < r) { if (s.charAt(l) != s.charAt(r)) return false; l++; r--; } return true; } private static boolean isSubsequence(String s, String t) { if (s == null || t == null) return false; Map<Character, List<Integer>> map = new HashMap<>(); //<character, index> //preprocess t for (int i = 0; i < t.length(); i++) { char curr = t.charAt(i); if (!map.containsKey(curr)) { map.put(curr, new ArrayList<Integer>()); } map.get(curr).add(i); } int prev = -1; //index of previous character for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (map.get(c) == null) { return false; } else { List<Integer> list = map.get(c); prev = lessThen(list, prev, false, 0, list.size() - 1); if (prev == -1) { return false; } prev++; } } return true; } private static StringBuilder removeLeadingZero(StringBuilder sb) { int i = 0; while (i < sb.length() && sb.charAt(i) == '0') i++; // debug("remove "+i); if (i == sb.length()) return new StringBuilder(); return new StringBuilder(sb.substring(i, sb.length())); } private static StringBuilder removeEndingZero(StringBuilder sb) { int i = sb.length() - 1; while (i >= 0 && sb.charAt(i) == '0') i--; // debug("remove "+i); if (i < 0) return new StringBuilder(); return new StringBuilder(sb.substring(0, i + 1)); } private static int stringToDecimal(String binaryString) { // debug(decimalToString(n<<1)); return Integer.parseInt(binaryString, 2); } private static int stringToInt(String s) { return Integer.parseInt(s); } private static String toString(long val) { return String.valueOf(val); } private static void debug(int[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(long[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(List<int[]> arr) { for (int[] a : arr) { err.println(Arrays.toString(a)); } } private static void debug(float[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(double[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(boolean[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void print(String s) throws IOException { out.println(s); } private static void debug(String s) throws IOException { err.println(s); } private static int charToIntS(char c) { return ((((int) (c - '0')) % 48)); } private static void print(double s) throws IOException { out.println(s); } private static void debug(char[] s1) throws IOException { debug(Arrays.toString(s1)); } private static void print(float s) throws IOException { out.println(s); } private static void print(long s) throws IOException { out.println(s); } private static void print(int s) throws IOException { out.println(s); } private static void debug(double s) throws IOException { err.println(s); } private static void debug(float s) throws IOException { err.println(s); } private static void debug(long s) { err.println(s); } private static void debug(int s) { err.println(s); } private static boolean isPrime(long n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return false; } return true; } private static List<List<Integer>> readUndirectedGraph(int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { int x = sc.nextInt(); int y = sc.nextInt(); graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readUndirectedGraph(int[][] intervals, int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < intervals.length; i++) { int x = intervals[i][0]; int y = intervals[i][1]; graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int[][] intervals, int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < intervals.length; i++) { int x = intervals[i][0]; int y = intervals[i][1]; graph.get(x).add(y); // graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { int x = sc.nextInt(); int y = sc.nextInt(); graph.get(x).add(y); // graph.get(y).add(x); } return graph; } static String[] readStringArray(int n) { String[] arr = new String[n]; for (int i = 0; i < n; i++) { arr[i] = sc.next(); } return arr; } private static Map<Character, Integer> freq(String s) { Map<Character, Integer> map = new HashMap<>(); for (char c : s.toCharArray()) { map.put(c, map.getOrDefault(c, 0) + 1); } return map; } private static Map<Long, Integer> freq(long[] arr) { Map<Long, Integer> map = new HashMap<>(); for (long x : arr) { map.put(x, map.getOrDefault(x, 0) + 1); } return map; } private static Map<Integer, Integer> freq(int[] arr) { Map<Integer, Integer> map = new HashMap<>(); for (int x : arr) { map.put(x, map.getOrDefault(x, 0) + 1); } return map; } static boolean[] sieveOfEratosthenes(long n) { boolean prime[] = new boolean[(int) n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } static int[] sieveOfEratosthenesInt(long n) { boolean prime[] = new boolean[(int) n + 1]; Set<Integer> li = new HashSet<>(); for (int i = 1; i <= n; i++) { prime[i] = true; li.add(i); } for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) { li.remove(i); prime[i] = false; } } } int[] arr = new int[li.size()]; int i = 0; for (int x : li) { arr[i++] = x; } return arr; } static boolean isMemberAC(int a, int d, int x) { // If difference is 0, then x must // be same as a. if (d == 0) return (x == a); // Else difference between x and a // must be divisible by d. return ((x - a) % d == 0 && (x - a) / d >= 0); } static boolean isMemberAC(long a, long d, long x) { // If difference is 0, then x must // be same as a. if (d == 0) return (x == a); // Else difference between x and a // must be divisible by d. return ((x - a) % d == 0 && (x - a) / d >= 0); } private static void sort(int[] arr) { int n = arr.length; List<Integer> li = new ArrayList<>(); for (int x : arr) { li.add(x); } Collections.sort(li); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sortReverse(int[] arr) { int n = arr.length; List<Integer> li = new ArrayList<>(); for (int x : arr) { li.add(x); } Collections.sort(li, Collections.reverseOrder()); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sort(double[] arr) { int n = arr.length; List<Double> li = new ArrayList<>(); for (double x : arr) { li.add(x); } Collections.sort(li); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sortReverse(double[] arr) { int n = arr.length; List<Double> li = new ArrayList<>(); for (double x : arr) { li.add(x); } Collections.sort(li, Collections.reverseOrder()); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sortReverse(long[] arr) { int n = arr.length; List<Long> li = new ArrayList<>(); for (long x : arr) { li.add(x); } Collections.sort(li, Collections.reverseOrder()); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sort(long[] arr) { int n = arr.length; List<Long> li = new ArrayList<>(); for (long x : arr) { li.add(x); } Collections.sort(li); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static long sum(int[] arr) { long sum = 0; for (int x : arr) { sum += x; } return sum; } private static long sum(long[] arr) { long sum = 0; for (long x : arr) { sum += x; } return sum; } private static void initializeIO() { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output"))); System.setErr(new PrintStream(new FileOutputStream("error.txt"))); } catch (Exception e) { System.err.println(e.getMessage()); } } private static int maxOfArray(int[] arr) { int max = Integer.MIN_VALUE; for (int x : arr) { max = Math.max(max, x); } return max; } private static long maxOfArray(long[] arr) { long max = Long.MIN_VALUE; for (long x : arr) { max = Math.max(max, x); } return max; } private static int[][] readIntIntervals(int n, int m) { int[][] arr = new int[n][m]; for (int j = 0; j < n; j++) { for (int i = 0; i < m; i++) { arr[j][i] = sc.nextInt(); } } return arr; } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static int[] readIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } return arr; } private static double[] readDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextDouble(); } return arr; } private static long[] readLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); } return arr; } private static void debug(String[] arr) { err.println(Arrays.toString(arr)); } private static void debug(Boolean[][] arr) { for (int i = 0; i < arr.length; i++) err.println(Arrays.toString(arr[i])); } private static void debug(int[] arr) { err.println(Arrays.toString(arr)); } private static void debug(long[] arr) { err.println(Arrays.toString(arr)); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { BufferedWriter bw; List<String> list = new ArrayList<>(); Set<String> set = new HashSet<>(); FastWriter() { bw = new BufferedWriter(new OutputStreamWriter(System.out)); } <T> void print(T obj) throws IOException { bw.write(obj.toString()); bw.flush(); } void println() throws IOException { print("\n"); } <T> void println(T obj) throws IOException { print(obj.toString() + "\n"); } void print(int[] arr) throws IOException { for (int x : arr) { print(x + " "); } println(); } void print(long[] arr) throws IOException { for (long x : arr) { print(x + " "); } println(); } void print(double[] arr) throws IOException { for (double x : arr) { print(x + " "); } println(); } void printCharN(char c, int n) throws IOException { for (int i = 0; i < n; i++) { print(c); } } } static class Dsu { int[] parent, size; Dsu(int n) { parent = new int[n + 1]; size = new int[n + 1]; for (int i = 0; i <= n; i++) { parent[i] = i; size[i] = 1; } } private int findParent(int u) { if (parent[u] == u) return u; return parent[u] = findParent(parent[u]); } private boolean union(int u, int v) { // System.out.println("uf "+u+" "+v); int pu = findParent(u); // System.out.println("uf2 "+pu+" "+v); int pv = findParent(v); // System.out.println("uf3 " + u + " " + pv); if (pu == pv) return false; if (size[pu] <= size[pv]) { parent[pu] = pv; size[pv] += size[pu]; } else { parent[pv] = pu; size[pu] += size[pv]; } return true; } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
08315321ecc73690a3130b2b7da6c519
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
// import static java.lang.System.out; import static java.lang.Math.*; import static java.lang.System.*; import java.lang.reflect.Array; import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc; static long mod = ((long) 1e9) + 7; static long lmax=Long.MAX_VALUE; static long lmin=Long.MIN_VALUE; static int imax=Integer.MAX_VALUE; static int imin=Integer.MIN_VALUE; // static FastWriter out; static FastWriter out; public static void main(String hi[]) throws IOException { initializeIO(); out = new FastWriter(); sc = new FastReader(); long startTimeProg = System.currentTimeMillis(); long endTimeProg = Long.MAX_VALUE; int t = sc.nextInt(); // int t=1; // boolean[] seave=sieveOfEratosthenes((int)(1e5)); // int[] seave2=sieveOfEratosthenesInt((long)sqrt(1e9)); // debug(seave2); while (t-- != 0) { int n=sc.nextInt(); int[] arr=readIntArray(n); // debug("done"); int i=0,j=n-1; int c=0; while(i<n-1){ if(arr[i]==arr[i+1]){ break; } i++; } if(i==n-1){ print(0); continue; } while(j>0){ if(arr[j]==arr[j-1])break; j--; } int values=j-i+1; if(values==2){ print(0); }else { print(max(1,values-3)); } } endTimeProg = System.currentTimeMillis(); debug("[finished : " + (endTimeProg - startTimeProg) + ".ms ]"); // System.out.println(String.format("%.9f", max)); } private static long findchanges(int[] arr, int ind, long avg) { long v=0; long changes=0; int n=arr.length; for(int i=0;i<n;i++){ if(ind==i)continue; v+=avg-arr[i]+v; changes+=abs(avg-arr[i]+v); } return changes; } static int c=0; private static int find(int[] arr, int i, int j) { List<Integer> li=new ArrayList<>(); while (i<=j)li.add(arr[i++]); c=0; return chk(li); } private static int chk(List<Integer> li) { if(li.size()<2)return c; List<Integer> res=new ArrayList<>(); int n=li.size(); int v=0; for(int i=0;i<n-1;i=i+2){ int l=li.get(i); int r=li.get(i+1); if(l+r>=10)c++; res.add((l+r)%10); } return chk(res); } private static boolean isPeak(int[] arr,int i,int l,int r){ if(i==l||i==r)return false; return (arr[i+1]<arr[i]&&arr[i]>arr[i-1]); } private static boolean isPeak(long[] arr,int i,int l,int r){ if(i==l||i==r)return false; return (arr[i+1]<arr[i]&&arr[i]>arr[i-1]); } private static long kadens(List<Long> li, int l, int r) { long max = Long.MIN_VALUE; long ans = 0; for (int i = l; i <= r; i++) { ans += li.get(i); max = max(ans, max); if (ans < 0) ans = 0; } return max; } public static boolean isNumeric(String strNum) { if (strNum == null) { return false; } try { double d = Double.parseDouble(strNum); } catch (NumberFormatException nfe) { return false; } return true; } private static boolean isSorted(List<Integer> li) { int n = li.size(); if (n <= 1) return true; for (int i = 0; i < n - 1; i++) { if (li.get(i) > li.get(i + 1)) return false; } return true; } static boolean isPowerOfTwo(long x) { return x != 0 && ((x & (x - 1)) == 0); } private static boolean ispallindromeList(List<Integer> res) { int l = 0, r = res.size() - 1; while (l < r) { if (res.get(l) != res.get(r)) return false; l++; r--; } return true; } private static class Pair { int first = 0; int sec = 0; int[] arr; char ch; String s; Map<Integer, Integer> map; Pair(int first, int sec) { this.first = first; this.sec = sec; } Pair(int[] arr) { this.map = new HashMap<>(); for (int x : arr) this.map.put(x, map.getOrDefault(x, 0) + 1); this.arr = arr; } Pair(char ch, int first) { this.ch = ch; this.first = first; } Pair(String s, int first) { this.s = s; this.first = first; } } private static int sizeOfSubstring(int st, int e) { int s = e - st + 1; return (s * (s + 1)) / 2; } private static Set<Long> factors(long n) { Set<Long> res = new HashSet<>(); // res.add(n); for (long i = 1; i * i <= (n); i++) { if (n % i == 0) { res.add(i); if (n / i != i) { res.add(n / i); } } } return res; } private static long fact(long n) { if (n <= 2) return n; return n * fact(n - 1); } private static long ncr(long n, long r) { return fact(n) / (fact(r) * fact(n - r)); } private static int lessThen(long[] nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = 0; while (l <= r) { int mid = l + ((r - l) / 2); if (nums[mid] <= val) { i = mid; l = mid + 1; } else { r = mid - 1; } } return i; } private static int lessThen(List<Long> nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = 0; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) <= val) { i = mid; l = mid + 1; } else { r = mid - 1; } } return i; } private static int lessThen(List<Integer> nums, int val, boolean work, int l, int r) { int i = -1; if (work) i = 0; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) <= val) { i = mid; l = mid + 1; } else { r = mid - 1; } } return i; } private static int greaterThen(List<Long> nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = r; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) >= val) { i = mid; r = mid - 1; } else { l = mid + 1; } } return i; } private static int greaterThen(List<Integer> nums, int val, boolean work) { int i = -1, l = 0, r = nums.size() - 1; if (work) i = r; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) >= val) { i = mid; r = mid - 1; } else { l = mid + 1; } } return i; } private static int greaterThen(long[] nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = r; while (l <= r) { int mid = l + ((r - l) / 2); if (nums[mid] >= val) { i = mid; r = mid - 1; } else { l = mid + 1; } } return i; } private static long gcd(long[] arr) { long ans = 0; for (long x : arr) { ans = gcd(x, ans); } return ans; } private static int gcd(int[] arr) { int ans = 0; for (int x : arr) { ans = gcd(x, ans); } return ans; } private static long sumOfAp(long a, long n, long d) { long val = (n * (2 * a + ((n - 1) * d))); return val / 2; } //geometrics private static double areaOftriangle(double x1, double y1, double x2, double y2, double x3, double y3) { double[] mid_point = midOfaLine(x1, y1, x2, y2); // debug(Arrays.toString(mid_point)+" "+x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+" "+y3); double height = distanceBetweenPoints(mid_point[0], mid_point[1], x3, y3); double wight = distanceBetweenPoints(x1, y1, x2, y2); // debug(height+" "+wight); return (height * wight) / 2; } private static double distanceBetweenPoints(double x1, double y1, double x2, double y2) { double x = x2 - x1; double y = y2 - y1; return (Math.pow(x, 2) + Math.pow(y, 2)); } public static boolean isPerfectSquareByUsingSqrt(long n) { if (n <= 0) { return false; } double squareRoot = Math.sqrt(n); long tst = (long) (squareRoot + 0.5); return tst * tst == n; } private static double[] midOfaLine(double x1, double y1, double x2, double y2) { double[] mid = new double[2]; mid[0] = (x1 + x2) / 2; mid[1] = (y1 + y2) / 2; return mid; } private static long sumOfN(long n) { return (n * (n + 1)) / 2; } private static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } /* Function to calculate x raised to the power y in O(logn)*/ static long power(long x, long y) { long temp; if (y == 0) return 1l; temp = power(x, y / 2); if (y % 2 == 0) return (temp * temp); else return (x * temp * temp); } private static StringBuilder reverseString(String s) { StringBuilder sb = new StringBuilder(s); int l = 0, r = sb.length() - 1; while (l <= r) { char ch = sb.charAt(l); sb.setCharAt(l, sb.charAt(r)); sb.setCharAt(r, ch); l++; r--; } return sb; } private static void swap(List<Integer> li, int i, int j) { int t = li.get(i); li.set(i, li.get(j)); li.set(j, t); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static String decimalToString(int x) { return Integer.toBinaryString(x); } private static String decimalToString(long x) { return Long.toBinaryString(x); } private static boolean isPallindrome(String s, int l, int r) { while (l < r) { if (s.charAt(l) != s.charAt(r)) return false; l++; r--; } return true; } private static boolean isSubsequence(String s, String t) { if (s == null || t == null) return false; Map<Character, List<Integer>> map = new HashMap<>(); //<character, index> //preprocess t for (int i = 0; i < t.length(); i++) { char curr = t.charAt(i); if (!map.containsKey(curr)) { map.put(curr, new ArrayList<Integer>()); } map.get(curr).add(i); } int prev = -1; //index of previous character for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (map.get(c) == null) { return false; } else { List<Integer> list = map.get(c); prev = lessThen(list, prev, false, 0, list.size() - 1); if (prev == -1) { return false; } prev++; } } return true; } private static StringBuilder removeLeadingZero(StringBuilder sb) { int i = 0; while (i < sb.length() && sb.charAt(i) == '0') i++; // debug("remove "+i); if (i == sb.length()) return new StringBuilder(); return new StringBuilder(sb.substring(i, sb.length())); } private static StringBuilder removeEndingZero(StringBuilder sb) { int i = sb.length() - 1; while (i >= 0 && sb.charAt(i) == '0') i--; // debug("remove "+i); if (i < 0) return new StringBuilder(); return new StringBuilder(sb.substring(0, i + 1)); } private static int stringToDecimal(String binaryString) { // debug(decimalToString(n<<1)); return Integer.parseInt(binaryString, 2); } private static int stringToInt(String s) { return Integer.parseInt(s); } private static String toString(long val) { return String.valueOf(val); } private static void debug(int[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(long[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(List<int[]> arr) { for (int[] a : arr) { err.println(Arrays.toString(a)); } } private static void debug(float[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(double[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(boolean[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void print(String s) throws IOException { out.println(s); } private static void debug(String s) throws IOException { err.println(s); } private static int charToIntS(char c) { return ((((int) (c - '0')) % 48)); } private static void print(double s) throws IOException { out.println(s); } private static void debug(char[] s1) throws IOException { debug(Arrays.toString(s1)); } private static void print(float s) throws IOException { out.println(s); } private static void print(long s) throws IOException { out.println(s); } private static void print(int s) throws IOException { out.println(s); } private static void debug(double s) throws IOException { err.println(s); } private static void debug(float s) throws IOException { err.println(s); } private static void debug(long s) { err.println(s); } private static void debug(int s) { err.println(s); } private static boolean isPrime(long n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return false; } return true; } private static List<List<Integer>> readUndirectedGraph(int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { int x = sc.nextInt(); int y = sc.nextInt(); graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readUndirectedGraph(int[][] intervals, int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < intervals.length; i++) { int x = intervals[i][0]; int y = intervals[i][1]; graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int[][] intervals, int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < intervals.length; i++) { int x = intervals[i][0]; int y = intervals[i][1]; graph.get(x).add(y); // graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { int x = sc.nextInt(); int y = sc.nextInt(); graph.get(x).add(y); // graph.get(y).add(x); } return graph; } static String[] readStringArray(int n) { String[] arr = new String[n]; for (int i = 0; i < n; i++) { arr[i] = sc.next(); } return arr; } private static Map<Character, Integer> freq(String s) { Map<Character, Integer> map = new HashMap<>(); for (char c : s.toCharArray()) { map.put(c, map.getOrDefault(c, 0) + 1); } return map; } private static Map<Long, Integer> freq(long[] arr) { Map<Long, Integer> map = new HashMap<>(); for (long x : arr) { map.put(x, map.getOrDefault(x, 0) + 1); } return map; } private static Map<Integer, Integer> freq(int[] arr) { Map<Integer, Integer> map = new HashMap<>(); for (int x : arr) { map.put(x, map.getOrDefault(x, 0) + 1); } return map; } static boolean[] sieveOfEratosthenes(long n) { boolean prime[] = new boolean[(int) n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } static int[] sieveOfEratosthenesInt(long n) { boolean prime[] = new boolean[(int) n + 1]; Set<Integer> li = new HashSet<>(); for (int i = 1; i <= n; i++) { prime[i] = true; li.add(i); } for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) { li.remove(i); prime[i] = false; } } } int[] arr = new int[li.size()]; int i = 0; for (int x : li) { arr[i++] = x; } return arr; } static boolean isMemberAC(int a, int d, int x) { // If difference is 0, then x must // be same as a. if (d == 0) return (x == a); // Else difference between x and a // must be divisible by d. return ((x - a) % d == 0 && (x - a) / d >= 0); } static boolean isMemberAC(long a, long d, long x) { // If difference is 0, then x must // be same as a. if (d == 0) return (x == a); // Else difference between x and a // must be divisible by d. return ((x - a) % d == 0 && (x - a) / d >= 0); } private static void sort(int[] arr) { int n = arr.length; List<Integer> li = new ArrayList<>(); for (int x : arr) { li.add(x); } Collections.sort(li); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sortReverse(int[] arr) { int n = arr.length; List<Integer> li = new ArrayList<>(); for (int x : arr) { li.add(x); } Collections.sort(li, Collections.reverseOrder()); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sort(double[] arr) { int n = arr.length; List<Double> li = new ArrayList<>(); for (double x : arr) { li.add(x); } Collections.sort(li); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sortReverse(double[] arr) { int n = arr.length; List<Double> li = new ArrayList<>(); for (double x : arr) { li.add(x); } Collections.sort(li, Collections.reverseOrder()); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sortReverse(long[] arr) { int n = arr.length; List<Long> li = new ArrayList<>(); for (long x : arr) { li.add(x); } Collections.sort(li, Collections.reverseOrder()); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sort(long[] arr) { int n = arr.length; List<Long> li = new ArrayList<>(); for (long x : arr) { li.add(x); } Collections.sort(li); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static long sum(int[] arr) { long sum = 0; for (int x : arr) { sum += x; } return sum; } private static long sum(long[] arr) { long sum = 0; for (long x : arr) { sum += x; } return sum; } private static void initializeIO() { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output"))); System.setErr(new PrintStream(new FileOutputStream("error.txt"))); } catch (Exception e) { System.err.println(e.getMessage()); } } private static int maxOfArray(int[] arr) { int max = Integer.MIN_VALUE; for (int x : arr) { max = Math.max(max, x); } return max; } private static long maxOfArray(long[] arr) { long max = Long.MIN_VALUE; for (long x : arr) { max = Math.max(max, x); } return max; } private static int[][] readIntIntervals(int n, int m) { int[][] arr = new int[n][m]; for (int j = 0; j < n; j++) { for (int i = 0; i < m; i++) { arr[j][i] = sc.nextInt(); } } return arr; } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static int[] readIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } return arr; } private static double[] readDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextDouble(); } return arr; } private static long[] readLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); } return arr; } private static void debug(String[] arr) { err.println(Arrays.toString(arr)); } private static void debug(Boolean[][] arr) { for (int i = 0; i < arr.length; i++) err.println(Arrays.toString(arr[i])); } private static void debug(int[] arr) { err.println(Arrays.toString(arr)); } private static void debug(long[] arr) { err.println(Arrays.toString(arr)); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { BufferedWriter bw; List<String> list = new ArrayList<>(); Set<String> set = new HashSet<>(); FastWriter() { bw = new BufferedWriter(new OutputStreamWriter(System.out)); } <T> void print(T obj) throws IOException { bw.write(obj.toString()); bw.flush(); } void println() throws IOException { print("\n"); } <T> void println(T obj) throws IOException { print(obj.toString() + "\n"); } void print(int[] arr) throws IOException { for (int x : arr) { print(x + " "); } println(); } void print(long[] arr) throws IOException { for (long x : arr) { print(x + " "); } println(); } void print(double[] arr) throws IOException { for (double x : arr) { print(x + " "); } println(); } void printCharN(char c, int n) throws IOException { for (int i = 0; i < n; i++) { print(c); } } } static class Dsu { int[] parent, size; Dsu(int n) { parent = new int[n + 1]; size = new int[n + 1]; for (int i = 0; i <= n; i++) { parent[i] = i; size[i] = 1; } } private int findParent(int u) { if (parent[u] == u) return u; return parent[u] = findParent(parent[u]); } private boolean union(int u, int v) { // System.out.println("uf "+u+" "+v); int pu = findParent(u); // System.out.println("uf2 "+pu+" "+v); int pv = findParent(v); // System.out.println("uf3 " + u + " " + pv); if (pu == pv) return false; if (size[pu] <= size[pv]) { parent[pu] = pv; size[pv] += size[pu]; } else { parent[pv] = pu; size[pu] += size[pv]; } return true; } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
250756a93672a595bf416a14800bdab8
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.util.*; public class JavaApplication3 { public static void main(String[]args){ Scanner in =new Scanner(System.in); int t=in.nextInt(); for(int c=0;c<t;++c){ int n= in.nextInt(); int[]r =new int[n]; for(int i=0;i<n;++i)r[i]=in.nextInt(); int begin =-1; int count =1; int temp =0; int top=0; for(int i=1;i<n;++i){ if(r[i] != r[i-1]){ if(count>1){ if(begin == -1)begin=temp;top=temp+(count);} count =1; temp=i; } else ++count; } if(count>1){ if(begin == -1)begin=temp;top=temp+(count);} int h = top -begin; if(h<6)System.out.println((h-1)/2); else if(h==6)System.out.println(3); else System.out.println((h-3)); } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
1d88b678cba2a9bac551052a720f29cd
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; public class B { static FastScanner fs = new FastScanner(); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(""); public static void main(String[] args) { int t = fs.nextInt(); for (int tt = 0; tt < t; tt++) { int n = fs.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = fs.nextInt(); int idx = -1, idx2 = -1; for (int i = 0; i < n - 1; i++) { if (a[i] == a[i + 1]) { idx = i + 1; break; } } for (int i = n - 1; i >= 1; i--) { if (a[i] == a[i - 1]) { idx2 = i - 1; break; } } if (idx2 != -1 && idx != -1 && n == 3) idx2++; if (idx2 != -1 && idx != -1 && idx == idx2) idx2++; sb.append((idx2 - idx) != -1 ? idx2-idx +"\n" : 0 + "\n"); } pw.print(sb.toString()); pw.close(); } static void sort(int[] a) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) al.add(i); Collections.sort(al); for (int i = 0; i < a.length; i++) a[i] = al.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() {while (!st.hasMoreTokens()) try {st = new StringTokenizer(br.readLine());} catch (IOException e) {}return st.nextToken();} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() {return Double.parseDouble(next());} } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
7180a456b158158864516f530645dd00
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA import java.util.*; import java.util.Map.Entry; import java.util.stream.*; import java.lang.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.io.*; public class CodeForces { static private final String INPUT = "input.txt"; static private final String OUTPUT = "output.txt"; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); static DecimalFormat df = new DecimalFormat("0.00000"); final static int mod = (int) (1e9 + 7); final static int MAX = Integer.MAX_VALUE; final static int MIN = Integer.MIN_VALUE; final static long INF = Long.MAX_VALUE; final static long NEG_INF = Long.MIN_VALUE; static Random rand = new Random(); // ======================= MAIN ================================== public static void main(String[] args) throws IOException { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; // ==== start ==== input(); preprocess(); int t = 1; t = readInt(); while (t-- > 0) { solve(); } out.flush(); // ==== end ==== if (!oj) System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } private static void solve() throws IOException { int n = readInt(); int[] arr = readIntArray(n); List<Integer> list = new ArrayList<>(); for (int i = 1; i < n; i++) { if (arr[i] == arr[i - 1]) list.add(i); } if (list.size() <= 1) out.println(0); else if (list.size() == 2) { if (list.get(0) == list.get(1) - 1) out.println(1); else out.println(list.get(1) - list.get(0) - 1); } else { out.println(list.get(list.size() - 1) - list.get(0) - 1); } } private static void preprocess() throws IOException { } // cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces // javac CodeForces.java // java CodeForces // javac CodeForces.java && java CodeForces // ==================== CUSTOM CLASSES ================================ static class Pair { int first, second; Pair(int f, int s) { first = f; second = s; } public int compareTo(Pair o) { if (this.first == o.first) return this.second - o.second; return this.first - o.first; } @Override public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (this.getClass() != obj.getClass()) return false; Pair other = (Pair) (obj); if (this.first != other.first) return false; if (this.second != other.second) return false; return true; } @Override public int hashCode() { return this.first ^ this.second; } @Override public String toString() { return this.first + " " + this.second; } } static class DequeNode { DequeNode prev, next; int val; DequeNode(int val) { this.val = val; } DequeNode(int val, DequeNode prev, DequeNode next) { this.val = val; this.prev = prev; this.next = next; } } // ======================= FOR INPUT ================================== private static void input() { FileInputStream instream = null; PrintStream outstream = null; try { instream = new FileInputStream(INPUT); outstream = new PrintStream(new FileOutputStream(OUTPUT)); System.setIn(instream); System.setOut(outstream); } catch (Exception e) { System.err.println("Error Occurred."); } br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(readLine()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readString() throws IOException { return next(); } static String readLine() throws IOException { return br.readLine().trim(); } static int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = readInt(); return arr; } static int[][] read2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = readIntArray(m); return arr; } static List<Integer> readIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readInt()); return list; } static long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = readLong(); return arr; } static long[][] read2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = readLongArray(m); return arr; } static List<Long> readLongList(int n) throws IOException { List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readLong()); return list; } static char[] readCharArray(int n) throws IOException { return readString().toCharArray(); } static char[][] readMatrix(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = readCharArray(m); return mat; } // ========================= FOR OUTPUT ================================== private static void printIList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIArray(arr[i]); } private static void printLArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLArray(arr[i]); } // ====================== TO CHECK IF STRING IS NUMBER ======================== private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // ==================== FASTER SORT ================================ private static void sort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void sort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // ==================== MATHEMATICAL FUNCTIONS =========================== private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static int mod_pow(int a, int b, int mod) { if (b == 0) return 1; int temp = mod_pow(a, b >> 1, mod); temp %= mod; temp = (int) ((1L * temp * temp) % mod); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % mod); return temp; } private static int multiply(int a, int b) { return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod); } private static int divide(int a, int b) { return multiply(a, mod_pow(b, mod - 2, mod)); } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } private static long nCr(long n, long r) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans *= i; for (long i = 2; i <= n - r; i++) ans /= i; return ans; } private static List<Integer> factors(int n) { List<Integer> list = new ArrayList<>(); for (int i = 1; 1L * i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } private static List<Long> factors(long n) { List<Long> list = new ArrayList<>(); for (long i = 1; i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } // ==================== Primes using Seive ===================== private static List<Integer> getPrimes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); for (int i = 2; 1L * i * i <= n; i++) if (prime[i]) for (int j = i * i; j <= n; j += i) prime[j] = false; // return prime; List<Integer> list = new ArrayList<>(); for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } private static int[] SeivePrime(int n) { int[] primes = new int[n]; for (int i = 0; i < n; i++) primes[i] = i; for (int i = 2; 1L * i * i < n; i++) { if (primes[i] != i) continue; for (int j = i * i; j < n; j += i) if (primes[j] == j) primes[j] = i; } return primes; } // ==================== STRING FUNCTIONS ================================ private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } // check if a is subsequence of b private static boolean isSubsequence(String a, String b) { int idx = 0; for (int i = 0; i < b.length() && idx < a.length(); i++) if (a.charAt(idx) == b.charAt(i)) idx++; return idx == a.length(); } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // ==================== LIS & LNDS ================================ private static int LIS(int arr[], int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find1(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // =============== Lower Bound & Upper Bound =========== // less than or equal private static int lower_bound(List<Integer> list, int val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(List<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(List<Long> list, long val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== UNION FIND ===================== private static int find(int x, int[] parent) { if (parent[x] == x) return x; return parent[x] = find(parent[x], parent); } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) return false; if (rank[lx] > rank[ly]) parent[ly] = lx; else if (rank[lx] < rank[ly]) parent[lx] = ly; else { parent[lx] = ly; rank[ly]++; } return true; } // ================== SEGMENT TREE (RANGE SUM & RANGE UPDATE) ================== public static class SegmentTree { int n; long[] arr, tree, lazy; SegmentTree(long arr[]) { this.arr = arr; this.n = arr.length; this.tree = new long[(n << 2)]; this.lazy = new long[(n << 2)]; build(1, 0, n - 1); } void build(int id, int start, int end) { if (start == end) tree[id] = arr[start]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; build(left, start, mid); build(right, mid + 1, end); tree[id] = tree[left] + tree[right]; } } void update(int l, int r, long val) { update(1, 0, n - 1, l, r, val); } void update(int id, int start, int end, int l, int r, long val) { distribute(id, start, end); if (end < l || r < start) return; if (start == end) tree[id] += val; else if (l <= start && end <= r) { lazy[id] += val; distribute(id, start, end); } else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; update(left, start, mid, l, r, val); update(right, mid + 1, end, l, r, val); tree[id] = tree[left] + tree[right]; } } long query(int l, int r) { return query(1, 0, n - 1, l, r); } long query(int id, int start, int end, int l, int r) { if (end < l || r < start) return 0L; distribute(id, start, end); if (start == end) return tree[id]; else if (l <= start && end <= r) return tree[id]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r); } } void distribute(int id, int start, int end) { if (start == end) tree[id] += lazy[id]; else { tree[id] += lazy[id] * (end - start + 1); lazy[(id << 1)] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; } lazy[id] = 0; } } // ==================== TRIE ================================ static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } void insert(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) curr.children[ch - 'a'] = new Node(); curr = curr.children[ch - 'a']; } curr.isEnd = true; } boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) return false; curr = curr.children[ch - 'a']; } return curr.isEnd; } } // ==================== FENWICK TREE ================================ static class FT { long[] tree; int n; FT(int[] arr, int n) { this.n = n; this.tree = new long[n + 1]; for (int i = 1; i <= n; i++) { update(i, arr[i - 1]); } } void update(int idx, int val) { while (idx <= n) { tree[idx] += val; idx += idx & -idx; } } long query(int l, int r) { return getSum(r) - getSum(l - 1); } long getSum(int idx) { long ans = 0L; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } } // ==================== BINARY INDEX TREE ================================ static class BIT { long[][] tree; int n, m; BIT(int[][] mat, int n, int m) { this.n = n; this.m = m; tree = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { update(i, j, mat[i - 1][j - 1]); } } } void update(int x, int y, int val) { while (x <= n) { int t = y; while (t <= m) { tree[x][t] += val; t += t & -t; } x += x & -x; } } long query(int x1, int y1, int x2, int y2) { return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1); } long getSum(int x, int y) { long ans = 0L; while (x > 0) { int t = y; while (t > 0) { ans += tree[x][t]; t -= t & -t; } x -= x & -x; } return ans; } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
fef7c5d2c27ae1b5ba22e5ec9ad131e1
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.util.Arrays; import java.util.Collections; import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class test { static void solve(int n, long[] a) { int cnt = 0; for (int i = 2; i < n + 1; i++) { if (a[i] == a[i - 1]) { cnt++; } } if (cnt <= 1) { System.out.println(0); return; } else { ArrayList<Integer> s = new ArrayList<>(); for (int i = 2; i < n + 1; i++) { if (a[i] == a[i - 1]) { s.add(i); } } cnt = s.get(s.size() - 1) - s.get(0) - 1; System.out.println(Math.max(cnt, 1)); } } public static void main(String[] args) { Scanner w = new Scanner(System.in); int t = w.nextInt(); for (int i = 0; i < t; i++) { int n = w.nextInt(); long[] a = new long[n + 1]; for (int j = 1; j < n + 1; j++) { a[j] = w.nextLong(); } solve(n, a); } } //------------------// static int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } static int lcm(int a, int b) { return (a * b) / gcd(a, b); } static void increment(ArrayList<Long> a) { for (int i = 0; i < a.size(); i++) { long val = a.get(i); val++; a.set(i, val); } } static void changeStatus(String a) { for (int i = 0; i < a.length(); i++) { if (a.charAt(i) == '0') { a.replace(a.charAt(i), '1'); } else { a.replace(a.charAt(i), '0'); } } } static long factorial(long n) { long res = 1; for (long i = 2; i <= n; i++) { res *= i; } return res; } static long cGeneral(long n, long r) { return factorial(n)/(factorial(r) * factorial(n-r)); } static String reverse(String s) { StringBuilder input1 = new StringBuilder(); input1.append(s); input1.reverse(); return input1.toString(); } static void reverseArray(String[] a) { for (int i = 0; i < a.length; i++) { a[i] = reverse(a[i]); } } static boolean equalsArray(int[] a, int[] b) { for (int i = 0; i < a.length; i++) { if (a[i] != b[i]) { return false; } } return true; } static long sum(ArrayList<Long> a) { long sum = 0; for (int i = 0; i < a.size(); i++) { sum += a.get(i); } return sum; } static long sum(long[] a) { long sum = 0; for (int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } static long max(ArrayList<Long> a) { long max = Long.MIN_VALUE; if (a.size() == 0) { max = 0; } for (int i = 0; i < a.size(); i++) { if (a.get(i) >= max) { max = a.get(i); } } return max; } static long min(ArrayList<Long> a) { long min = Long.MAX_VALUE; if (a.size() == 0) { min = 0; } for (int i = 0; i < a.size(); i++) { if (a.get(i) <= min) { min = a.get(i); } } return min; } static long max(long[] a) { long max = Long.MIN_VALUE; if (a.length == 0) { max = 0; } for (int i = 0; i < a.length; i++) { if (a[i] >= max) { max = a[i]; } } return max; } static long min(long[] a) { long min = Long.MAX_VALUE; if (a.length == 0) { min = 0; } for (int i = 0; i < a.length; i++) { if (a[i] <= min) { min = a[i]; } } return min; } static boolean isPrime(long num) { if(num<=1) { return false; } for(long i=2;i<=num/2;i++) { if((num%i)==0) return false; } return true; } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
696d8287170224123c113fa5ac72df76
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
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.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class cf1672C { // https://codeforces.com/problemset/problem/1672/C public static void main(String[] args) { Kattio io = new Kattio(); int t = io.nextInt(), n; int[] arr; for (int i = 0; i < t; i++) { n = io.nextInt(); arr = new int[n]; int eq = 0; int last = -1; for (int k = 0; k < n; k++) { arr[k] = io.nextInt(); if (k > 0) { if (arr[k] == last) eq ++; } last = arr[k]; } int moves = 0; if (eq > 1) { for (int k = 0; k < n - 2; k++) { if (arr[k] == arr[k+1]) { moves ++; if (k + 4 <= n && (arr[k+2] == arr[k+3] || arr[k+1] == arr[k+2])) { if (arr[k] == arr[k+2] && arr[k] == arr[k+3]) eq -=2; else eq --; if (eq <= 1) break; } arr[k+1] = 0; arr[k+2] = 0; } } } io.println(moves); } io.close(); } // Kattio static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in,System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(problemName+".out"); r = new BufferedReader(new FileReader(problemName+".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) {} return null; } public String nextLine() { try { st = null; return r.readLine(); } catch (Exception e) {} return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
5e1b78923ba321d1f50f811ea7f87c7b
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; //import java.util.*; public class Solution { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-->0) { Integer n = Integer.parseInt(br.readLine()); Integer[] a = Arrays.stream(br.readLine().split("\\s+")).map(Integer::parseInt).toArray(Integer[]::new); int firstfist = Integer.MAX_VALUE; int lastlast = -1; int times = 0; for(int i=1;i<n;i++){ if(a[i].equals(a[i - 1])){ times++; firstfist = Math.min(firstfist, i-1); lastlast = Math.max(lastlast, i); } } if(times<=1) System.out.println(0); else System.out.println(Math.max(1, lastlast-firstfist-2)); } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
f4c83919317d5a608ff550f6c154cecf
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.util.Scanner; public class UnequalArray { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } int first = 0; int second = 0; boolean flag = false; for (int i = 0; i < n - 1; i++) { if (a[i] == a[i + 1]) { first = i + 1; flag = true; break; } } for (int i = n - 1; i >= 1; i--) { if (a[i] == a[i - 1]) { second = i - 1; flag = true; break; } } if (flag) { if (second < first) { System.out.println(0); } else if (first == second) { System.out.println(1); } else { System.out.println(second - first); } } else { System.out.println(0); } } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
f3b5bde8eab48d8e97ca685dd2f75ec0
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.awt.*; import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.StringTokenizer; public class C { { MULTI_TEST = true; FILE_NAME = ""; NEED_FILE_IO = false; INF = (long) 1e18; } // fields void solve() { int n = ri(); int[] a = ria(n); ArrayList<Point> list = new ArrayList<>(); int sum = 0; int l = 0, r = 0; while(r < n) { while(r + 1 < n && a[r] == a[r + 1]) r++; if(r - l >= 1) { list.add(new Point(l, r)); sum += r - l + 1; } l = r + 1; r = l; } if(sum > 2) { if(list.size() != 1) { int firstPos = list.get(0).x + 1; int lastPos = list.get(list.size() - 1).y - 1; int size = lastPos - firstPos + 1; out.println(size - 1); } else { int size = list.get(0).y - list.get(0).x + 1; out.println(size == 3 ? 1 : size - 3); } } else { out.println(0); } } public static void main(String[] args) { new C().run(); } //main @SuppressWarnings("unused") long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } @SuppressWarnings("unused") long lcm(long a, long b) { return a / gcd(a, b) * b; } @SuppressWarnings("unused") int gcd(int a, int b) { return (int) gcd((long) a, b); } @SuppressWarnings("unused") int lcm(int a, int b) { return (int) lcm(a, (long) b); } @SuppressWarnings("unused") int sqrtInt(int x) { return (int) sqrtLong(x); } @SuppressWarnings("unused") long sqrtLong(long x) { long root = (long) Math.sqrt(x); while (root * root > x) --root; while ((root + 1) * (root + 1) <= x) ++root; return root; } @SuppressWarnings("unused") int cbrtLong(long x) { int cbrt = (int) Math.cbrt(x); while ((long) cbrt * cbrt >= x) { cbrt--; } while ((long) (cbrt + 1) * (cbrt + 1) <= x) cbrt++; return cbrt; } @SuppressWarnings("unused") long binpow(long a, long power) { return binpow(a, power, Long.MAX_VALUE); } @SuppressWarnings("unused") long binpow(long a, long power, long modulo) { if (power == 0) return 1 % modulo; if (power % 2 == 1) { long b = binpow(a, power - 1, modulo) % modulo; return ((a % modulo) * b) % modulo; } else { long b = binpow(a, power / 2, modulo) % modulo; return (b * b) % modulo; } } @SuppressWarnings("unused") long fastMod(String s1, long n2) { long num = 0; for (int i = 0; i < s1.length() - 1; i++) { num += Integer.parseInt(String.valueOf(s1.charAt(i))); num *= 10; if (num >= n2) { num = num % n2; } } return (num + Integer.parseInt(String.valueOf(s1.charAt(s1.length() - 1)))) % n2; } @SuppressWarnings("unused") long factorialMod(long n, long p) { long res = 1; while (n > 1) { res = (res * ((n / p) % 2 == 1 ? p - 1 : 1)) % p; for (int i = 2; i <= n % p; ++i) res = (res * i) % p; n /= p; } return res % p; } @SuppressWarnings("unused") boolean isPrime(int number) { for (int i = 2; i * i <= number; i++) { if (number % i == 0) { return false; } } return number > 1; } @SuppressWarnings("unused") boolean[] primes(int border) { boolean[] isPrimes = new boolean[border + 1]; Arrays.fill(isPrimes, true); isPrimes[0] = false; isPrimes[1] = false; for (int i = 2; i < border + 1; i++) { if (!isPrimes[i]) continue; for (int k = i * 2; k < border; k += i) { isPrimes[k] = false; } } return isPrimes; } //Number theory @SuppressWarnings("unused") void sort(int[] a) { int n = a.length; Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = a[i]; } Arrays.sort(arr); for (int i = 0; i < n; i++) { a[i] = arr[i]; } } @SuppressWarnings("unused") void sort(long[] a) { int n = a.length; Long[] arr = new Long[n]; for (int i = 0; i < n; i++) { arr[i] = a[i]; } Arrays.sort(arr); for (int i = 0; i < n; i++) { a[i] = arr[i]; } } @SuppressWarnings("unused") int max(int[] a) { int n = a.length; int max = Integer.MIN_VALUE; for (int j : a) { if (j > max) max = j; } return max; } @SuppressWarnings("unused") long max(long[] a) { int n = a.length; long max = Long.MIN_VALUE; for (long l : a) { if (l > max) max = l; } return max; } @SuppressWarnings("unused") int maxIndex(int[] a) { int n = a.length; int max = Integer.MIN_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] > max) { max = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int maxIndex(long[] a) { int n = a.length; long max = Long.MIN_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] > max) { max = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int min(int[] a) { int n = a.length; int min = Integer.MAX_VALUE; for (int j : a) { if (j < min) min = j; } return min; } @SuppressWarnings("unused") int minIndex(int[] a) { int n = a.length; int min = Integer.MAX_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] < min) { min = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int minIndex(long[] a) { int n = a.length; long min = Long.MAX_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] < min) { min = a[i]; index = i; } } return index; } @SuppressWarnings("unused") long min(long[] a) { int n = a.length; long min = Long.MAX_VALUE; for (long l : a) { if (l < min) min = l; } return min; } @SuppressWarnings("unused") long sum(int[] a) { int n = a.length; long sum = 0; for (int j : a) { sum += j; } return sum; } @SuppressWarnings("unused") long sum(long[] a) { int n = a.length; long sum = 0; for (long l : a) { sum += l; } return sum; } //Arrays Operations @SuppressWarnings("unused") String readLine() { try { return in.readLine(); } catch (Exception e) { throw new InputMismatchException(); } } @SuppressWarnings("unused") String rs() { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(readLine()); } return tok.nextToken(); } @SuppressWarnings("unused") int ri() { return Integer.parseInt(rs()); } @SuppressWarnings("unused") long rl() { return Long.parseLong(rs()); } @SuppressWarnings("unused") double rd() { return Double.parseDouble(rs()); } @SuppressWarnings("unused") int[] ria(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ri(); } return a; } @SuppressWarnings("unused") int[] riawd(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ri() - 1; } return a; } @SuppressWarnings("unused") long[] rla(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = rl(); } return a; } @SuppressWarnings("unused") private boolean yesNo(boolean yes, String yesString, String noString) { out.println(yes ? yesString : noString); return yes; } //fastIO void run() { try { long start = System.currentTimeMillis(); initIO(); if (MULTI_TEST) { long t = rl(); while (t-- > 0) { solve(); } } else { solve(); } out.close(); System.err.println("Time(ms) - " + (System.currentTimeMillis() - start)); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } void initConsoleIO() { out = new PrintWriter(System.out); in = new BufferedReader(new InputStreamReader(System.in)); } void initFileIO(String inName, String outName) throws FileNotFoundException { out = new PrintWriter(outName); in = new BufferedReader(new FileReader(inName)); } void initIO() throws FileNotFoundException { if (!FILE_NAME.isEmpty()) { initFileIO(FILE_NAME + ".in", FILE_NAME + ".out"); } else { if (NEED_FILE_IO && new File("input.txt").exists()) { initFileIO("input.txt", "output.txt"); } else { initConsoleIO(); } } tok = new StringTokenizer(""); } //initIO private final String FILE_NAME; private final boolean MULTI_TEST; private final boolean NEED_FILE_IO; private final long INF; BufferedReader in; PrintWriter out; StringTokenizer tok; //fields }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output
PASSED
27289055f428adfb3c32036dfeedc966
train_108.jsonl
1650722700
You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.sort; public class Codeforces { static int max = (int) 1e5 + 5; public static void main(String[] args) { FastReader fastReader = new FastReader(); PrintWriter out = new PrintWriter(System.out); int tt = fastReader.nextInt(); while (tt-- > 0) { int n = fastReader.nextInt(); int a[] = fastReader.ria(n); int min = IMAX, max = 0; for (int i = 0; i < n - 1; i++) { if (a[i] == a[i + 1]) { min = min(min, i); max = max(max, i + 1); } } if (min == IMAX) { out.println(0); continue; } else { int ans = max - min + 1; if (ans == 3) { out.println(1); } else { out.println(max(0, ans - 3)); } } } out.close(); } // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final long LMAX = 9223372036854775807L; static Random __r = new Random(); // math util static int minof(int a, int b, int c) { return min(a, min(b, c)); } static int minof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static long minof(long a, long b, long c) { return min(a, min(b, c)); } static long minof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static int maxof(int a, int b, int c) { return max(a, max(b, c)); } static int maxof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static long maxof(long a, long b, long c) { return max(a, max(b, c)); } static long maxof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static int powi(int a, int b) { if (a == 0) return 0; int ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static long powl(long a, int b) { if (a == 0) return 0; long ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static int fli(double d) { return (int) d; } static int cei(double d) { return (int) ceil(d); } static long fll(double d) { return (long) d; } static long cel(double d) { return (long) ceil(d); } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static int[] exgcd(int a, int b) { if (b == 0) return new int[] { 1, 0 }; int[] y = exgcd(b, a % b); return new int[] { y[1], y[0] - y[1] * (a / b) }; } static long[] exgcd(long a, long b) { if (b == 0) return new long[] { 1, 0 }; long[] y = exgcd(b, a % b); return new long[] { y[1], y[0] - y[1] * (a / b) }; } static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static long mix(long x) { x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31); } public static boolean[] findPrimes(int limit) { assert limit >= 2; final boolean[] nonPrimes = new boolean[limit]; nonPrimes[0] = true; nonPrimes[1] = true; int sqrt = (int) Math.sqrt(limit); for (int i = 2; i <= sqrt; i++) { if (nonPrimes[i]) continue; for (int j = i; j < limit; j += i) { if (!nonPrimes[j] && i != j) nonPrimes[j] = true; } } return nonPrimes; } // array util static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] ria(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = Integer.parseInt(next()); return a; } long nextLong() { return Long.parseLong(next()); } long[] rla(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = Long.parseLong(next()); return a; } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"]
1 second
["2\n1\n2\n0"]
NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation" ]
a91be662101762bcb2c58b8db9ff61e0
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$
1,100
For each test case, print the minimum number of operations needed.
standard output