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 | ce68ecc5b8b8ddaff8b6ba8d875180ad | 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 Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
while(T-->0) {
int n=sc.nextInt();
long[] a=new long[n];
for(int i=0;i<n;i++)
a[i]=sc.nextLong();
int l=-1,r=-1;
for(int i=0;i<n-1;i++) {
if(a[i]==a[i+1]) {
if(l==-1)
l=i;
r=i;
}
}
if(l==r)
System.out.println("0");
else
System.out.println(Math.max(r-l-1,1));
}
sc.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 8 | 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 | eb82a196393378072c3e8fef8ff1f089 | 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.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
public class CodeForces {
static int mod = (int)1e9+7;
public static void main(String[] args) throws InterruptedException {
// PrintWriter out = new PrintWriter("output.txt");
// File input = new File("input.txt");
// FastScanner fs = new FastScanner(input);
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int testNumber =fs.nextInt();
for (int T =0;T<testNumber;T++){
int n = fs.nextInt();
int [] arr = fs.readArray(n);
ArrayList<Pair> w = new ArrayList<>();
for (int i=0;i<n;i++){
int j=i;
while(j<n-1&&arr[j+1]==arr[i])j++;
if(j!=i)w.add(new Pair(i,j));
i=j;
}
// int ans =0;
// boolean two=false;
// for (int a=0;a<w.size();a++){
// Pair p = w.get(a);
// int i=p.x;
// int j=p.y;
//
//
// if(a<w.size()-1){
// Pair p2 = w.get(a+1);
// int i2=p2.x;
// if(i2==j+1){
// j-=2;
// w.get(a+1).x++;
// ans+=2;
// }
//
// }
//
// if(i==j)continue;
// if(j-i-1==0){
// two=true;
// ans+=2;
// }
// else ans+=j-i-1;
//
// }
//
// if(ans>1)
// ans--;
// if(ans>1)
// if(two)ans--;
int ans = 0;
if(w.size()==1){
int i= w.get(0).x;
int j = w.get(0).y;
ans =j-i-1;
}else if(w.size()>1){
int i= w.get(0).x;
int j = w.get(w.size()-1).y;
ans =j-i-1;
}
if(ans>1)ans--;
out.print(Math.max(ans,0)+"\n");
}
out.flush();
}
static long modInverse( long n, long p)
{
return FastPower(n, p - 2, p);
}
static int[] factorials(int max,int mod){
int [] ans = new int[max+1];
ans[0]=1;
for (int i=1;i<=max;i++){
ans[i]=ans[i-1]*i;
ans[i]%=mod;
}
return ans;
}
static String toBinary(int num,int bits){
String res =Integer.toBinaryString(num);
while(res.length()<bits)res="0"+res;
return res;
}
static String toBinary(long num,int bits){
String res =Long.toBinaryString(bits);
while(res.length()<bits)res="0"+res;
return res;
}
static long LCM(long a,long b){
return a*b/gcd(a,b);
}
static long FastPower(long x,long p,long mod){
if(p==0)return 1;
long ans =FastPower(x, p/2,mod);
ans%=mod;
ans*=ans;
ans%=mod;
if(p%2==1)ans*=x;
ans%=mod;
return ans;
}
static double FastPower(double x,int p){
if(p==0)return 1.0;
double ans =FastPower(x, p/2);
ans*=ans;
if(p%2==1)ans*=x;
return ans;
}
static int FastPower(int x,int p){
if(p==0)return 1;
int ans =FastPower(x, p/2);
ans*=ans;
if(p%2==1)ans*=x;
return ans;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(){
br=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
}
public FastScanner(File f){
try {
br=new BufferedReader(new FileReader(f));
st=new StringTokenizer("");
} catch(FileNotFoundException e){
br=new BufferedReader(new InputStreamReader(System.in));
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());
}
double nextDouble() {
return Double.parseDouble(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());
}
}
public static long factorial(int n){
if(n==0)return 1;
return (long)n*factorial(n-1);
}
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static void sort (int[]a){
ArrayList<Integer> b = new ArrayList<>();
for(int i:a)b.add(i);
Collections.sort(b);
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static void sortReversed (int[]a){
ArrayList<Integer> b = new ArrayList<>();
for(int i:a)b.add(i);
Collections.sort(b,new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static void sort (long[]a){
ArrayList<Long> b = new ArrayList<>();
for(long i:a)b.add(i);
Collections.sort(b);
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static ArrayList<Integer> sieveOfEratosthenes(int n)
{
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
ArrayList<Integer> ans = new ArrayList<>();
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
ans.add(i);
}
return ans;
}
static int binarySearchSmallerOrEqual(int arr[], int key)
{
int n = arr.length;
int left = 0, right = n;
int mid = 0;
while (left < right) {
mid = (right + left) >> 1;
if (arr[mid] == key) {
while (mid + 1 < n && arr[mid + 1] == key)
mid++;
break;
}
else if (arr[mid] > key)
right = mid;
else
left = mid + 1;
}
while (mid > -1 && arr[mid] > key)
mid--;
return mid;
}
static int binarySearchSmallerOrEqual(long arr[], long key)
{
int n = arr.length;
int left = 0, right = n;
int mid = 0;
while (left < right) {
mid = (right + left) >> 1;
if (arr[mid] == key) {
while (mid + 1 < n && arr[mid + 1] == key)
mid++;
break;
}
else if (arr[mid] > key)
right = mid;
else
left = mid + 1;
}
while (mid > -1 && arr[mid] > key)
mid--;
return mid;
}
public static int binarySearchStrictlySmaller(int[] arr, int target)
{
int start = 0, end = arr.length-1;
if(end == 0) return -1;
if (target > arr[end]) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
public static int binarySearchStrictlySmaller(long[] arr, long target)
{
int start = 0, end = arr.length-1;
if(end == 0) return -1;
if (target > arr[end]) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
static int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
static int binarySearch(long arr[], long x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
static void init(int[]arr,int val){
for(int i=0;i<arr.length;i++){
arr[i]=val;
}
}
static void init(int[][]arr,int val){
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
arr[i][j]=val;
}
}
}
static void init(long[]arr,long val){
for(int i=0;i<arr.length;i++){
arr[i]=val;
}
}
static<T> void init(ArrayList<ArrayList<T>>arr,int n){
for(int i=0;i<n;i++){
arr.add(new ArrayList());
}
}
static int binarySearchStrictlySmaller(ArrayList<Pair> arr, int target)
{
int start = 0, end = arr.size()-1;
if(end == 0) return -1;
if (target > arr.get(end).y) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr.get(mid).y >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
static int binarySearchStrictlyGreater(int[] arr, int target)
{
int start = 0, end = arr.length - 1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] <= target) {
start = mid + 1;
}
else {
ans = mid;
end = mid - 1;
}
}
return ans;
}
public static long pow(long n, long pow) {
if (pow == 0) {
return 1;
}
long retval = n;
for (long i = 2; i <= pow; i++) {
retval *= n;
}
return retval;
}
static String reverse(String s){
StringBuffer b = new StringBuffer(s);
b.reverse();
return b.toString();
}
static String charToString (char[] arr){
String t="";
for(char c :arr){
t+=c;
}
return t;
}
int[] copy (int [] arr , int start){
int[] res = new int[arr.length-start];
for (int i=start;i<arr.length;i++)res[i-start]=arr[i];
return res;
}
static int[] swap(int [] A,int l,int r){
int[] B=new int[A.length];
for (int i=0;i<l;i++){
B[i]=A[i];
}
int k=0;
for (int i=r;i>=l;i--){
B[l+k]=A[i];
k++;
}
for (int i=r+1;i<A.length;i++){
B[i]=A[i];
}
return B;
}
static int mex (int[] d){
int [] a = Arrays.copyOf(d, d.length);
sort(a);
if(a[0]!=0)return 0;
int ans=1;
for(int i=1;i<a.length;i++){
if(a[i]==a[i-1])continue;
if(a[i]==a[i-1]+1)ans++;
else break;
}
return ans;
}
static int[] mexes(int[] arr){
int[] freq = new int [100000+7];
for (int i:arr)freq[i]++;
int maxMex =0;
for (int i=0;i<=100000+7;i++){
if(freq[i]!=0)maxMex++;
else break;
}
int []ans = new int[arr.length];
ans[arr.length-1] = maxMex;
for (int i=arr.length-2;i>=0;i--){
freq[arr[i+1]]--;
if(freq[arr[i+1]]<=0){
if(arr[i+1]<maxMex)
maxMex=arr[i+1];
ans[i]=maxMex;
} else {
ans[i]=ans[i+1];
}
}
return ans;
}
static int [] freq (int[]arr,int max){
int []b = new int[max];
for (int i:arr)b[i]++;
return b;
}
static int[] prefixSum(int[] arr){
int [] a = new int[arr.length];
a[0]=arr[0];
for (int i=1;i<arr.length;i++)a[i]=a[i-1]+arr[i];
return a;
}
static class Pair {
int x;
int y;
int extra;
public Pair(int x,int y){
this.x=x;
this.y=y;
}
public Pair(int x,int y,int extra){
this.x=x;
this.y=y;
this.extra=extra;
}
// @Override
// public boolean equals(Object o) {
// if(o instanceof Pair){
// if(o.hashCode()!=hashCode()){
// return false;
// } else {
// return x==((Pair)o).x&&y==((Pair)o).y;
// }
// }
//
// return false;
//
// }
//
//
//
//
//
// @Override
// public int hashCode() {
// return x+(int)y*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 8 | 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 | 486d1cc05fb026900966ee753e1dbbdf | 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 B {
public static long gcd(long a, long b) {
return a == 0 ? b : gcd(b, a % b);
}
public static void print(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
public static String concat(String s1, String s2) {
return new StringBuilder(s1).append(s2).toString();
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
/*
* 7
1 1 2 3 2 1 1
*
*
*/
int t1 = sc.nextInt();
outer: while (t1-- > 0) {
int n = sc.nextInt();
int l [] = sc.readArray(n);
int first = 0,last = 0;
boolean ff = false;
int ans = 0;
for (int i = 0;i<n-1;i++) {
if (l[i] == l[i+1]) {
if (!ff) {
ff = true;
first = i;
}else {
last = i+1;
}
}
}
ans = (last - first)-2;
if (ans<0)ans = 0;
else if (last - first == 2) ans = 1;
out.println(ans);
}
out.close();
}
static class Pair implements Comparable<Pair> {
int first;
int second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
public int compareTo(Pair p) {
if (first != p.first)
return Integer.compare(first, p.first);
else if (second != p.second)
return Integer.compare(second, p.second);
else
return 0;
}
}
static final Random random = new Random();
static void shuffleSort(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int r = random.nextInt(n), temp = a[r];
a[r] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
int[] readArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
}
} | 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 8 | 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 | 0291881f96c7f4eecb0604765ef16359 | 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.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class G
{
static PrintWriter out = new PrintWriter(System.out);
static FastReader in=new FastReader();
public static void main(String args[])throws IOException
{
int t =i();
outer:while(t-->0) {
int n=i();
int[] A=input(n);
int min=-1;int max=-1;
for(int i=1;i<n;i++) {
if(A[i]==A[i-1]) {
min=i;break;
}
}
for(int i=0;i<n-1;i++) {
if(A[i]==A[i+1]) {
max=i;
}
}
if(max==min && min!=-1) {
max++;
}
out.println(Math.max(0, max-min));
}
out.close();
}
public static String reverseString(String str)
{
if(str.isEmpty())
{
return str;
}
else
{
return reverseString(str.substring(1))+str.charAt(0);
}
}
public static int count(long[] A, int i,int n) {
int cnt=0;
for( ;i<=n;i++) {
cnt+=A[i];
}
//print(A);
return cnt;
}
public static int MAX(ArrayList<Integer> a) {
return Collections.max(a);
}
public static int MEX(ArrayList<Integer> a, int max) {
HashSet<Integer> h =new HashSet<>();
for(int i:a)h.add(i);
for(int i=0;i<=max;i++) {
if(!h.contains(i))return i;
}
return max+1;
}
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i < Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static int lcm(int First_number, int Second_number) {
int x,max=0,min=0,lcm=0;
if(First_number>Second_number)
{
max=First_number;
min=Second_number;
}
else
{
max=Second_number;
min=First_number;
}
for(int i=1;i<=min;i++)
{
x=max*i;
if(x%min==0)
{
lcm=x;
break;
}
}
return lcm;
}
static int[] Swap(int[] A, int i, int j) {
int temp = A[i];
A[i]=A[j];
A[j]=temp;
return A;
}
static String ChartoString(char[] x) {
String ans="";
for(char i:x) {
ans+=i;
}
return ans;
}
static int HCF(int num1, int num2) {
int temp1 = num1;
int temp2 = num2;
while(temp2 != 0){
int temp = temp2;
temp2 = temp1%temp2;
temp1 = temp;
}
int hcf = temp1;
return hcf;
}
static boolean palindrome(String s) {
char[] x = s.toCharArray();
int i=0; int r= x.length-1;
while(i<r) {
if(x[i]!=x[r]) {
return false;
}
i++;
r--;
}
return true;
}
static void sorting(long[] a) //check for long
{
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 findGcd(int x, int y)
{
if (x == 0)
return y;
return findGcd(y % x, x);
}
static int findLcm(int x, int y)
{
return (x / findGcd(x, y)) * y;
}
public static int checkTriangle(long a,
long b, long c)
{
if (a + b <= c || a + c <= b || b + c <= a)
return 0;
else
return 1;
}
static boolean isitSorted(long A[])
{
for(int i=1; i<A.length; i++) {
if(A[i]<=A[i-1])return false;
}
return true;
}
static void print(long A[])
{
for(long i:A)System.out.print(i+ " ");
System.out.println();
}
static void print(ArrayList<Integer> A)
{
for(long a:A)System.out.print(a);
System.out.println();
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static String S() {
return in.next();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputL(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++) {
A[i]=in.nextLong();
}
return A;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["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 8 | 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 | 3a4fab2572bbbbbc5ab3c63ec72204ba | 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 | // package GlobalRound20;
import java.io.*;
import java.util.StringTokenizer;
public class C { // Unequal Array X'P
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = 1;
t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
long [] a = sc.nextlongArray(n);
int start=-1;
int end =-1;
for(int i =0;i<n-1;i++){
if(a[i]==a[i+1]){
if(start==-1)start=i;
end=i;
}
}
// examples
// 1 1 2 3 4 4 5 5
// first=
// last =
// ans =
if(start+1==end)
pw.println(1);
else
pw.println(Math.max(0,end-start-1));
}
pw.close();
}
// -------------------------------------------------------Scanner---------------------------------------------------
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 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 | ["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 8 | 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 | 31b37796370732f101eec0cd37b03383 | 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 n = sc.nextInt();
while (n-- > 0) {
long arr [] = new long [sc.nextInt()];
int eq = 0;
for (int i = 0 ; i < arr.length; i++) arr[i] = sc.nextLong();
int ans = 0;
// int start = Integer.MIN_VALUE;
int start = -1;
int end = -1;
for (int i = 1 ; i < arr.length ; i++) {
if (arr[i-1] == arr[i]) {
if (start == -1)
start = i;
// if (end == 0)
end = i;
}
}
ans = start == end ? 0 : Math.max(1, end - start -1);
System.out.println(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 8 | 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 | da1be297c1ba3eb73d3edab87589fa11 | 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 | // package faltu;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.Map.Entry;
public class Main {
public static int upperBound(long[] arr, long m, int l, int r) {
while(l<=r) {
int mid=(l+r)/2;
if(arr[mid]<=m) l=mid+1;
else r=mid-1;
}
return l;
}
public static int lowerBound(long[] a, long m, int l, int r) {
while(l<=r) {
int mid=(l+r)/2;
if(a[mid]<m) l=mid+1;
else r=mid-1;
}
return l;
}
public static long getClosest(long val1, long val2,long target)
{
if (target - val1 >= val2 - target)
return val2;
else
return val1;
}
static void ruffleSort(long[] a) {
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
long oi=r.nextInt(n), temp=a[i];
a[i]=a[(int)oi];
a[(int)oi]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(int[] a){
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n), temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
Arrays.sort(a);
}
int ceilIndex(int input[], int T[], int end, int s){
int start = 0;
int middle;
int len = end;
while(start <= end){
middle = (start + end)/2;
if(middle < len && input[T[middle]] < s && s <= input[T[middle+1]]){
return middle+1;
}else if(input[T[middle]] < s){
start = middle+1;
}else{
end = middle-1;
}
}
return -1;
}
public static int findIndex(long arr[], long t)
{
if (arr == null) {
return -1;
}
int len = arr.length;
int i = 0;
while (i < len) {
if (arr[i] == t) {
return i;
}
else {
i = i + 1;
}
}
return -1;
}
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 long lcm(long a,long b)
{
return (a / gcd(a, b)) * b;
}
public static int[] swap(int a[], int left, int right)
{
int temp = a[left];
a[left] = a[right];
a[right] = temp;
return a;
}
public static void swap(long x,long max1)
{
long temp=x;
x=max1;
max1=temp;
}
public static int[] reverse(int a[], int left, int right)
{
// Reverse the sub-array
while (left < right) {
int temp = a[left];
a[left++] = a[right];
a[right--] = temp;
}
return a;
}
static int lowerLimitBinarySearch(ArrayList<Integer> A,int B) {
int n =A.size();
int first = 0,second = n;
while(first <second) {
int mid = first + (second-first)/2;
if(A.get(mid) > B) {
second = mid;
}else {
first = mid+1;
}
}
if(first < n && A.get(first) < B) {
first++;
}
return first; //1 index
}
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;
}
}
// *******----segement tree implement---*****
// -------------START--------------------------
void buildTree (int[] arr,int[] tree,int start,int end,int treeNode)
{
if(start==end)
{
tree[treeNode]=arr[start];
return;
}
buildTree(arr,tree,start,end,2*treeNode);
buildTree(arr,tree,start,end,2*treeNode+1);
tree[treeNode]=tree[treeNode*2]+tree[2*treeNode+1];
}
void updateTree(int[] arr,int[] tree,int start,int end,int treeNode,int idx,int value)
{
if(start==end)
{
arr[idx]=value;
tree[treeNode]=value;
return;
}
int mid=(start+end)/2;
if(idx>mid)
{
updateTree(arr,tree,mid+1,end,2*treeNode+1,idx,value);
}
else
{
updateTree(arr,tree,start,mid,2*treeNode,idx,value);
}
tree[treeNode]=tree[2*treeNode]+tree[2*treeNode+1];
}
// disjoint set implementation --start
static void makeSet(int n)
{
parent=new int[n];
rank=new int[n];
for(int i=0;i<n;i++)
{
parent[i]=i;
rank[i]=0;
}
}
static void union(int u,int v)
{
u=findpar(u);
v=findpar(v);
if(rank[u]<rank[v])parent[u]=v;
else if(rank[v]<rank[u])parent[v]=u;
else
{
parent[v]=u;
rank[u]++;
}
}
private static int findpar(int node)
{
if(node==parent[node])return node;
return parent[node]=findpar(parent[node]);
}
static int parent[];
static int rank[];
// *************end
static void presumbit(int[][]prebitsum) {
for(int i=1;i<=200000;i++) {
int z=i;
int j=0;
while(z>0) {
if((z&1)==1) {
prebitsum[i][j]+=(prebitsum[i-1][j]+1);
}else {
prebitsum[i][j]=prebitsum[i-1][j];
}
z=z>>1;
j++;
}
}
}
public static int[] sort(int[] arr) {
ArrayList<Integer> al = new ArrayList<>();
for(int i=0;i<arr.length;i++) al.add(arr[i]);
Collections.sort(al);
for(int i=0;i<arr.length;i++) arr[i]=al.get(i);
return arr;
}
static ArrayList<String>powof2s;
static void powof2S() {
long i=1;
while(i<(long)2e18) {
powof2s.add(String.valueOf(i));
i*=2;
}
}
static boolean coprime(int a, long l){
return (gcd(a, l) == 1);
}
static int[][] dirs = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
static Long MOD=(long) (1e9+7);
static int prebitsum[][];
static ArrayList<Integer>arr;
static boolean[] vis;
static ArrayList<ArrayList<Integer>>adj;
public static int[] readIntArray(int tokens) {
int[] ret = new int[tokens];
for (int i = 0; i < tokens; i++) {
ret[i] =s.nextInt();
}
return ret;
}
public static long[] readLongArray(int tokens) {
long[] ret = new long[tokens];
for (int i = 0; i < tokens; i++) {
ret[i] =s.nextLong();
}
return ret;
}
static FastReader s = new FastReader();
public static void main(String[] args) throws IOException
{
// sieve();
// prebitsum=new int[200001][18];
// presumbit(prebitsum);
// powof2S();
int tt = s.nextInt();
int cnt=1;
while(tt-->0) {
int n=s.nextInt();
int[]a=readIntArray(n);
int low=Integer.MAX_VALUE;
int high=-1;
for(int i=1;i<n;i++) {
if(a[i]==a[i-1]) {
low=Math.min(low, i);
high=Math.max(high, i-1);
}
}
if(low==-1||high<low)System.out.println("0");
else if(low==high)System.out.println(1);
else System.out.println(high-low);
}
}
private static void solver(char[] c1, char[] c2) {
}
static void pc2d(char[][]a) {
int n=a.length;
int m=a[0].length;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
static void pi2d(int[][]a) {
int n=a.length;
int m=a[0].length;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
static void DFSUtil(int v, boolean[] vis)
{
vis[v] = true;
Iterator<Integer> it = adj.get(v).iterator();
while (it.hasNext()) {
int n = it.next();
if (!vis[n])
DFSUtil(n, vis);
}
}
static long DFS(int n)
{
vis = new boolean[n+1];
long cnt=0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
DFSUtil(i, vis);
cnt++;
}
}
return cnt;
}
public static String revStr(String str){
String input = str;
StringBuilder input1 = new StringBuilder();
input1.append(input);
input1.reverse();
return input1.toString();
}
public static String sortString(String inputString){
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static long myPow(long n, long i){
if(i==0) return 1;
if(i%2==0) return (myPow(n,i/2)%MOD * myPow(n,i/2)%MOD)%MOD;
return (n%MOD* myPow(n,i-i)%MOD)%MOD;
}
static void palindromeSubStrs(String str) {
HashSet<String>set=new HashSet<>();
char[]a =str.toCharArray();
int n=str.length();
int[][]dp=new int[n][n];
for(int g=0;g<n;g++){
for(int i=0,j=g;j<n;j++,i++){
if(!set.contains(str.substring(i,i+1))&&g==0) {
dp[i][j]=1;
set.add(str.substring(i,i+1));
}
else {
if(!set.contains(str.substring(i,j+1))&&isPalindrome(str,i,j)) {
dp[i][j]=1;
set.add(str.substring(i,j+1));
}
}
}
}
int ans=0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
System.out.print(dp[i][j]+" ");
if(dp[i][j]==1)ans++;
}
System.out.println();
}
System.out.println(ans);
}
static boolean isPalindrome(String str,int i,int j)
{
while (i < j) {
if (str.charAt(i) != str.charAt(j))
return false;
i++;
j--;
}
return true;
}
static boolean sign(long num) {
return num>0;
}
static boolean isSquare(long x){
if(x==1)return true;
long y=(long) Math.sqrt(x);
return y*y==x;
}
static long s(long a,long b) {
if(b == 0){
return 1;
}
long ans = power(a,b/2);
ans *= ans%MOD;
if(b % 2!=0){
ans *= a%MOD;
}
return ans%MOD;
}
static void swap(StringBuilder sb,int l,int r)
{
char temp = sb.charAt(l);
sb.setCharAt(l,sb.charAt(r));
sb.setCharAt(r,temp);
}
// function to reverse the string between index l and r
static void reverse(StringBuilder sb,int l,int r)
{
while(l < r)
{
swap(sb,l,r);
l++;
r--;
}
}
// function to search a character lying between index l and r
// which is closest greater (just greater) than val
// and return it's index
static int binarySearch(StringBuilder sb,int l,int r,char val)
{
int index = -1;
while (l <= r)
{
int mid = (l+r)/2;
if (sb.charAt(mid) <= val)
{
r = mid - 1;
}
else
{
l = mid + 1;
if (index == -1 || sb.charAt(index) >= sb.charAt(mid))
index = mid;
}
}
return index;
}
// this function generates next permutation (if there exists any such permutation) from the given string
// and returns True
// Else returns false
static boolean nextPermutation(StringBuilder sb)
{
int len = sb.length();
int i = len-2;
while (i >= 0 && sb.charAt(i) >= sb.charAt(i+1))
i--;
if (i < 0)
return false;
else
{
int index = binarySearch(sb,i+1,len-1,sb.charAt(i));
swap(sb,i,index);
reverse(sb,i+1,len-1);
return true;
}
}
private static int lps(int m ,int n,String s1,String s2,int[][]mat)
{
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(s1.charAt(i-1)==s2.charAt(j-1))mat[i][j]=1+mat[i-1][j-1];
else mat[i][j]=Math.max(mat[i-1][j],mat[i][j-1]);
}
}
return mat[m][n];
}
static int lcs(String X, String Y, int m, int n)
{
int[][] L = new int[m+1][n+1];
// Following steps build L[m+1][n+1] in bottom up fashion. Note
// that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1]
for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X.charAt(i-1) == Y.charAt(j-1))
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = Math.max(L[i-1][j], L[i][j-1]);
}
}
return L[m][n];
// Following code is used to print LCS
// int index = L[m][n];
// int temp = index;
//
// // Create a character array to store the lcs string
// char[] lcs = new char[index+1];
// lcs[index] = '\u0000'; // Set the terminating character
//
// // Start from the right-most-bottom-most corner and
// // one by one store characters in lcs[]
// int i = m;
// int j = n;
// while (i > 0 && j > 0)
// {
// // If current character in X[] and Y are same, then
// // current character is part of LCS
// if (X.charAt(i-1) == Y.charAt(j-1))
// {
// // Put current character in result
// lcs[index-1] = X.charAt(i-1);
//
// // reduce values of i, j and index
// i--;
// j--;
// index--;
// }
//
// // If not same, then find the larger of two and
// // go in the direction of larger value
// else if (L[i-1][j] > L[i][j-1])
// i--;
// else
// j--;
// }
// return String.valueOf(lcs);
// Print the lcs
// System.out.print("LCS of "+X+" and "+Y+" is ");
// for(int k=0;k<=temp;k++)
// System.out.print(lcs[k]);
}
static long lis(long[] aa2, int n)
{
long lis[] = new long[n];
int i, j;
long max = 0;
for (i = 0; i < n; i++)
lis[i] = 1;
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (aa2[i] >= aa2[j] && lis[i] <= lis[j] + 1)
lis[i] = lis[j] + 1;
for (i = 0; i < n; i++)
if (max < lis[i])
max = lis[i];
return max;
}
static boolean isPalindrome(String str)
{
int i = 0, j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j))
return false;
i++;
j--;
}
return true;
}
static boolean issafe(int i, int j, int r,int c, char ch)
{
if (i < 0 || j < 0 || i >= r || j >= c|| ch!= '1')return false;
else return true;
}
static long power(long a, long b)
{
a %=MOD;
long out = 1;
while (b > 0) {
if((b&1)!=0)out = out * a % MOD;
a = a * a % MOD;
b >>= 1;
a*=a;
}
return out;
}
static long[] sieve;
public static void sieve()
{
int nnn=(int) 1e6+1;
long nn=(int) 1e6;
sieve=new long[(int) nnn];
int[] freq=new int[(int) nnn];
sieve[0]=0;
sieve[1]=1;
for(int i=2;i<=nn;i++)
{
sieve[i]=1;
freq[i]=1;
}
for(int i=2;i*i<=nn;i++)
{
if(sieve[i]==1)
{
for(int j=i*i;j<=nn;j+=i)
{
if(sieve[j]==1)
{
sieve[j]=0;
}
}
}
}
}
}
class decrease implements Comparator<Long> {
// Used for sorting in ascending order of
// roll number
public int compare(long a, long b)
{
return (int) (b - a);
}
@Override
public int compare(Long o1, Long o2) {
// TODO Auto-generated method stub
return (int) (o2-o1);
}
}
class pair{
long x;
long y;
long c;
char ch;
public pair(long x,long y) {
this.x=x;
this.y=y;
}
public pair(long x,char ch) {
this.x=x;
this.ch=ch;
}
public pair(long x,long y,long c)
{
this.x=x;
this.y=y;
this.c=c;
}
} | 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 8 | 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 | 444dd947d68b1e04c78cbcb2bb5268be | 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 habd {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int tc = sc.nextInt();
while(tc-->0){
int n = sc.nextInt();
int[] arr = sc.nextIntArray(n);
int min = -1, 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)pw.println(0);
else pw.println(Math.max(1, max - min - 1));
}
pw.flush();
}
static class SegmentTree{
long[] tree;
int N;
public SegmentTree(long[] arr){
N = arr.length;
tree = new long[2*N - 1];
build(tree, arr);
}
public void build(long[] tree, long[] arr){
for(int i = N-1, j = 0; i<tree.length; i++, j++)tree[i] = arr[j];
for(int i = tree.length - 1, j = i - 1, k = N-2; k>=0; i -= 2, j-= 2, k--){
tree[k] = tree[i] + tree[j];
}
}
public void update(int idx, int val){
tree[idx + N - 2] = val;
boolean f = true;
int i = idx + N - 2;
int j = i - 1;
if(i % 2 != 0){
i++;
j++;
}
for(int k = (tree.length - N - 1) - ((tree.length - 1 - i)/2); k>=0; ){
tree[k] = tree[i] + tree[j];
f = !f;
i = k;
j = k - 1;
if(k % 2 != 0){
i++;
j++;
}
k = (tree.length - N - 1) - ((tree.length - 1 - i)/2);
}
}
}
public static boolean isSorted(Long[] arr){
boolean f = true;
for(int i = 1; i<arr.length; i++){
if(arr[i] < arr[i - 1]){
f = false;
break;
}
}
return f;
}
public static int binary_Search(long key, Long[] arr, int start){
int low = start;
int high = arr.length;
int mid = (low + high) / 2;
while(low < high){
mid = (low + high) / 2;
if(arr[mid] == key)break;
else if(arr[mid] > key){
high = mid;
}else{
low = mid;
}
}
return mid;
}
public static int differences(int n, int test){
int changes = 0;
while(test > 0){
if(test % 10 != n % 10)changes++;
test/=10;
n/=10;
}
return changes;
}
static int maxSubArraySum(int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0,start = 0,
end = 0, s = 0;
for (int i = 0; i < size; i++)
{
max_ending_here += a[i];
if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
start = s;
end = i;
}
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
return start;
}
static int maxSubArraySum2(int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0,start = 0,
end = 0, s = 0;
for (int i = 0; i < size; i++)
{
max_ending_here += a[i];
if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
start = s;
end = i;
}
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
return end;
}
public static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int x, int y){
this.x = x;
this.y = y;
}
public int compareTo(Pair x){
if(this.y - x.y != 0) return this.y - x.y;
else return this.x - x.x;
}
public String toString(){
return "("+this.x + ", " + this.y + ")";
}
}
public static boolean isSubsequence(char[] arr, String s){
boolean ans = false;
for(int i = 0, j = 0; i<arr.length; i++){
if(arr[i] == s.charAt(j)){
j++;
}
if(j == s.length()){
ans = true;
break;
}
}
return ans;
}
public static void sortIdx(long[]a,long[]idx) {
mergesortidx(a, idx, 0, a.length-1);
}
static void mergesortidx(long[] arr,long[]idx,int b,int e) {
if(b<e) {
int m=b+(e-b)/2;
mergesortidx(arr,idx,b,m);
mergesortidx(arr,idx,m+1,e);
mergeidx(arr,idx,b,m,e);
}
return;
}
static void mergeidx(long[] arr,long[]idx,int b,int m,int e) {
int len1=m-b+1,len2=e-m;
long[] l=new long[len1];
long[] lidx=new long[len1];
long[] r=new long[len2];
long[] ridx=new long[len2];
for(int i=0;i<len1;i++) {
l[i]=arr[b+i];
lidx[i]=idx[b+i];
}
for(int i=0;i<len2;i++) {
r[i]=arr[m+1+i];
ridx[i]=idx[m+1+i];
}
int i=0,j=0,k=b;
while(i<len1 && j<len2) {
if(l[i]<=r[j]) {
arr[k++]=l[i++];
idx[k-1]=lidx[i-1];
}
else {
arr[k++]=r[j++];
idx[k-1]=ridx[j-1];
}
}
while(i<len1) {
idx[k]=lidx[i];
arr[k++]=l[i++];
}
while(j<len2) {
idx[k]=ridx[j];
arr[k++]=r[j++];
}
return;
}
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 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 | ["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 8 | 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 | ed19318cc6fda9b397c80d5f5250c896 | 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 Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] arr = sc.nextIntArray(n);
int l = -1, r = -1;
for (int i = 0; i < n - 1; i++) {
if (arr[i] == arr[i + 1]) {
if (l == -1)
l=i;
r=i;
}
}
if (l == r)
pw.println(0);
else
pw.println(Math.max(r - l - 1, 1));
}
pw.flush();
pw.close();
}
static class Pair {
long c, n;
public Pair(long c, long n) {
this.c = c;
this.n = n;
}
public String toString() {
return c + " " + n;
}
}
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 | ["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 8 | 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 | 6d38bf2e1a3c3e0936573182009d4fc1 | 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 | //Author : -crazy_coder-
import static java.lang.Math.*;
import static java.lang.System.out;
import java.io.*;
import java.util.*;
public class Main{
public static void readArray(int[] arr,int st,int length,BufferedReader br)throws Exception{String[] str=br.readLine().split(" ");
for(int i=st;i<length;i++){arr[i]=Integer.parseInt(str[i]);}}
public static void printArray(int[] arr){for(int i=0;i<arr.length;i++){out.print(arr[i]+" ");}out.println();}
public static void primeUsingBits(BitSet bit,int n){bit.flip(0,n+1);bit.set(0,false);bit.set(1,false);for(int i=2;i*i<=n;i++){if(bit.get(i)){for(int j=i*i;j<=n;j+=i){bit.set(j,false);}}}}
public static int Gcd(int a,int b){if(b==0)return a;return Gcd(b,a%b);}
public static int Lcm(int a,int b){return a*b/Gcd(a,b);}
//<---------------------------------------Main Function---------------------------------------->
public static void main(String[] args)throws Exception{
BufferedReader br=new BufferedReader(new BufferedReader(new InputStreamReader(System.in)));
int t=Integer.parseInt(br.readLine());
// int t=1;
while(t-->0){
//write your code here
int n=Integer.parseInt(br.readLine());
String[] str=br.readLine().split(" ");
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(str[i]);
}
int first=0,last=0;
int cnt=0,seglength=1,pseglength=1;
for(int i=0;i<n-1;i++){
if(arr[i]==arr[i+1]){
seglength++;
if(first==0)first=i+1;
last=i+1;
}else{
if(seglength>1){
cnt++;
pseglength=seglength;
}
seglength=1;
}
}
// out.println("seglength-->"+seglength);
if(seglength>1){cnt+=1;pseglength=seglength;}
// out.println("cnt-->"+cnt);
// out.println("pseglength-->"+pseglength);
if(cnt<=1){
if(pseglength==3)
out.println("1");
else if(pseglength>3)
out.println(last-first-1);
else out.println("0");
}else out.println(last-first-1);
}
}
public static void manupulate(char[][] ch,int r,int c,int cnt){
for(int j=r-1;j>=0;j--){
if(ch[j][c]=='.'&&cnt>0){
ch[j][c]='*';
cnt--;
}
}
}
// <----------------------------------------XXXXXXXXXXXXXXXXXXXXX----------------------------------------->
////////////////////////********fastModExpo********/////////////////////////
public static long fastModExpo(long a,long n,long mod){
if(n==0)return 1;
long ans=1;
while(n>1){
if((n&1)==1){
ans=ans*a%mod;
}
a=a*a%mod;
n>>=1;
}
ans=ans*a%mod;
return ans;
}
// ****** next permutation=================
public static int[] swap(int arr[], int start, int end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
return arr;
}
public static int[] reverse(int arr[], int start, int end)
{
while (start < end) {
int temp = arr[start];
arr[start++] = arr[end];
arr[end--] = temp;
}
return arr;
}
public static boolean next_permutation(int arr[])
{
if (arr.length <= 1)
return false;
int last = arr.length - 2;
while (last >= 0) {
if (arr[last] < arr[last + 1]) {
break;
}
last--;
}
if (last < 0)
return false;
int nextGreater = arr.length - 1;
for (int i = arr.length - 1; i > last; i--) {
if (arr[i] > arr[last]) {
nextGreater = i;
break;
}
}
arr = swap(arr, nextGreater, last);
arr = reverse(arr, last + 1, arr.length - 1);
return true;
}
////////////////////////////*******FastReader********//////////////////////////////////
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;
}
}
////////////////////////////*******FastWriter********//////////////////////////////////
static class FastWriter
{
private final BufferedWriter bw;
public FastWriter(){
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 close()throws IOException{
bw.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 8 | 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 | 9700e25debb7507edc455e0d7e73c093 | 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.*;
// BEFORE 31ST MARCH 2022 !!
//MAX RATING EVER ACHIEVED-1622(LETS SEE WHEN WILL I GET TO CHANGE THIS)
////***************************************************************************
/* public class E_Gardener_and_Tree implements Runnable{
public static void main(String[] args) throws Exception {
new Thread(null, new E_Gardener_and_Tree(), "E_Gardener_and_Tree", 1<<28).start();
}
public void run(){
WRITE YOUR CODE HERE!!!!
JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!!
}
}
*/
/////**************************************************************************
//am i a fucking dumbass
//how could i just not check this simple trivial case
public class C_Unequal_Array{
public static void main(String[] args) {
FastScanner s= new FastScanner();
//PrintWriter out=new PrintWriter(System.out);
//end of program
//out.println(answer);
//out.close();
StringBuilder res = new StringBuilder();
int t=s.nextInt();
int p=0;
while(p<t){
int n=s.nextInt();
long array[]= new long[n];
long count=0;
int flag=0;
int index1=-1;//stores correct
int index2=-1;
for(int i=0;i<n;i++){
array[i]=s.nextLong();
if(i>0){
if(flag==0){
if(array[i]==array[i-1]){
flag=1;
if(count==0){
index1=i;
}
}
else{
}
}
else{
if(array[i]==array[i-1]){
}
else{
count++;
index2=i-1;
flag=0;
}
}
}
}
if(flag==1){
count++;
index2=n-1;
flag=0;
}
if(count==0){
res.append("0 \n");
}
else if(count==1){
long size=index2-index1+1;
size++;
if(size<=2){
res.append("0 \n");
}
else{
long hh=size-3;
if(hh<=0){
hh=1;
}
res.append(hh+" \n");
}
}
else{
long index3=index2-2;
long hh=index3-index1+1;
res.append(hh+" \n");
}
p++;
}
System.out.println(res);
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
static long modpower(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;
}
// SIMPLE POWER FUNCTION=>
static long power(long x, long y)
{
long res = 1; // Initialize result
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = res * x;
// y must be even now
y = y >> 1; // y = y/2
x = x * x; // Change x to x^2
}
return res;
}
} | 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 8 | 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 | f969c26ea40b282ebb7010978645bc6e | 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 | /* Author:Farhan Shaikh */
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 Pupil
{
static FastReader sc = new FastReader();
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int t=sc.nextInt();
while(t>0){
int n=sc.nextInt();
int arr[]=new int[n+1];
for(int i=1;i<=n;i++)
{
arr[i]=sc.nextInt();
}
if(n<=2)
{
System.out.println("0");
}
else {
int start = 1, end = n-1;
while(start < n && arr[start] != arr[start+1])
{
start++;
}
if(start == n) {
System.out.println("0");
}
else {
while(end > 1 && arr[end] != arr[end+1]) {
end--;
}
end++;
if(start == end-1) {
System.out.println("0");
}
else
{
System.out.println(Math.max(end-start-2, 1));
}
}
}
t--;
}
}
// 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();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static boolean two(int n)//power of two
{
if((n&(n-1))==0)
{
return true;
}
else{
return false;
}
}
public static boolean isPrime(long n){
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static int digit(int n)
{
int n1=(int)Math.floor((int)Math.log10(n)) + 1;
return n1;
}
public static long gcd(long a,long b) {
if(b==0)
return a;
return gcd(b,a%b);
}
public static long highestPowerof2(long x)
{
// check for the set bits
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
// Then we remove all but the top bit by xor'ing the
// string of 1's with that string of 1's shifted one to
// the left, and we end up with just the one top bit
// followed by 0's.
return x ^ (x >> 1);
}
public static void yes()
{
System.out.println("YES");
return;
}
public static void no()
{
System.out.println("NO");
return ;
}
public static void al(long arr[],int n)
{
for(int i=0;i<n;i++)
{
arr[i]=sc.nextLong();
}
return ;
}
public static void ai(int arr[],int n)
{
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
return ;
}
}
//CAAL THE BELOW FUNCTION IF PARING PRIORITY IS NEEDED //
// PriorityQueue<pair> pq = new PriorityQueue<>(); **********declare the syntax in the main function******
// pq.add(1,2)///////
// class pair implements Comparable<pair> {
// int value, index;
// pair(int v, int i) { index = i; value = v; }
// @Override
// public int compareTo(pair o) { return o.value - value; }
// }
// User defined Pair class
// class Pair {
// int x;
// int y;
// // Constructor
// public Pair(int x, int y)
// {
// this.x = x;
// this.y = y;
// }
// }
// Arrays.sort(arr, new Comparator<Pair>() {
// @Override public int compare(Pair p1, Pair p2)
// {
// return p1.x - p2.x;
// }
// });
// class Pair {
// int height, id;
//
// public Pair(int i, int s) {
// this.height = s;
// this.id = i;
// }
// }
//Arrays.sort(trips, (a, b) -> Integer.compare(a[1], b[1]));
// ArrayList<ArrayList<Integer>> connections = new ArrayList<ArrayList<Integer>>();
// for(int i = 0; i<n;i++)
// connections.add(new ArrayList<Integer>()); | 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 8 | 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 | 98dfb8ddf7e3350a0bfcc3f1646f8ef1 | 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 |
/*
* way to red
*/
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.util.*;
import java.io.*;
import java.math.*;
public class GlobalC
{
public static void main(String AC[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int T = Integer.parseInt(st.nextToken());
StringBuilder sb = new StringBuilder();
while(T-->0)
{
st = new StringTokenizer(infile.readLine());
// long N = Long.parseLong(st.nextToken());
int N = Integer.parseInt(st.nextToken());
int[] arr = readArr(N, infile, st);
// String s = String.valueOf(st.nextToken());
int c =Integer.MAX_VALUE;
int p =0;
for(int i=0;i<N-1;i++) {
if(arr[i]==arr[i+1]) {
c = min(c,i);
p = max(p,i);
}
}
if(c==p||c==Integer.MAX_VALUE) {
sb.append(0).append('\n');
}else {
int ans = (p-c<3?1:p-c-1);
sb.append(ans).append('\n');
}
}
out.print(sb);
out.flush();
//BufferedReader infile = new BufferedReader(new FileReader("input.txt"));
//System.setOut(new PrintStream(new File("output.txt")));
}
static final int mod = 1_000_000_007;
public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception
{
int[] arr = new int[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
return arr;
}
public static long[] readArr2(int N, BufferedReader infile, StringTokenizer st) throws Exception
{
long[] arr = new long[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Long.parseLong(st.nextToken());
return arr;
}
public static void print(int[] arr)
{
//for debugging only
for(int x: arr)
out.print(x+" ");
out.println();
}
//input shenanigans
/*
Random stuff to try when stuck:
-if it's 2C then it's dp
-for combo/probability problems, expand the given form we're interested in
-make everything the same then build an answer (constructive, make everything 0 then do something)
-something appears in parts of 2 --> model as graph
-assume a greedy then try to show why it works
-find way to simplify into one variable if multiple exist
-treat it like fmc (note any passing thoughts/algo that could be used so you can revisit them)
-find lower and upper bounds on answer
-figure out what ur trying to find and isolate it
-see what observations you have and come up with more continuations
-work backwards (in constructive, go from the goal to the start)
-turn into prefix/suffix sum argument (often works if problem revolves around adjacent array elements)
-instead of solving for answer, try solving for complement (ex, find n-(min) instead of max)
-draw something
-simulate a process
-dont implement something unless if ur fairly confident its correct
-after 3 bad submissions move on to next problem if applicable
-do something instead of nothing and stay organized
-write stuff down
Random stuff to check when wa:
-if code is way too long/cancer then reassess
-switched N/M
-int overflow
-switched variables
-wrong MOD
-hardcoded edge case incorrectly
Random stuff to check when tle:
-continue instead of break
-condition in for/while loop bad
Random stuff to check when rte:
-switched N/M
-long to int/int overflow
-division by 0
-edge case for empty list/data structure/N=1
*/
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 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 long totient(long n)
{
long result = n;
for (int p = 2; p*p <= n; ++p)
if (n % p == 0)
{
while(n%p == 0)
n /= p;
result -= result/p;
}
if (n > 1)
result -= result/n;
return result;
/*
find phi(i) from 1 to N fast
O(N*loglogN)
long[] arr = new long[N+1];
for(int i=1; i <= N; i++)
arr[i] = i;
for(int v=2; v <= N; v++)
if(arr[v] == v)
for(int a=v; a <= N; a+=v)
arr[a] -= arr[a]/v;
*/
}
public static ArrayList<Integer> findDiv(int N)
{
//gens all divisors of N
ArrayList<Integer> ls1 = new ArrayList<Integer>();
ArrayList<Integer> ls2 = new ArrayList<Integer>();
for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++)
if(N%i == 0)
{
ls1.add(i);
ls2.add(N/i);
}
Collections.reverse(ls2);
for(int b: ls2)
if(b != ls1.get(ls1.size()-1))
ls1.add(b);
return ls1;
}
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 long power(long x, long y, long p)
{
//0^0 = 1
long res = 1L;
x = x%p;
while(y > 0)
{
if((y&1)==1)
res = (res*x)%p;
y >>= 1;
x = (x*x)%p;
}
return res;
}
//custom multiset (replace with HashMap if needed)
public static void push(TreeMap<Integer, Integer> map, int k, int v)
{
//map[k] += v;
if(!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k)+v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v)
{
//assumes map[k] >= v
//map[k] -= v
int lol = map.get(k);
if(lol == v)
map.remove(k);
else
map.put(k, lol-v);
}
public static int[] compress(int[] arr)
{
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int boof = 1; //min value
for(int x: ls)
if(!map.containsKey(x))
map.put(x, boof++);
int[] brr = new int[arr.length];
for(int i=0; i < arr.length; i++)
brr[i] = map.get(arr[i]);
return brr;
}
public static long[][] multiply(long[][] left, long[][] right)
{
long MOD = 1000000007L;
int N = left.length;
int M = right[0].length;
long[][] res = new long[N][M];
for(int a=0; a < N; a++)
for(int b=0; b < M; b++)
for(int c=0; c < left[0].length; c++)
{
res[a][b] += (left[a][c]*right[c][b])%MOD;
if(res[a][b] >= MOD)
res[a][b] -= MOD;
}
return res;
}
public static long[][] power(long[][] grid, long pow)
{
long[][] res = new long[grid.length][grid[0].length];
for(int i=0; i < res.length; i++)
res[i][i] = 1L;
long[][] curr = grid.clone();
while(pow > 0)
{
if((pow&1L) == 1L)
res = multiply(curr, res);
pow >>= 1;
curr = multiply(curr, curr);
}
return res;
}
/*
* Data Structures
*/
class DSU
{
public int[] dsu;
public int[] size;
public DSU(int N)
{
dsu = new int[N+1];
size = new int[N+1];
for(int i=0; i <= N; i++)
{
dsu[i] = i;
size[i] = 1;
}
}
//with path compression, no find by rank
public int find(int x)
{
return dsu[x] == x ? x : (dsu[x] = find(dsu[x]));
}
public void merge(int x, int y)
{
int fx = find(x);
int fy = find(y);
dsu[fx] = fy;
}
public void merge(int x, int y, boolean sized)
{
int fx = find(x);
int fy = find(y);
size[fy] += size[fx];
dsu[fx] = fy;
}
}
class FenwickTree
{
//Binary Indexed Tree
//1 indexed
public int[] tree;
public int size;
public FenwickTree(int size)
{
this.size = size;
tree = new int[size+5];
}
public void add(int i, int v)
{
while(i <= size)
{
tree[i] += v;
i += i&-i;
}
}
public int find(int i)
{
int res = 0;
while(i >= 1)
{
res += tree[i];
i -= i&-i;
}
return res;
}
public int find(int l, int r)
{
return find(r)-find(l-1);
}
}
class SegmentTree
{
//Tlatoani's segment tree
//iterative implementation = low constant runtime factor
//range query, non lazy
final int[] val;
final int treeFrom;
final int length;
public SegmentTree(int treeFrom, int treeTo)
{
this.treeFrom = treeFrom;
int length = treeTo - treeFrom + 1;
int l;
for (l = 0; (1 << l) < length; l++);
val = new int[1 << (l + 1)];
this.length = 1 << l;
}
public void update(int index, int delta)
{
//replaces value
int node = index - treeFrom + length;
val[node] = delta;
for (node >>= 1; node > 0; node >>= 1)
val[node] = comb(val[node << 1], val[(node << 1) + 1]);
}
public int query(int from, int to)
{
//inclusive bounds
if (to < from)
return 0; //0 or 1?
from += length - treeFrom;
to += length - treeFrom + 1;
//0 or 1?
int res = 0;
for (; from + (from & -from) <= to; from += from & -from)
res = comb(res, val[from / (from & -from)]);
for (; to - (to & -to) >= from; to -= to & -to)
res = comb(res, val[(to - (to & -to)) / (to & -to)]);
return res;
}
public int comb(int a, int b)
{
//change this
return Math.max(a,b);
}
}
class LazySegTree
{
//definitions
private int NULL = -1;
private int[] tree;
private int[] lazy;
private int length;
public LazySegTree(int N)
{
length = N; int b;
for(b=0; (1<<b) < length; b++);
tree = new int[1<<(b+1)];
lazy = new int[1<<(b+1)];
}
public int query(int left, int right)
{
//left and right are 0-indexed
return get(1, 0, length-1, left, right);
}
private int get(int v, int currL, int currR, int L, int R)
{
if(L > R)
return NULL;
if(L <= currL && currR <= R)
return tree[v];
propagate(v);
int mid = (currL+currR)/2;
return comb(get(v*2, currL, mid, L, Math.min(R, mid)),
get(v*2+1, mid+1, currR, Math.max(L, mid+1), R));
}
public void update(int left, int right, int delta)
{
add(1, 0, length-1, left, right, delta);
}
private void add(int v, int currL, int currR, int L, int R, int delta)
{
if(L > R)
return;
if(currL == L && currR == R)
{
//exact covering
tree[v] += delta;
lazy[v] += delta;
return;
}
propagate(v);
int mid = (currL+currR)/2;
add(v*2, currL, mid, L, Math.min(R, mid), delta);
add(v*2+1, mid+1, currR, Math.max(L, mid+1), R, delta);
tree[v] = comb(tree[v*2], tree[v*2+1]);
}
private void propagate(int v)
{
//tree[v] already has lazy[v]
if(lazy[v] == 0)
return;
tree[v*2] += lazy[v];
lazy[v*2] += lazy[v];
tree[v*2+1] += lazy[v];
lazy[v*2+1] += lazy[v];
lazy[v] = 0;
}
private int comb(int a, int b)
{
return Math.max(a,b);
}
}
class RangeBit
{
//FenwickTree and RangeBit are faster than LazySegTree by constant factor
final int[] value;
final int[] weightedVal;
public RangeBit(int treeTo)
{
value = new int[treeTo+2];
weightedVal = new int[treeTo+2];
}
private void updateHelper(int index, int delta)
{
int weightedDelta = index*delta;
for(int j = index; j < value.length; j += j & -j)
{
value[j] += delta;
weightedVal[j] += weightedDelta;
}
}
public void update(int from, int to, int delta)
{
updateHelper(from, delta);
updateHelper(to + 1, -delta);
}
private int query(int to)
{
int res = 0;
int weightedRes = 0;
for (int j = to; j > 0; j -= j & -j)
{
res += value[j];
weightedRes += weightedVal[j];
}
return ((to + 1)*res)-weightedRes;
}
public int query(int from, int to)
{
if (to < from)
return 0;
return query(to) - query(from - 1);
}
}
class SparseTable
{
public int[] log;
public int[][] table;
public int N; public int K;
public SparseTable(int N)
{
this.N = N;
log = new int[N+2];
K = Integer.numberOfTrailingZeros(Integer.highestOneBit(N));
table = new int[N][K+1];
sparsywarsy();
}
private void sparsywarsy()
{
log[1] = 0;
for(int i=2; i <= N+1; i++)
log[i] = log[i/2]+1;
}
public void lift(int[] arr)
{
int n = arr.length;
for(int i=0; i < n; i++)
table[i][0] = arr[i];
for(int j=1; j <= K; j++)
for(int i=0; i + (1 << j) <= n; i++)
table[i][j] = Math.min(table[i][j-1], table[i+(1 << (j - 1))][j-1]);
}
public int query(int L, int R)
{
//inclusive, 1 indexed
L--; R--;
int mexico = log[R-L+1];
return Math.min(table[L][mexico], table[R-(1 << mexico)+1][mexico]);
}
}
class LCA
{
public int N, root;
public ArrayDeque<Integer>[] edges;
private int[] enter;
private int[] exit;
private int LOG = 17; //change this
private int[][] dp;
public LCA(int n, ArrayDeque<Integer>[] edges, int r)
{
N = n; root = r;
enter = new int[N+1];
exit = new int[N+1];
dp = new int[N+1][LOG];
this.edges = edges;
int[] time = new int[1];
//change to iterative dfs if N is large
dfs(root, 0, time);
dp[root][0] = 1;
for(int b=1; b < LOG; b++)
for(int v=1; v <= N; v++)
dp[v][b] = dp[dp[v][b-1]][b-1];
}
private void dfs(int curr, int par, int[] time)
{
dp[curr][0] = par;
enter[curr] = ++time[0];
for(int next: edges[curr])
if(next != par)
dfs(next, curr, time);
exit[curr] = ++time[0];
}
public int lca(int x, int y)
{
if(isAnc(x, y))
return x;
if(isAnc(y, x))
return y;
int curr = x;
for(int b=LOG-1; b >= 0; b--)
{
int temp = dp[curr][b];
if(!isAnc(temp, y))
curr = temp;
}
return dp[curr][0];
}
private boolean isAnc(int anc, int curr)
{
return enter[anc] <= enter[curr] && exit[anc] >= exit[curr];
}
}
class BitSet
{
private int CONS = 62; //safe
public long[] sets;
public int size;
public BitSet(int N)
{
size = N;
if(N%CONS == 0)
sets = new long[N/CONS];
else
sets = new long[N/CONS+1];
}
public void add(int i)
{
int dex = i/CONS;
int thing = i%CONS;
sets[dex] |= (1L << thing);
}
public int and(BitSet oth)
{
int boof = Math.min(sets.length, oth.sets.length);
int res = 0;
for(int i=0; i < boof; i++)
res += Long.bitCount(sets[i] & oth.sets[i]);
return res;
}
public int xor(BitSet oth)
{
int boof = Math.min(sets.length, oth.sets.length);
int res = 0;
for(int i=0; i < boof; i++)
res += Long.bitCount(sets[i] ^ oth.sets[i]);
return res;
}
}
class MaxFlow
{
//Dinic with optimizations (see magic array in dfs function)
public int N, source, sink;
public ArrayList<Edge>[] edges;
private int[] depth;
public MaxFlow(int n, int x, int y)
{
N = n;
source = x;
sink = y;
edges = new ArrayList[N+1];
for(int i=0; i <= N; i++)
edges[i] = new ArrayList<Edge>();
depth = new int[N+1];
}
public void addEdge(int from, int to, long cap)
{
Edge forward = new Edge(from, to, cap);
Edge backward = new Edge(to, from, 0L);
forward.residual = backward;
backward.residual = forward;
edges[from].add(forward);
edges[to].add(backward);
}
public long mfmc()
{
long res = 0L;
int[] magic = new int[N+1];
while(assignDepths())
{
long flow = dfs(source, Long.MAX_VALUE/2, magic);
while(flow > 0)
{
res += flow;
flow = dfs(source, Long.MAX_VALUE/2, magic);
}
magic = new int[N+1];
}
return res;
}
private boolean assignDepths()
{
Arrays.fill(depth, -69);
ArrayDeque<Integer> q = new ArrayDeque<Integer>();
q.add(source);
depth[source] = 0;
while(q.size() > 0)
{
int curr = q.poll();
for(Edge e: edges[curr])
if(e.capacityLeft() > 0 && depth[e.to] == -69)
{
depth[e.to] = depth[curr]+1;
q.add(e.to);
}
}
return depth[sink] != -69;
}
private long dfs(int curr, long bottleneck, int[] magic)
{
if(curr == sink)
return bottleneck;
for(; magic[curr] < edges[curr].size(); magic[curr]++)
{
Edge e = edges[curr].get(magic[curr]);
if(e.capacityLeft() > 0 && depth[e.to]-depth[curr] == 1)
{
long val = dfs(e.to, Math.min(bottleneck, e.capacityLeft()), magic);
if(val > 0)
{
e.augment(val);
return val;
}
}
}
return 0L; //no flow
}
private class Edge
{
public int from, to;
public long flow, capacity;
public Edge residual;
public Edge(int f, int t, long cap)
{
from = f;
to = t;
capacity = cap;
}
public long capacityLeft()
{
return capacity-flow;
}
public void augment(long val)
{
flow += val;
residual.flow -= val;
}
}
}
class FastScanner
{
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) 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 8 | 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 | 6f480e61e74544d7b625ca17c04207e4 | 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 | /*
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 x1672C
{
public static void main(String omkar[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int T = Integer.parseInt(st.nextToken());
StringBuilder sb = new StringBuilder();
while(T-->0)
{
st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
int[] arr = readArr(N, infile, st);
int equality = 0;
for(int i=1; i < N; i++)
if(arr[i] == arr[i-1])
equality++;
if(equality < 2)
sb.append("0\n");
else
{
ArrayList<Integer> ls = new ArrayList<Integer>();
int tag = arr[0];
int size = 1;
for(int i=1; i < N; i++)
{
if(arr[i] == tag)
size++;
else
{
ls.add(size);
size = 1;
tag = arr[i];
}
}
ls.add(size);
int left = -1;
int right = -1;
for(int i=0; i < ls.size(); i++)
if(ls.get(i) >= 2)
{
if(left == -1)
left = i;
right = i;
}
if(left == right)
{
int len = ls.get(left);
int res = len-3;
if(len == 3)
res = 1;
sb.append(res+"\n");
}
else
{
int sum = 0;
for(int i=left; i <= right; i++)
sum += ls.get(i);
sum -= 2;
int res = sum-1;
sb.append(res+"\n");
}
}
}
System.out.print(sb);
}
public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception
{
int[] arr = new int[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
return arr;
}
}
/*
2 2 1 3 3 --> 2
2 2 3 3 --> 1
all moves done must overlap into one segment
2 [2 2 1 1 3] 3
2 [1 1] 1 3
2 1 [1 1] 1 3
2 1 [1 1 1] 1 3
2 1 1 3 4 5 6 2 2 3
1 1 1 2 2 3 3 3
dp[i] = min number of moves needed?
1 1 1 2 2 3 3
1 4 4 2 2 3 3
1 4 3 3 2 3 3
1 4 3 3 5 5 3
1 4 3 7 7 5 3
1 1 1 2 2 3 3
1 2 2 2 2 3 3
1 2 4 4 2 3 3
1 2 4 3 3 3 3
1 2 4 3 5 5 3
1 1 1 2 2 2
1 4 4 2 2 2
1 4 4 5 5 2
1 4 6 6 5 2
1 1 1 2 2 2
1 2 2 2 2 2
1 2 4 4 2 2
1 2 4 5 5 2
1 1 1
1 2 2
1 1 1 1
1 2 2 1
1 1 1 1 1
1 2 3 3 1
1 1 1 1 1 1
1 2 3 4 4 1
2 2 1 3 3
6
5
2 2 1 3 3
4
1 1 1 1
4
1 2 2 2
4
1 2 2 1
6
1 1 1 2 2 2
7
1 1 1 2 2 3 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 8 | 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 | 4e0a041238de87aac2142d97deb460e7 | 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.*;
//import org.graalvm.compiler.core.common.Fields.ObjectTransformer;
import java.math.*;
import java.math.BigInteger;
public final class A
{
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans=new StringBuilder();
static FastReader in=new FastReader();
// static int g[][];
static ArrayList<Integer> g[];
static long mod=(long)998244353,INF=Long.MAX_VALUE;
static boolean set[];
static int max=0;
static int lca[][];
static int par[],col[],D[];
static long fact[];
static int size[],N;
static long dp[][],sum[][],f[];
static int seg[];
static ArrayList<Integer> A;
public static void main(String args[])throws IOException
{
/*
* star,rope,TPST
* BS,LST,MS,MQ
*/
int T=i();
outer:while(T-->0)
{
int N=i();
long A[]=inputLong(N);
int min=-1,max=-1;
for(int i=1; i<N; i++)
{
if(A[i]==A[i-1])
{
min=i;
break;
}
}
for(int i=0; i<N-1; i++)
{
if(A[i]==A[i+1])
{
max=i;
// break;
}
}
if(max==min && min!=-1)max++;
out.println(Math.max(0, max-min));
}
out.print(ans);
out.close();
}
static int f(int A[])
{
int s=0;
for(int i=1; i<4; i++)
{
s+=Math.abs(A[i]-A[i-1]);
}
return s;
}
static boolean f(int A[],int B[],int N)
{
for(int i=0; i<N; i++)
{
if(Math.abs(A[i]-B[i])>1)return false;
}
return true;
}
static void dfS(int n,int p)
{
int child=0;
for(int c:g[n])
{
if(c!=p)
{
child++;
dfS(c,n);
}
}
if(child!=0)A.add(child);
}
static int [] prefix(char s[],int N) {
// int n = (int)s.length();
// vector<int> pi(n);
N=s.length;
int pi[]=new int[N];
for (int i = 1; i < N; i++) {
int j = pi[i-1];
while (j > 0 && s[i] != s[j])
j = pi[j-1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}
static int count(long N)
{
int cnt=0;
long p=1L;
while(p<=N)
{
if((p&N)!=0)cnt++;
p<<=1;
}
return cnt;
}
static long kadane(long A[])
{
long lsum=A[0],gsum=0;
gsum=Math.max(gsum, lsum);
for(int i=1; i<A.length; i++)
{
lsum=Math.max(lsum+A[i],A[i]);
gsum=Math.max(gsum,lsum);
}
return gsum;
}
public static boolean pal(int i)
{
StringBuilder sb=new StringBuilder();
StringBuilder rev=new StringBuilder();
int p=1;
while(p<=i)
{
if((i&p)!=0)
{
sb.append("1");
}
else sb.append("0");
p<<=1;
}
rev=new StringBuilder(sb.toString());
rev.reverse();
if(i==8)System.out.println(sb+" "+rev);
return (sb.toString()).equals(rev.toString());
}
public static void reverse(int i,int j,int A[])
{
while(i<j)
{
int t=A[i];
A[i]=A[j];
A[j]=t;
i++;
j--;
}
}
public static int ask(int a,int b,int c)
{
System.out.println("? "+a+" "+b+" "+c);
return i();
}
static int[] reverse(int A[],int N)
{
int B[]=new int[N];
for(int i=N-1; i>=0; i--)
{
B[N-i-1]=A[i];
}
return B;
}
static boolean isPalin(char X[])
{
int i=0,j=X.length-1;
while(i<=j)
{
if(X[i]!=X[j])return false;
i++;
j--;
}
return true;
}
static int distance(int a,int b)
{
int d=D[a]+D[b];
int l=LCA(a,b);
l=2*D[l];
return d-l;
}
static int LCA(int a,int b)
{
if(D[a]<D[b])
{
int t=a;
a=b;
b=t;
}
int d=D[a]-D[b];
int p=1;
for(int i=0; i>=0 && p<=d; i++)
{
if((p&d)!=0)
{
a=lca[a][i];
}
p<<=1;
}
if(a==b)return a;
for(int i=max-1; i>=0; i--)
{
if(lca[a][i]!=-1 && lca[a][i]!=lca[b][i])
{
a=lca[a][i];
b=lca[b][i];
}
}
return lca[a][0];
}
static void dfs(int n,int p)
{
lca[n][0]=p;
if(p!=-1)D[n]=D[p]+1;
for(int c:g[n])
{
if(c!=p)
{
dfs(c,n);
}
}
}
static int[] prefix_function(char X[])//returns pi(i) array
{
int N=X.length;
int pre[]=new int[N];
for(int i=1; i<N; i++)
{
int j=pre[i-1];
while(j>0 && X[i]!=X[j])
j=pre[j-1];
if(X[i]==X[j])j++;
pre[i]=j;
}
return pre;
}
static TreeNode start;
public static void f(TreeNode root,TreeNode p,int r)
{
if(root==null)return;
if(p!=null)
{
root.par=p;
}
if(root.val==r)start=root;
f(root.left,root,r);
f(root.right,root,r);
}
static int right(int A[],int Limit,int l,int r)
{
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]<Limit)l=m;
else r=m;
}
return l;
}
static int left(int A[],int a,int l,int r)
{
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]<a)l=m;
else r=m;
}
return l;
}
static void build(int v,int tl,int tr,int A[])
{
if(tl==tr)
{
seg[v]=A[tl];
return;
}
int tm=(tl+tr)/2;
build(v*2,tl,tm,A);
build(v*2+1,tm+1,tr,A);
seg[v]=seg[v*2]+seg[v*2+1];
}
static void update(int v,int tl,int tr,int index)
{
if(index==tl && index==tr)
{
seg[v]--;
}
else
{
int tm=(tl+tr)/2;
if(index<=tm)update(v*2,tl,tm,index);
else update(v*2+1,tm+1,tr,index);
seg[v]=seg[v*2]+seg[v*2+1];
}
}
static int ask(int v,int tl,int tr,int l,int r)
{
if(l>r)return 0;
if(tl==l && r==tr)
{
return seg[v];
}
int tm=(tl+tr)/2;
return ask(v*2,tl,tm,l,Math.min(tm, r))+ask(v*2+1,tm+1,tr,Math.max(tm+1, l),r);
}
static boolean f(long A[],long m,int N)
{
long B[]=new long[N];
for(int i=0; i<N; i++)
{
B[i]=A[i];
}
for(int i=N-1; i>=0; i--)
{
if(B[i]<m)return false;
if(i>=2)
{
long extra=Math.min(B[i]-m, A[i]);
long x=extra/3L;
B[i-2]+=2L*x;
B[i-1]+=x;
}
}
return true;
}
static int f(int l,int r,long A[],long x)
{
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]>=x)l=m;
else r=m;
}
return r;
}
static boolean f(long m,long H,long A[],int N)
{
long s=m;
for(int i=0; i<N-1;i++)
{
s+=Math.min(m, A[i+1]-A[i]);
}
return s>=H;
}
static long ask(long l,long r)
{
System.out.println("? "+l+" "+r);
return l();
}
static long f(long N,long M)
{
long s=0;
if(N%3==0)
{
N/=3;
s=N*M;
}
else
{
long b=N%3;
N/=3;
N++;
s=N*M;
N--;
long a=N*M;
if(M%3==0)
{
M/=3;
a+=(b*M);
}
else
{
M/=3;
M++;
a+=(b*M);
}
s=Math.min(s, a);
}
return s;
}
static int ask(StringBuilder sb,int a)
{
System.out.println(sb+""+a);
return i();
}
static void swap(char X[],int i,int j)
{
char x=X[i];
X[i]=X[j];
X[j]=x;
}
static int min(int a,int b,int c)
{
return Math.min(Math.min(a, b), c);
}
static long and(int i,int j)
{
System.out.println("and "+i+" "+j);
return l();
}
static long or(int i,int j)
{
System.out.println("or "+i+" "+j);
return l();
}
static int len=0,number=0;
static void f(char X[],int i,int num,int l)
{
if(i==X.length)
{
if(num==0)return;
//update our num
if(isPrime(num))return;
if(l<len)
{
len=l;
number=num;
}
return;
}
int a=X[i]-'0';
f(X,i+1,num*10+a,l+1);
f(X,i+1,num,l);
}
static boolean is_Sorted(int A[])
{
int N=A.length;
for(int i=1; i<=N; i++)if(A[i-1]!=i)return false;
return true;
}
static boolean f(StringBuilder sb,String Y,String order)
{
StringBuilder res=new StringBuilder(sb.toString());
HashSet<Character> set=new HashSet<>();
for(char ch:order.toCharArray())
{
set.add(ch);
for(int i=0; i<sb.length(); i++)
{
char x=sb.charAt(i);
if(set.contains(x))continue;
res.append(x);
}
}
String str=res.toString();
return str.equals(Y);
}
static boolean all_Zero(int f[])
{
for(int a:f)if(a!=0)return false;
return true;
}
static long form(int a,int l)
{
long x=0;
while(l-->0)
{
x*=10;
x+=a;
}
return x;
}
static int count(String X)
{
HashSet<Integer> set=new HashSet<>();
for(char x:X.toCharArray())set.add(x-'0');
return set.size();
}
static int f(long K)
{
long l=0,r=K;
while(r-l>1)
{
long m=(l+r)/2;
if(m*m<K)l=m;
else r=m;
}
return (int)l;
}
// static void build(int v,int tl,int tr,long A[])
// {
// if(tl==tr)
// {
// seg[v]=A[tl];
// }
// else
// {
// int tm=(tl+tr)/2;
// build(v*2,tl,tm,A);
// build(v*2+1,tm+1,tr,A);
// seg[v]=Math.min(seg[v*2], seg[v*2+1]);
// }
// }
static int [] sub(int A[],int B[])
{
int N=A.length;
int f[]=new int[N];
for(int i=N-1; i>=0; i--)
{
if(B[i]<A[i])
{
B[i]+=26;
B[i-1]-=1;
}
f[i]=B[i]-A[i];
}
for(int i=0; i<N; i++)
{
if(f[i]%2!=0)f[i+1]+=26;
f[i]/=2;
}
return f;
}
static int[] f(int N)
{
char X[]=in.next().toCharArray();
int A[]=new int[N];
for(int i=0; i<N; i++)A[i]=X[i]-'a';
return A;
}
static int max(int a ,int b,int c,int d)
{
a=Math.max(a, b);
c=Math.max(c,d);
return Math.max(a, c);
}
static int min(int a ,int b,int c,int d)
{
a=Math.min(a, b);
c=Math.min(c,d);
return Math.min(a, c);
}
static HashMap<Integer,Integer> Hash(int A[])
{
HashMap<Integer,Integer> mp=new HashMap<>();
for(int a:A)
{
int f=mp.getOrDefault(a,0)+1;
mp.put(a, f);
}
return mp;
}
static long mul(long a, long b)
{
return ( a %mod * 1L * b%mod )%mod;
}
static void swap(int A[],int a,int b)
{
int t=A[a];
A[a]=A[b];
A[b]=t;
}
static int find(int a)
{
if(par[a]<0)return a;
return par[a]=find(par[a]);
}
static void union(int a,int b)
{
a=find(a);
b=find(b);
if(a!=b)
{
if(par[a]>par[b]) //this means size of a is less than that of b
{
int t=b;
b=a;
a=t;
}
par[a]+=par[b];
par[b]=a;
}
}
static boolean isSorted(int A[])
{
for(int i=1; i<A.length; i++)
{
if(A[i]<A[i-1])return false;
}
return true;
}
static boolean isDivisible(StringBuilder X,int i,long num)
{
long r=0;
for(; i<X.length(); i++)
{
r=r*10+(X.charAt(i)-'0');
r=r%num;
}
return r==0;
}
static int lower_Bound(int A[],int low,int high, int x)
{
if (low > high)
if (x >= A[high])
return A[high];
int mid = (low + high) / 2;
if (A[mid] == x)
return A[mid];
if (mid > 0 && A[mid - 1] <= x && x < A[mid])
return A[mid - 1];
if (x < A[mid])
return lower_Bound( A, low, mid - 1, x);
return lower_Bound(A, mid + 1, high, x);
}
static String f(String A)
{
String X="";
for(int i=A.length()-1; i>=0; i--)
{
int c=A.charAt(i)-'0';
X+=(c+1)%2;
}
return X;
}
static void sort(long[] a) //check for long
{
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 String swap(String X,int i,int j)
{
char ch[]=X.toCharArray();
char a=ch[i];
ch[i]=ch[j];
ch[j]=a;
return new String(ch);
}
static int sD(long n)
{
if (n % 2 == 0 )
return 2;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0 )
return i;
}
return (int)n;
}
// static void setGraph(int N,int nodes)
// {
//// size=new int[N+1];
// par=new int[N+1];
// col=new int[N+1];
//// g=new int[N+1][];
// D=new int[N+1];
// int deg[]=new int[N+1];
// int A[][]=new int[nodes][2];
// for(int i=0; i<nodes; i++)
// {
// int a=i(),b=i();
// A[i][0]=a;
// A[i][1]=b;
// deg[a]++;
// deg[b]++;
// }
// for(int i=0; i<=N; i++)
// {
// g[i]=new int[deg[i]];
// deg[i]=0;
// }
// for(int a[]:A)
// {
// int x=a[0],y=a[1];
// g[x][deg[x]++]=y;
// g[y][deg[y]++]=x;
// }
// }
static long pow(long a,long b)
{
//long mod=1000000007;
long pow=1;
long x=a;
while(b!=0)
{
if((b&1)!=0)pow=(pow*x)%mod;
x=(x*x)%mod;
b/=2;
}
return pow;
}
static long toggleBits(long x)//one's complement || Toggle bits
{
int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;
return ((1<<n)-1)^x;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static void print(char A[])
{
for(char c:A)System.out.print(c+" ");
System.out.println();
}
static void print(boolean A[])
{
for(boolean c:A)System.out.print(c+" ");
System.out.println();
}
static void print(int A[])
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static void print(long A[])
{
for(long i:A)System.out.print(i+ " ");
System.out.println();
}
static void print(boolean A[][])
{
for(boolean a[]:A)print(a);
}
static void print(long A[][])
{
for(long a[]:A)print(a);
}
static void print(int A[][])
{
for(int a[]:A)print(a);
}
static void print(ArrayList<Integer> A)
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++)A[i]=in.nextLong();
return A;
}
static long GCD(long a,long b)
{
if(b==0)
{
return a;
}
else return GCD(b,a%b );
}
}
class segNode
{
long pref,suff,sum,max;
segNode(long a,long b,long c,long d)
{
pref=a;
suff=b;
sum=c;
max=d;
}
}
//class TreeNode
//{
// int cnt,index;
// TreeNode left,right;
// TreeNode(int c)
// {
// cnt=c;
// index=-1;
// }
// TreeNode(int c,int index)
// {
// cnt=c;
// this.index=index;
// }
//}
class role
{
String skill;
int level;
role(String s,int l)
{
skill=s;
level=l;
}
}
class project implements Comparable<project>
{
int score,index;
project(int s,int i)
{
// roles=r;
index=i;
score=s;
// skill=new String[r];
// lvl=new int[r];
}
public int compareTo(project x)
{
return x.score-this.score;
}
}
class post implements Comparable<post>
{
long x,y,d,t;
post(long a,long b,long c)
{
x=a;
y=b;
d=c;
}
public int compareTo(post X)
{
if(X.t==this.t)
{
return 0;
}
else
{
long xt=this.t-X.t;
if(xt>0)return 1;
return -1;
}
}
}
class TreeNode
{
int val;
TreeNode left, right,par;
TreeNode() {}
TreeNode(int item)
{
val = item;
left =null;
right = null;
par=null;
}
}
class edge
{
int a,wt;
edge(int a,int w)
{
this.a=a;
wt=w;
}
}
class pair3 implements Comparable<pair3>
{
long a;
int index;
pair3(long x,int i)
{
a=x;
index=i;
}
public int compareTo(pair3 x)
{
if(this.a>x.a)return 1;
if(this.a<x.a)return -1;
return 0;
// return this.index-x.index;
}
}
//Code For FastReader
//Code For FastReader
//Code For FastReader
//Code For FastReader
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while(st==null || !st.hasMoreElements())
{
try
{
st=new StringTokenizer(br.readLine());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
} | Java | ["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 8 | 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 | 8649d3ccf728d233d478eaedef2d16e6 | 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.*;
import java.util.StringTokenizer;
public class Unequal_Array
{
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();
}
}
public static PrintWriter out;
public static void main (String[] args) throws java.lang.Exception
{
out = new PrintWriter(System.out);
try
{
//Scanner obj = new Scanner (System.in);
Reader obj = new Reader ();
int t = obj.nextInt();
while (t > 0)
{
t--;
int n = obj.nextInt();
//String str = obj.next();
int arr[] = new int[n];
int i, j = n - 1;
for (i=0;i<n;i++)
{
arr[i] = obj.nextInt();
}
if (n == 2)
{
out.println ("0");
continue;
}
i = 0;
while (i <= j)
{
if (arr[i] == arr[i+1] && arr[j] == arr[j-1])
{
i++;
j--;
break;
}
if (arr[i] != arr[i+1])
{
i++;
}
if (arr[j] != arr[j-1])
{
j--;
}
}
if (i > j)
{
out.println ("0");
}
else if (i == j)
{
out.println ("1");
}
else
{
out.println (j - i);
}
}
}catch(Exception e){
return;
}
out.flush();
out.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 8 | 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 | 5c9c8863e45e0b87ac5f3c85ba054932 | 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.*;
import java.util.StringTokenizer;
public class Unequal_Array
{
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();
}
}
public static PrintWriter out;
public static void main (String[] args) throws java.lang.Exception
{
out = new PrintWriter(System.out);
try
{
//Scanner obj = new Scanner (System.in);
Reader obj = new Reader ();
int t = obj.nextInt();
while (t > 0)
{
t--;
int n = obj.nextInt();
//String str = obj.next();
int arr[] = new int[n];
int i, j = n - 1;
for (i=0;i<n;i++)
{
arr[i] = obj.nextInt();
}
if (n == 2)
{
out.println ("0");
continue;
}
i = 0;
boolean b = false;
while (i <= j)
{
if (arr[i] == arr[i+1] && arr[j] == arr[j-1])
{
i++;
j--;
break;
}
if (arr[i] != arr[i+1])
{
i++;
}
if (arr[j] != arr[j-1])
{
j--;
}
}
if (i > j)
{
out.println ("0");
}
else if (i == j)
{
out.println ("1");
}
else
{
out.println (j - i);
}
}
}catch(Exception e){
return;
}
out.flush();
out.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 8 | 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 | 235beedd3bdcd22ab1516a1f245f7509 | 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 C {
public static void main(String[] args) throws IOException {
Reader sc = new Reader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0;i < n;i++)
arr[i] = sc.nextInt();
int equity_cnt = 0;
for(int i = 0;i < n - 1;i++) {
if(arr[i] == arr[i + 1])
equity_cnt++;
}
if(equity_cnt <= 1)
out.println(0);
else {
int start = -1;
int end = -1;
for(int i = 0;i < n - 1;i++) {
if(arr[i] == arr[i + 1]) {
start = i + 1;
break;
}
}
for(int i = 0;i < n - 1;i++) {
if(arr[i] == arr[i + 1])
end = i;
}
out.println(Math.max(1,end - start));
}
}
out.flush();
}
public static String bin(long n,long i) {
StringBuilder list = new StringBuilder();
list.append(0);
while(n > 0) {
list.append(n % i);
n = n / i;
}
return new String(list);
}
static int[] sort(int[] arr,int n) {
List<Integer> list = new ArrayList<>();
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);
return arr;
}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static int isPrime(int n) {
if(n < 2)
return 0;
if(n < 4)
return 1;
if((n % 2) == 0 || (n % 3) == 0)
return 0;
for(int i = 5; (i * i) <= n; i += 6)
if((n % i) == 0 || (n % (i + 2)) == 0)
return 0;
return 1;
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
if(st.hasMoreTokens())
str = st.nextToken("\n");
else
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
// ********************* Custom Pair Class *********************
//class Pair implements Comparable<Pair> {
// int a,b;
// public Pair(int a,int b) {
// this.a = a;
// this.b = b;
// }
// @Override
// public int compareTo(Pair other) {
//// if(this.b == other.b)
//// return Integer.compare(this.a,other.a);
// return Integer.compare(other.b,this.b);
// }
//}
// ****************** Segment Tree ******************
//public class SegmentTreeNode {
// public SegmentTreeNode left;
// public SegmentTreeNode right;
// public int Start;
// public int End;
// public int Sum;
// public SegmentTreeNode(int start, int end) {
// Start = start;
// End = end;
// Sum = 0;
// }
//}
//public SegmentTreeNode buildTree(int start, int end) {
// if(start > end)
// return null;
// SegmentTreeNode node = new SegmentTreeNode(start, end);
// if(start == end)
// return node;
// int mid = start + (end - start) / 2;
// node.left = buildTree(start, mid);
// node.right = buildTree(mid + 1, end);
// return node;
//}
//public void update(SegmentTreeNode node, int index) {
// if(node == null)
// return;
// if(node.Start == index && node.End == index) {
// node.Sum += 1;
// return;
// }
// int mid = node.Start + (node.End - node.Start) / 2;
// if(index <= mid)
// update(node.left, index);
// else
// update(node.right, index);
// node.Sum = node.left.Sum + node.right.Sum;
//}
//public int SumRange(SegmentTreeNode root, int start, int end) {
// if(root == null || start > end)
// return 0;
// if(root.Start == start && root.End == end)
// return root.Sum;
// int mid = root.Start + (root.End - root.Start) / 2;
// if(end <= mid)
// return SumRange(root.left, start, end);
// else if(start > mid)
// return SumRange(root.right, start, end);
// return SumRange(root.left, start, mid) + SumRange(root.right, mid + 1, end);
//} | 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 8 | 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 | 4cc8385256b4f62206a71de70880fab2 | 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 Main implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args) {
new Thread(null, new Main(), "", 256 * (1L << 20)).start();
}
public void run() {
try {
long t1 = System.currentTimeMillis();
if (System.getProperty("ONLINE_JUDGE") != null) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
Locale.setDefault(Locale.US);
solve();
in.close();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
long[] readLongArray(int n) throws IOException{
long[] array = new long[n];
for (int i = 0; i < n; i++) {
array[i] = readLong();
}
return array;
}
int[] readIntArray(int n) throws IOException{
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = readInt();
}
return array;
}
// if using Long array with Long search swap the input parameters to longs
int bsForMinNumberGreaterEqual(int[] n, int search) {
int lowB = 0;
int highB = n.length - 1;
int k;
while (lowB <= highB) {
k = (lowB + highB) / 2;
if (k > 0 && n[k] >= search && n[k - 1] < search) {
return k;
} else if (k == 0 && n[k] >= search) {
return k;
} else if (k > 0 && n[k - 1] >= search) {
highB = k - 1;
} else if (n[k] < search) {
lowB = k + 1;
}
}
return -1;
}
static final Random random=new Random();
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi = random.nextInt(n);
long temp = a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
void solveTest() throws IOException {
int n = readInt();
int max = 0;
int[] a = readIntArray(n);
int numU = 0;
for (int i = 0; i < n; i++) {
max = Math.max(max, a[i]);
if (i < n - 1 && a[i] == a[i + 1]) {
numU++;
}
}
int moves = 0;
for (int i = 0; i < n - 1; i++) {
if (numU <= 1) {
out.println(moves);
return;
}
if (a[i] == a[i + 1]) {
if (i < n - 2) {
if (i < n - 3 && a[i] == a[i + 1] && a[i + 2] == a[i + 3]) {
numU--;
if (a[i + 1] == a[i + 2]) {
numU--;
}
} else if (i < n - 2 && a[i] == a[i + 1] && a[i + 1] == a[i + 2]) {
numU--;
}
moves++;
a[i + 1] = max + 1;
a[i + 2] = max + 1;
max++;
}
}
}
out.println(moves);
}
void solve() throws IOException {
int testCases = readInt();
for (int tests = 0; tests < testCases; tests++) {
solveTest();
}
}
}
| 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 8 | 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 | 9a7093869c09e3544b386e7e9cdf1c09 | 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 | /* 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 hindu
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
try {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
int l=0;
int r=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1])
{
l=i+1;
break;
}
}
for(int i=n-1;i>0;i--)
{
if(arr[i]==arr[i-1])
{
r=i-1;
break;
}
}
if(l==0 || l>r)
{
System.out.println(0);
continue;
}
if(l==r)
{
System.out.println(1);
continue;
}
System.out.println(r-l);
}
} catch(Exception e) {
}
}
}
| 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 8 | 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 | 033cedb96e4f10e0f09d510dbd222ac4 | 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 | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
try {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
int l=0;
int r=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1])
{
l=i+1;
break;
}
}
for(int i=n-1;i>0;i--)
{
if(arr[i]==arr[i-1])
{
r=i-1;
break;
}
}
if(l==0 || l>r)
{
System.out.println(0);
continue;
}
if(l==r)
{
System.out.println(1);
continue;
}
System.out.println(r-l);
}
} catch(Exception e) {
}
}
}
| 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 8 | 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 | 30d8d6d191b6791a9202f1ab6a44814c | 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.*;
// cd C:\Users\Lenovo\Desktop\New
//ArrayList<Integer> a=new ArrayList<>();
//List<Integer> lis=new ArrayList<>();
//StringBuilder ans = new StringBuilder();
//HashMap<Integer,Integer> map=new HashMap<>();
public class cf {
static FastReader in=new FastReader();
static final Random random=new Random();
//static long m=1000000007l;
static long mod=1000000007l;
//static long dp[]=new long[200002];
//static ArrayList<Integer> ans;
//static ArrayList<ArrayList<String>> ans;
static long ans;
public static void main(String args[]) throws IOException {
FastReader sc=new FastReader();
//Scanner s=new Scanner(System.in);
int tt=sc.nextInt();
//int tt=1;
while(tt-->0){
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int cnt=0;
int end=0;
int st=0;
for(int i=0;i<n-1;i++){
if(arr[i]==arr[i+1]){
end=i;
cnt++;
}
}
for(int i=0;i<n-1;i++){
if(arr[i]==arr[i+1]){
st=i+1;
break;
}
}
if(cnt<=1){
System.out.println(0);
}
else{
//System.out.println(en-st-1);
int ans=end-st+1;
if(ans==1){
System.out.println(1);
}
else{
System.out.println(ans-1);
}
}
}
}
public static boolean isPal(String temp){
for(int i=0;i<temp.length();i++){
if(temp.charAt(i)!=temp.charAt(temp.length()-1-i)){
return false;
}
}
return true;
}
/*static void bfs(){
dist[1]=0;
for(int i=2;i<dist.length;i++){
dist[i]=-1;
}
Queue<Integer> q=new LinkedList<>();
q.add(1);
while(q.size()!=0){
int cur=q.peek();
q.remove();
for(int i=1;i<dist.length;i++ ){
int next=cur+cur/i;
int ndis=dist[cur]+1;
if(next<dist.length && dist[next]==-1){
dist[next]=ndis;
q.add(next);
}
}
}
}*/
static long comb(int n,int k){
return factorial(n) * pow(factorial(k), mod-2) % mod * pow(factorial(n-k), mod-2) % mod;
}
static long pow(long a, long b) {
// long mod=1000000007;
long res = 1;
while (b != 0) {
if ((b & 1) != 0) {
res = (res * a) % mod;
}
a = (a * a) % mod;
b /= 2;
}
return res;
}
static boolean powOfTwo(long n){
while(n%2==0){
n=n/2;
}
if(n!=1){
return false;
}
return true;
}
static int upper_bound(long arr[], long key)
{
int mid, N = arr.length;
int low = 0;
int high = N;
// Till low is less than high
while (low < high && low != N) {
mid = low + (high - low) / 2;
if (key >= arr[mid]) {
low = mid + 1;
}
else {
high = mid;
}
}
return low;
}
static boolean prime(int n){
for(int i=2;i<=Math.sqrt(n);i++){
if(n%i==0){
return false;
}
}
return true;
}
static long factorial(int n){
long ret = 1;
while(n > 0){
ret = ret * n % mod;
n--;
}
return ret;
}
static long find(ArrayList<Long> arr,long n){
int l=0;
int r=arr.size();
while(l+1<r){
int mid=(l+r)/2;
if(arr.get(mid)<n){
l=mid;
}
else{
r=mid;
}
}
return arr.get(l);
}
static void rotate(int ans[]){
int last=ans[0];
for(int i=0;i<ans.length-1;i++){
ans[i]=ans[i+1];
}
ans[ans.length-1]=last;
}
static int countprimefactors(int n){
int ans=0;
int z=(int)Math.sqrt(n);
for(int i=2;i<=z;i++){
while(n%i==0){
ans++;
n=n/i;
}
}
if(n>1){
ans++;
}
return ans;
}
static String reverse_String(String s){
String ans="";
for(int i=s.length()-1;i>=0;i--){
ans+=s.charAt(i);
}
return ans;
}
static void reverse_array(int arr[]){
int l=0;
int r=arr.length-1;
while(l<r){
int temp=arr[l];
arr[l]=arr[r];
arr[r]=temp;
l++;
r--;
}
}
static int msb(int x){
int ans=0;
while(x!=0){
x=x/2;
ans++;
}
return ans;
}
static void ruffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
int temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static long gcd(long a,long b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
/* Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<Integer, Integer> entry = iterator.next();
int value = entry.getValue();
if(value==1){
iterator.remove();
}
else{
entry.setValue(value-1);
}
}
*/
static class Pair implements Comparable
{
int a;
int b;
public String toString()
{
return a+" " + b;
}
public Pair(int x , int y)
{
a=x;b=y;
}
@Override
public int compareTo(Object o) {
Pair p = (Pair)o;
if(b!=p.b){
return b-p.b;
}
else{
return a-p.a;
}
/*if(p.a!=a){
return a-p.a;//in
}
else{
return b-p.b;//
}*/
}
}
public static boolean checkAP(List<Integer> lis){
for(int i=1;i<lis.size()-1;i++){
if(2*lis.get(i)!=lis.get(i-1)+lis.get(i+1)){
return false;
}
}
return true;
}
/* public static int minBS(int[]arr,int val){
int l=-1;
int r=arr.length;
while(r>l+1){
int mid=(l+r)/2;
if(arr[mid]>=val){
r=mid;
}
else{
l=mid;
}
}
return r;
}
public static int maxBS(int[]arr,int val){
int l=-1;
int r=arr.length;
while(r>l+1){
int mid=(l+r)/2;
if(arr[mid]<=val){
l=mid;
}
else{
r=mid;
}
}
return l;
}
*/
static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["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 8 | 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 | 248712fbb7fdd1e8fd2e47216fb45c19 | 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.math.BigInteger;
import java.util.*;
import java.io.*;
public class Vaibhav {
/*-----------==============-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
static long bit[];
static boolean prime[];
public static long lcm(long x, long y) {
return (x * y) / gcd(x, y);
}
static class Pair implements Comparable<Pair> {
int x;
ArrayList<Integer> y;
Pair(int x, ArrayList<Integer> y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
return this.y.size() - o.y.size();
}
}
public static void update(long bit[], int i, int x) {
for (; i < bit.length; i += (i & (-i))) {
bit[i] += x;
}
}
public static long sum(int i) {
long sum = 0;
for (; i > 0; i -= (i & (-i))) {
sum += bit[i];
}
return sum;
}
public static int CeilIndex(int A[], int l, int r, int key) {
while (r - l > 1) {
int m = l + (r - l) / 2;
if (A[m] >= key) r = m;
else l = m;
}
return r;
}
public static int LongestIncreasingSubsequenceLength(int A[], int size) {
int[] tailTable = new int[size];
int len;
tailTable[0] = A[0];
len = 1;
for (int i = 1; i < size; i++) {
if (A[i] < tailTable[0]) tailTable[0] = A[i];
else if (A[i] > tailTable[len - 1]) tailTable[len++] = A[i];
else tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i];
}
return len;
}
static long power(long x, long y, long p) {
if (y == 0) return 1;
if (x == 0) return 0;
long res = 1l;
x = x % p;
while (y > 0) {
if (y % 2 == 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long power(long x, long y) {
if (y == 0) return 1;
if (x == 0) return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1) res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
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 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[] readlongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static void sieveOfEratosthenes(int n) {
prime = new boolean[n + 1];
for (int i = 0; i <= n; i++) prime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p) prime[i] = false;
}
}
}
public static int log2(int x) {
return (int) (Math.log(x) / Math.log(2));
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static long nCk(int n, int k) {
long res = 1;
for (int i = n - k + 1; i <= n; ++i) res *= i;
for (int i = 2; i <= k; ++i) res /= i;
return res;
}
static BigInteger bi(String str) {
return new BigInteger(str);
}
/*------------=========------------------------------------===================================================------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------===============================================------=================-=-------=============--------*/
static FastScanner fs = null;
static int dp[];
static ArrayList<Integer> al[];
public static void main(String[] args) {
fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
outer:
while (t-- > 0) {
int n = fs.nextInt();
int a[] = fs.readArray(n);
boolean f = false;
int ans = 0;
int count = 0;
int left = -1;
int right = -1;
for(int i=1;i<n;i++){
if(a[i]==a[i-1]){
if(left==-1){
left = i;
}else{
right = i-1;
}
}
}
if(right==-1){
out.println(0);
}else{
int v = right-left+1;
ans = v==1?1:v-1;
out.println(ans);
}
}
out.close();
}
public static boolean check(ArrayList<Integer> al,ArrayList<Integer>al1,String s){
for(int i=1;i<al1.size();i++){
if(s.charAt(al1.get(i)) >= s.charAt(al1.get(i-1))){
}else{
return false;
}
}
if(al.size()==0) return true;
if(al1.size()!=0) {
if (s.charAt(al1.get(al1.size() - 1)) > s.charAt(al.get(0))) {
return false;
}
}
for(int i=1;i<al.size();i++){
if(s.charAt(al.get(i))>= s.charAt(al.get(i-1))){
}else{
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 8 | 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 | 8dfcd4ec49281845a34c804433666e58 | 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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Ashutosh Patel (ashutoshpatelnoida@gmail.com) Linkedin : ( https://www.linkedin.com/in/ashutosh-patel-7954651ab/ )
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
CUnequalArray solver = new CUnequalArray();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class CUnequalArray {
public void solve(int testNumber, InputReader sc, OutputWriter out) {
int n = sc.readInt();
int a[] = sc.readIntArray(n);
ArrayList<Integer> al = new ArrayList<>();
int l = 0, r = n - 1;
while (l < n - 1 && a[l] != a[l + 1]) l++;
while (r > 0 && a[r] != a[r - 1]) r--;
// if (c != 0)
// al.add(c);
// out.printLine(al);
// long res = 0;
// for (int i = 0; i < al.size(); i++) {
//
// }
int c = r - l;
if (c <= 1) {
out.printLine(0);
return;
}
if (c == 2) {
out.printLine(1);
return;
}
out.printLine(c - 2);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void printLine(int i) {
writer.println(i);
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = readInt();
}
return array;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
boolean isSpaceChar(int ch);
}
}
}
| 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 8 | 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 | d4d616c9321c39c93a0dc680dae42f29 | 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 Main {
static PrintStream out = new PrintStream(System.out);
static LinkedList<LinkedList<Integer>> adj;
static boolean[] vis;
static long ans;
static int target;
static HashMap<ArrayList<Integer>, Integer> seqs;
//static ArrayList<ArrayList<Integer>> lists;
public static void main(String[] args){
FastScanner sc = new FastScanner();
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int[] arr = sc.nextIntArray(n);
//int i = 0;
int ans = 0;
ArrayList<Pair> list = new ArrayList();
int tmp = 0;
int num = arr[0];
for(int i = 1; i < n; i++){
if(arr[i] != num){
Pair p = new Pair(tmp, i-1);
list.add(p);
tmp = i;
num = arr[i];
//continue;
}
//tmp++;
}
list.add(new Pair(tmp, n - 1));
ArrayList<Pair> segs = new ArrayList();
for(Pair p : list){
if(p.b - p.a > 0) segs.add(p);
}
if(segs.size() == 0){
out.println(ans);
continue;
}
Pair first = segs.get(0);
Pair last = segs.get(segs.size() - 1);
int len = last.b - first.a + 1;
if(len <= 2) ans = 0;
else if(len == 3 || len == 4) ans = 1;
else ans = len - 3;
out.println(ans);
}
}
public static void dfs(int v){
if(vis[v]) return;
vis[v] = true;
LinkedList<Integer> neighbors = adj.get(v);
Iterator<Integer> it = neighbors.iterator();
while(it.hasNext()){
int node = it.next();
if(!vis[node]){
dfs(node);
}
}
}
public static void mergeSort(int[] inputArray){
int n = inputArray.length;
// if input array is empty or contains only one element (meaning already sorted)
if(n < 2){
return;
}
// split input array
int mid = n / 2;
int[] leftHalf = new int[mid];
int[] rightHalf = new int[n - mid];
for(int i = 0; i < mid; i++){
leftHalf[i] = inputArray[i];
}
for(int i = mid; i < n; i++){
rightHalf[i - mid] = inputArray[i];
}
// merge sort both halves
mergeSort(leftHalf);
mergeSort(rightHalf);
// merge halves back into one array
merge(inputArray, leftHalf, rightHalf);
}
public static void merge(int[] inputArray, int[] leftArray, int[] rightArray){
int leftSize = leftArray.length;
int rightSize = rightArray.length;
int i = 0, j = 0, k = 0;
// get smaller element between two arrays
while(i < leftSize && j < rightSize){
if(leftArray[i] <= rightArray[j]){
inputArray[k] = leftArray[i++];
}
else{
for(int x = i ; x < leftSize; x++){
//out.print(leftArray[x] + " " + rightArray[j]);
int u = leftArray[x];
int v = rightArray[j];
adj.get(u).add(v);
adj.get(v).add(u);
}
inputArray[k] = rightArray[j++];
}
k++;
}
// check left for leftovers
while(i < leftSize){
inputArray[k++] = leftArray[i++];
}
// check right for leftovers
while(j < rightSize){
inputArray[k++] = rightArray[j++];
}
}
public static void addUndirectedEdge(int u, int v){
adj.get(u).add(v);
adj.get(v).add(u);
}
}
// custom I/O
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int[] nextIntArray(int length) {
int[] arr = new int[length];
for (int i = 0; i < length; i++)
arr[i] = nextInt();
return arr;
}
}
class Pair {
int a;
int b;
public Pair(int a, int b){
this.a = a;
this.b = b;
}
} | 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 8 | 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 | 213e15feacb2889344a7f3f0459f3088 | 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 Main {
static final int MOD = 1000000007;
static InputReader in = new InputReader(System.in);
static BufferOutput out = new BufferOutput();
public static void main(String[] args) {
for(int i = in.nextInt();i>0;i--) {
int n = in.nextInt();
int[] arr = in.nextIntArray(n);
int contL = 0;
int contR = 0;
for(int o = 0;o<n-1;o++) {
if(arr[o] == arr[o+1]) {
contL = o;
break;
}
}
for(int o = n-2;o>=0;o--) {
if(arr[o] == arr[o+1]) {
contR = o;
break;
}
}
if(contR-contL>1) {
System.out.println(contR-contL-1);
}else {
System.out.println(contR-contL);
}
//System.out.println(contL+" "+contR);
}
}
static class BufferOutput {
private DataOutputStream dout;
final private int BUFFER_SIZE = 1 << 16;
private byte[] buffer;
private int pointer = 0;
public BufferOutput() {
buffer = new byte[BUFFER_SIZE];
dout = new DataOutputStream(System.out);
}
public BufferOutput(OutputStream out) {
buffer = new byte[BUFFER_SIZE];
dout = new DataOutputStream(out);
}
public void writeBytes(byte arr[]) throws IOException {
int bytesToWrite = arr.length;
if (pointer + bytesToWrite >= BUFFER_SIZE) {
flush();
}
for (int i = 0; i < bytesToWrite; i++) {
buffer[pointer++] = arr[i];
}
}
public void writeString(String str) throws IOException {
writeBytes(str.getBytes());
}
public void flush() throws IOException {
dout.write(buffer, 0, pointer);
dout.flush();
pointer = 0;
}
public void close() throws IOException{
dout.close();
}
}
static class InputReader {
private InputStream in;
private byte[] buffer = new byte[1024];
private int curbuf;
private int lenbuf;
public InputReader(InputStream in) {
this.in = in;
this.curbuf = this.lenbuf = 0;
}
public String nextLine() {
StringBuilder sb = new StringBuilder();
int b = readByte();
while (b != 10) {
sb.appendCodePoint(b);
b = readByte();
}
if (sb.length() == 0)
return "";
return sb.toString().substring(0, sb.length() - 1);
}
public boolean hasNextByte() {
if (curbuf >= lenbuf) {
curbuf = 0;
try {
lenbuf = in.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return false;
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[curbuf++];
else
return -1;
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private void skip() {
while (hasNextByte() && isSpaceChar(buffer[curbuf]))
curbuf++;
}
public boolean hasNext() {
skip();
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (!isSpaceChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public char[][] nextCharMap(int n, int m) {
char[][] map = new char[n][m];
for (int i = 0; i < n; i++)
map[i] = next().toCharArray();
return map;
}
}
} | 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 8 | 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 | 8f691604f2a39918a6654be55b302135 | 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.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Solution
{
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) {
int n = sc.nextInt();
int cf[] = new int[n];
int c = 0, p = -1;
for(int i=0;i<n;i++) {
int v = sc.nextInt();
if(p == v) {
cf[i] = 1;
c++;
}
p = v;
}
if(c < 2) {
System.out.println(0);
continue;
}
int sum = cf[0]+cf[1]+cf[2], ans = 0, p1 = 0, p2 = 2;
while(true) {
if(cf[p1] == 1) {
c -= sum - 1;
sum = 1;
ans++;
cf[p1] = 0;
cf[p1+1] = 1;
cf[p2] = 0;
}
if(c == 1)
break;
p1++;
if(p2 == n-1) {
ans++;
break;
}
sum += cf[++p2];
}
System.out.println(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 8 | 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 | cc09f08f396b324d52c919a6cc1702a3 | 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.lang.*;
import java.util.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int z=0;z<t;z++){
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<a.length;i++){
a[i]=sc.nextInt();
}
int first=-1;
int last=-1;
for(int i=1;i<a.length;i++){
if(a[i]==a[i-1]){
if(first==-1){
first=i;
last=i;
}else{
last=i;
}
}
}
if(last==first){
System.out.println(0);
}else{
int ans=0;
for(int i=first;i<last-1;i++){
ans++;
}
if(ans==0){
System.out.println(1);
continue;
}
System.out.println(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 8 | 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 | fad47675ebab34f352e134261c96630a | 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.util.*;
import java.io.*;
import java.math.*;
/**
*
* @Har_Har_Mahadev
*/
/**
* Main , Solution , Remove Public
*/
public class Solution {
public static void process() throws IOException {
int n = sc.nextInt();
int arr[] = sc.readArray(n);
int cc = 0;
int l = n+1, r = 0;
for(int i = 1; i<n; i++) {
if(arr[i] == arr[i-1]) {
cc++;
l = min(l, i);
r = i;
}
}
if(cc <= 1) {
System.out.println(0);
return;
}
System.out.println(max(1, r-l-1));
}
//=============================================================================
//--------------------------The End---------------------------------
//=============================================================================
private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353;
private static int N = 0;
private static void google(int tt) {
System.out.print("Case #" + (tt) + ": ");
}
static FastScanner sc;
static FastWriter out;
public static void main(String[] args) throws IOException {
boolean oj = true;
if (oj) {
sc = new FastScanner();
out = new FastWriter(System.out);
} else {
sc = new FastScanner("input.txt");
out = new FastWriter("output.txt");
}
long s = System.currentTimeMillis();
int t = 1;
t = sc.nextInt();
int TTT = 1;
while (t-- > 0) {
// google(TTT++);
process();
}
out.flush();
// tr(System.currentTimeMillis()-s+"ms");
}
private static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static void tr(Object... o) {
if (!oj)
System.err.println(Arrays.deepToString(o));
}
static class Pair implements Comparable<Pair> {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return Integer.compare(this.x, o.x);
}
/*
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair key = (Pair) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
*/
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static int ceil(int x, int y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long sqrt(long z) {
long sqz = (long) Math.sqrt(z);
while (sqz * 1L * sqz < z) {
sqz++;
}
while (sqz * 1L * sqz > z) {
sqz--;
}
return sqz;
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
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 long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int lower_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = -1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] > x) {
high = mid - 1;
} else {
ans = mid;
low = mid + 1;
}
}
return ans;
}
public static int upper_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = arr.length;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
static void ruffleSort(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void reverseArray(int[] a) {
int n = a.length;
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
//custom multiset (replace with HashMap if needed)
public static void push(TreeMap<Integer, Integer> map, int k, int v) {
//map[k] += v;
if (!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k) + v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v) {
//assumes map[k] >= v
//map[k] -= v
int lol = map.get(k);
if (lol == v)
map.remove(k);
else
map.put(k, lol - v);
}
// compress Big value to Time Limit
public static int[] compress(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int boof = 1; //min value
for (int x : ls)
if (!map.containsKey(x))
map.put(x, boof++);
int[] brr = new int[arr.length];
for (int i = 0; i < arr.length; i++)
brr[i] = map.get(arr[i]);
return brr;
}
// Fast Writer
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE)
innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000)
return 10;
if (l >= 100000000)
return 9;
if (l >= 10000000)
return 8;
if (l >= 1000000)
return 7;
if (l >= 100000)
return 6;
if (l >= 10000)
return 5;
if (l >= 1000)
return 4;
if (l >= 100)
return 3;
if (l >= 10)
return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L)
return 19;
if (l >= 100000000000000000L)
return 18;
if (l >= 10000000000000000L)
return 17;
if (l >= 1000000000000000L)
return 16;
if (l >= 100000000000000L)
return 15;
if (l >= 10000000000000L)
return 14;
if (l >= 1000000000000L)
return 13;
if (l >= 100000000000L)
return 12;
if (l >= 10000000000L)
return 11;
if (l >= 1000000000L)
return 10;
if (l >= 100000000L)
return 9;
if (l >= 10000000L)
return 8;
if (l >= 1000000L)
return 7;
if (l >= 100000L)
return 6;
if (l >= 10000L)
return 5;
if (l >= 1000L)
return 4;
if (l >= 100L)
return 3;
if (l >= 10L)
return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map)
write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
// Fast Inputs
static class FastScanner {
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1)
return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] readArray(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] readArrayLong(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public int[][] readArrayMatrix(int N, int M, int Index) {
if (Index == 0) {
int[][] res = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
int[][] res = new int[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
public long[][] readArrayMatrixLong(int N, int M, int Index) {
if (Index == 0) {
long[][] res = new long[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = nextLong();
}
return res;
}
long[][] res = new long[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC)
c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-')
neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] readArrayDouble(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32)
return true;
while (true) {
c = getChar();
if (c == NC)
return false;
else if (c > 32)
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 8 | 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 | b05f50775c71edead53c7849658bb3ca | 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.lang.*;
import java.io.*;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.System.out;
public class n {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = 1;
t = in.nextInt();
while (t-- > 0) {
long n = in.nextLong();
long[] arr = new long[(int)n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextLong();
}
long r = -1,l = -1;
for (int i = 0; i < n - 1; i++) {
if(arr[i]==arr[i+1]){
l = i;
break;
}
}
for (int i = (int) (n-2); i >= 0; i--) {
if(arr[i]==arr[i+1]){
r = i;
break;
}
}
if(l == r){
out.println(0);
continue;
}
out.println(max(1,r-l-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 8 | 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 | ce1865f4aa408f5590dc8a6f079218ac | 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.StringTokenizer;
import static java.lang.System.currentTimeMillis;
public class Ac {
static FastReader in = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
// long start = currentTimeMillis();
int t = i();
head: while (t-- > 0) {
int n = i();
int[] nums = input(n);
int l = -1, r = 0;
for (int i = 0; i < n - 1;) {
if (nums[i] == nums[i + 1]){
if (l == -1) {
l = i;
}
while (i < n - 1 && nums[i] == nums[i + 1]){
i++;
}
r = i;
}else{
i++;
}
}
if (l == -1 || r - l + 1 == 2){
out.println(0);
}else if (r - l + 1 == 3){
out.println(1);
}else{
out.println(r - l - 2);
}
}
// long end = currentTimeMillis();
// out.println(end - start);
out.flush();
out.close();
}
public static boolean check(int[] nums, int i){
boolean ans = nums[i] % 2 == 0;
for (int j = i; j < nums.length; j+=2){
if ((nums[j] % 2 == 0) != ans){
return false;
}
}
return true;
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s(){
return in.nextLine();
}
static int[] input(int n) {
int nums[] = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextInt();
}
return nums;
}
static long[] inputLong(int n) {
long nums[] = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextLong();
}
return nums;
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
| Java | ["4\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 8 | 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 | f396cf04e46f11edbd192a700f388ba1 | 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 Main
{
public static void main(String[] args)throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(System.out);
int t=Integer.parseInt(br.readLine());
while(t-->0)
{
int n=Integer.parseInt(br.readLine());
String s[]=(br.readLine()).split(" ");
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(s[i]);
}
int start=0;
while(start<n-1)
{
if(a[start]==a[start+1])
{
break;
}
start++;
}
if(start==n)
{
pw.println(0);
continue;
}
int end=n-1;
while(end>0)
{
if(a[end]==a[end-1])
{
break;
}
end--;
}
int len=end-start+1;
if(len==3)
{
len++;
}
int ans=Math.max(0,len-3);
pw.println(ans);
}
pw.flush();
}
}
| 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 8 | 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 | 162b0af3788c3ed7ffaaa6d3fc6822c6 | 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.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
public class UnequalArray {
static InputReader inputReader=new InputReader(System.in);
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static void solve() throws IOException
{
int n=inputReader.nextInt();
int arr[]=new int[n];
for (int i=0;i<n;i++)
{
arr[i]=inputReader.nextInt();
}
int mn=-1;
int mx=-1;
int ans=0;
int count=1;
for (int i=1;i<n;i++)
{
if (arr[i]==arr[i-1])
{
if (mn==-1)
{
mn=i;
}
mx=i;
}
}
if (mx==mn)
{
out.println(0);
}
else
{
int fans=Math.max(1,mx-mn-1);
out.println(fans);
}
}
static PrintWriter out=new PrintWriter((System.out));
static void SortDec(long arr[])
{
List<Long>list=new ArrayList<>();
for(long ele:arr) {
list.add(ele);
}
Collections.sort(list,Collections.reverseOrder());
for (int i=0;i<list.size();i++)
{
arr[i]=list.get(i);
}
}
static void Sort(long arr[])
{
List<Long>list=new ArrayList<>();
for(long ele:arr) {
list.add(ele);
}
Collections.sort(list);
for (int i=0;i<list.size();i++)
{
arr[i]=list.get(i);
}
}
public static void main(String args[])throws IOException
{
int t=inputReader.nextInt();
while (t-->0) {
solve();
}
long s = System.currentTimeMillis();
// out.println(System.currentTimeMillis()-s+"ms");
out.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["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 8 | 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 | 7506a1eaf551af34359ed185f0f372b2 | 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.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* #
*
* @author pttrung
*/
public class C_Global_Round_20 {
public static long MOD = 998244353;
static int[] dp;
public static void main(String[] args) throws FileNotFoundException {
// PrintWriter out = new PrintWriter(new FileOutputStream(new File(
// "output.txt")));
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner();
int T = in.nextInt();
//System.out.println(cal(1608737403, 1000000000) - 923456789987654321L);
for (int z = 0; z < T; z++) {
int n = in.nextInt();
int[] data = new int[n];
int min = n;
int max = 0;
for (int i = 0; i < n; i++) {
data[i] = in.nextInt();
if (i > 0 && data[i] == data[i - 1]) {
max = i - 1;
min = Integer.min(min, i - 1);
}
}
if (max <= min) {
out.println(0);
continue;
}
int gap = max - min;
//System.out.println(gap);
out.println(Integer.max(1, gap - 1));
}
out.close();
}
static int find(int index, int[] u) {
if (u[index] != index) {
return u[index] = find(u[index], u);
}
return index;
}
static int abs(int a) {
return a < 0 ? -a : a;
}
public static int[] KMP(String val) {
int i = 0;
int j = -1;
int[] result = new int[val.length() + 1];
result[0] = -1;
while (i < val.length()) {
while (j >= 0 && val.charAt(j) != val.charAt(i)) {
j = result[j];
}
j++;
i++;
result[i] = j;
}
return result;
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static int digit(long n) {
int result = 0;
while (n > 0) {
n /= 10;
result++;
}
return result;
}
public static double dist(long a, long b, long x, long y) {
double val = (b - a) * (b - a) + (x - y) * (x - y);
val = Math.sqrt(val);
double other = x * x + a * a;
other = Math.sqrt(other);
return val + other;
}
public static class Point {
int x;
int y;
public Point(int start, int end) {
this.x = start;
this.y = end;
}
public String toString() {
return x + " " + y;
}
}
public static class FT {
long[] data;
FT(int n) {
data = new long[n];
}
public void update(int index, long value) {
while (index < data.length) {
data[index] += value;
index += (index & (-index));
}
}
public long get(int index) {
long result = 0;
while (index > 0) {
result += data[index];
index -= (index & (-index));
}
return result;
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long pow(long a, int b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2);
if (b % 2 == 0) {
return (val * val) % MOD;
} else {
return (val * ((val * a) % MOD)) % MOD;
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
// System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
//br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
} | 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 8 | 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 | e8cf492e6b86fb7ddb8710276c5b7858 | 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.CharArrayReader;
import java.util.*;
public class Wifi {
static void sort(long[] arr) {
ArrayList<Long> a = new ArrayList<>();
for (long i : arr) {
a.add(i);
}
Collections.sort(a);
for (int i = 0; i < a.size(); i++) {
arr[i] = a.get(i);
}
}
static long upper( long l, long last,long sum) {
long low = l;
long high = last;
long ans = -1;
while(low <= high){
long mid = (low + (high-low)/2);
if(mid == sum){
ans = mid;
break;
}
if(mid < sum){
ans = mid;
low = mid+1;
}else{
high = mid-1;
}
}
return ans;
}
static long get(long[] arr, int s, int end) {
long max_so_far = Long.MIN_VALUE;
long curren_max = 0;
for (int i = s; i <= end; i++) {
curren_max = curren_max + arr[i];
if (curren_max > max_so_far) {
max_so_far = curren_max;
}
if (curren_max < 0) {
curren_max = 0;
}
}
return max_so_far;
}
public static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b%a, a);
}
public static long count(long n){
long ans = 0;
for(long i = 1; i <= Math.sqrt(n); i++){
if(n % i == 0){
if(n/i == i){
ans++;
}else{
ans +=2;
}
}
}
return ans;
}
public static void main(String[] adfdf) {
Scanner sc = new Scanner(System.in);
long t = sc.nextLong();
test: while (t-- > 0) {
int n = sc.nextInt();
long[] arr = new long[n];
for(int i = 0; i < n; i++){
arr[i] = sc.nextLong();
}
int ind = -1;
long c = 0;
int last = n-1;
for(int i= 0; i < n-1; i++){
if(arr[i]==arr[i+1]){
last = i;
c++;
if(ind == -1)
ind = i;
}
}
// System.out.println(ind+" "+c);
if(ind == -1 || c == 1){
System.out.println("0");
}else{
last = n;
for(int i = n-1; i >0; i--){
if(arr[i] != arr[i-1]){
last = i;
}else{
break;
}
}
long ans = 1;
arr[ind+1] = -1;
arr[ind+2] = -1;
long chn = -2;
for(int i = ind+2; i < n-1; i++){
if(i+1 == last-1 || i >= last-1) break;
if(arr[i] == arr[i-1]){
ans++;
arr[i] = chn;
arr[i+1] = chn--;
}
}
// System.out.println(Arrays.toString(arr));
System.out.println(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 8 | 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 | 2d6acfff0eebbb8c5b2c93c101354703 | 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.math.*;
import java.util.*;
public class Main{
static final int INF = 0x3f3f3f3f;
static final long LNF = 0x3f3f3f3f3f3f3f3fL;
public static void main(String[] args) throws IOException {
initReader();
int t=nextInt();
while (t--!=0){
int n=nextInt();
int[]arr=new int[n+1];
for(int i=1;i<=n;i++)arr[i]=nextInt();
int id1=0,id2=0;
for(int i=2;i<=n;i++){
if(arr[i]==arr[i-1]){
id1=i-1;
break;
}
}
for(int i=n-1;i>=1;i--){
if(arr[i]==arr[i+1]){
id2=i+1;
break;
}
}
if(id2-id1+1<=2)pw.println("0");
else if(id2-id1+1<=4)pw.println("1");
else pw.println(id2-id1-2);
}
pw.close();
}
/***************************************************************************************/
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter pw;
public static void initReader() throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = new StringTokenizer("");
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
// 从文件读写
// reader = new BufferedReader(new FileReader("test.in"));
// tokenizer = new StringTokenizer("");
// pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out")));
}
public static boolean hasNext() {
try {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
public static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static char nextChar() throws IOException {
return next().charAt(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 8 | 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 | e6d446d0b284a249041b36da59ddcf85 | 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 C_Unequal_Array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] arr = new int[n];
int first = 0, last = 0;
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
if (i > 0 && arr[i] == arr[i - 1]) {
if (first == 0)
first = i;
last = i;
}
}
int ans = last - first - 1;
if (ans <= 0)
System.out.println(ans + 1);
else
System.out.println(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 8 | 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 | e611ba45d9ccbe3301becb5ba746bc22 | 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.*;
// "static void main" must be defined in a public class.
public class Main {
public static void main(String[] args) {
new Solution().function();
}
}
class Solution {
private Scanner sc = new Scanner(System.in);
public void function() {
int T = sc.nextInt();
while (T-- > 0) {
int len = sc.nextInt();
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = sc.nextInt();
}
List<int[]> list = new ArrayList<>();
int i = 0;
int j = 0;
while (i < len) {
while (j < len && a[j] == a[i]) {
j++;
}
if (j - i > 1) {
list.add(new int[]{i, j - 1});
}
i = j;
}
if (list.size() == 0) {
sout(0);
continue;
}
int l = list.get(0)[0];
int r = list.get(list.size() - 1)[1];
// 1123345556
// 111111
int ll = r - l + 1;
if (ll <= 3) {
sout(ll - 2);
} else {
sout(ll - 3);
}
}
}
private void sout(Object o) {
System.out.println(o);
}
} | 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 8 | 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 | 4c11d00aa1f51cfec67d7e8eea16a820 | 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 C {
public static void main(String[] args) {
new C().run();
}
BufferedReader br;
PrintWriter out;
long mod = (long) (1e9 + 7), inf = (long) (3e18);
class pair {
int F, S;
pair(int f, int s) {
F = f; S = s;
}
}
// ---------------------- solve --------------------------
void solve() {
int t=1;
t = ni(); // comment for no test cases
while(t-- > 0) {
//TODO:
int n= ni();
int[] a= na(n);
int i=0, max=(int)1e9, min=0;;
while(i<n-1){
if(a[i]==a[i+1]){
max=Math.min(max, i);
min=Math.max(min, i);
}
i++;
}
if(max==min || max==(int)1e9){
out.println(0);
continue;
}
if(min-max<3){
out.println(1);
}
else{
out.println(min-max-1);
}
}
}
// -------- I/O Template -------------
public long pow(long A, long B, long C) {
if(A==0) return 0;
if(B==0) return 1;
long n=pow(A, B/2, C);
if(B%2==0){
return (long)((n*n)%C + C )%C;
}
else{
return (long)(((n*n)%C * A)%C +C)%C;
}
}
char nc() {
return ns().charAt(0);
}
String nLine() {
try {
return br.readLine();
} catch(IOException e) {
return "-1";
}
}
double nd() {
return Double.parseDouble(ns());
}
long nl() {
return Long.parseLong(ns());
}
int ni() {
return Integer.parseInt(ns());
}
int[] na(int n) {
int a[] = new int[n];
for(int i = 0; i < n; i++) a[i] = ni();
return a;
}
long[] nal(int n) {
long a[] = new long[n];
for(int i = 0; i < n; i++) a[i] = nl();
return a;
}
StringTokenizer ip;
String ns() {
if(ip == null || !ip.hasMoreTokens()) {
try {
ip = new StringTokenizer(br.readLine());
if(ip == null || !ip.hasMoreTokens())
ip = new StringTokenizer(br.readLine());
} catch(IOException e) {
throw new InputMismatchException();
}
}
return ip.nextToken();
}
void run() {
try {
if (System.getProperty("ONLINE_JUDGE") == null) {
br = new BufferedReader(new FileReader("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/input.txt"));
out = new PrintWriter("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/output.txt");
} else {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
} catch (FileNotFoundException e) {
System.out.println(e);
}
solve();
out.flush();
}
} | 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 8 | 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 | a89624bc96aa29b9f41c433f2dab2678 | 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.lang.*;
import java.io.*;
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
try{
FastReader read=new FastReader();
StringBuilder sb = new StringBuilder();
int t=read.nextInt();
while(t>0)
{
int n = read.nextInt();
int a[] = new int[n];
int ans = 0;
for(int i=0;i<n;i++)
{
a[i] = read.nextInt();
}
int count = 0;
for(int i=0;i<n-1;i++)
{
if(a[i]==a[i+1])
count++;
}
int start = -1,end = -1;
for(int i=0;i<n-1;i++)
{
if(a[i]==a[i+1])
{
start = i;
break;
}
}
for(int i=n-1;i>0;i--)
{
if(a[i]==a[i-1])
{
end = i;
break;
}
}
int ans1=0,ans2=0;
if((start==-1 && end==-1) || count<=1)
{
sb.append("0");
}
else
{
end--;
start++;
ans1 = end-start;
if(ans1==0)
ans1++;
sb.append(ans1);
}
sb.append('\n');
t--;
}
System.out.println(sb);
}
catch(Exception e)
{return;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void sort(int[] A) {
int n = A.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = A[i];
int randomPos = i + rnd.nextInt(n - i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(long[] A) {
int n = A.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = A[i];
int randomPos = i + rnd.nextInt(n - i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(ArrayList<Long> A,char ch) {
int n = A.size();
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = A.get(i);
int randomPos = i + rnd.nextInt(n - i);
A.set(i,A.get(randomPos));
A.set(randomPos,tmp);
}
Collections.sort(A);
}
static void sort(ArrayList<Integer> A) {
int n = A.size();
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = A.get(i);
int randomPos = i + rnd.nextInt(n - i);
A.set(i,A.get(randomPos));
A.set(randomPos,tmp);
}
Collections.sort(A);
}
static String sort(String s) {
Character ch[] = new Character[s.length()];
for (int i = 0; i < s.length(); i++) {
ch[i] = s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st = new StringBuffer("");
for (int i = 0; i < s.length(); i++) {
st.append(ch[i]);
}
return st.toString();
}
}
class pair implements Comparable<pair>
{
int X,Y,C;
pair(int s,int e,int c)
{
X=s;
Y=e;
C=c;
}
public int compareTo(pair p )
{
return this.C-p.C;
}
}
| 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 8 | 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 | 47ac0b1a778978d1b285cccd7bb7d0fa | 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.datatransfer.StringSelection;
import java.util.*;
import javax.management.Query;
import java.io.*;
public class practice {
static String s;
static HashMap<Long,Integer>hm;
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[] a=sc.nextIntArray(n);
int init=-1,fin=-1;
for (int i = 0; i <n-1 ; i++) {
if(a[i]==a[i+1]){
init=i+1;
break;
}
}
for (int i =n-1; i >0 ; i--) {
if(a[i]==a[i-1]){
fin=i-1;
break;
}
}
// pw.println(init+" "+fin);
if(init==-1||fin==-1)
pw.println(0);
else if(init>fin)
pw.println(0);
else if(fin-init<2)
pw.println(1);
else
pw.println(fin-init);
}
pw.close();
}
public static int next(long[] arr, int target,int days)
{
// pw.println("days="+days);
int start = 0, end = arr.length-1;
// Minimum size of the array should be 1
// If target lies beyond the max element, than the index of strictly smaller
// value than target should be (end - 1)
if (target >= 0l+arr[end]+1l*(end+1)*days) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
// Move to the left side if the target is smaller
if (0l+arr[mid]+1l*(mid+1)*days > target) {
end = mid - 1;
}
// Move right side
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
public static long factorial(int n){
int y=1;
for (int i = 2; i <=n ; i++) {
y*=i;
}
return y;
}
public static void sort(int[] in) {
shuffle(in);
Arrays.sort(in);
}
public static void shuffle(int[] in) {
for (int i = 0; i < in.length; i++) {
int idx = (int) (Math.random() * in.length);
int tmp = in[i];
in[i] = in[idx];
in[idx] = tmp;
}
}
static LinkedList getfact(int n){
LinkedList<Integer>ll=new LinkedList<>();
LinkedList<Integer>ll2=new LinkedList<>();
for (int i = 1; i <= Math.sqrt(n); i++) {
if(n%i==0) {
ll.add(i);
if(i!=n/i)
ll2.addLast(n/i);
}
}
while (!ll2.isEmpty()){
ll.add(ll2.removeLast());
}
return ll;
}
static void rev(int n){
String s1=s.substring(0,n);
s=s.substring(n);
for (int i = 0; i <n ; i++) {
s=s1.charAt(i)+s;
}
}
static class SegmentTree { // 1-based DS, OOP
int N; //the number of elements in the array as a power of 2 (i.e. after padding)
long[] array, sTree;
Long[]lazy;
SegmentTree(long[] in)
{
array = in; N = in.length - 1;
sTree = new long[N<<1]; //no. of nodes = 2*N - 1, we add one to cross out index zero
lazy = new Long[N<<1];
build(1,1,N);
}
void build(int node, int b, int e) // O(n)
{
if(b == e)
sTree[node] = array[b];
else
{
int mid = b + e >> 1;
build(node<<1,b,mid);
build(node<<1|1,mid+1,e);
sTree[node] = sTree[node<<1]+sTree[node<<1|1];
}
}
void update_point(int index, int val) // O(log n)
{
index += N - 1;
sTree[index] += val;
while(index>1)
{
index >>= 1;
sTree[index] = sTree[index<<1] + sTree[index<<1|1];
}
}
void update_range(int i, int j, int val) // O(log n)
{
update_range(1,1,N,i,j,val);
}
void update_range(int node, int b, int e, int i, int j, int val)
{
if(i > e || j < b)
return;
if(b >= i && e <= j)
{
sTree[node] = (e-b+1)*val;
lazy[node] = val*1l;
}
else
{
int mid = b + e >> 1;
propagate(node, b, mid, e);
update_range(node<<1,b,mid,i,j,val);
update_range(node<<1|1,mid+1,e,i,j,val);
sTree[node] = sTree[node<<1] + sTree[node<<1|1];
}
}
void propagate(int node, int b, int mid, int e)
{
if(lazy[node]!=null) {
lazy[node << 1] = lazy[node];
lazy[node << 1 | 1] = lazy[node];
sTree[node << 1] = (mid - b + 1) * lazy[node];
sTree[node << 1 | 1] = (e - mid) * lazy[node];
}
lazy[node] = null;
}
long query(int i, int j)
{
return query(1,1,N,i,j);
}
long query(int node, int b, int e, int i, int j) // O(log n)
{
if(i>e || j <b)
return 0;
if(b>= i && e <= j)
return sTree[node];
int mid = b + e >> 1;
propagate(node, b, mid, e);
long q1 = query(node<<1,b,mid,i,j);
long q2 = query(node<<1|1,mid+1,e,i,j);
return q1 + q2;
}
}
// public static long dp(int idx) {
// if (idx >= n)
// return Long.MAX_VALUE/2;
// return Math.min(dp(idx+1),memo[idx]+dp(idx+k));
// }
// if(num==k)
// return dp(0,idx+1);
// if(memo[num][idx]!=-1)
// return memo[num][idx];
// long ret=0;
// if(num==0) {
// if(s.charAt(idx)=='a')
// ret= dp(1,idx+1);
// else if(s.charAt(idx)=='?') {
// ret=Math.max(1+dp(1,idx+1),dp(0,idx+1) );
// }
// }
// else {
// if(num%2==0) {
// if(s.charAt(idx)=='a')
// ret=dp(num+1,idx+1);
// else if(s.charAt(idx)=='?')
// ret=Math.max(1+dp(num+1,idx+1),dp(0,idx+1));
// }
// else {
// if(s.charAt(idx)=='b')
// ret=dp(num+1,idx+1);
// else if(s.charAt(idx)=='?')
// ret=Math.max(1+dp(num+1,idx+1),dp(0,idx+1));
// }
// }
// }
static void putorrem(long x){
if(hm.getOrDefault(x,0)==1){
hm.remove(x);
}
else
hm.put(x,hm.getOrDefault(x,0)-1);
}
public static int max4(int a,int b, int c,int d) {
int [] s= {a,b,c,d};
Arrays.sort(s);
return s[3];
}
public static double logbase2(int k) {
return( (Math.log(k)+0.0)/Math.log(2));
}
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 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();
}
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
public tuble add(tuble t){
return new tuble(this.x+t.x,this.y+t.y,this.z+t.z);
}
}
static long mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
} | 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 8 | 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 | d19ededaddc111aac09eef89f60ec5dd | 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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CUnequalArray solver = new CUnequalArray();
solver.solve(1, in, out);
out.close();
}
static class CUnequalArray {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
int[] a = new int[n + 1];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
a[n] = -1;
int num = 0;
int p = 0;
int start = -1;
int end = -1;
for (int i = 1; i <= n; i++) {
if (a[i] == a[i - 1]) {
} else {
if (i - 1 != p) {
num++;
if (start == -1) {
start = p;
}
end = i - 1;
}
p = i;
}
}
if (num == 0) {
out.println(0);
} else {
int k = end - start - 2;
if (k == 0) k = 1;
else if (k < 0) k = 0;
out.println(k);
}
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
}
public String next() {
while (st == null || (!st.hasMoreElements())) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["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 8 | 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 | 7b006e7993a53ad43787d6545b66caf1 | 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.*;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
public class Main {
private final static long mod = 1000000007;
private final static int MAXN = 1000001;
private static long power(long x, long y, long m) {
long temp;
if (y == 0)
return 1;
temp = power(x, y / 2, m);
temp = (temp * temp) % m;
if (y % 2 == 0)
return temp;
else
return ((x % m) * temp) % m;
}
private static long power(long x, long y) {
return power(x, y, mod);
}
private static long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static int nextPowerOf2(int a) {
return 1 << nextLog2(a);
}
static int nextLog2(int a) {
return (a == 0 ? 0 : 32 - Integer.numberOfLeadingZeros(a - 1));
}
private static long modInverse(long a, long m) {
long m0 = m;
long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
long q = a / m;
long t = m;
m = a % m;
a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
private static int[] getLogArr(int n) {
int arr[] = new int[n + 1];
for (int i = 1; i < n + 1; i++) {
arr[i] = (int) (Math.log(i) / Math.log(2) + 1e-10);
}
return arr;
}
private static int log[] = getLogArr(MAXN);
private static int getLRSpt(int st[][], int L, int R, BinaryOperator<Integer> binaryOperator) {
int j = log[R - L + 1];
return binaryOperator.apply(st[L][j], st[R - (1 << j) + 1][j]);
}
private static int[][] getSparseTable(int array[], BinaryOperator<Integer> binaryOperator) {
int k = log[array.length + 1] + 1;
int st[][] = new int[array.length + 1][k + 1];
for (int i = 0; i < array.length; i++)
st[i][0] = array[i];
for (int j = 1; j <= k; j++) {
for (int i = 0; i + (1 << j) <= array.length; i++) {
st[i][j] = binaryOperator.apply(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
return st;
}
private static int[][] getSparseTable(List<Integer> list, BinaryOperator<Integer> binaryOperator) {
int n=list.size();
int k = log[n + 1] + 1;
int st[][] = new int[n + 1][k + 1];
for (int i = 0; i < n; i++)
st[i][0] = list.get(i);
for (int j = 1; j <= k; j++) {
for (int i = 0; i + (1 << j) <= n; i++) {
st[i][j] = binaryOperator.apply(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
return st;
}
static class Subset {
int parent;
int rank;
@Override
public String toString() {
return "" + parent;
}
}
static int find(Subset[] Subsets, int i) {
if (Subsets[i].parent != i)
Subsets[i].parent = find(Subsets, Subsets[i].parent);
return Subsets[i].parent;
}
static void union(Subset[] Subsets, int x, int y) {
int xroot = find(Subsets, x);
int yroot = find(Subsets, y);
if (Subsets[xroot].rank < Subsets[yroot].rank)
Subsets[xroot].parent = yroot;
else if (Subsets[yroot].rank < Subsets[xroot].rank)
Subsets[yroot].parent = xroot;
else {
Subsets[xroot].parent = yroot;
Subsets[yroot].rank++;
}
}
private static int maxx(Integer... a) {
return Collections.max(Arrays.asList(a));
}
private static int minn(Integer... a) {
return Collections.min(Arrays.asList(a));
}
private static long maxx(Long... a) {
return Collections.max(Arrays.asList(a));
}
private static long minn(Long... a) {
return Collections.min(Arrays.asList(a));
}
private static class Pair<T extends Comparable<T>, U extends Comparable<U>> implements Comparable<Pair<T, U>> {
T a;
U b;
public Pair(T a, U b) {
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return a.equals(pair.a) &&
b.equals(pair.b);
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
@Override
public String toString() {
return "(" + a +
"," + b +
')';
}
@Override
public int compareTo(Pair<T, U> o) {
return (this.a.equals(o.a) ? this.b.equals(o.b) ? 0 : this.b.compareTo(o.b) : this.a.compareTo(o.a));
}
}
public static int upperBound(List<Integer> list, int value) {
int low = 0;
int high = list.size();
while (low < high) {
final int mid = (low + high) / 2;
if (value >= list.get(mid)) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
private static int[] getLPSArray(String pattern) {
int i, j, n = pattern.length();
int[] lps = new int[pattern.length()];
lps[0] = 0;
for (i = 1, j = 0; i < n; ) {
if (pattern.charAt(i) == pattern.charAt(j)) {
lps[i++] = ++j;
} else if (j > 0) {
j = lps[j - 1];
} else {
lps[i++] = 0;
}
}
return lps;
}
private static List<Integer> findPattern(String text, String pattern) {
List<Integer> matchedIndexes = new ArrayList<>();
if (pattern.length() == 0) {
return matchedIndexes;
}
int[] lps = getLPSArray(pattern);
int i = 0, j = 0, n = text.length(), m = pattern.length();
while (i < n) {
if (text.charAt(i) == pattern.charAt(j)) {
i++;
j++;
}
if (j == m) {
matchedIndexes.add(i - j);
j = lps[j - 1];
}
if (i < n && text.charAt(i) != pattern.charAt(j)) {
if (j > 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
return matchedIndexes;
}
private static Set<Long> getDivisors(long n) {
Set<Long> divisors = new HashSet<>();
divisors.add(1L);
divisors.add(n);
for (long i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
divisors.add(i);
divisors.add(n / i);
}
}
return divisors;
}
private static long getLCM(long a, long b) {
return (Math.max(a, b) / gcd(a, b)) * Math.min(a, b);
}
static long fac[] = new long[2000005];
static long ifac[] = new long[2000005];
private static void preCompute(int n) {
fac = new long[n + 1];
ifac = new long[n + 1];
fac[0] = ifac[0] = fac[1] = ifac[1] = 1;
int i;
for (i = 2; i < n + 1; i++) {
fac[i] = (i * fac[i - 1]) % mod;
ifac[i] = (power(i, mod - 2) * ifac[i - 1]) % mod;
}
}
private static long C(int n, int r) {
if (n < 0 || r < 0) return 1;
if (r > n) return 1;
return (fac[n] * ((ifac[r] * ifac[n - r]) % mod)) % mod;
}
private boolean isValid(char board[][], int r1, int c1, int r2, int c2) {
Map<Character, Integer> cntMap = new HashMap<>();
for (int i = r1; i <= r2; i++) {
for (int j = c1; j <= c2; j++) {
char c = board[i][j];
if (c != '.' && (c < '1' || c > '9')) {
return false;
}
int v = cntMap.getOrDefault(c, 0);
if (c != '.' && v > 1) return false;
cntMap.put(c, v + 1);
}
}
return true;
}
public boolean isValidSudoku(char[][] board) {
for (int i = 0; i < 9; i++) {
if (!isValid(board, i, 0, i, 8)) {
return false;
}
}
for (int j = 0; j < 9; j++) {
if (!isValid(board, 0, j, 8, j)) {
return false;
}
}
for (int i = 0; i < 9; i += 3) {
for (int j = 0; j < 9; j += 3) {
if (!isValid(board, i, j, i + 2, j + 2)) {
return false;
}
}
}
return true;
}
static long getSum(long BITree[], int index) {
long sum = 0;
index = index + 1;
while (index > 0) {
index -= index & (-index);
}
return sum;
}
public static void updateBIT(long BITree[], int index, long val) {
index = index + 1;
while (index <= BITree.length - 1) {
BITree[index] += val;
index += index & (-index);
}
}
long[] constructBITree(int arr[], int m) {
int n = arr.length;
long BITree[] = new long[m + 1];
for (int i = 1; i <= n; i++)
BITree[i] = 0;
for (int i = 0; i < n; i++)
updateBIT(BITree, i, arr[i]);
return BITree;
}
private static int upperBound(int a[], int l, int r, int x) {
if (l > r) return -1;
if (l == r) {
if (a[l] <= x) {
return -1;
} else {
return a[l];
}
}
int m = (l + r) / 2;
if (a[m] <= x) {
return upperBound(a, m + 1, r, x);
} else {
return upperBound(a, l, m, x);
}
}
private static void mul(long A[][], long B[][]) {
int i, j, k, n = A.length;
long C[][] = new long[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < n; k++) {
C[i][j] = (C[i][j] + (A[i][k] * B[k][j]) % mod) % mod;
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
A[i][j] = C[i][j];
}
}
}
private static void power(long A[][], long base[][], long n) {
if (n < 2) {
return;
}
power(A, base, n / 2);
mul(A, A);
if (n % 2 == 1) {
mul(A, base);
}
}
private static void print(int... a) {
System.out.println(Arrays.toString(a));
}
private static int[] get3(int a[], int i, int si) {
int ret[] = new int[3];
ret[2] = Integer.MIN_VALUE;
for (int j = i, k = 0; j < a.length && k < 3; j++) {
if (si == j) continue;
ret[k++] = a[j];
}
return ret;
}
static void reverse(int a[], int l, int r) {
while (l < r) {
int t = a[l];
a[l] = a[r];
a[r] = t;
l++;
r--;
}
}
public void rotate(int[] nums, int k) {
k = k % nums.length;
reverse(nums, 0, nums.length - k - 1);
reverse(nums, nums.length - k, nums.length - 1);
reverse(nums, 0, nums.length - 1);
}
private static boolean isSorted(int a[]) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) return false;
}
return true;
}
private static long getMoves(int a[],int idx){
long ans=0;
long prev=0;
for(int i=idx+1;i<a.length;i++){
long d=prev/a[i]+1;
ans+=d;
prev=d*a[i];
}
prev=0;
for(int i=idx-1;i>=0;i--){
long d=-prev/a[i]+1;
ans+=d;
prev=-d*a[i];
}
return ans;
}
public static void main(String[] args) throws Exception {
long START_TIME = System.currentTimeMillis();
try (FastReader in = new FastReader();
FastWriter out = new FastWriter()) {
int n, t, i, j, f, k, m, ti, tidx, gm, q, g, l, r;
for (t = in.nextInt(), tidx = 1; tidx <= t; tidx++)
{
long x = 0, y = 0, z = 0, sum = 0, bhc = 0, ans = Long.MAX_VALUE;
//out.print(String.format("Case #%d: ", tidx));
n=in.nextInt();
int a[]=in.nextIntArr(n);
l=-1;
r=n;
for(i=1;i<n;i++){
if(a[i]==a[i-1]){
l=i;
break;
}
}
for(i=n-1;i>0;i--){
if(a[i]==a[i-1]){
r=i-1;
break;
}
}
if(l==-1||l>r){
out.println(0);
} else {
if(r==l)r++;
m=r-l;
out.println(m);
}
}
/*if (args.length > 0 && "ex_time".equals(args[0])) {
out.print("\nTime taken: ");
out.println(System.currentTimeMillis() - START_TIME);
}*/
out.commit();
}
}
public static class FastReader implements Closeable {
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer st;
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
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[] nextIntArr(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
Integer[] nextIntegerArr(int n) {
Integer[] arr = new Integer[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
double[] nextDoubleArr(int n) {
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = nextDouble();
}
return arr;
}
long[] nextLongArr(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
List<Long> nextLongList(int n) {
List<Long> retList = new ArrayList<>();
for (int i = 0; i < n; i++) {
retList.add(nextLong());
}
return retList;
}
String[] nextStrArr(int n) {
String[] arr = new String[n];
for (int i = 0; i < n; i++) {
arr[i] = next();
}
return arr;
}
int[][] nextIntArr2(int n, int m) {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
arr[i] = nextIntArr(m);
}
return arr;
}
long[][] nextLongArr2(int n, int m) {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++) {
arr[i] = nextLongArr(m);
}
return arr;
}
@Override
public void close() throws IOException {
br.close();
}
}
public static class FastWriter implements Closeable {
BufferedWriter bw;
StringBuilder sb = new StringBuilder();
List<String> list = new ArrayList<>();
Set<String> set = new HashSet<>();
public FastWriter() {
bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
<T> void commit() throws IOException {
bw.write(sb.toString());
bw.flush();
sb = new StringBuilder();
}
public <T> void print(T obj) throws IOException {
sb.append(obj.toString());
}
public void println() throws IOException {
print("\n");
}
public <T> void println(T obj) throws IOException {
print(obj.toString() + "\n");
}
<T> void printArrLn(T[] arr) throws IOException {
for (int i = 0; i < arr.length - 1; i++) {
print(arr[i] + " ");
}
println(arr[arr.length - 1]);
}
<T> void printArr2(T[][] arr) throws IOException {
for (int j = 0; j < arr.length; j++) {
for (int i = 0; i < arr[j].length - 1; i++) {
print(arr[j][i] + " ");
}
println(arr[j][arr[j].length - 1]);
}
}
<T> void printColl(Collection<T> coll) throws IOException {
List<String> stringList = coll.stream().map(e -> ""+e).collect(Collectors.toList());
println(String.format("{%s}",String.join(",", stringList)));
}
void printCharN(char c, int n) throws IOException {
for (int i = 0; i < n; i++) {
print(c);
}
}
void printIntArr2(int[][] arr) throws IOException {
for (int j = 0; j < arr.length; j++) {
for (int i = 0; i < arr[j].length - 1; i++) {
print(arr[j][i] + " ");
}
println(arr[j][arr[j].length - 1]);
}
}
@Override
public void close() throws IOException {
bw.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 8 | 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 | 9adac22d4abc5f428cae54cfd06a852b | 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.*;
import java.math.*;
/**
* @author Naitik
*
*/
public class Main
{
static FastReader sc=new FastReader();
static long dp[][];
// static boolean v[][][];
static int mod=998244353;;
// static int mod=1000000007;
static int max;
static int bit[];
//static long fact[];
static HashMap<Long,Integer> map;
//static StringBuffer sb=new StringBuffer("");
//static HashMap<Integer,Integer> map;
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args)
{
// StringBuffer sb=new StringBuffer("");
int ttt=1;
ttt =i();
outer :while (ttt-- > 0)
{
int n=i();
int A[]=input(n);
int ans=0;
for(int i=0;i<n-1;i++) {
if(A[i]==A[i+1])
ans++;
}
if(ans<2) {
System.out.println("0");
continue outer;
}
int last=-1;
for(int i=n-1;i>0;i--){
if(A[i]==A[i-1]) {
last=i-1;
break;
}
}
int first=-1;
for(int i=0;i<n-1;i++) {
if(A[i]==A[i+1])
{
first=i+1;
break;
}
}
int ind=-1;
ans=0;
for(int i=0;i<n-1;i++) {
if(A[i]==A[i+1]) {
ind=i+1;
break;
}
}
ans=last-ind;
ind=-1;
for(int i=n-1;i>0;i--) {
if(A[i]==A[i-1]) {
ind=i-1;
break;
}
}
int len=ind-first;
System.out.println(max(min(len,ans),1));
}
//System.out.println(sb.toString());
out.close();
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1
}
static class Pair implements Comparable<Pair>
{
int x;
int y;
// int z;
Pair(int x,int y){
this.x=x;
this.y=y;
// this.z=z;
}
@Override
public int compareTo(Pair o) {
if(this.x>o.x)
return 1;
else if(this.x<o.x)
return -1;
else {
if(this.y>o.y)
return 1;
else if(this.y<o.y)
return -1;
else
return 0;
}
}
public int hashCode()
{
final int temp = 14;
int ans = 1;
ans =x*31+y*13;
return ans;
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (this.getClass() != o.getClass()) {
return false;
}
Pair other = (Pair)o;
if (this.x != other.x || this.y!=other.y) {
return false;
}
return true;
}
//
/* FOR TREE MAP PAIR USE */
// public int compareTo(Pair o) {
// if (x > o.x) {
// return 1;
// }
// if (x < o.x) {
// return -1;
// }
// if (y > o.y) {
// return 1;
// }
// if (y < o.y) {
// return -1;
// }
// return 0;
// }
}
static int find(int A[],int a) {
if(A[a]==a)
return a;
return A[a]=find(A, A[a]);
}
//static int find(int A[],int a) {
// if(A[a]==a)
// return a;
// return find(A, A[a]);
//}
//FENWICK TREE
static void update(int i, int x){
for(; i < bit.length; i += (i&-i))
bit[i] += x;
}
static int sum(int i){
int ans = 0;
for(; i > 0; i -= (i&-i))
ans += bit[i];
return ans;
}
//END
static void add(long v) {
if(!map.containsKey(v)) {
map.put(v, 1);
}
else {
map.put(v, map.get(v)+1);
}
}
static void remove(long v) {
if(map.containsKey(v)) {
map.put(v, map.get(v)-1);
if(map.get(v)==0)
map.remove(v);
}
}
public static int upper(int A[],int k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
ans=mid;
l=mid+1;
}
else {
u=mid-1;
}
}
return ans;
}
public static int lower(int A[],int k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
l=mid+1;
}
else {
ans=mid;
u=mid-1;
}
}
return ans;
}
static int[] copy(int A[]) {
int B[]=new int[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static long[] copy(long A[]) {
long B[]=new long[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static void reverse(long A[]) {
int n=A.length;
long B[]=new long[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void reverse(int A[]) {
int n=A.length;
int B[]=new int[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void input(int A[],int B[]) {
for(int i=0;i<A.length;i++) {
A[i]=sc.nextInt();
B[i]=sc.nextInt();
}
}
static int[][] input(int n,int m){
int A[][]=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
A[i][j]=i();
}
}
return A;
}
static char[][] charinput(int n,int m){
char A[][]=new char[n][m];
for(int i=0;i<n;i++) {
String s=s();
for(int j=0;j<m;j++) {
A[i][j]=s.charAt(j);
}
}
return A;
}
static int nextPowerOf2(int n)
{
if(n==0)
return 1;
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
static int highestPowerof2(int x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static long highestPowerof2(long x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static int max(int A[]) {
int max=Integer.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static int min(int A[]) {
int min=Integer.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long max(long A[]) {
long max=Long.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static long min(long A[]) {
long min=Long.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long [] prefix(long A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] prefix(int A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] suffix(long A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static long [] suffix(int A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static void fill(int dp[]) {
Arrays.fill(dp, -1);
}
static void fill(int dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(int dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(int dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static void fill(long dp[]) {
Arrays.fill(dp, -1);
}
static void fill(long dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(long dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(long dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long power(long x, long y, long p)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long power(long x, long y)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void print(int A[]) {
for(int i : A) {
out.print(i+" ");
}
out.println();
}
static void print(long A[]) {
for(long i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static void sort(int[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(long[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static String sort(String s) {
Character ch[]=new Character[s.length()];
for(int i=0;i<s.length();i++) {
ch[i]=s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st=new StringBuffer("");
for(int i=0;i<s.length();i++) {
st.append(ch[i]);
}
return st.toString();
}
static HashMap<Integer,Integer> hash(int A[]){
HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static HashMap<Long,Integer> hash(long A[]){
HashMap<Long,Integer> map=new HashMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Integer,Integer> tree(int A[]){
TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Long,Integer> tree(long A[]){
TreeMap<Long,Integer> map=new TreeMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\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 8 | 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 | 99076ee9764af2c8f5dda958caf930de | 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.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Practice2 {
//
// static class Pair{
// int val;
// int data;
// Pair(int val,int data){
// this.val=val;
// this.data=data;
// }
// }
//
//
public static void printarr(int[] arr) {
int n=arr.length;
for(int i=0;i<n;i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
// /** Code for Dijkstra's algorithm **/
public static class ListNode {
int vertex, weight;
ListNode(int v,int w) {
vertex = v;
weight = w;
}
int getVertex() { return vertex; }
int getWeight() { return weight; }
}
public static int[] dijkstra(
int V, HashMap<Integer,ArrayList<ListNode> > graph,
int source) {
int[] distance = new int[V];
for (int i = 0; i < V; i++)
distance[i] = Integer.MAX_VALUE;
distance[0] = 0;
PriorityQueue<ListNode> pq = new PriorityQueue<>(
(v1, v2) -> v1.getWeight() - v2.getWeight());
pq.add(new ListNode(source, 0));
while (pq.size() > 0) {
ListNode current = pq.poll();
for (ListNode n :
graph.get(current.getVertex())) {
if (distance[current.getVertex()]
+ n.getWeight()
< distance[n.getVertex()]) {
distance[n.getVertex()]
= n.getWeight()
+ distance[current.getVertex()];
pq.add(new ListNode(
n.getVertex(),
distance[n.getVertex()]));
}
}
}
// If you want to calculate distance from source to
// a particular target, you can return
// distance[target]
return distance;
}
//Methos to return all divisor of a number
static ArrayList<Long> allDivisors(long n) {
ArrayList<Long> al=new ArrayList<>();
long i=2;
while(i*i<=n) {
if(n%i==0) al.add(i);
if(n%i==0&&i*i!=n) al.add(n/i);
i++;
}
return al;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int find(boolean[] vis,int x,int y,int n,int m) {
if(x<0||y<0||x>=n||y>=m) return 0;
// if(vis[i][j]==true) return
return 0;
}
static long power(long N,long R)
{
long x=998244353;
if(R==0) return 1;
if(R==1) return N;
long temp= power(N,R/2)%x; // (a*b)%p = (a%p*b%p)*p
temp=(temp*temp)%x;
if(R%2==0){
return temp%x;
}else{
return (N*temp)%x;
}
}
static int[] sort(int[] arr) {
int n=arr.length;
ArrayList<Integer> al=new ArrayList<>();
for(int i=0;i<n;i++) {
al.add(arr[i]);
}
Collections.sort(al);
for(int i=0;i<n;i++) {
arr[i]=al.get(i);
}
return arr;
}
static long[] sort(long[] arr) {
long n=arr.length;
ArrayList<Long> al=new ArrayList<>();
for(int i=0;i<n;i++) {
al.add(arr[i]);
}
Collections.sort(al);
for(int i=0;i<n;i++) {
arr[i]=al.get(i);
}
return arr;
}
static class Pair{
int val;
int res;
Pair(int val,int res){
this.val=val;
this.res=res;
}
}
public static long find(ArrayList<Long> al,ArrayList<Integer> li,int ind) {
int n=al.size();
if(n==0) return 0;
//System.out.println("al : "+al);
ArrayList<Long> one=new ArrayList<>();
ArrayList<Long> zero=new ArrayList<>();
int a=li.get(ind);
for(Long i: al) {
if((i&(1<<a))==0) {
zero.add(i);
}else {
one.add(i);
}
}
//System.out.println("al : "+al);
if(ind==li.size()-1) {
long size1=one.size();
long size2=zero.size();
return size1*size1+size2*size2;
}else {
return find(one,li,ind+1)+find(zero,li,ind+1);
}
}
public static void main (String[] args) {
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
// out.print();
//out.println();
FastReader sc=new FastReader();
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
int count=0;
for(int i=0;i<n-1;i++) {
if(arr[i]==arr[i+1]) {
count++;
}
}
if(count<=1) {
out.println(0);
continue;
}
int pos=n-1;
for(int i=n-1;i>0;i--) {
if(arr[i]==arr[i-1]) {
pos=i-1;
break;
}
}
int i=0;
while(i+1<=n-1) {
if(arr[i]==arr[i+1]) {
i++;
break;
}
i++;
}
if(i==pos) {
out.println(1);
}else {
int ans=pos-i;
out.println(ans);
}
}
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["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 8 | 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 | 9cb9fea04613fc95ca6c68270b1a8de8 | 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 A {
static int n,k;
static long ans;
static ArrayList<Integer>[] g;
static boolean[] vis;
static int[][] binLift;
static int[] depth;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
// Scanner sc = new Scanner(new File("consistency_chapter_1_input.txt"));
// PrintWriter pw = new PrintWriter("A_out.txt");
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 s = -1;
int e = -1;
for (int i = 1; i < n; i++) {
if(a[i]==a[i-1]){
s=i;
break;
}
}
for (int i = n-1; i > 0; i--) {
if(a[i]==a[i-1]){
e=i-1;
break;
}
}
// pw.println(s+" "+ e);
if(s==-1 || s>e)
pw.println(0);
else
pw.println(s==e?1:e-s);
}
pw.flush();
}
static int mod = 998244353;
static int modPow(int a, int e, int mod) // O(log e)
{
a %= mod;
int res = 1;
while (e > 0) {
if ((e & 1) == 1)
res = (res * a) % mod;
a = (a * a) % mod;
e >>= 1;
}
return res;
}
static class pair implements Comparable<pair> {
int x, y;
public pair(int u, int s) {
x = u;
y = s;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return x + " " + y;
}
@Override
public int compareTo(pair o) {
// TODO Auto-generated method stub
return x - o.x;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(File s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | Java | ["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 8 | 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 | 03b2fe5ef08cb9c3ebd012cb996a2aa3 | 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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, FastScanner in, PrintWriter out) {
int numTests = in.nextInt();
for (int test = 0; test < numTests; test++) {
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
boolean[] pref = new boolean[n];
for (int i = 1; i < n; i++) {
pref[i] = pref[i - 1];
if (a[i] == a[i - 1]) {
pref[i] = true;
}
}
boolean[] suff = new boolean[n];
for (int i = n - 2; i >= 0; i--) {
suff[i] = suff[i + 1];
if (a[i] == a[i + 1]) {
suff[i] = true;
}
}
int l = 0;
while (l < n && !pref[l]) {
++l;
}
int r = n - 1;
while (r >= 0 && !suff[r]) {
--r;
}
int ans;
if (l < r) {
ans = r - l;
} else if (l == r) {
ans = 1;
} else {
ans = 0;
}
out.println(ans);
}
}
}
static class FastScanner {
private BufferedReader in;
private StringTokenizer st;
public FastScanner(InputStream stream) {
try {
in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
} catch (Exception e) {
throw new AssertionError();
}
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String rl = in.readLine();
if (rl == null) {
return null;
}
st = new StringTokenizer(rl);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(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 8 | 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 | a479a5fce990e3d6d8dee147427a20ac | 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.StringTokenizer;
public class C {
static int numEquality= -10;
static int x = -10;
public static void main(String[] args) throws NumberFormatException, IOException {
MyReader reader = new MyReader();
int t = reader.nextInt();
for(int testcase=0; testcase<t; testcase++) {
numEquality = 0;
x = 1_000_000_010;
int n = reader.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++) {
arr[i] = reader.nextInt();
}
int firstEquality = -1;
for(int i=0; i<n-1; i++) {
if(arr[i] == arr[i+1]) {
numEquality++;
if(firstEquality == -1) firstEquality = i;
}
}
if(numEquality == 0) System.out.println(0);
else {
int numFlips = 0;
int i = firstEquality+1;
while(numEquality >= 2) {
flip(arr, i);
numFlips++;
i++;
}
System.out.println(numFlips);
}
}
}
public static void flip(int[] arr, int i) {
if(arr[i-1] == arr[i]) numEquality--;
if(arr[i] == arr[i+1]) numEquality--;
if(i+2<arr.length && arr[i+1] == arr[i+2]) numEquality--;
numEquality++;
arr[i] = x;
arr[i+1] = x;
x++;
}
static class MyReader {
BufferedReader br;
StringTokenizer st;
public MyReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(next());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(next());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(next());
}
String nextLine() throws IOException {
return br.readLine();
}
}
}
| 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 8 | 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 | 91a1ccc5ed659396660951abc6215b20 | 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 Main2 {
static long mod = 998244353;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int test = in.nextInt();
for(int t = 1; t<=test;t++) {
int n = in.nextInt();
int a[] = new int[n];
int prev = -1;
int left = Integer.MAX_VALUE,right = Integer.MIN_VALUE;
//HashSet<Integer> set = new HashSet<>();
int count = 0;
for(int i = 0;i<n;i++){
a[i] = in.nextInt();
// set.add(a[i]);
if(a[i]==prev){
left = Math.min(i+1,left);
right = Math.max(right,i);
count++;
}
prev=a[i];
}
if(count<=1){
pw.println(0);
}
else {
if (n < 3) {
pw.println(0);
}
else if (n == 3) {
if (a[0] == a[1] && a[1] == a[2]) {
pw.println(1);
} else {
pw.println(0);
}
} else {
if (left == right || left + 1 == right) {
pw.println(1);
} else {
pw.println(right - left);
}
}
// debug(left+" "+right);
}
}
pw.close();
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
}
| 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 8 | 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 | e980804165312d2f6931a7077d82e659 | 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.*;
import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.System.in;
import static java.lang.System.out;
public class Main{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(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 parseInt(next());
}
long nextLong() {
return parseLong(next());
}
double nextDouble() {
return parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(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 close() throws IOException {
bw.close();
}
}
public static void main(String[] args) {
try {
FastReader in = new FastReader();
FastWriter out = new FastWriter();
int testCases = in.nextInt();
while (testCases-- > 0) {
// write code here
int n = in.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextLong();
}
int f =-1 ,s=-1;
for(int i =0 ; i<n-1 ; i++){
if(arr[i] == arr[i+1]){
if(f==-1){
f=i;
}
else{
s=i;
}
}
}
if(f==-1 || s==-1){
System.out.println(0);
}
else if(f+1==s){
System.out.println(1);
}
else{
System.out.println(s-f-1);
}
}
out.close();
} catch (Exception e) {
return;
}
}
} | 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 8 | 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 | c600b133885019d80923f2e0bdab92b8 | 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 Main {
public static void main(String[] args) {
//4
//5
//1 1 1 1 1
//5
//2 1 1 1 2
//6
//1 1 2 3 3 4
//6
//1 2 1 4 5 4
Scanner sc = new Scanner(System.in);
int cnt = sc.nextInt();
for(int i = 0; i < cnt; i++){
int[] item = new int[sc.nextInt()];
Arrays.setAll(item,t->sc.nextInt());
int a = -1;
int b = -1;
int count = 0;
for(int j = 0;j < item.length-1; j++){
if(item[j]==item[j+1]){
++count;
if(a==-1) a = j+1;
b = j;
}
}
if(count<=1){
System.out.println(0);
continue;
}
System.out.println(Math.max(b-a,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 8 | 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 | 1d8b1174992214242d9e2529665fa8d5 | 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 Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(in.readLine());
while(t-- > 0)
{
int n = Integer.parseInt(in.readLine());
String[] sArray = in.readLine().split(" ");
long start = -1L;
long end = -1L;
//System.out.println(Arrays.toString(arr));
long i = 0L;
String pre = "";
for(String s : sArray)
{
if(i == 0)
{
i++;
pre = s;
continue;
}
if(s.equals(pre))
{
if(start == -1L)
{
start = i;
end = start-1L;
}
else{
end = i - 1L;
}
}
pre = s;
i++;
}
//System.out.println(start+" "+end);
if(start > end || start == -1)
{
out.write("0\n");
}
else if( start == end)
{
out.write("1\n");
}
else{
long r = end - start;
out.write(r+"\n");
}
out.flush();
}
}
}
| 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 8 | 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 | cf4ac7f1101a053eb484e39d6945830a | 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 C {
public static void main(String[] args) throws IOException, InterruptedException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
int n= sc.nextInt();
int ans=0;
int [] arr= new int [n];
for(int i=0;i<n;i++) arr[i]=sc.nextInt();
int c=0;
for(int i=0;i+1<n;i++) if(arr[i]==arr[i+1]) c++;
if(c>1) {
c=-1;
int z=-1;
for(int i=0;i+1<n;i++) {
if(arr[i]==arr[i+1]) {
if(c!=-1) {
z=i;
}
else c=i+1;
}
}
ans=z-c;
if(ans==0) ans++;
}
pw.println(ans);
}
pw.flush();
}
}
class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
} | Java | ["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 8 | 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 | 3b0c591b75401b0281a54ab9e3162b28 | 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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
if (st.hasMoreTokens()) {
str = st.nextToken("\n");
} else {
str = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader scn = new FastReader();
int t = scn.nextInt();
while (t-- > 0) {
int n = scn.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = scn.nextInt();
int i = -1, j = -1;
for(int k = 0; k < n-1; k++){
if(arr[k] == arr[k+1]){
if(i == -1){
i = k;
}
j = k;
}
}
System.out.println(i == j ? 0 : Math.max(1, j-i-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 8 | 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 | 96b97caba562fa3c118194d7d5f30c0c | 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.concurrent.CountDownLatch;
import javax.lang.model.element.QualifiedNameable;
import javax.swing.ViewportLayout;
import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction;
import javax.swing.plaf.metal.MetalComboBoxUI.MetalPropertyChangeListener;
import javax.swing.text.html.HTMLDocument.HTMLReader.CharacterAction;
import org.w3c.dom.Node;
import java.lang.*;
import java.math.BigInteger;
import java.net.CookieHandler;
import java.security.cert.CollectionCertStoreParameters;
import java.text.BreakIterator;
import java.io.*;
@SuppressWarnings("unchecked")
public class Main {
static FastReader in;
static PrintWriter out;
static int bit(long n) {
return (n == 0) ? 0 : (1 + bit(n & (n - 1)));
}
static void p(Object o) {
out.print(o);
}
static void pn(Object o) {
out.println(o);
}
static void pni(Object o) {
out.println(o);
out.flush();
}
static String n() throws Exception {
return in.next();
}
static String nln() throws Exception {
return in.nextLine();
}
static int ni() throws Exception {
return Integer.parseInt(in.next());
}
static long nl() throws Exception {
return Long.parseLong(in.next());
}
static double nd() throws Exception {
return Double.parseDouble(in.next());
}
static class FastReader {
static BufferedReader br;
static 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;
}
}
static long power(long a, long b) {
if (a == 0L)
return 0L;
if (b == 0)
return 1;
long val = power(a, b / 2);
val = val * val;
if ((b % 2) != 0)
val = val * a;
return val;
}
static long power(long a, long b, long mod) {
if (a == 0L)
return 0L;
if (b == 0)
return 1;
long val = power(a, b / 2L, mod) % mod;
val = (val * val) % mod;
if ((b % 2) != 0)
val = (val * a) % mod;
return val;
}
static ArrayList<Long> prime_factors(long n) {
ArrayList<Long> ans = new ArrayList<Long>();
while (n % 2 == 0) {
ans.add(2L);
n /= 2L;
}
for (long i = 3; i <= Math.sqrt(n); i++) {
while (n % i == 0) {
ans.add(i);
n /= i;
}
}
if (n > 2) {
ans.add(n);
}
return ans;
}
static void sort(ArrayList<Long> a) {
Collections.sort(a);
}
static void reverse_sort(ArrayList<Long> a) {
Collections.sort(a, Collections.reverseOrder());
}
static void swap(long[] a, int i, int j) {
long temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void swap(List<Long> a, int i, int j) {
long temp = a.get(i);
a.set(j, a.get(i));
a.set(j, temp);
}
static void sieve(boolean[] prime) {
int n = prime.length - 1;
Arrays.fill(prime, true);
for (int i = 2; i * i <= n; i++) {
if (prime[i]) {
for (int j = i * i; j <= n; j += i) {
prime[j] = false;
}
}
}
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
public static void sort(long[] arr, int l, int r) {
if (l >= r)
return;
int mid = (l + r) / 2;
sort(arr, l, mid);
sort(arr, mid + 1, r);
merge(arr, l, mid, r);
}
public static void sort(int[] arr, int l, int r) {
if (l >= r)
return;
int mid = (l + r) / 2;
sort(arr, l, mid);
sort(arr, mid + 1, r);
merge(arr, l, mid, r);
}
static void merge(int[] arr, int l, int mid, int r) {
int[] left = new int[mid - l + 1];
int[] right = new int[r - mid];
for (int i = l; i <= mid; i++) {
left[i - l] = arr[i];
}
for (int i = mid + 1; i <= r; i++) {
right[i - (mid + 1)] = arr[i];
}
int left_start = 0;
int right_start = 0;
int left_length = mid - l + 1;
int right_length = r - mid;
int temp = l;
while (left_start < left_length && right_start < right_length) {
if (left[left_start] < right[right_start]) {
arr[temp] = left[left_start++];
} else {
arr[temp] = right[right_start++];
}
temp++;
}
while (left_start < left_length) {
arr[temp++] = left[left_start++];
}
while (right_start < right_length) {
arr[temp++] = right[right_start++];
}
}
static void merge(long[] arr, int l, int mid, int r) {
long[] left = new long[mid - l + 1];
long[] right = new long[r - mid];
for (int i = l; i <= mid; i++) {
left[i - l] = arr[i];
}
for (int i = mid + 1; i <= r; i++) {
right[i - (mid + 1)] = arr[i];
}
int left_start = 0;
int right_start = 0;
int left_length = mid - l + 1;
int right_length = r - mid;
int temp = l;
while (left_start < left_length && right_start < right_length) {
if (left[left_start] < right[right_start]) {
arr[temp] = left[left_start++];
} else {
arr[temp] = right[right_start++];
}
temp++;
}
while (left_start < left_length) {
arr[temp++] = left[left_start++];
}
while (right_start < right_length) {
arr[temp++] = right[right_start++];
}
}
static HashMap<Long, Integer> map_prime_factors(long n) {
HashMap<Long, Integer> map = new HashMap<>();
while (n % 2 == 0) {
map.put(2L, map.getOrDefault(2L, 0) + 1);
n /= 2L;
}
for (long i = 3; i <= Math.sqrt(n); i++) {
while (n % i == 0) {
map.put(i, map.getOrDefault(i, 0) + 1);
n /= i;
}
}
if (n > 2) {
map.put(n, map.getOrDefault(n, 0) + 1);
}
return map;
}
static long divisor(long n) {
long count = 0;
for (long i = 1L; i * i <= n; i++) {
if (n % i == 0) {
if (i == n / i)
count += i;
else {
count += i;
count += n / i;
}
}
}
return count;
}
// static void smallest_prime_factor(int n) {
// smallest_prime_factor[1] = 1;
// for (int i = 2; i <= n; i++) {
// if (smallest_prime_factor[i] == 0) {
// smallest_prime_factor[i] = i;
// for (int j = i * i; j <= n; j += i) {
// if (smallest_prime_factor[j] == 0) {
// smallest_prime_factor[j] = i;
// }
// }
// }
// }
// }
// static int[] smallest_prime_factor;
// static int count = 1;
// static int[] p = new int[100002];
// static long[] flat_tree = new long[300002];
// static int[] in_time = new int[1000002];
// static int[] out_time = new int[1000002];
// static long[] subtree_gcd = new long[100002];
// static int w = 0;
// static boolean poss = true;
/*
* (a^b^c)%mod
* Using fermats Little theorem
* x^(mod-1)=1(mod)
* so b^c can be written as b^c=x*(mod-1)+y
* then (a^(x*(mod-1)+y))%mod=(a^(x*(mod-1))*a^(y))mod
* the term (a^(x*(mod-1)))%mod=a^(mod-1)*a^(mod-1)
*
*/
static class pair implements Comparable<pair> {
int a;
int b;
int index;
public pair(int a, int b, int index) {
this.a = a;
this.b = b;
this.index = index;
}
public int compareTo(pair p) {
// if (this.b == Integer.MIN_VALUE || p.b == Integer.MIN_VALUE)
// return (int) (this.index - p.index);
return (int) (this.a - p.a);
}
}
static class pair2 implements Comparable<pair2> {
long a;
int index;
public pair2(long a, int index) {
this.a = a;
this.index = index;
}
public int compareTo(pair2 p) {
return (int) (this.a - p.a);
}
}
static class node {
long value;
int index;
public node(long value, int index) {
this.value = value;
this.index = index;
}
}
static class comparator implements Comparator<node> {
public int compare(node a, node b) {
return a.value - b.value > 0 ? 1 : -1;
}
}
static class Segment_Tree {
static {
final int val;
val = 3;
pn(val);
}
private long[] segment_tree;
public Segment_Tree(int n) {
this.segment_tree = new long[4 * n + 1];
}
void build(int index, int left, int right, int[] a) {
if (left == right) {
segment_tree[index] = a[left];
return;
}
int mid = (left + right) / 2;
build(2 * index + 1, left, mid, a);
build(2 * index + 2, mid + 1, right, a);
segment_tree[index] = segment_tree[2 * index + 1] + segment_tree[2 * index + 2];
}
long query(int index, int left, int right, int l, int r) {
if (left > right)
return 0;
if (left >= l && r >= right) {
return segment_tree[index];
}
if (l > right || left > r)
return 0;
int mid = (left + right) / 2;
return query(2 * index + 1, left, mid, l, r) + query(2 * index + 2, mid + 1, right, l, r);
}
void update(int index, int left, int right, int node, int val) {
if (left == right) {
segment_tree[index] += val;
return;
}
int mid = (left + right) / 2;
if (node <= mid)
update(2 * index + 1, left, mid, node, val);
else
update(2 * index + 2, mid + 1, right, node, val);
segment_tree[index] = segment_tree[2 * index + 1] + segment_tree[2 * index + 2];
}
}
// static int ans = 0;
static int ans = 0;
static int leaf = 0;
static boolean poss = true;
static int bottom = Integer.MAX_VALUE;
static int right = Integer.MAX_VALUE;
static int max_right;
static int max_bottom;
static long mod = 1000000007L;
int glo = 0;
public static void main(String[] args) throws Exception {
in = new FastReader();
out = new PrintWriter(System.out);
int tc = ni();
while (tc-- > 0) {
int n=ni();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=ni();
}
int first=-1;
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1]){
first=i;
break;
}
}
if(first==-1){
pn(0);
continue;
}
int last=-1;
for(int j=n-2;j>first;j--){
if(a[j]==a[j+1]){
last=j;
break;
}
}
if(last==-1)
{
pn(0);
continue;
}
if(first+1==last)pn(1);
else pn(last-(first+1));
}
out.flush();
out.close();
}
static int dp(int i,int n, int[] arr,int sum){
if(i>=n)return 0;
int ans=0;
if(arr[i]+sum>=0){
ans=Math.max(ans,1+dp(i+1,n,arr,sum+arr[i]));
ans=Math.max(ans,dp(i+1,n,arr,sum));
}else ans=Math.max(ans,dp(i+1,n,arr,sum));
return ans;
}
static void dfs(int i, List<List<Integer>> arr, boolean[] visited,int parent, List<Integer> leaf) {
visited[i]=true;
int count=0;
for(int v:arr.get(i)){
if(v==parent || visited[v])continue;
count++;
dfs(v,arr,visited,i,leaf);
}
// pn(i+" "+dp1[0][1]+" "+dp1[1][1]);
if(count==0)leaf.add(i);
}
static void factorial(long[] fact, long[] fact_inv, int n, long mod) {
fact[0] = 1;
for (int i = 1; i < n; i++) {
fact[i] = (i * fact[i - 1]) % mod;
}
for (int i = 0; i < n; i++) {
fact_inv[i] = power(fact[i], mod - 2, mod);// (1/x)%m can be calculated by fermat's little theoram which is
// (x**(m-2))%m when m is prime
}
}
static void find(int i, int n, int[] row, int[] col, int[] d1, int[] d2) {
if (i >= n) {
ans++;
return;
}
for (int j = 0; j < n; j++) {
if (col[j] == 0 && d1[i - j + n - 1] == 0 && d2[i + j] == 0) {
col[j] = 1;
d1[i - j + n - 1] = 1;
d2[i + j] = 1;
find(i + 1, n, row, col, d1, d2);
col[j] = 0;
d1[i - j + n - 1] = 0;
d2[i + j] = 0;
}
}
}
static int answer(int l, int r, int[][] dp) {
if (l > r)
return 0;
if (l == r) {
dp[l][r] = 1;
return 1;
}
if (dp[l][r] != -1)
return dp[l][r];
int val = Integer.MIN_VALUE;
int mid = l + (r - l) / 2;
val = 1 + Math.max(answer(l, mid - 1, dp), answer(mid + 1, r, dp));
return dp[l][r] = val;
}
static TreeSet<Integer> ans(int n) {
TreeSet<Integer> set = new TreeSet<>();
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
set.add(i);
set.add(n / i);
}
}
set.remove(1);
return set;
}
// static long find(String s, int i, int n, long[] dp) {
// // pn(i);
// if (i >= n)
// return 1L;
// if (s.charAt(i) == '0')
// return 0;
// if (i == n - 1)
// return 1L;
// if (dp[i] != -1)
// return dp[i];
// if (s.substring(i, i + 2).equals("10") || s.substring(i, i + 2).equals("20"))
// {
// return dp[i] = (find(s, i + 2, n, dp)) % mod;
// }
// if ((s.charAt(i) == '1' || (s.charAt(i) == '2' && s.charAt(i + 1) - '0' <=
// 6))
// && ((i + 2 < n ? (s.charAt(i + 2) != '0' ? true : false) : (i + 2 == n ? true
// : false)))) {
// return dp[i] = (find(s, i + 1, n, dp) + find(s, i + 2, n, dp)) % mod;
// } else
// return dp[i] = (find(s, i + 1, n, dp)) % mod;
static void print(int[] a) {
for (int i = 0; i < a.length; i++)
p(a[i] + " ");
pn("");
}
static long count(long n) {
long count = 0;
while (n != 0) {
count += n % 10;
n /= 10;
}
return count;
}
static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static int LcsOfPrefix(String a, String b) {
int i = 0;
int j = 0;
int count = 0;
while (i < a.length() && j < b.length()) {
if (a.charAt(i) == b.charAt(j)) {
j++;
count++;
}
i++;
}
return a.length() + b.length() - 2 * count;
}
static void reverse(int[] a, int n) {
for (int i = 0; i < n / 2; i++) {
int temp = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = temp;
}
}
static char get_char(int a) {
return (char) (a + 'a');
}
static int find1(int[] a, int val) {
int ans = -1;
int l = 0;
int r = a.length - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (a[mid] <= val) {
l = mid + 1;
ans = mid;
} else
r = mid - 1;
}
return ans;
}
static int find2(int[] a, int val) {
int l = 0;
int r = a.length - 1;
int ans = -1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (a[mid] <= val) {
ans = mid;
l = mid + 1;
} else
r = mid - 1;
}
return ans;
}
// static void dfs(List<List<Integer>> arr, int node, int parent, long[] val) {
// p[node] = parent;
// in_time[node] = count;
// flat_tree[count] = val[node];
// subtree_gcd[node] = val[node];
// count++;
// for (int adj : arr.get(node)) {
// if (adj == parent)
// continue;
// dfs(arr, adj, node, val);
// subtree_gcd[node] = gcd(subtree_gcd[adj], subtree_gcd[node]);
// }
// out_time[node] = count;
// flat_tree[count] = val[node];
// count++;
// }
}
| 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 8 | 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 | eef85aa2e403283dd6b0373d5e7f4bdb | 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 java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(new InputStreamReader(System.in));
int t = cin.nextInt();
for(int i=0;i<t;i++) {
int prenum =-1;
int minid =-1;
int maxid =-1;
int n = cin.nextInt();
for(int j=0;j<n;j++) {
int temp = cin.nextInt();
if(temp == prenum) {
if(minid==-1) {
minid =j;
}
maxid =j;
}
prenum = temp;
}
if(maxid== minid) {
System.out.println(0);
} else if(maxid== minid +1) {
System.out.println(1);
} else {
System.out.println(maxid-minid-1);
}
}
cin.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 8 | 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 | 0521f4273747d56efba4ed684a5c2c7c | 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 C1672 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t=0; t<T; t++) {
int N = in.nextInt();
int[] A = new int[N];
for (int n=0; n<N; n++) {
A[n] = in.nextInt();
}
int first = -1;
int last = -1;
for (int n=1; n<N; n++) {
if (A[n-1] == A[n]) {
if (first == -1) {
first = n;
}
last = n;
}
}
int dist = last - first;
int answer;
if (dist <= 1) {
answer = dist;
} else {
answer = dist-1;
}
System.out.println(answer);
}
}
}
| 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 8 | 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 | 7749b0de1d09ab6eb7a798296ed57e1c | 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 UnArray1672C {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- != 0) {
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int s = 0, e = -1, ans = 0;
boolean p = false;
while(s < n-1) {
if(arr[s] == arr[s+1]) {
if(!p) e = s+1; // last pair end idx
else {
// ans += s-e+1;
if(s == e) {
ans++;
s++;
}
else {
ans += s-e;
}
e = s;
}
p = true;
}
s++;
}
System.out.println(ans);
}
sc.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 8 | 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 | 2f7b9a59b0be3f5dff1b67f651fd6166 | 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 Solution{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int rows=Integer.parseInt(sc.nextLine());
while(rows>0){
int n=sc.nextInt(),o=0,l=-1,r=0,len=0;
int[] nums=new int[n];
for(int i=0;i<n;i++){
nums[i]=sc.nextInt();
if(i>0&&nums[i]==nums[i-1]){
if(l==-1){
l=i-1;
}
r=i;
}
}
len=r-l+1;
if(len==3){
o=1;
}else if(len>3){
o=len-3;
}
System.out.println(o);
rows--;
}
}
} | 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 8 | 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 | 02f791ae4aaf97cf0fc52199a324301b | 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.text.DecimalFormat;
import java.util.*;
public class Codeforces {
static long mod= Long.MAX_VALUE;
public static void main(String[] args) throws Exception {
PrintWriter out=new PrintWriter(System.out);
FastScanner fs=new FastScanner();
// DecimalFormat formatter= new DecimalFormat("#0.000000");
int t=fs.nextInt();
// int t=1;
outer:for(int time=1;time<=t;time++) {
int n=fs.nextInt();
int arr[]=fs.readArray(n);
int l=-1, r=-1;
for(int i=0;i<n-1;i++) {
if(arr[i]==arr[i+1]) {
l=i;
break;
}
}
for(int i=n-2;i>=0;i--) {
if(arr[i]==arr[i+1]) {
r=i;
break;
}
}
if(l==r) {
out.println(0);
continue outer;
}
out.println(Math.max(r-l-1, 1));
}
out.close();
}
static long pow(long a,long b) {
if(b<0) return 1;
long res=1;
while(b!=0) {
if((b&1)!=0) {
res*=a;
res%=mod;
}
a*=a;
a%=mod;
b=b>>1;
}
return res;
}
static long gcd(long a,long b) {
if(b==0) return a;
return gcd(b,a%b);
}
static long nck(int n,int k) {
if(k>n) return 0;
long res=1;
res*=fact(n);
res%=mod;
res*=modInv(fact(k));
res%=mod;
res*=modInv(fact(n-k));
res%=mod;
return res;
}
static long fact(long n) {
// return fact[(int)n];
long res=1;
for(int i=2;i<=n;i++) {
res*=i;
res%=mod;
}
return res;
}
static long modInv(long n) {
return pow(n,mod-2);
}
static void sort(int[] a) {
//suffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n);
int temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
static void sort(long[] a) {
//suffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n);
long temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
// Use this to input code since it is faster than a Scanner
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();
}
String nextLine() {
String str="";
try {
str= (br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long[] readArrayL(int n) {
long a[]=new long[n];
for(int i=0;i<n;i++) a[i]=nextLong();
return a;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["4\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 8 | 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 | e3122f61d453f9efa7b283b916b6aa6a | 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.*;
/**
* Made by egor https://github.com/chermehdi/egor.
*
* @author Azuz
*
*/
public class Main {
void solve(InputReader in, PrintWriter out) {
int t = in.nextInt();
while (t-->0) {
int n = in.nextInt();
int[] arr = in.nextIntArray(n);
int low = Integer.MAX_VALUE;
int hi = -1;
for (int i = 1; i < n; ++i) {
if (arr[i] == arr[i - 1]) {
low = Math.min(low, i);
hi = Math.max(hi, i - 1);
}
}
if (low == -1 || hi < low) {
out.println(0);
continue;
} else if (low == hi) {
out.println(1);
} else {
out.println(hi - low);
}
}
}
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
try (PrintWriter out = new PrintWriter(System.out)) {
new Main().solve(in, out);
}
}
}
class InputReader {
private InputStream stream;
private static final int DEFAULT_BUFFER_SIZE = 1 << 16;
private static final int EOF = -1;
private byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int[] readIntArray(int tokens) {
int[] ret = new int[tokens];
for (int i = 0; i < tokens; i++) {
ret[i] = nextInt();
}
return ret;
}
public int read() {
if (this.numChars == EOF) {
throw new UnknownError();
} else {
if (this.curChar >= this.numChars) {
this.curChar = 0;
try {
this.numChars = this.stream.read(this.buf);
} catch (IOException ex) {
throw new InputMismatchException();
}
if (this.numChars <= 0) {
return EOF;
}
}
return this.buf[this.curChar++];
}
}
public int nextInt() {
int c;
for (c = this.read(); isSpaceChar(c); c = this.read()) {
}
byte sgn = 1;
if (c == 45) {
sgn = -1;
c = this.read();
}
int res = 0;
while (c >= 48 && c <= 57) {
res *= 10;
res += c - 48;
c = this.read();
if (isSpaceChar(c)) {
return res * sgn;
}
}
throw new InputMismatchException();
}
public long nextLong() {
int c;
for (c = this.read(); isSpaceChar(c); c = this.read()) {
}
byte sgn = 1;
if (c == 45) {
sgn = -1;
c = this.read();
}
long res = 0;
while (c >= 48 && c <= 57) {
res *= 10L;
res += c - 48;
c = this.read();
if (isSpaceChar(c)) {
return res * sgn;
}
}
throw new InputMismatchException();
}
public double nextDouble() {
double ret = 0, div = 1;
int c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) {
return -ret;
}
return ret;
}
public String next() {
int c;
while (isSpaceChar(c = this.read())) {
}
StringBuilder result = new StringBuilder();
result.appendCodePoint(c);
while (!isSpaceChar(c = this.read())) {
result.appendCodePoint(c);
}
return result.toString();
}
public String nextLine() {
int c;
StringBuilder result = new StringBuilder();
boolean read = false;
while ((c = this.read()) != '\n') {
if (c == -1) {
return null;
}
result.appendCodePoint(c);
read = true;
}
if (!read) {
return null;
}
return result.toString();
}
public static boolean isSpaceChar(int c) {
return c == 32 || c == 10 || c == 13 || c == 9 || c == EOF;
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public int[][] nextIntMatrix(int n, int m) {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = nextInt();
}
}
return arr;
}
public long[][] nextLongMatrix(int n, int m) {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = nextLong();
}
}
return arr;
}
public char[] nextCharArray() {
return next().toCharArray();
}
public double[] nextDoubleArray(int n) {
double[] ret = new double[n];
for (int i = 0; i < n; i++) {
ret[i] = nextDouble();
}
return ret;
}
public int[]
nextIntArrayOneBased(int n) {
int[] ret = new int[n + 1];
for (int i = 1; i <= n; i++) {
ret[i] = nextInt();
}
return ret;
}
public char[][] nextCharMatrix(int n, int m) {
char[][] res = new char[n][m];
for (int i = 0; i < n; ++i) {
res[i] = nextCharArray();
}
return res;
}
}
| 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 8 | 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 | 4ac732108cdee40c55671a8ba7776209 | 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 MyClass {
static int minCost(int arr[]){
ArrayList<Integer> list = new ArrayList<Integer>();
int ec=0;
int indx1=0;
int indx2=0;
for(int i=0; i<arr.length-1; i++){
if(arr[i]==arr[i+1]){
if(ec==0){
indx1=i+1;
}
else{
indx2=i+1;
}
ec++;
}
}
return ec>1?Math.max( (indx2-indx1-1),1 ):0;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t!=0){
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++){
arr[i] = sc.nextInt();
}
if(n<=2){
System.out.println(0);
}
else{
System.out.println(minCost(arr));
}
t--;
}
}
} | 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 8 | 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 | fa743db1223c115b63fe9b00b35043f2 | 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 Solution
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
for(int k=1;k<=T;k++)
{
int N=sc.nextInt();
long A[]=new long[N];
for(int i=0;i<N;i++)
A[i]=sc.nextLong();
boolean equal[]=new boolean[N];
int eq=0,count=0;
for(int i=0;i<N-1;i++)
{
if(A[i]==A[i+1])
{
eq++;
equal[i]=true;
}
}
if(eq<=1)
{
System.out.println(0);
continue;
}
for(int i=0;i<N-2&&eq>1;i++)
{
if(equal[i])
{
count++;
eq--;
if(!equal[i+1])
{
equal[i+1]=true;
eq++;
}
if(equal[i+2])
{
equal[i+2]=false;
eq--;
}
}
}
System.out.println(count);
}
}
} | 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 8 | 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 | b80d1cf5b3f06fd28d56945aad0898a1 | 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_Bajrang_Bali
* @Har_Har_Mahadev
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class practice2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
while (t-- > 0) {
int n=sc.nextInt();
int[] arr=new int[n];
for (int i = 0; i < n; i++) {
arr[i]=sc.nextInt();
}
int l=-1,r=-1;
for (int i = 0; i < n-1; i++) {
if(arr[i]==arr[i+1]){
if(l==-1) l=i;
r=i;
}
}
if(l==r) System.out.println("0");
else System.out.println(Math.max(r-l-1,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 8 | 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 | 7f4b4a8ba8807f51f008b0d9e69c2040 | 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 Div2C {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
FastReader fsr = new FastReader();
int T = fsr.nextInt();
int MAX_SIZE = 200001;
int[] arr = new int[MAX_SIZE];
for (int i = 1; i <= T; i++) {
int n = fsr.nextInt();
for (int j = 1; j <= n; j++) {
arr[j] = fsr.nextInt();
}
solution(n, arr, out);
}
out.flush();
out.close();
}
private static void solution(int n, int[] arr, PrintWriter out) {
int[] numEqualIndicesAhead = new int[n + 1];
for (int i = n - 1; i >= 1; i--) {
numEqualIndicesAhead[i] = numEqualIndicesAhead[i + 1];
if (arr[i] == arr[i + 1]) {
numEqualIndicesAhead[i]++;
}
}
int minNumMoves = 0; boolean incremented = false;
for (int i = 2; i + 1 <= n; i++) {
if (incremented && arr[i] == arr[i + 1]) {
numEqualIndicesAhead[i]--;
}
incremented = false;
if (numEqualIndicesAhead[i] <= 0) {
break;
}
if (arr[i] == arr[i - 1]) {
minNumMoves++;
incremented = true;
arr[i] = arr[i + 1];
}
}
out.println(minNumMoves);
}
}
| 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 8 | 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 | c9906ab2cb04ac521c6ea999e2252087 | 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.ArrayList;
import java.util.StringTokenizer;
public class EqualityArr {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
PrintWriter pr = new PrintWriter (System.out);
for(int i = 0 ; i<t ; i++){
int n = Integer.parseInt(br.readLine());
int[] equal = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j = 0 ; j<n ; j++){
equal[j] = Integer.parseInt(st.nextToken());
}
int smallest = 1000000000;
int largest = -1;
for(int j = 0 ; j<n-1 ; j++){
if(equal[j]==equal[j+1]){
if(smallest>j)
smallest = j;
if(j>largest)
largest = j;
}
}
if(largest<=smallest)
pr.println(0);
else
pr.println(Math.max(largest-smallest-1, 1));
}
pr.flush();
}
}
| 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 8 | 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 | 96f28346d0a6ec3b1555a550b53874de | 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.Scanner;
import java.util.*;
import java.lang.Math;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args){
int a[], b, t;
String str;
FastReader in = new FastReader();
t = in.nextInt();
while(t-- > 0){
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.clear();
int n = in.nextInt();
a = in.readArray(n);
for(int i = 0; i < n - 1;i++){
if(a[i] == a[i + 1]){
arr.add(i);
}
}
if(arr.size() < 2){
System.out.println(0);
continue;
}else{
System.out.println(Math.max(arr.get(arr.size() - 1)- arr.get(0) - 1, 1));
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
int[] readArray(int n){
int[] a = new int[n];
for(int i = 0; i < n;i++){
a[i] = nextInt();
}
return a;
}
String nextLine()
{
String str = "";
try {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
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 8 | 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 | 311230efd1f248c4883364db5ca1523f | 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 MyClass {
static Scanner in = new Scanner(System.in);
static int testCases, n;
static long a[];
static void solve() {
int start = -1, end = -1;
for(int i = 0; i < n - 1; ++i) {
if(a[i] == a[i + 1]) {
start = i + 1;
break;
}
}
for(int i = n - 1; i >= 1; --i) {
if(a[i] == a[i - 1]) {
end = i + 1;
break;
}
}
if(start == -1 || end == -1) {
System.out.println(0);
return;
}
//System.out.println(start + " " + end);
int len = end - start + 1;
if(len < 3) {
System.out.println(0);
} else if(len == 3 || len == 4 ) {
System.out.println(1);
} else {
System.out.println(len - 3);
}
}
public static void main(String args[]) {
testCases = in.nextInt();
for(int t = 0; t < testCases; ++t) {
n = in.nextInt();
a = new long[n];
for(int i = 0; i < n; ++i) {
a[i] = in.nextInt();
}
solve();
}
}
} | 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 8 | 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 | 78231f146b799d39eba770609029c0a9 | 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 | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef {
public static void main (String[] args) throws java.lang.Exception {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-- > 0){
int n = scn.nextInt();
int[]arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = scn.nextInt();
}
int count = 0;
for(int i = 1; i < n; i++){
if(arr[i] == arr[i - 1]){
count++;
}
}
if(count < 2){
System.out.println(0);
continue;
}
int l = -1, r = -1;
for(int i = 1; i < n; i++){
if(arr[i] == arr[i - 1]){
if(l == -1){
l = i - 1;
}
r = i;
}
}
int d = r - l + 1;
if(d == 3){
System.out.println(1);
}else{
System.out.println(d - 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 8 | 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 | c6baff32cfdc10d26a64e3a88038cd7a | 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.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Contest_yandexA{
//static final int MAXN = (int)1e6;
public static void main(String[] args) throws IOException{
Scanner input = new Scanner(System.in);
/*int n = input.nextInt();
int k = input.nextInt();
k = k%4;
int[] a = new int[n];
for(int i = 0;i<n;i++){
a[i] = input.nextInt();
}
int[] count = new int[n];
int sum = 0;
for(int tt = 0;tt<k;tt++){
for(int i = 0;i<n;i++){
count[a[i]-1]++;
}
for(int i = 0;i<n;i++){
sum+= count[i];
}
for(int i = 0;i<n;i++){
a[i] = sum;
sum-= count[i];
}
}
for(int i = 0;i<n;i++){
System.out.print(a[i] + " ");
}*/
int t = input.nextInt();
for(int tt = 0;tt<t;tt++){
int n = input.nextInt();
int[] a = new int[n];
for(int i = 0;i<n;i++){
a[i] = input.nextInt();
}
int start = 0;
int end = 0;
for(int i = 0;i<n-1;i++){
if(a[i] == a[i+1]){
start = i;
break;
}
}
for(int i = n-1;i>=1;i--){
if(a[i] == a[i-1]){
end = i-1;
break;
}
}
if(end == start){
System.out.println(0);
}
else if(end-start == 1){
System.out.println(end-start);
}
else{
System.out.println(end-start-1);
}
}
}
public static long gcd(long a,long b){
if(b == 0){
return a;
}
return gcd(b,a%b);
}
/*public static int lcm(int a,int b){
return (a / gcd(a, b)) * b;
}*/
}
class Pair implements Comparable<Pair>{
int x;
int y;
Pair(int x,int y){
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair p){
return this.x-p.x;
}
}
| 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 8 | 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 | 2619229f88eb7c9498aea1cf16b20c67 | 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 | /*############################################################################################################
########################################## >>>> Diaa12360 <<<< ###############################################
########################################### Just Nothing #################################################
#################################### If You Need it, Fight For IT; #########################################
###############################################.-. 1 5 9 2 .-.################################################
############################################################################################################*/
import java.io.*;
import java.lang.reflect.Array;
import java.net.Inet4Address;
import java.util.*;
import java.util.List;
import java.util.function.Function;
public class Solution {
final static int N = 200002; // 2*10^5
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// BufferedReader in = new BufferedReader(new FileReader("src/input"));
StringBuilder out = new StringBuilder();
StringTokenizer tk;
int t = ints(in.readLine());
while (t-- > 0) {
int n = ints(in.readLine());
tk = new StringTokenizer(in.readLine());
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ints(tk.nextToken());
}
int min = -1, max = -1;
for (int i = 1; i < n; i++) {
if(arr[i] == arr[i-1]){
if(min == -1)
min = i;
max = i;
}
}
out.append(max == min ? 0 : Math.max(max - min - 1, 1)).append('\n');
}
System.out.print(out);
}
// 5 - 4
// 1 1 1 1 1 1
// 1 1 2 3 3 4
// mn = -1 1
// mx = -1 1
// max(n-3, 1);
// mx - mn - 1
// 2
// colum
static List<List<Integer>> sortByColum(List<List<Integer>> arr) {
List<List<Integer>> a = new ArrayList<>();
for (int i = 0; i < arr.size(); i++) {
ArrayList<Integer> x = new ArrayList<>();
for (int j = 0; j < arr.get(i).size(); j++) {
x.add(arr.get(j).get(i));
}
a.add(x);
}
a.sort(Comparator.comparing(o -> o.get(0)));
List<List<Integer>> a1 = new ArrayList<>();
for (int i = 0; i < a.size(); i++) {
ArrayList<Integer> x = new ArrayList<>();
for (int j = 0; j < a.get(i).size(); j++) {
x.add(a.get(j).get(i));
}
a1.add(x);
}
return a1;
}
static int ints(String s) {
return Integer.parseInt(s);
}
static long ll(String s) {
return Long.parseLong(s);
}
static int[] readArray(String s, int n) {
StringTokenizer tk = new StringTokenizer(s);
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = ints(tk.nextToken());
return arr;
}
static int[] readArray(String s) {
StringTokenizer tk = new StringTokenizer(s);
int[] arr = new int[tk.countTokens()];
for (int i = 0; i < arr.length; i++)
arr[i] = ints(tk.nextToken());
return arr;
}
static void printArray(char[][] arr, StringBuilder out) {
out.append("YES").append('\n');
for (char[] chars : arr) {
for (char c : chars) {
out.append(c);
}
out.append('\n');
}
}
static void printArray(String[] arr) {
for (String x : arr) {
System.out.println(x);
}
}
static <T, E> Map<T, E> createMapFromList(List<T> l, Function<T, E> fun) {
Map<T, E> mp = new HashMap<>();
for (T x : l) {
mp.put(x, fun.apply(x));
}
return mp;
}
}
class ArrayStack<E> {
public static final int CAPACITY = 1000;
private E[] data;
private int t = -1;
public ArrayStack() {
this(CAPACITY);
}
public ArrayStack(int capacity) {
data = (E[]) new Object[capacity];
}
public int size() {
return t + 1;
}
public boolean isEmpty() {
return t == -1;
}
public E push(E e) throws IllegalStateException {
if (size() == data.length) throw new IllegalStateException("Stack is full");
data[++t] = e;
return e;
}
public E peek() {
return isEmpty() ? null : data[t];
}
public E pop() {
if (isEmpty())
return null;
E d = data[t];
data[t] = null;
t--;
return d;
}
}
class Pair<E, T> {
E first;
T second;
Pair(E f, T s) {
first = f;
second = s;
}
public E getFirst() {
return first;
}
public T getSecond() {
return second;
}
}
class pair implements Comparable<pair> {
int first;
int second;
pair(int f, int s) {
first = f;
second = s;
}
@Override
public int compareTo(pair o) {
return 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 8 | 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 | 299ba78954c5402dc2d493a1fa30e005 | 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 Failed {
static BufferedReader bf;
static PrintWriter out;
public static void main (String[] args)throws IOException {
bf = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int t = nextInt();
while(t-->0){
solve();
}
}
public static void solve()throws IOException{
int n = nextInt();
int[]arr = nextIntArray(n);
int first = -1;
int last = -1;
for(int i = 0;i<n-1;i++){
if(arr[i] == arr[i+1]){
if(first == -1){
first = i+1;
}
else{
last = i;
}
}
}
if(first == -1 || last == -1){
println(0);
}
else if(last - first == 0){
println(1);
}
else{
println(last - first);
}
}
public static long gcd(long a,long b){
if(b == 0)return a;
return gcd(b,a%b);
}
// code for input
public static void print(String s ){
System.out.print(s);
}
public static void sort(long[]arr){
List<Long>list = new ArrayList<>();
for(int i =0;i<arr.length;i++){
list.add(arr[i]);
}
Collections.sort(list);
for(int i =0;i<arr.length;i++){
arr[i] = list.get(i);
}
}
public static void print(int num ){
System.out.print(num);
}
public static void print(long num ){
System.out.print(num);
}
public static void println(String s){
System.out.println(s);
}
public static void println(int num){
System.out.println(num);
}
public static void println(long num){
System.out.println(num);
}
public static void println(){
System.out.println();
}
public static int toInt(String s){
return Integer.parseInt(s);
}
public static long toLong(String s){
return Long.parseLong(s);
}
public static String[] nextStringArray()throws IOException{
return bf.readLine().split(" ");
}
public static int nextInt()throws IOException{
return Integer.parseInt(bf.readLine());
}
public static long nextLong()throws IOException{
return Long.parseLong(bf.readLine());
}
public static String nextString()throws IOException{
return bf.readLine();
}
public static int[] nextIntArray(int n)throws IOException{
String[]str = bf.readLine().split(" ");
int[]arr = new int[n];
for(int i =0;i<n;i++){
arr[i] = Integer.parseInt(str[i]);
}
return arr;
}
public static long[] nextLongArray(int n)throws IOException{
String[]str = bf.readLine().split(" ");
long[]arr = new long[n];
for(int i =0;i<n;i++){
arr[i] = Long.parseLong(str[i]);
}
return arr;
}
public static int[][] newIntMatrix(int r,int c)throws IOException{
int[][]arr = new int[r][c];
for(int i =0;i<r;i++){
String[]str = bf.readLine().split(" ");
for(int j =0;j<c;j++){
arr[i][j] = Integer.parseInt(str[j]);
}
}
return arr;
}
public static long[][] newLongMatrix(int r,int c)throws IOException{
long[][]arr = new long[r][c];
for(int i =0;i<r;i++){
String[]str = bf.readLine().split(" ");
for(int j =0;j<c;j++){
arr[i][j] = Long.parseLong(str[j]);
}
}
return arr;
}
}
// if a problem is related to binary string it could also be related to parenthesis
// try to use binary search in the question it might work
// try sorting
// try to think in opposite direction of question it might work in your way
// if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+ or a,b,c,d in general
// gcd(1.p1,2.p2,3.p3,4.p4....n.pn) cannot be greater than 2 it has been proveds | 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 8 | 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 | 9778fdc12982b4cf0ef8bf875635d09b | 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 | // ol' school style
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer zer=null;
int test=Integer.parseInt(br.readLine());
while(test-->0){
int n=Integer.parseInt(br.readLine());
long a[]=new long[n];
zer=new StringTokenizer(br.readLine());
for(int i=0;i<n;i++){
a[i]=Long.parseLong(zer.nextToken());
}
int fidx=n,lidx=-1;
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1]){
fidx=Math.min(fidx,i);
lidx=Math.max(lidx,i);
}
}
if(fidx==n || fidx==lidx){
System.out.println(0);
}
else{
System.out.println(Math.max(1,lidx+1-fidx+1-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 8 | 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 | 3c7fd15e7885988f27facfb82d7ee16c | 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 | // package CodeForces.RoadMap.Diff1300;
import java.util.*;
import java.io.*;
/**
* @author SyedAli
* @createdAt 24-04-2022, Sunday, 12:54
*/
public class UnequalArray {
private static Scanner s = new Scanner(System.in);
private static void solve() {
int n = s.nextInt();
int[] arr = new int[n];
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
for (int i = 1; i < n; i++) {
if (arr[i] == arr[i - 1]) {
min = Math.min(i - 1, min);
max = Math.max(i, max);
}
}
int ele = max - min + 1;
if (ele >= 4) {
System.out.println(ele - 3);
} else if (ele == 3) {
System.out.println(1);
} else {
System.out.println(0);
}
}
public static void main(String[] args) {
int t = s.nextInt();
while (t-- > 0) {
solve();
}
s.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 8 | 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 | 02c52da78bc5e19be56f7831570e06fe | 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 CodeforcesGlobal20{
static long mod = 1000000007L;
static MyScanner sc = new MyScanner();
static void solve() {
int n = sc.nextInt();
long sum = 0;
for(int i = 0;i<n;i++){
sum+=sc.nextInt();
}
sum-= n;
if(sum%2==0){
out.println("maomao90");
}else{
out.println("errorgorn");
}
}
static void solve2(){
String str = sc.nextLine();
int a = 0;
int b = 0;
for(int i = 0;i<str.length();i++){
if(str.charAt(i)=='A') a++;
else b++;
if(b>a){
out.println("NO");
return;
}
}
int n = str.length();
if(str.charAt(n-1)=='A'){
out.println("NO");
}else
out.println("YES");
}
static void solve3(){
int n = sc.nextInt();
int arr[] = sc.readIntArray(n);
int l = -1;
int r = -1;
for(int i = 0;i<n-1;i++){
if(arr[i]==arr[i+1]){
if(l==-1) l = i;
else r = i;
}
}
if(r==-1){
out.println(0);
}else{
out.println(Math.max(1,r-l-1));
}
}
static void solve4(){
}
static void solve5(){
}
static void reverse(int arr[]){
int i = 0;int j = arr.length-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
static long pow(long a, long b) {
if (b == 0) return 1;
long res = pow(a, b / 2);
res = (res * res) % 1_000_000_007;
if (b % 2 == 1) {
res = (res * a) % 1_000_000_007;
}
return res;
}
static int lis(int arr[],int n){
int lis[] = new int[n];
lis[0] = 1;
for(int i = 1;i<n;i++){
lis[i] = 1;
for(int j = 0;j<i;j++){
if(arr[i]>arr[j]){
lis[i] = Math.max(lis[i],lis[j]+1);
}
}
}
int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
max = Math.max(lis[i],max);
}
return max;
}
static boolean isPali(String str){
int i = 0;
int j = str.length()-1;
while(i<j){
if(str.charAt(i)!=str.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
static long gcd(long a,long b){
if(b==0) return a;
return gcd(b,a%b);
}
static String reverse(String str){
char arr[] = str.toCharArray();
int i = 0;
int j = arr.length-1;
while(i<j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
String st = new String(arr);
return st;
}
static boolean isprime(int n){
if(n==1) return false;
if(n==3 || n==2) 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;
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
// int t= 1;
while(t-- >0){
// solve();
// solve2();
solve3();
// solve4();
// solve5();
}
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
int[] reverse(int arr[]){
int n= arr.length;
int i = 0;
int j = n-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;i++;
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
} | 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 8 | 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 | f0384beef6f07d7c59dd0de132370c71 | 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 C23042022{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] arr = new int[n];
int first = 0, last = 0;
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
if (i > 0 && arr[i] == arr[i - 1]) {
if (first == 0)
first = i;
last = i;
}
}
int ans = last - first - 1;
if (ans <= 0)
System.out.println(ans + 1);
else
System.out.println(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 8 | 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 | b6d621fbc9e03dca974acce1f268831c | 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.*;
public class GlobalRound20ProbC {
public static void main(String args[]) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(in.readLine());
while (t-- > 0) {
StringTokenizer st1 = new StringTokenizer(in.readLine());
int n = Integer.parseInt(st1.nextToken());
StringTokenizer st2 = new StringTokenizer(in.readLine());
int[] a = new int[n];
int firstIndex = -1;
int lastIndex = -1;
for (int i=0; i<n; i++) {
a[i] = Integer.parseInt(st2.nextToken());
if (i>0 && a[i] == a[i-1]) {
if (firstIndex == -1)
firstIndex = i-1;
else
lastIndex = i;
}
}
if (lastIndex == -1)
out.println(0);
else {
int dist = lastIndex - firstIndex + 1;
if (dist <= 2)
out.println(0);
else if (dist == 3)
out.println(1);
else
out.println(dist-3);
}
}
in.close();
out.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 8 | 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 | 651b95d8b18264f5ca26ca3a00ebacff | 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.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class RandomClass {
static final Random random = new Random();
public static class Node implements Comparable<Node> {
int x;
int y;
public Node(int a, int b) {
x = a;
y = b;
}
@Override
public int compareTo(Node o) {
if (this.x > o.x) {
return +1;
} else if (this.x == o.x) {
return 0;
} else {
return -1;
}
}
}
public static class Node1 {
int number;
int count;
public Node1(int num, int c) {
number = num;
count = c;
}
}
public static void main(String args[]) throws Exception {
FastReader fs = new FastReader();
StringBuilder sb = new StringBuilder();
int t = fs.nextInt();
while (t-- > 0) {
int n = fs.nextInt();
int arr[] = new int[n];
for(int i=0; i<n; i++) {
arr[i] = fs.nextInt();
}
ArrayList<String> temp = new ArrayList<>();
for(int i=0; i<n-1; i++) {
if(arr[i] == arr[i+1]) {
String s = (i+1)+" "+(i+2);
temp.add(s);
}
}
if(temp.size()<=1) {
sb.append(0+"\n");
} else {
String left = temp.get(0);
String right = temp.get(temp.size()-1);
String[] arr1 = new String[2];
arr1 = left.split(" ");
String[] arr2 = new String[2];
arr2 = right.split(" ");
if(Integer.parseInt(arr2[0]) == Integer.parseInt(arr1[1])) {
sb.append(1+"\n");
} else {
int ans = Integer.parseInt(arr2[0]) - Integer.parseInt(arr1[1]);
sb.append(ans+"\n");
}
}
}
System.out.println(sb);
}
static int gcd(int a, int b) {
return (a % b == 0) ?
Math.abs(b) : gcd(b, a % b);
}
static boolean isPossible(int a,
int b, int c) {
return (c % gcd(a, b) == 0);
}
//Fast Reader
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 void ruffleSort(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int oi = random.nextInt(n), temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
} | 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 8 | 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 | 1f454b04f021721dd7d03f1e4b32dd92 | 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 C {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = in.nextInt();
for (int tt = 0; tt < t; tt++) {
int n = in.nextInt();
int[] a = new int[n];
Arrays.setAll(a, i->in.nextInt());
pw.println(solve(a, n));
}
pw.close();
}
static int solve(int[] a, int n) {
int res = 0;
if (n == 1 || n == 2) return 0;
int cnt = 0;
for (int i = 1; i < n; i++) {
if (a[i - 1] == a[i]) cnt++;
}
if (cnt <= 1) return 0;
int l = 0;
int r = n;
for (int i = 0; i < n - 1; i++) {
if (a[i] == a[i + 1]) {
l = i;
break;
}
}
for (int i = n - 1; i >= 1; i--) {
if (a[i] == a[i - 1]) {
r = i;
break;
}
}
if (cnt == 2 && consecutive(a, n)) return 1;
res = r - l - 2;
return res;
}
static boolean consecutive(int[] a, int n) {
for (int i = 1; i < n - 1; i++) {
if (a[i - 1] == a[i] && a[i] == a[i + 1]) return true;
}
return false;
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
}
| 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 8 | 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 | 7d4a2881605503b57161108e964ec5e5 | 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 C {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = in.nextInt();
for (int tt = 0; tt < t; tt++) {
int n = in.nextInt();
int[] a = new int[n];
Arrays.setAll(a, i->in.nextInt());
pw.println(solve(a, n));
}
pw.close();
}
static int solve(int[] a, int n) {
int res = 0;
if (n == 1 || n == 2) return 0;
int cnt = 0;
for (int i = 1; i < n; i++) {
if (a[i - 1] == a[i]) cnt++;
}
if (cnt <= 1) return 0;
int l = 0;
int r = n;
for (int i = 0; i < n - 1; i++) {
if (a[i] == a[i + 1]) {
l = i;
break;
}
}
for (int i = n - 1; i >= 1; i--) {
if (a[i] == a[i - 1]) {
r = i;
break;
}
}
if (cnt == 2 && consecutive(a, n)) return 1;
res = r - l - 2;
return res;
}
static boolean consecutive(int[] a, int n) {
for (int i = 1; i < n - 1; i++) {
if (a[i - 1] == a[i] && a[i] == a[i + 1]) return true;
}
return false;
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
}
| 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 8 | 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 | 0177708c6ddbef268f7df8457165b02e | 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.StringTokenizer;
public class C_Unequal_Array {
static Scanner in = new Scanner();
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans = new StringBuilder();
static int testCases, n;
static long a[];
static int solve(int index, long a[], int increase) {
if (index + increase >= n || index + increase < 0) {
return -1;
}
if (index + increase < n && index + increase >= 0 && a[index] == a[index + increase]) {
return index;
}
return solve(index + increase, a, increase);
}
static void solve(int t) {
int start = -1, end = -1;
start = solve(0, a, 1);
end = solve(n - 1, a, -1);
int len = Integer.parseInt(sum(sub(String.valueOf(end), String.valueOf(start)), String.valueOf(1)));
int prev = ans.length();
if (len - 3 < 0) {
ans.append(0);
}
int present = ans.length();
if (present > prev) {
if (t != testCases) {
ans.append("\n");
}
return;
}
switch (len) {
case 3:
case 4:
ans.append(1);
break;
default:
ans.append(len - 3);
break;
}
if (t != testCases) {
ans.append("\n");
}
}
public static void main(String[] priya) throws IOException {
testCases = in.nextInt();
for (int t = 0; t < testCases; ++t) {
n = in.nextInt();
a = new long[n];
for (int i = 0; i < n; ++i) {
a[i] = in.nextLong();
}
solve(t + 1);
}
out.print(ans.toString());
out.flush();
in.close();
}
static boolean isSmaller(String str1, String str2) {
int n1 = str1.length(), n2 = str2.length();
if (n1 < n2) {
return true;
}
if (n2 < n1) {
return false;
}
for (int i = 0; i < n1; i++) {
if (str1.charAt(i) < str2.charAt(i)) {
return true;
} else if (str1.charAt(i) > str2.charAt(i)) {
return false;
}
}
return false;
}
static String sub(String str1, String str2) {
if (isSmaller(str1, str2)) {
String t = str1;
str1 = str2;
str2 = t;
}
String str = "";
int n1 = str1.length(), n2 = str2.length();
int diff = n1 - n2;
int carry = 0;
for (int i = n2 - 1; i >= 0; i--) {
int sub
= (((int) str1.charAt(i + diff) - (int) '0')
- ((int) str2.charAt(i) - (int) '0')
- carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
} else {
carry = 0;
}
str += String.valueOf(sub);
}
for (int i = n1 - n2 - 1; i >= 0; i--) {
if (str1.charAt(i) == '0' && carry > 0) {
str += "9";
continue;
}
int sub = (((int) str1.charAt(i) - (int) '0')
- carry);
if (i > 0 || sub > 0) {
str += String.valueOf(sub);
}
carry = 0;
}
return new StringBuilder(str).reverse().toString();
}
static String sum(String str1, String str2) {
if (str1.length() > str2.length()) {
String t = str1;
str1 = str2;
str2 = t;
}
String str = "";
int n1 = str1.length(), n2 = str2.length();
int diff = n2 - n1;
int carry = 0;
for (int i = n1 - 1; i >= 0; i--) {
int sum = ((int) (str1.charAt(i) - '0')
+ (int) (str2.charAt(i + diff) - '0') + carry);
str += (char) (sum % 10 + '0');
carry = sum / 10;
}
for (int i = n2 - n1 - 1; i >= 0; i--) {
int sum = ((int) (str2.charAt(i) - '0') + carry);
str += (char) (sum % 10 + '0');
carry = sum / 10;
}
if (carry > 0) {
str += (char) (carry + '0');
}
return new StringBuilder(str).reverse().toString();
}
static long detect_sum(int i, long a[], long sum) {
if (i >= a.length) {
return sum;
}
return detect_sum(i + 1, a, sum + a[i]);
}
static String mul(String num1, String num2) {
int len1 = num1.length();
int len2 = num2.length();
if (len1 == 0 || len2 == 0) {
return "0";
}
int result[] = new int[len1 + len2];
int i_n1 = 0;
int i_n2 = 0;
for (int i = len1 - 1; i >= 0; i--) {
int carry = 0;
int n1 = num1.charAt(i) - '0';
i_n2 = 0;
for (int j = len2 - 1; j >= 0; j--) {
int n2 = num2.charAt(j) - '0';
int sum = n1 * n2 + result[i_n1 + i_n2] + carry;
carry = sum / 10;
result[i_n1 + i_n2] = sum % 10;
i_n2++;
}
if (carry > 0) {
result[i_n1 + i_n2] += carry;
}
i_n1++;
}
int i = result.length - 1;
while (i >= 0 && result[i] == 0) {
i--;
}
if (i == -1) {
return "0";
}
String s = "";
while (i >= 0) {
s += (result[i--]);
}
return s;
}
static class Node<T> {
T data;
Node<T> next;
public Node() {
this.next = null;
}
public Node(T data) {
this.data = data;
this.next = null;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
@Override
public String toString() {
return this.getData().toString() + " ";
}
}
static class ArrayList<T> {
Node<T> head, tail;
int len;
public ArrayList() {
this.head = null;
this.tail = null;
this.len = 0;
}
int size() {
return len;
}
boolean isEmpty() {
return len == 0;
}
int indexOf(T data) {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp = head;
int index = -1, i = 0;
while (temp != null) {
if (temp.getData() == data) {
index = i;
}
i++;
temp = temp.getNext();
}
return index;
}
void add(T data) {
Node<T> newNode = new Node<>(data);
if (isEmpty()) {
head = newNode;
tail = newNode;
len++;
} else {
tail.setNext(newNode);
tail = newNode;
len++;
}
}
void see() {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp = head;
while (temp != null) {
out.print(temp.getData().toString() + " ");
out.flush();
temp = temp.getNext();
}
out.println();
out.flush();
}
void inserFirst(T data) {
Node<T> newNode = new Node<>(data);
Node<T> temp = head;
if (isEmpty()) {
head = newNode;
tail = newNode;
len++;
} else {
newNode.setNext(temp);
head = newNode;
len++;
}
}
T get(int index) {
if (isEmpty() || index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp = head;
int i = 0;
T data = null;
while (temp != null) {
if (i == index) {
data = temp.getData();
}
i++;
temp = temp.getNext();
}
return data;
}
void addAt(T data, int index) {
if (index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> newNode = new Node<>(data);
int i = 0;
Node<T> temp = head;
while (temp.next != null) {
if (i == index) {
newNode.setNext(temp.next);
temp.next = newNode;
}
i++;
temp = temp.getNext();
}
// temp.setNext(temp);
len++;
}
void popFront() {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
}
if (head == tail) {
head = null;
tail = null;
} else {
head = head.getNext();
}
len--;
}
void removeAt(int index) {
if (index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
if (index == 0) {
this.popFront();
return;
}
Node<T> temp = head;
int i = 0;
Node<T> n = new Node<>();
while (temp != null) {
if (i == index) {
n.next = temp.next;
temp.next = n;
break;
}
i++;
n = temp;
temp = temp.getNext();
}
tail = n;
--len;
}
void clearAll() {
this.head = null;
this.tail = null;
}
}
static void merge(long a[], int left, int right, int mid) {
int n1 = mid - left + 1, n2 = right - mid;
long L[] = new long[n1];
long R[] = new long[n2];
for (int i = 0; i < n1; i++) {
L[i] = a[left + i];
}
for (int i = 0; i < n2; i++) {
R[i] = a[mid + 1 + i];
}
int i = 0, j = 0, k1 = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
a[k1] = L[i];
i++;
} else {
a[k1] = R[j];
j++;
}
k1++;
}
while (i < n1) {
a[k1] = L[i];
i++;
k1++;
}
while (j < n2) {
a[k1] = R[j];
j++;
k1++;
}
}
static void sort(long a[], int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
sort(a, left, mid);
sort(a, mid + 1, right);
merge(a, left, right, mid);
}
static class Scanner {
BufferedReader in;
StringTokenizer st;
public Scanner() {
in = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
String nextLine() throws IOException {
return in.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
void close() throws IOException {
in.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 8 | 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 | c0e8d9426c8231e21967d43907d2883d | 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.lang.*;
import java.io.*;
public class solution{
public static void main (String[] args) {
int t=sc.nextInt();
while(t--!=0)
{
int n=sc.nextInt();
long a[]=new long[n];int a1=0;int a2=0;int count=0;
for(int i=0;i<n;i++) {
a[i]=sc.nextLong();
}
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1]&&count==0){
a1=i+1;count++;
}else if(a[i]==a[i+1]&&count>=1) {
a2=i;count++;
}
}
if(count>1) {
if((a2-a1)==0) {
out.println(1);
}else {
out.println(a2-a1);
}
}else {
out.println(0);
}
}
////////////////////////////////////////////////////////////////////
out.flush();out.close();
}//*END OF MAIN METHOD*
static final Random random = new Random();
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());
}
long[] readArrayL(int n) {
long a[]=new long[n];
for(int i=0;i<n;i++) a[i]=nextLong();
return a;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static FastScanner sc = new FastScanner();
}//*END OF MAIN CLASS*
| 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 8 | 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 | 8d13469ecaf3020e8564e423ebb6df7a | 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.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class GlobalRound_20_C {
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static String YES = "YES";
private static String NO = "NO";
private static void solve() throws IOException{
//Your Code Goes Here;
int n = in.nextInt();
long[] arr = new long[n];
for(int i = 0; i < n; i++){
arr[i] = in.nextLong();
}
int startCount = 0, endCount = 0, increment = 0;
while(increment < n - 1){
if(arr[increment] == arr[increment + 1]){
startCount = increment;
break;
}
increment++;
}
increment = n - 1;
while(increment > 0){
if(arr[increment] == arr[increment - 1]){
endCount = increment;
break;
}
increment--;
}
int tc = endCount - startCount + 1;
boolean first = tc < 3;
boolean second = tc == 3;
int answer = tc - 3;
if(first){
System.out.println(0);
}
else if(second){
System.out.println(1);
}
else{
System.out.println(answer);
}
}
public static void main(String[] args) throws IOException, InterruptedException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int T = in.nextInt();
while(T --> 0){
solve();
}
out.flush();
in.close();
out.close();
}
static final Random random = new Random();
static final int mod = 1_000_000_007;
private static boolean check(long[] arr, long n){
long ch = arr[0];
for(int i = 0; i < n; i++){
if(ch != arr[i]){
return false;
}
}
return true;
}
//This function gives the max occuring of any element in an array;(HashMap version)
private static long maxFreqHashMap(long[] arr, long n){
Map<Long, Long> hp = new HashMap<Long, Long>();
for(int i = 0; i < n; i++) {
long key = arr[i];
if(hp.containsKey(key)) {
long freq = hp.get(key);
freq++;
hp.put(key, freq);
}
else {
hp.put(key, 1L);
}
}
long max_count = 0, res = 1, count = 0;
for(Map.Entry<Long, Long> val : hp.entrySet()) {
if (max_count < val.getValue()) {
res = Math.toIntExact(val.getKey());
max_count = Math.toIntExact(val.getValue());
count = max_count;
}
}
return count;
}
//This function gives the max occuring of any element in an array; this also known as the "MOORE's Algorithm"
private static long maxFreq(long []arr, long n) {
// using moore's voting algorithm
long res = 0;
long count = -1;
for(int i = 1; i < n; i++) {
if(arr[i] == arr[(int) res]) {
count++;
}
else {
count--;
}
if(count == 0) {
res = i;
count = 1;
}
}
return arr[(int) res];
/*
you've to add below code in the solve()
long freq = maxFreq(arr, n);
int count = 0;
for(int i = 0; i < n; i++){
if(arr[i] == freq){
count++;
}
}
*/
}
private static int LCSubStr(char[] X, char[] Y, int m, int n) {
int[][] LCStuff = new int[m + 1][n + 1];
int result = 0;
for (int i = 0; i <= m; i++){
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0) {
LCStuff[i][j] = 0;
}
else if (X[i - 1] == Y[j - 1]) {
LCStuff[i][j] = LCStuff[i - 1][j - 1] + 1;
result = Integer.max(result, LCStuff[i][j]);
}
else {
LCStuff[i][j] = 0;
}
}
}
return result;
}
private static long longCusBsearch(long[] arr, long n, long h){
long ans = h;
long l = 1;
long r = h;
while(l <= r){
long mid = (l + r) / 2;
long ok = 0;
for(long i = 0; i < n; i++){
if(i == n - 1) {
ok += mid;
}
else{
long x = arr[(int) (i + 1)] - arr[(int) i];
if(x >= mid) {
ok += mid;
}
else{
ok += x;
}
}
}
if(ok >= h){
ans = mid;
r = mid - 1;
}
else{
l = mid + 1;
}
}
return ans;
}
public static int intCusBsearch(int[] arr, int n, int h){
int ans = h, l = 1, r = h;
while(l <= r){
int mid = (l + r) / 2;
int ok = 0;
for(int i = 0; i < n; i++){
if(i == n - 1){
ok += mid;
}
else{
int x = arr[i + 1] - arr[i];
if(x >= mid){
ok += mid;
}
else{
ok += x;
}
}
}
if(ok >= h){
ans = mid;
r = mid - 1;
}
else{
l = mid + 1;
}
}
return ans;
}
/* Method to check if x is power of 2*/
private static boolean isPowerOfTwo (int x) {
/* First x in the below expression is
for the case when x is 0 */
return x!=0 && ((x&(x-1)) == 0);
}
//Taken From "Second Thread"
static void ruffleSort(int[] a){
int n = a.length;//shuffles, then sort;
for(int i=0; i<n; i++){
int oi = random.nextInt(n), temp = a[oi];
a[oi] = a[i]; a[i] = temp;
}
sort(a);
}
private static void longRuffleSort(long[] a){
long n = a.length;
for(long i = 0;i<n;i++){
long oi = random.nextInt((int) n), temp = a[(int) oi];
a[(int) oi] = a[(int) i]; a[(int) i] = temp;
}
longSort(a);
}
private static int gcd(int a, int b){
if(a == 0 || b == 0){
return 0;
}
while(b != 0){
int temp;
temp = a % b;
a = b;
b = temp;
}
return a;
}
private static long gcd(long a, long b){
if(a == 0 || b == 0){
return 0;
}
while(b != 0){
long temp;
temp = a % b;
a = b;
b = temp;
}
return a;
}
private static int lowestCommonMultiple(int a, int b){
return (a / gcd(a, b) * b);
}
/*
The Below func: the for loop runs in sqrt times. The second if is used to if the
divisors are equal to print only one of them otherwise we're printing both;
*/
private static void pDivisors(int n){
for(int i=1;i<Math.sqrt(n);i++){
if(n % i == 0){
if(n / i == i){
System.out.println(i + " ");
}
else{
System.out.println(i + " " + (n / i) + " ");
}
}
}
}
private static void returnNothing(){return;}
private static int numOfdigitsinN(int a){return (int) (Math.floor(Math.log10(a)) + 1);}
//prime Num till N: it takes any number and prints all the prime till that
//num;
private static boolean isPrime(int n){
if(n <= 1) return false;
if(n <= 3) return true;
//This is checked so that we can skip middle five number in below loop:
if(n % 2 == 0 || n % 3 == 0) return false;
for(int i = 5; i * i <= n; i = i + 6){
if(n % i == 0 || n % (i + 2) == 0){
return false;
}
}
return true;
}
//below func is to print the isPrime func();
private static void printTheIsPrimeFunc(int n){
for(int i=2;i<=n;i++){
if(isPrime(i)) System.out.println(i + " ");
}
}
public static boolean primeFinder(int n){
int m = 0;
int flag = 0;
m = n / 2;
if(n == 0 ||n == 1){
return false;
}
else{
for(int i=2;i<=m;i++){
if(n % i == 0){
return false;
}
}
return true;
}
}
private static boolean evenOdd(int num){
//System.out.println((num & 1) == 0 ? "EVEN" : "ODD");
return (num & 1) == 0 ? true : false;
}
private static boolean[] sieveOfEratosthenes(long n) {
boolean prime[] = new boolean[(int)n + 1];
for (int i = 0; i <= n; i++) {
prime[i] = true;
}
for (long p = 2; p * p <= n; p++) {
if (prime[(int)p] == true) {
for (long i = p * p; i <= n; i += p)
prime[(int)i] = false;
}
}
return prime;
}
private static int log2(int n){
int result = (int) (Math.log(n) / Math.log(2));
return result;
}
private static long add(long a, long b){
return (a + b) % mod;
}
private static long lcm(long a, long b){
return (a / gcd((int) a, (int) b) * b);
}
private 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);
}
private static void longSort(long[] a){
ArrayList<Long> l = new ArrayList<Long>();
for(long i:a) l.add(i);
Collections.sort(l);
for(int i=0;i<a.length;i++)a[i] = l.get(i);
}
public static int[][] prefixsum( int n , int m , int arr[][] ){
int prefixsum[][] = new int[n+1][m+1];
for( int i = 1 ;i <= n ;i++) {
for( int j = 1 ; j<= m ; j++) {
int toadd = 0;
if( arr[i-1][j-1] == 1) {
toadd = 1;
}
prefixsum[i][j] = toadd + prefixsum[i][j-1] + prefixsum[i-1][j] - prefixsum[i-1][j-1];
}
}
return prefixsum;
}
//call this method when you want to read an integer array;
private static int[] readArray(int len) throws IOException{
int[] a = new int[len];
for(int i=0;i<len;i++)a[i] = in.nextInt();
return a;
}
//call this method when you want to read an Long array;
private static long[] readLongArray(int len) throws IOException{
long[] a = new long[len];
for(int i=0;i<len;i++) a[i] = in.nextLong();
return a;
}
//call this method to print the integer array;
private static void printArray(int[] array){
for(int now : array) out.print(now);out.print(' ');out.close();
}
//call this method to print the long array;
private static void printLongArray(long[] array){
for(long now:array) out.print(now); out.print(' '); out.close();
}
/*Another way of Reading input...collected from a online blog
from here => https://codeforces.com/contest/1625/submission/144334744;*/
private static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {//Constructor;
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {//To take user input for String values;
final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException {//For taking the signs like plus or minus ...
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final 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();
}
final 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();
}
final 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 {
din.close();
}
}
/* A class representing a Mutable Multiset (Collectied From TechieDelight)*/
private static class Multiset<E> {
/* List to store distinct values */
private List<E> values;
/* List to store counts of distinct values */
private List<Integer> frequency;
private final String ERROR_MSG = "Count cannot be negative: ";
/* Constructor */
public Multiset() {
values = new ArrayList<>();
frequency = new ArrayList<>();
}
/**
* Adds an element to this multiset specified number of times
*
* @param `element` The element to be added
* @param `count` The number of times
* @return The previous count of the element
*/
public int add(E element, int count) {
if (count < 0) {
throw new IllegalArgumentException(ERROR_MSG + count);
}
int index = values.indexOf(element);
int prevCount = 0;
if (index != -1) {
prevCount = frequency.get(index);
frequency.set(index, prevCount + count);
}
else if (count != 0) {
values.add(element);
frequency.add(count);
}
return prevCount;
}
/**
* Adds specified element to this multiset
*
* @param `element` The element to be added
* @return true always
*/
public boolean add(E element) {
return add(element, 1) >= 0;
}
/**
* Adds all elements in the specified collection to this multiset
*
* @param `c` Collection containing elements to be added
* @return true if all elements are added to this multiset
*/
boolean addAll(Collection<? extends E> c) {
for (E element: c) {
add(element, 1);
}
return true;
}
/**
* Adds all elements in the specified array to this multiset
*
* @param `arr` An array containing elements to be added
*/
public void addAll(E... arr) {
for (E element: arr) {
add(element, 1);
}
}
/**
* Performs the given action for each element of the Iterable,
* including duplicates
*
* @param `action` The action to be performed for each element
*/
public void forEach(Consumer<? super E> action) {
List<E> all = new ArrayList<>();
for (int i = 0; i < values.size(); i++)
{
for (int j = 0; j < frequency.get(i); j++) {
all.add(values.get(i));
}
all.forEach(action);
}
}
/**
* Removes a single occurrence of the specified element from this multiset
*
* @param `element` The element to removed
* @return true if an occurrence was found and removed
*/
public boolean remove(Object element) {
return remove(element, 1) > 0;
}
/**
* Removes a specified number of occurrences of the specified element
* from this multiset
*
* @param `element` The element to removed
* @param `count` The number of occurrences to be removed
* @return The previous count
*/
public int remove(Object element, int count) {
if (count < 0) {
throw new IllegalArgumentException(ERROR_MSG + count);
}
int index = values.indexOf(element);
if (index == -1) {
return 0;
}
int prevCount = frequency.get(index);
if (prevCount > count) {
frequency.set(index, prevCount - count);
}
else {
values.remove(index);
frequency.remove(index);
}
return prevCount;
}
/**
* Check if this multiset contains at least one occurrence of the
* specified element
*
* @param `element` The element to be checked
* @return true if this multiset contains at least one occurrence
* of the element
*/
public boolean contains(Object element) {
return values.contains(element);
}
/**
* Check if this multiset contains at least one occurrence of each element
* in the specified collection
*
* @param `c` The collection of elements to be checked
* @return true if this multiset contains at least one occurrence
* of each element
*/
public boolean containsAll(Collection<?> c) {
return values.containsAll(c);
}
/**
* Update the frequency of an element to the specified count or
* add element to this multiset if not present
*
* @param `element` The element to be updated
* @param `count` The new count
* @return The previous count
*/
public int setCount(E element, int count) {
if (count < 0) {
throw new IllegalArgumentException(ERROR_MSG + count);
}
if (count == 0) {
remove(element);
}
int index = values.indexOf(element);
if (index == -1) {
return add(element, count);
}
int prevCount = frequency.get(index);
frequency.set(index, count);
return prevCount;
}
/**
* Find the frequency of an element in this multiset
*
* @param `element` The element to be counted
* @return The frequency of the element
*/
public int count(Object element) {
int index = values.indexOf(element);
return (index == -1) ? 0 : frequency.get(index);
}
/**
* @return A view of the set of distinct elements in this multiset
*/
public Set<E> elementSet() {
return values.stream().collect(Collectors.toSet());
}
/**
* @return true if this multiset is empty
*/
public boolean isEmpty() {
return values.size() == 0;
}
/**
* @return Total number of elements in this multiset, including duplicates
*/
public int size() {
int size = 0;
for (Integer i: frequency) {
size += i;
}
return size;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < values.size(); i++)
{
sb.append(values.get(i));
if (frequency.get(i) > 1) {
sb.append(" x ").append(frequency.get(i));
}
if (i != values.size() - 1) {
sb.append(", ");
}
}
return sb.append("]").toString();
}
}
} | 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 8 | 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 | c5f30782922a45ac652204d6ffc15e80 | 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.lang.*;
import java.io.*;
public class Main {
public static void main (String[] args) throws java.lang.Exception {
Scanner scn = new Scanner(System.in);
int tc = scn.nextInt();
while (tc != 0) {
int n = scn.nextInt();
int[] arr = new int[n];
for (int i = 0 ; i < n ; i++) {
arr[i] = scn.nextInt();
}
solve(arr);
tc--;
}
}
public static void solve(int[] arr) {
int n = arr.length;
int startIndex = 0;
int endIndex = 0;
for (int i = 1 ; i <= n - 1 ; i++) {
if (arr[i] == arr[i - 1]) {
startIndex = i - 1;
break;
}
}
for (int i = n - 1 ; i >= 1 ; i--) {
if (arr[i] == arr[i - 1]) {
endIndex = i;
break;
}
}
int noOfElementsInBW = endIndex - startIndex + 1;
if (noOfElementsInBW == 0 || noOfElementsInBW == 1 || noOfElementsInBW == 2) {
System.out.println("0");
return;
}
else if (noOfElementsInBW == 3 || noOfElementsInBW == 4) {
System.out.println("1");
return;
}
else if (noOfElementsInBW >= 5) {
System.out.println(noOfElementsInBW - 3);
return;
}
}
} | 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 8 | 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 | e2457685e499dd4497a238d6a334625a | 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;
/**
* <a href = "https://codeforces.com/contest/1672/problem/C"> Link </a>
* @author Bris
* @version 1.0
* @since 9:52:13 PM - Apr 23, 2022
*/
public class C1672 {
/**
* The main
* @param args .
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-->0) {
int n = scanner.nextInt();
int index = -1;
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
for (int i = 0; i < n-1; i++) {
if (a[i] == a[i+1]) {
index = i;
break;
}
}
int index1 = index;
for (int i = index+1; i < n-1; i++) {
if (a[i] == a[i+1]) {
index1 = i;
}
}
if (index1 > index) {
if (index1 == index+1) {
System.out.println(1);
}else {
System.out.println(index1 - index -1);
}
}else {
System.out.println(0);
}
}
scanner.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 8 | 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 | 0aff7fcf00b54d9dd76239d139428b4d | 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.*;
//import java.math.BigInteger;
public class code{
public static class Pair{
int a;
int b;
Pair(int i,int j){
a=i;
b=j;
}
}
public static int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
public static void shuffle(int a[], int n)
{
for (int i = 0; i < n; i++) {
// getting the random index
int t = (int)Math.random() * a.length;
// and swapping values a random index
// with the current index
int x = a[t];
a[t] = a[i];
a[i] = x;
}
}
public static void shuffle(long a[], int n)
{
for (int i = 0; i < n; i++) {
// getting the random index
int t = (int)Math.random() * a.length;
// and swapping values a random index
// with the current index
long x = a[t];
a[t] = a[i];
a[i] = x;
}
}
public static PrintWriter out = new PrintWriter(System.out);
public static long[][] dp;
//@SuppressWarnings("unchecked")
public static void main(String[] arg) throws IOException{
//Reader in=new Reader();
//PrintWriter out = new PrintWriter(System.out);
//Scanner in = new Scanner(System.in);
FastScanner in=new FastScanner();
int t=in.nextInt();
while(t-- > 0){
int n=in.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++) arr[i]=in.nextInt();
ArrayList<Pair> al=new ArrayList<Pair>();
int i=0;
while(i<n){
int j=i+1;
while(j<n && arr[j]==arr[i]) j++;
int c=j-i;
if(c>1){
Pair p=new Pair(i,j-1);
al.add(p);
}
i=j;
}
if(al.size()==0) out.println(0);
else if(al.size()==1){
Pair p=al.get(0);
int k=p.b-p.a+1;
if(k<4){
out.println(k-2);
}
else out.println(k-3);
}
else{
int k=al.get(al.size()-1).b-al.get(0).a;
out.println(k-2);
}
}
out.flush();
}
}
class Fenwick{
int[] bit;
public Fenwick(int n){
bit=new int[n];
//int sum=0;
}
public void update(int index,int val){
index++;
for(;index<bit.length;index += index&(-index)) bit[index]+=val;
}
public int presum(int index){
int sum=0;
for(;index>0;index-=index&(-index)) sum+=bit[index];
return sum;
}
public int sum(int l,int r){
return presum(r+1)-presum(l);
}
}
class FastScanner {
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) 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 8 | 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 | 7431d9ea3e2360430c8bc1225c6110cc | 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.*;
import java.lang.*;
import java.math.*;
public class Main {
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] ar = new int[n];
for (int i = 0; i < n; i++)
ar[i] = nextInt();
return ar;
}
long[] readArray2(int n) {
long[] ar = new long[n];
for (int i = 0; i < n; i++)
ar[i] = nextLong();
return ar;
}
}
static class DSU {
int[] parent;
int[] rank;
int[] sz;
public DSU(int N) {
parent = new int[N];
for (int i = 0; i < N; ++i)
parent[i] = i;
rank = new int[N];
sz = new int[N];
Arrays.fill(sz, 1);
}
public int find(int x) {
if (parent[x] != x) parent[x] = find(parent[x]);
return parent[x];
}
public void union(int x, int y) {
int xr = find(x), yr = find(y);
if (xr == yr) return;
if (rank[xr] < rank[yr]) {
int tmp = yr;
yr = xr;
xr = tmp;
}
if (rank[xr] == rank[yr])
rank[xr]++;
parent[yr] = xr;
sz[xr] += sz[yr];
}
public int size(int x) {
return sz[find(x)];
}
public int top() {
return size(sz.length - 1) - 1;
}
}
static class Pair{
int x;
int y;
char d,pd;
public Pair(int x,int y,char d,char pd){
this.x=x;this.y=y;this.d=d;this.pd=pd;
}
public String toString(){
return x+" "+y;
}
}
static class Query{
int a,b,c,d;
public Query(int a,int b,int c,int d){
this.a=a+1;this.b=b+1;this.c=c+1;this.d=d+1;
}
}
static class Recruit{
long cost;
long power;
public Recruit(long cost,long power){
this.cost=cost;this.power=power;
}
}
static class SegmentTree {
int[] segmentTree;
public SegmentTree(int n) {
segmentTree=new int[4*n];
segmentTree[0]=0;
buildSegmentTree(0,n-1,1);
}
private void buildSegmentTree(int start,int end,int treeIndex)
{
if(start==end)
{
segmentTree[treeIndex]=0;
return;
}
int mid=(start+end)/2;
buildSegmentTree(start,mid,2*treeIndex);
buildSegmentTree(mid+1,end,2*treeIndex+1);
segmentTree[treeIndex]=segmentTree[2*treeIndex]+segmentTree[2*treeIndex+1];
}
private void updateSegmentTree(int start,int end,int treeIndex,
int index,int val)
{
if(start==end)
{
segmentTree[treeIndex]=val;
return;
}
int mid=(start+end)/2;
if(index<=mid)
updateSegmentTree(start,mid,2*treeIndex,index,val);
else
updateSegmentTree(mid+1,end,2*treeIndex+1,index,val);
segmentTree[treeIndex]=segmentTree[2*treeIndex]+segmentTree[2*treeIndex+1];
}
private int findSumRangeSegmentTree(int start,int end,int treeIndex,
int left,int right)
{
if(start>=left&&end<=right)
return segmentTree[treeIndex];
if(start>right||end<left)
return 0;
if(start==end)
return 0;
int mid=(start+end)/2;
return findSumRangeSegmentTree(start,mid,2*treeIndex,left,right)+
findSumRangeSegmentTree(mid+1,end,2*treeIndex+1,left,right);
}
}
static class Next{
int even,odd;
public Next(){
even=odd=-1;
}
}
private static MyScanner sc;
private static SegmentTree st;
public static void main(String[] args) {
sc=new MyScanner();
int T=sc.nextInt();
for(int i=0;i<T;i++){
solve(i+1);
}
// solve(0);
}
private static void solve(int t) {
int n=sc.nextInt();
int[] a=sc.readArray(n);
int equality=0;
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1])
equality++;
}
if(equality<=1){
System.out.println(0);
return;
}
//System.out.println(Arrays.toString(a));
int first=-1,last=-1;
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1]){
if(first==-1)
first=i;
else
last=i;
}
}
System.out.println(last-first-1<=0?1:last-first-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 8 | 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 | 89ac025bc3b33d76daa5e5e3d1dbd8cc | 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 Main {
static long mod=(long)1e18+7;
static long[]fac=new long[1002];
static int n, x=0,me,op;
static int[]pe,a,aa, prime=new int[(int)1e7+1];
static int[][]perm;
static long[][]memo;
static Integer[]ps;
static TreeSet<Long>p=new TreeSet<Long>();
public static void main(String[] args) throws Exception{
int t =sc.nextInt();
while(t-->0) {
int n =sc.nextInt();
int[]a=sc.nextArrint(n);
int b =-1;
int l =-1;
for (int i = 0; i < a.length-1; i++) {
if(a[i]==a[i+1]) {
if(b==-1) {
b=i;
}
l=i+1;
}
}
int len = l-b+1;
if(b==-1) {
pw.println(0);
}else if(len==2)pw.println(0);
else {
if(len==3)pw.println(1);
else {
int res = 0;
len-=2;
res+=len-1;
pw.println(res);
}
}
}
pw.close();
}
public static long solFul(int m, int o) {
if(o<m)return 0;
if(o == op)return 1;
if(memo[m][o]!=-1) {
return memo[m][o];
}
return memo[m][o] = (solFul(m+1,o)+solFul(m, o+1))%mod;
}
public static long solFree(int m, int o) {
if(m<=o)return 0;
if(m == me)return 1;
if(o == op&&m>op)return 1;
if(memo[m][o]!=-1)return memo[m][o];
return memo[m][o] = (solFree(m+1,o)+solFree(m, o+1))%mod;
}
public static String rev(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
sb.append(s.charAt(s.length()-1-i));
}
return sb.toString();
}
public static int divNum(int n) {
int total = 1;
while(n>1) {
int p=prime[n];
int count =1;
while(n%p==0) {
n/=p;
count++;
}
total*=count;
}
return total;
}
public static void sieve() {
for (int i = 2; i < prime.length; i++) {
if(prime[i]==0) {
p.add((long)i);
for (int j = i; j < prime.length; j+=i) {
prime[j]++;
}
}
}
}
public static long[] Extended(long p, long q) {
if (q == 0)
return new long[] { p, 1, 0 };
long[] vals = Extended(q, p % q);
long d = vals[0];
long a = vals[2];
long b = vals[1] - (p / q) * vals[2];
return new long[] { d, a, b };
}
public static int LIS(int[] a) {
int n = a.length;
int[] ser = new int[n];
Arrays.fill(ser, Integer.MAX_VALUE);
int cur = -1;
for (int i = 0; i < n; i++) {
int low = 0;
int high = n - 1;
int mid = (low + high) / 2;
while (low <= high) {
if (ser[mid] < a[i]) {
low = mid + 1;
} else {
high = mid - 1;
}
mid = (low + high) / 2;
}
cur = Math.max(cur, high + 1);
ser[high + 1] = Math.min(ser[high + 1], a[i]);
}
return cur + 1;
}
public static void permutation(int idx,int v) {
if(v==(1<<n)-1) {
perm[x++]=pe.clone();
return ;
}
for (int i = 0; i < n; i++) {
if((v&1<<i)==0) {
pe[idx]=aa[i];
permutation(idx+1, v|1<<i);
}
}
return ;
}
public static void sort(int[]a) {
mergesort(a, 0, a.length-1);
}
public static void sortIdx(long[]a,long[]idx) {
mergesortidx(a, idx, 0, a.length-1);
}
public static long C(int a,int b) {
long x=fac[a];
long y=fac[a-b]*fac[b];
return x*pow(y,mod-2)%mod;
}
public static long pow(long a,long b) {
long ans=1;a%=mod;
for(long i=b;i>0;i/=2) {
if((i&1)!=0)
ans=ans*a%mod;
a=a*a%mod;
}
return ans;
}
public static void pre(){
fac[0]=1;
fac[1]=1;
fac[2]=2;
for (int i = 3; i < fac.length; i++) {
fac[i]=fac[i-1]*i%mod;
}
}
public static long eval(String s) {
long p=1;
long res=0;
for (int i = 0; i < s.length(); i++) {
res+=p*(s.charAt(s.length()-1-i)=='1'?1:0);
p*=2;
}
return res;
}
public static String binary(long x) {
String s="";
while(x!=0) {
s=(x%2)+s;
x/=2;
}
return s;
}
public static boolean allSame(String s) {
char x=s.charAt(0);
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i)!=x)return false;
}
return true;
}
public static boolean isPalindrom(String s) {
int l=0;
int r=s.length()-1;
while(l<r) {
if(s.charAt(r--)!=s.charAt(l++))return false;
}
return true;
}
public static boolean isSubString(String s,String t) {
int ls=s.length();
int lt=t.length();
boolean res=false;
for (int i = 0; i <=lt-ls; i++) {
if(t.substring(i, i+ls).equals(s)) {
res=true;
break;
}
}
return res;
}
public static boolean isSorted(long[]a) {
for (int i = 0; i < a.length-1; i++) {
if(a[i]>a[i+1])return false;
}
return true;
}
public static long evaln(String x,int n) {
long res=0;
for (int i = 0; i < x.length(); i++) {
res+=Long.parseLong(x.charAt(x.length()-1-i)+"")*Math.pow(n, i);
}
return res;
}
static void mergesort(int[] arr,int b,int e) {
if(b<e) {
int m=b+(e-b)/2;
mergesort(arr,b,m);
mergesort(arr,m+1,e);
merge(arr,b,m,e);
}
return;
}
static void merge(int[] arr,int b,int m,int e) {
int len1=m-b+1,len2=e-m;
int[] l=new int[len1];
int[] r=new int[len2];
for(int i=0;i<len1;i++)l[i]=arr[b+i];
for(int i=0;i<len2;i++)r[i]=arr[m+1+i];
int i=0,j=0,k=b;
while(i<len1 && j<len2) {
if(l[i]<r[j])arr[k++]=l[i++];
else arr[k++]=r[j++];
}
while(i<len1)arr[k++]=l[i++];
while(j<len2)arr[k++]=r[j++];
return;
}
static void mergesortidx(long[] arr,long[]idx,int b,int e) {
if(b<e) {
int m=b+(e-b)/2;
mergesortidx(arr,idx,b,m);
mergesortidx(arr,idx,m+1,e);
mergeidx(arr,idx,b,m,e);
}
return;
}
static void mergeidx(long[] arr,long[]idx,int b,int m,int e) {
int len1=m-b+1,len2=e-m;
long[] l=new long[len1];
long[] lidx=new long[len1];
long[] r=new long[len2];
long[] ridx=new long[len2];
for(int i=0;i<len1;i++) {
l[i]=arr[b+i];
lidx[i]=idx[b+i];
}
for(int i=0;i<len2;i++) {
r[i]=arr[m+1+i];
ridx[i]=idx[m+1+i];
}
int i=0,j=0,k=b;
while(i<len1 && j<len2) {
if(l[i]<=r[j]) {
arr[k++]=l[i++];
idx[k-1]=lidx[i-1];
}
else {
arr[k++]=r[j++];
idx[k-1]=ridx[j-1];
}
}
while(i<len1) {
idx[k]=lidx[i];
arr[k++]=l[i++];
}
while(j<len2) {
idx[k]=ridx[j];
arr[k++]=r[j++];
}
return;
}
static long mergen(int[] arr,int b,int m,int e) {
int len1=m-b+1,len2=e-m;
int[] l=new int[len1];
int[] r=new int[len2];
for(int i=0;i<len1;i++)l[i]=arr[b+i];
for(int i=0;i<len2;i++)r[i]=arr[m+1+i];
int i=0,j=0,k=b;
long c=0;
while(i<len1 && j<len2) {
if(l[i]<r[j])arr[k++]=l[i++];
else {
arr[k++]=r[j++];
c=c+(long)(len1-i);
}
}
while(i<len1)arr[k++]=l[i++];
while(j<len2)arr[k++]=r[j++];
return c;
}
static long mergesortn(int[] arr,int b,int e) {
long c=0;
if(b<e) {
int m=b+(e-b)/2;
c=c+(long)mergesortn(arr,b,m);
c=c+(long)mergesortn(arr,m+1,e);
c=c+(long)mergen(arr,b,m,e);
}
return c;
}
public static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static long sumd(long x) {
long sum=0;
while(x!=0) {
sum+=x%10;
x=x/10;
}
return sum;
}
public static ArrayList<Integer> findDivisors(int n){
ArrayList<Integer>res=new ArrayList<Integer>();
for (int i=1; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
// If divisors are equal, print only one
if (n/i == i)
res.add(i);
else {
res.add(i);
res.add(n/i);
}
}
}
return res;
}
public static void sort2darray(Integer[][]a){
Arrays.sort(a,Comparator.<Integer[]>comparingInt(x -> x[0]).thenComparingInt(x -> x[1]));
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws FileNotFoundException {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public 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 {
return Double.parseDouble(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public int[] nextArrint(int size) throws IOException {
int[] a=new int[size];
for (int i = 0; i < a.length; i++) {
a[i]=sc.nextInt();
}
return a;
}
public long[] nextArrlong(int size) throws IOException {
long[] a=new long[size];
for (int i = 0; i < a.length; i++) {
a[i]=sc.nextLong();
}
return a;
}
public int[][] next2dArrint(int rows,int columns) throws IOException{
int[][]a=new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
a[i][j]=sc.nextInt();
}
}
return a;
}
public long[][] next2dArrlong(int rows,int columns) throws IOException{
long[][]a=new long[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
a[i][j]=sc.nextLong();
}
}
return a;
}
}
static class Segment{
long x;
long y;
long m;
public Segment(long x,long y, long m) {
this.x=x;
this.y=y;
this.m=m;
}
@Override
public String toString() {
return x+" "+y+" "+m;
}
}
static Scanner sc=new Scanner(System.in);
static PrintWriter pw=new PrintWriter(System.out);
} | 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 8 | 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 | 37c1d728e9da9a3f355f4f81bde70231 | 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.*;
//import javafx.util.*;
public class Main
{
static PrintWriter out = new PrintWriter(System.out);
static FastReader in = new FastReader();
static int INF = Integer.MAX_VALUE;
static int NINF = Integer.MIN_VALUE;
public static StringBuilder str = new StringBuilder();
public static boolean flag = true;
public static ArrayList<Integer> arr = new ArrayList<>();
public static void main (String[] args) throws java.lang.Exception
{
//check if you have to take product or the constraints are big
int t = i();
while(t-- > 0){
int n = i();
int[] arr = input(n);
int count = 1;
long ans = 0l;
int count_total = 0;
boolean check = false;
for(int i = 1;i < n;i++){
if(arr[i] == arr[i-1]){
count++;
if(!check){
count_total++;
check = true;
}
}else{
check = false;
}
}
if(count_total == 0){
out.println(0);
continue;
}
if(count_total == 1){
if(count == 2){
out.println(0);
}else if(count == 3){
out.println(1);
}else{
out.println(count - 3);
}
continue;
}
int si = -1;
int ei = -1;
for(int i = 1;i < n;i++){
if(arr[i] == arr[i-1]){
si = i;
break;
}
}
for(int i = n-1;i > 0;i--){
if(arr[i] == arr[i-1]){
ei = i-1;
break;
}
}
out.println(ei - si);
}
out.close();
}
public static void sort(int[] arr){
ArrayList<Integer> ls = new ArrayList<>();
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 reverse(int[] arr){
int n = arr.length;
for(int i = 0;i < n/2;i++){
int temp = arr[i];
arr[i] = arr[n-1-i];
arr[n-1-i] = temp;
}
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++)A[i]=in.nextLong();
return A;
}
}
class pair implements Comparable<pair> {
int x;
int y;
public pair(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
// public int hashCode() {
// return new Integer(x).hashCode() * 31 + new Integer(y).hashCode();
// }
public int compareTo(pair other) {
if (this.x == other.x) {
return Integer.compare(this.y, other.y);
}
return Integer.compare(this.x, other.x);
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while(st==null || !st.hasMoreElements())
{
try
{
st=new StringTokenizer(br.readLine());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
} | Java | ["4\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 8 | 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 | c79b3925fa7db20663a07049e4e5fe78 | 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 Main {
public static void main(String[] args) {
// try {
// System.setIn(new FileInputStream("input.txt"));
// System.setOut(new PrintStream(new FileOutputStream("output.txt")));
// } catch (Exception e) {
// System.err.println("Error");
// }
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-- > 0) {
int n = in.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
System.out.println(solve(arr));
}
}
static int solve(int[] arr) {
int s = -1;
int e = -1;
boolean flag = true;
for(int i = 0; i < arr.length-1; i++) {
if(arr[i] == arr[i+1]) {
if(flag) {
s = i+1;
flag = false;
} else {
e = i;
}
}
}
if(s == -1 || e == -1) return 0;
else if(s == e) return 1;
return e - s;
}
}
| 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 8 | 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 | 2c572deb8c62eaff50d9fcf41c15c017 | 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 Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-->0) {
solve(in);
}
}
public static void solve(Scanner in) {
int n = in.nextInt();
int[] arr = new int[n];
int low = Integer.MAX_VALUE;
int hi = Integer.MIN_VALUE;
for (int i = 0; i < n; ++i) {
arr[i] = in.nextInt();
if (i > 0 && arr[i] == arr[i - 1]) {
low = Math.min(low, i);
hi = Math.max(hi, i - 1);
}
}
if (low == Integer.MAX_VALUE || low > hi) {
System.out.println(0);
return ;
}
System.out.println(Math.max(1, hi - low));
}
}
| 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 8 | 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 | d2ebc752ab2476375d4920572fa0b8ee | train_108.jsonl | 1650722700 | There is an array $$$a$$$ of length $$$n$$$. You may perform the following operation any number of times: Choose two indices $$$l$$$ and $$$r$$$ where $$$1 \le l < r \le n$$$ and $$$a_l = a_r$$$. Then, set $$$a[l \ldots r] = [a_{l+1}, a_{l+2}, \ldots, a_r, a_l]$$$. You are also given another array $$$b$$$ of length $$$n$$$ which is a permutation of $$$a$$$. Determine whether it is possible to transform array $$$a$$$ into an array $$$b$$$ using the above operation some number of times. | 256 megabytes |
import java.sql.Array;
import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class Main {
public static FastReader cin;
public static PrintWriter out;
public static void main(String[] args) throws Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
cin = new FastReader();
int ttt = cin.nextInt();
label:for(int qqq = 1; qqq <= ttt; qqq++){
int n = cin.nextInt();
int[] a = new int [n];
int[] b = new int [n];
int[] cnt = new int [n + 1];
for(int i = 0; i < n; i++){
a[i] = cin.nextInt();
}
for(int i = 0; i < n; i++){
b[i] = cin.nextInt();
}
int pa = 0;
int pb = 0;
while(pb < n){
//如果当前两个指针指向的元素相同那么直接+1
if(pa < n && pb < n && a[pa] == b[pb]){
pa++;
pb++;
continue;
}
//指向元素不相同
//满足条件将当前元素替换
if(cnt[b[pb]] > 0 && b[pb] == b[pb - 1]){
cnt[b[pb++]]--;
}else if(pa < n){
cnt[a[pa++]]++;
}else{
out.println("NO");
continue label;
}
}
if(pa != n){
out.println("NO");
continue label;
}
for(int i = 0; i <= n; i++){
if(cnt[i] != 0){
out.println("NO");
continue label;
}
}
out.println("YES");
}
out.close();
}
public static long lcm(long a,long b ){
long ans = a / gcd(a,b) * b ;
return ans;
}
public static long gcd(long a,long b){
if(b==0)return a;
else return gcd(b,a%b);
}
static class FastReader {
BufferedReader br;
StringTokenizer str;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (str == null || !str.hasMoreElements()) {
try {
str = new StringTokenizer(br.readLine());
} catch (IOException lastMonthOfVacation) {
lastMonthOfVacation.printStackTrace();
}
}
return str.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 lastMonthOfVacation) {
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
}
| Java | ["5\n\n5\n\n1 2 3 3 2\n\n1 3 3 2 2\n\n5\n\n1 2 4 2 1\n\n4 2 2 1 1\n\n5\n\n2 4 5 5 2\n\n2 2 4 5 5\n\n3\n\n1 2 3\n\n1 2 3\n\n3\n\n1 1 2\n\n2 1 1"] | 1 second | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case, we can choose $$$l=2$$$ and $$$r=5$$$ to form $$$[1, 3, 3, 2, 2]$$$.In the second test case, we can choose $$$l=2$$$ and $$$r=4$$$ to form $$$[1, 4, 2, 2, 1]$$$. Then, we can choose $$$l=1$$$ and $$$r=5$$$ to form $$$[4, 2, 2, 1, 1]$$$.In the third test case, it can be proven that it is not possible to transform array $$$a$$$ to $$$b$$$ using the operation. | Java 17 | standard input | [
"constructive algorithms",
"greedy",
"implementation",
"two pointers"
] | 158bd0ab8f4319f5fd1e6062caddad4e | 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$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — elements of the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le n$$$) — elements of the array $$$b$$$. It is guaranteed that $$$b$$$ is a permutation of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$ | 1,700 | For each test case, print "YES" (without quotes) if it is possible to transform array $$$a$$$ to $$$b$$$, 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 | 2fd5be76082ebb26cfc9c19d792a38be | train_108.jsonl | 1650722700 | There is an array $$$a$$$ of length $$$n$$$. You may perform the following operation any number of times: Choose two indices $$$l$$$ and $$$r$$$ where $$$1 \le l < r \le n$$$ and $$$a_l = a_r$$$. Then, set $$$a[l \ldots r] = [a_{l+1}, a_{l+2}, \ldots, a_r, a_l]$$$. You are also given another array $$$b$$$ of length $$$n$$$ which is a permutation of $$$a$$$. Determine whether it is possible to transform array $$$a$$$ into an array $$$b$$$ using the above operation some number of times. | 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 b[] = io.readArray(n);
int done[] = new int[n + 1];
int i = 0, j = 0, lastElement = -1;
while(i < n && j < n) {
// io.println(i + " " + j + " " + st);
if(a[i] != b[j]) {
if(done[b[j]] >= 1 && b[j] == lastElement) {
done[b[j]]--;
j++;
// io.println("here");
}else {
done[a[i]]++;
i++;
lastElement = -1;
}
}else {
lastElement = a[i];
i++;
j++;
}
}
while(j < n && done[b[j]] >= 1 && lastElement == b[j]) {
done[b[j]]--;
j++;
}
boolean ans = true;
for(int k = 1; k <= n; k++) {
ans &= done[k] == 0;
}
binaryOutput(ans, io);
}
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 | ["5\n\n5\n\n1 2 3 3 2\n\n1 3 3 2 2\n\n5\n\n1 2 4 2 1\n\n4 2 2 1 1\n\n5\n\n2 4 5 5 2\n\n2 2 4 5 5\n\n3\n\n1 2 3\n\n1 2 3\n\n3\n\n1 1 2\n\n2 1 1"] | 1 second | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case, we can choose $$$l=2$$$ and $$$r=5$$$ to form $$$[1, 3, 3, 2, 2]$$$.In the second test case, we can choose $$$l=2$$$ and $$$r=4$$$ to form $$$[1, 4, 2, 2, 1]$$$. Then, we can choose $$$l=1$$$ and $$$r=5$$$ to form $$$[4, 2, 2, 1, 1]$$$.In the third test case, it can be proven that it is not possible to transform array $$$a$$$ to $$$b$$$ using the operation. | Java 17 | standard input | [
"constructive algorithms",
"greedy",
"implementation",
"two pointers"
] | 158bd0ab8f4319f5fd1e6062caddad4e | 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$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — elements of the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le n$$$) — elements of the array $$$b$$$. It is guaranteed that $$$b$$$ is a permutation of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$ | 1,700 | For each test case, print "YES" (without quotes) if it is possible to transform array $$$a$$$ to $$$b$$$, 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 | c24ed3fea23a05c3afcd59a86204b984 | train_108.jsonl | 1650722700 | There is an array $$$a$$$ of length $$$n$$$. You may perform the following operation any number of times: Choose two indices $$$l$$$ and $$$r$$$ where $$$1 \le l < r \le n$$$ and $$$a_l = a_r$$$. Then, set $$$a[l \ldots r] = [a_{l+1}, a_{l+2}, \ldots, a_r, a_l]$$$. You are also given another array $$$b$$$ of length $$$n$$$ which is a permutation of $$$a$$$. Determine whether it is possible to transform array $$$a$$$ into an array $$$b$$$ using the above operation some number of times. | 256 megabytes |
import java.util.*;
import java.io.*;
public class G {
public static void main(String[] args) throws IOException {
Soumit sc = new Soumit();
int tc = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (tc-->0){
int n = sc.nextInt();
int[] a = sc.nextIntArray(n);
int[] b = sc.nextIntArray(n);
HashMap<Integer, Integer> hash = new HashMap<>();
int ptr = n-1;
boolean flag = true;
for(int i=n-1;i>=0;i--){
if(a[ptr] != b[i]){
if(ptr == n-1){
flag = false;
break;
}
while((b[i] != a[ptr] && b[i] != b[i+1]) && ptr > i && hash.getOrDefault(a[ptr], 0) > 0){
hash.put(a[ptr], hash.get(a[ptr]) - 1);
ptr--;
}
if(b[i] != a[ptr] && b[i] != b[i+1]){
flag = false;
break;
}
if(a[ptr] == b[i]){
ptr--;
}
else if(b[i] == b[i+1]){
hash.put(b[i], hash.getOrDefault(b[i], 0) + 1);
}
}
else{
ptr--;
}
}
if(flag){
sb.append("YES\n");
}
else{
sb.append("NO\n");
}
}
System.out.println(sb);
sc.close();
}
static class Soumit {
final private int BUFFER_SIZE = 1 << 18;
final private DataInputStream din;
final private byte[] buffer;
private PrintWriter pw;
private int bufferPointer, bytesRead;
StringTokenizer st;
public Soumit() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Soumit(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public void streamOutput(String file) throws IOException {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
pw = new PrintWriter(bw);
}
public void println(String a) {
pw.println(a);
}
public void print(String a) {
pw.print(a);
}
public String readLine() throws IOException {
byte[] buf = new byte[3000064]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public void sort(int[] arr) {
ArrayList<Integer> arlist = new ArrayList<>();
for (int i : arr)
arlist.add(i);
Collections.sort(arlist);
for (int i = 0; i < arr.length; i++)
arr[i] = arlist.get(i);
}
public void sort(long[] arr) {
ArrayList<Long> arlist = new ArrayList<>();
for (long i : arr)
arlist.add(i);
Collections.sort(arlist);
for (int i = 0; i < arr.length; i++)
arr[i] = arlist.get(i);
}
public int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = nextDouble();
}
return arr;
}
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;*/
if (din != null) din.close();
if (pw != null) pw.close();
}
}
}
| Java | ["5\n\n5\n\n1 2 3 3 2\n\n1 3 3 2 2\n\n5\n\n1 2 4 2 1\n\n4 2 2 1 1\n\n5\n\n2 4 5 5 2\n\n2 2 4 5 5\n\n3\n\n1 2 3\n\n1 2 3\n\n3\n\n1 1 2\n\n2 1 1"] | 1 second | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case, we can choose $$$l=2$$$ and $$$r=5$$$ to form $$$[1, 3, 3, 2, 2]$$$.In the second test case, we can choose $$$l=2$$$ and $$$r=4$$$ to form $$$[1, 4, 2, 2, 1]$$$. Then, we can choose $$$l=1$$$ and $$$r=5$$$ to form $$$[4, 2, 2, 1, 1]$$$.In the third test case, it can be proven that it is not possible to transform array $$$a$$$ to $$$b$$$ using the operation. | Java 17 | standard input | [
"constructive algorithms",
"greedy",
"implementation",
"two pointers"
] | 158bd0ab8f4319f5fd1e6062caddad4e | 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$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — elements of the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le n$$$) — elements of the array $$$b$$$. It is guaranteed that $$$b$$$ is a permutation of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$ | 1,700 | For each test case, print "YES" (without quotes) if it is possible to transform array $$$a$$$ to $$$b$$$, 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 | 44c202af749caa3032143be13d3e9a51 | train_108.jsonl | 1650722700 | There is an array $$$a$$$ of length $$$n$$$. You may perform the following operation any number of times: Choose two indices $$$l$$$ and $$$r$$$ where $$$1 \le l < r \le n$$$ and $$$a_l = a_r$$$. Then, set $$$a[l \ldots r] = [a_{l+1}, a_{l+2}, \ldots, a_r, a_l]$$$. You are also given another array $$$b$$$ of length $$$n$$$ which is a permutation of $$$a$$$. Determine whether it is possible to transform array $$$a$$$ into an array $$$b$$$ using the above operation some number of times. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.*;
import java.util.Scanner;
import java.util.StringTokenizer;
public class copy {
static int log=18;
static int[][] ancestor;
static int[] depth;
static void sieveOfEratosthenes(int n, ArrayList<Integer> arr) {
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then it is a
// prime
if (prime[p]) {
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for (int i = 2; i <= n; i++) {
if (prime[i]) {
arr.add(i);
}
}
}
public static long fac(long N, long mod) {
if (N == 0)
return 1;
if(N==1)
return 1;
return ((N % mod) * (fac(N - 1, mod) % mod)) % mod;
}
static long power(long x, long y, long p) {
// Initialize result
long res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p) {
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static long nCrModPFermat(long n, long r,
long p) {
if (n < r)
return 0;
// Base case
if (r == 0)
return 1;
return ((fac(n, p) % p * (modInverse(fac(r, p), p)
% p)) % p * (modInverse(fac(n - r, p), p)
% p)) % p;
}
public static int find(int[] parent, int x) {
if (parent[x] == x)
return x;
return find(parent, parent[x]);
}
public static void merge(int[] parent, int[] rank, int x, int y,int[] child) {
int x1 = find(parent, x);
int y1 = find(parent, y);
if (rank[x1] > rank[y1]) {
parent[y1] = x1;
child[x1]+=child[y1];
} else if (rank[y1] > rank[x1]) {
parent[x1] = y1;
child[y1]+=child[x1];
} else {
parent[y1] = x1;
child[x1]+=child[y1];
rank[x1]++;
}
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static int power(int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static int modInverse(int n, int p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static int nCrModPFermat(int n, int r,
int p)
{
if (n<r)
return 0;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
int[] fac = new int[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p)
% p * modInverse(fac[n - r], p)
% p)
% p;
}
public static long[][] ncr(int n,int r)
{
long[][] dp=new long[n+1][r+1];
for(int i=0;i<=n;i++)
dp[i][0]=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=r;j++)
{
if(j>i)
continue;
dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
}
}
return dp;
}
public static boolean prime(long N)
{
int c=0;
for(int i=2;i*i<=N;i++)
{
if(N%i==0)
++c;
}
return c==0;
}
public static int sparse_ancestor_table(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] child)
{
int c=0;
for(int i:arr.get(x))
{
if(i!=parent)
{
// System.out.println(i+" hello "+x);
depth[i]=depth[x]+1;
ancestor[i][0]=x;
// if(i==2)
// System.out.println(parent+" hello");
for(int j=1;j<log;j++)
ancestor[i][j]=ancestor[ancestor[i][j-1]][j-1];
c+=sparse_ancestor_table(arr,i,x,child);
}
}
child[x]=1+c;
return child[x];
}
public static int lca(int x,int y)
{
if(depth[x]<depth[y])
{
int temp=x;
x=y;
y=temp;
}
x=get_kth_ancestor(depth[x]-depth[y],x);
if(x==y)
return x;
// System.out.println(x+" "+y);
for(int i=log-1;i>=0;i--)
{
if(ancestor[x][i]!=ancestor[y][i])
{
x=ancestor[x][i];
y=ancestor[y][i];
}
}
return ancestor[x][0];
}
public static int get_kth_ancestor(int K,int x)
{
if(K==0)
return x;
int node=x;
for(int i=0;i<log;i++)
{
if(K%2!=0)
{
node=ancestor[node][i];
}
K/=2;
}
return node;
}
public static ArrayList<Integer> primeFactors(int n)
{
// Print the number of 2s that divide n
ArrayList<Integer> factors=new ArrayList<>();
if(n%2==0)
factors.add(2);
while (n%2==0)
{
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
if(n%i==0)
factors.add(i);
while (n%i == 0)
{
n /= i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
{
factors.add(n);
}
return factors;
}
static long ans=1,mod=1000000007;
public static void recur(long X,long N,int index,ArrayList<Integer> temp)
{
// System.out.println(X);
if(index==temp.size())
{
System.out.println(X);
ans=((ans%mod)*(X%mod))%mod;
return;
}
for(int i=0;i<=60;i++)
{
if(X*Math.pow(temp.get(index),i)<=N)
recur(X*(long)Math.pow(temp.get(index),i),N,index+1,temp);
else
break;
}
}
public static int upper(ArrayList<Integer> temp,int X)
{
int l=0,r=temp.size()-1;
while(l<=r)
{
int mid=(l+r)/2;
if(temp.get(mid)==X)
return mid;
// System.out.println(mid+" "+temp.get(mid));
if(temp.get(mid)<X)
l=mid+1;
else
{
if(mid-1>=0 && temp.get(mid-1)>=X)
r=mid-1;
else
return mid;
}
}
return -1;
}
public static int lower(ArrayList<Integer> temp,int X)
{
int l=0,r=temp.size()-1;
while(l<=r)
{
int mid=(l+r)/2;
if(temp.get(mid)==X)
return mid;
// System.out.println(mid+" "+temp.get(mid));
if(temp.get(mid)>X)
r=mid-1;
else
{
if(mid+1<temp.size() && temp.get(mid+1)<=X)
l=mid+1;
else
return mid;
}
}
return -1;
}
public static int[] check(String shelf,int[][] queries)
{
int[] arr=new int[queries.length];
ArrayList<Integer> indices=new ArrayList<>();
for(int i=0;i<shelf.length();i++)
{
char ch=shelf.charAt(i);
if(ch=='|')
indices.add(i);
}
for(int i=0;i<queries.length;i++)
{
int x=queries[i][0]-1;
int y=queries[i][1]-1;
int left=upper(indices,x);
int right=lower(indices,y);
if(left<=right && left!=-1 && right!=-1)
{
arr[i]=indices.get(right)-indices.get(left)+1-(right-left+1);
}
else
arr[i]=0;
}
return arr;
}
static boolean check;
public static void check(ArrayList<ArrayList<Integer>> arr,int x,int[] color,boolean[] visited)
{
visited[x]=true;
PriorityQueue<Integer> pq=new PriorityQueue<>();
for(int i:arr.get(x))
{
if(color[i]<color[x])
pq.add(color[i]);
if(color[i]==color[x])
check=false;
if(!visited[i])
check(arr,i,color,visited);
}
int start=1;
while(pq.size()>0)
{
int temp=pq.poll();
if(temp==start)
++start;
else
break;
}
if(start!=color[x])
check=false;
}
static boolean cycle;
public static void cycle(boolean[] stack,boolean[] visited,int x,ArrayList<ArrayList<Integer>> arr)
{
if(stack[x])
{
cycle=true;
return;
}
visited[x]=true;
for(int i:arr.get(x))
{
if(!visited[x])
{
cycle(stack,visited,i,arr);
}
}
stack[x]=false;
}
public static int check(char[][] ch,int x,int y)
{
int cnt=0;
int c=0;
int N=ch.length;
int x1=x,y1=y;
while(c<ch.length)
{
if(ch[x][y]=='1')
++cnt;
// if(x1==0 && y1==3)
// System.out.println(x+" "+y+" "+cnt);
x=(x+1)%N;
y=(y+1)%N;
++c;
}
return cnt;
}
public static void s(char[][] arr,int x)
{
char start=arr[arr.length-1][x];
for(int i=arr.length-1;i>0;i--)
{
arr[i][x]=arr[i-1][x];
}
arr[0][x]=start;
}
public static void shuffle(char[][] arr,int x,int down)
{
int N= arr.length;
down%=N;
char[] store=new char[N-down];
for(int i=0;i<N-down;i++)
store[i]=arr[i][x];
for(int i=0;i<arr.length;i++)
{
if(i<down)
{
// Printing rightmost
// kth elements
arr[i][x]=arr[N + i - down][x];
}
else
{
// Prints array after
// 'k' elements
arr[i][x]=store[i-down];
}
}
}
public static String form(int C1,char ch1,char ch2)
{
char ch=ch1;
String s="";
for(int i=1;i<=C1;i++)
{
s+=ch;
if(ch==ch1)
ch=ch2;
else
ch=ch1;
}
return s;
}
public static void printArray(long[] arr)
{
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
public static boolean check(long mid,long[] arr,long K)
{
long[] arr1=Arrays.copyOfRange(arr,0,arr.length);
long ans=0;
for(int i=0;i<arr1.length-1;i++)
{
if(arr1[i]+arr1[i+1]>=mid)
{
long check=(arr1[i]+arr1[i+1])/mid;
// if(mid==5)
// System.out.println(check);
long left=check*mid;
left-=arr1[i];
if(left>=0)
arr1[i+1]-=left;
ans+=check;
}
// if(mid==5)
// printArray(arr1);
}
// if(mid==5)
// System.out.println(ans);
ans+=arr1[arr1.length-1]/mid;
return ans>=K;
}
public static long search(long sum,long[] arr,long K)
{
long l=1,r=sum/K;
while(l<=r)
{
long mid=(l+r)/2;
if(check(mid,arr,K))
{
if(mid+1<=sum/K && check(mid+1,arr,K))
l=mid+1;
else
return mid;
}
else
r=mid-1;
}
return -1;
}
public static void primeFactors(int n,HashSet<Integer> hp)
{
// Print the number of 2s that divide n
ArrayList<Integer> factors=new ArrayList<>();
if(n%2==0)
hp.add(2);
while (n%2==0)
{
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
if(n%i==0)
hp.add(i);
while (n%i == 0)
{
n /= i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
{
hp.add(n);
}
}
public static boolean check(String s)
{
HashSet<Character> hp=new HashSet<>();
char ch=s.charAt(0);
for(int i=1;i<s.length();i++)
{
// System.out.println(hp+" "+s.charAt(i));
if(hp.contains(s.charAt(i)))
{
// System.out.println(i);
// System.out.println(hp);
// System.out.println(s.charAt(i));
return false;
}
if(s.charAt(i)!=ch)
{
hp.add(ch);
ch=s.charAt(i);
}
}
return true;
}
public static int check_end(String[] arr,boolean[] st,char ch)
{
for(int i=0;i<arr.length;i++)
{
if(ch==arr[i].charAt(0) && !st[i] && ch==arr[i].charAt(arr[i].length()-1))
return i;
}
for(int i=0;i<arr.length;i++)
{
if(ch==arr[i].charAt(0) && !st[i])
return i;
}
return -1;
}
public static int check_start(String[] arr,boolean[] st,char ch)
{
for(int i=0;i<arr.length;i++)
{
// if(ch=='B')
// {
// if(!st[i])
// System.out.println(arr[i]+" hello");
// }
if(ch==arr[i].charAt(arr[i].length()-1) && !st[i] && ch==arr[i].charAt(0))
return i;
}
for(int i=0;i<arr.length;i++)
{
// if(ch=='B')
// {
// if(!st[i])
// System.out.println(arr[i]+" hello");
// }
if(ch==arr[i].charAt(arr[i].length()-1) && !st[i])
return i;
}
return -1;
}
public static boolean palin(int N)
{
String s="";
while(N>0)
{
s+=N%10;
N/=10;
}
int l=0,r=s.length()-1;
while(l<=r)
{
if(s.charAt(l)!=s.charAt(r))
return false;
++l;
--r;
}
return true;
}
public static boolean check(long org_s,long org_d,long org_n,long check_ele)
{
if(check_ele<org_s)
return false;
if((check_ele-org_s)%org_d!=0)
return false;
long num=(check_ele-org_s)/org_d;
// if(check_ele==5)
// System.out.println(num+" "+org_n);
return num+1<=org_n;
}
public static long check(long c,long c_diff,long mod,long b_start,long c_start, long c_end,long b_end,long b_diff)
{
// System.out.println(c);
long max=Math.max(c,b_diff);
long min=Math.min(c,b_diff);
long lcm=(max/gcd(max,min))*min;
// System.out.println(lcm);
// System.out.println(c);
// System.out.println(c_diff);
// if(b_diff>c)
// {
long start_point=c_diff/c-c_diff/lcm;
// System.out.println(start_point);
// }
// else
// {
// start_point=c_diff/b_diff-c_diff/c;
// }
// System.out.println(c+" "+start_point);
return (start_point%mod*start_point%mod)%mod;
}
public static boolean check_bounds(int x,int y,int[][] arr,int N,int M)
{
return x>=0 && x<N && y>=0 && y<M && (arr[x][y]==1 || arr[x][y]==9);
}
static boolean found=false;
public static void check(int x,int y,int[][] arr,boolean status[][])
{
if(arr[x][y]==9)
{
found=true;
return;
}
status[x][y]=true;
if(check_bounds(x-1,y,arr,arr.length,arr[0].length)&& !status[x-1][y])
check(x-1,y,arr,status);
if(check_bounds(x+1,y,arr,arr.length,arr[0].length)&& !status[x+1][y])
check(x+1,y,arr,status);
if(check_bounds(x,y-1,arr,arr.length,arr[0].length)&& !status[x][y-1])
check(x,y-1,arr,status);
if(check_bounds(x,y+1,arr,arr.length,arr[0].length)&& !status[x][y+1])
check(x,y+1,arr,status);
}
public static int check(String s1,String s2,int M)
{
int ans=0;
for(int i=0;i<M;i++)
{
ans+=Math.abs(s1.charAt(i)-s2.charAt(i));
}
return ans;
}
public static int check(int[][] arr,int dir1,int dir2,int x1,int y1)
{
int sum=0,N=arr.length,M=arr[0].length;
int x=x1+dir1,y=y1+dir2;
while(x<N && x>=0 && y<M && y>=0)
{
sum+=arr[x][y];
x=x+dir1;
y+=dir2;
}
return sum;
}
public static int check(long[] pref,long X,int N)
{
if(X>pref[N-1])
return -1;
// System.out.println(pref[0]);
if(X<=pref[0])
return 1;
int l=0,r=N-1;
while(l<=r)
{
int mid=(l+r)/2;
if(pref[mid]>=X)
{
if(mid-1>=0 && pref[mid-1]<X)
return mid+1;
else
r=mid-1;
}
else
l=mid+1;
}
return -1;
}
public static div dfs(ArrayList<ArrayList<Integer>> arr,int x,int parent,char[] c)
{
int w=0,b=0;
for(int i:arr.get(x))
{
if(i!=parent)
{
div temp=dfs(arr,i,x,c);
w+=temp.x;
b+=temp.y;
}
}
if(c[x]=='W')
w+=1;
else
b+=1;
if(w==b)
++ans;
return new div(w,b);
}
private static long mergeAndCount(int[] arr, int l,
int m, int r)
{
// Left subarray
int[] left = Arrays.copyOfRange(arr, l, m + 1);
// Right subarray
int[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l;long swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
swaps += (m + 1) - (l + i);
}
}
while (i < left.length)
arr[k++] = left[i++];
while (j < right.length)
arr[k++] = right[j++];
return swaps;
}
// Merge sort function
private static long mergeSortAndCount(int[] arr, int l,
int r)
{
// Keeps track of the inversion count at a
// particular node of the recursion tree
long count = 0;
if (l < r) {
int m = (l + r) / 2;
// Total inversion count = left subarray count
// + right subarray count + merge count
// Left subarray count
count += mergeSortAndCount(arr, l, m);
// Right subarray count
count += mergeSortAndCount(arr, m + 1, r);
// Merge count
count += mergeAndCount(arr, l, m, r);
}
return count;
}
public static void main(String[] args) throws IOException {
Reader.init(System.in);
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int T=Reader.nextInt();
for(int m=1;m<=T;m++)
{
int N=Reader.nextInt();
int[] a=new int[N];
int[] b=new int[N];
for(int i=0;i<N;i++)
a[i]=Reader.nextInt();
for(int i=0;i<N;i++)
b[i]=Reader.nextInt();
HashMap<Integer,Integer> hp=new HashMap<>();
int l=N-1,r=N-1;
boolean status=true;
while(l>=0)
{
while(r>=0 && r+1<N && b[r]==b[r+1])
{
hp.put(b[r],hp.getOrDefault(b[r],0)+1);
--r;
}
if(r>=0 && a[l]==b[r])
{
--l;
--r;
continue;
}
if(!hp.containsKey(a[l]))
{
status=false;
break;
}
else
{
if(hp.get(a[l])==1)
hp.remove(a[l]);
else
hp.put(a[l],hp.get(a[l])-1);
--l;
}
}
// boolean status=true;
// System.out.println(hp);
if(status && hp.size()==0)
output.write("yes"+"\n");
else
output.write("no"+"\n");
}
output.flush();
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
class TreeNode
{
int data;
TreeNode left;
TreeNode right;
TreeNode(int data)
{
left=null;
right=null;
this.data=data;
}
}
class div {
int x;
int y;
div(int x,int y) {
this.x=x;
this.y=y;
}
}
class sortat implements Comparator<div>
{
public int compare(div o1, div o2)
{
return o1.x-o2.x;
}
}
| Java | ["5\n\n5\n\n1 2 3 3 2\n\n1 3 3 2 2\n\n5\n\n1 2 4 2 1\n\n4 2 2 1 1\n\n5\n\n2 4 5 5 2\n\n2 2 4 5 5\n\n3\n\n1 2 3\n\n1 2 3\n\n3\n\n1 1 2\n\n2 1 1"] | 1 second | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case, we can choose $$$l=2$$$ and $$$r=5$$$ to form $$$[1, 3, 3, 2, 2]$$$.In the second test case, we can choose $$$l=2$$$ and $$$r=4$$$ to form $$$[1, 4, 2, 2, 1]$$$. Then, we can choose $$$l=1$$$ and $$$r=5$$$ to form $$$[4, 2, 2, 1, 1]$$$.In the third test case, it can be proven that it is not possible to transform array $$$a$$$ to $$$b$$$ using the operation. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"implementation",
"two pointers"
] | 158bd0ab8f4319f5fd1e6062caddad4e | 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$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — elements of the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le n$$$) — elements of the array $$$b$$$. It is guaranteed that $$$b$$$ is a permutation of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$ | 1,700 | For each test case, print "YES" (without quotes) if it is possible to transform array $$$a$$$ to $$$b$$$, 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 | 5428baf9545c4d1c5e3a374a947d5a9e | train_108.jsonl | 1650722700 | There is an array $$$a$$$ of length $$$n$$$. You may perform the following operation any number of times: Choose two indices $$$l$$$ and $$$r$$$ where $$$1 \le l < r \le n$$$ and $$$a_l = a_r$$$. Then, set $$$a[l \ldots r] = [a_{l+1}, a_{l+2}, \ldots, a_r, a_l]$$$. You are also given another array $$$b$$$ of length $$$n$$$ which is a permutation of $$$a$$$. Determine whether it is possible to transform array $$$a$$$ into an array $$$b$$$ using the above operation some number of times. | 256 megabytes | //Utilities
import java.io.*;
import java.util.*;
public class a {
static int t;
static int n;
static int[] b;
static int[] a;
static int i, j;
public static void main(String[] args) throws IOException {
t = in.iscan();
outer : while (t-- > 0) {
n = in.iscan();
a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.iscan();
}
b = new int[n];
for (int i = 0; i < n; i++) {
b[i] = in.iscan();
}
i = n-1; j = n-1;
int[] cnt = new int[n+1];
while (i >= 0) {
if (i >= 0 && j >= 0) {
if (a[i] == b[j]) {
i--; j--;
}
else {
if (i == n-1 || j == n-1) {
out.println("NO");
continue outer;
}
if (a[i+1] == b[j] && b[j+1] == b[j]) {
cnt[b[j]]++;
j--;
}
else if (cnt[a[i]] > 0) {
cnt[a[i]]--;
i--;
}
else {
out.println("NO");
continue outer;
}
}
}
else {
// i >= 0
if (cnt[a[i]] > 0) {
cnt[a[i]]--;
i--;
}
else {
out.println("NO");
continue outer;
}
}
}
out.println("YES");
}
out.close();
}
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 | ["5\n\n5\n\n1 2 3 3 2\n\n1 3 3 2 2\n\n5\n\n1 2 4 2 1\n\n4 2 2 1 1\n\n5\n\n2 4 5 5 2\n\n2 2 4 5 5\n\n3\n\n1 2 3\n\n1 2 3\n\n3\n\n1 1 2\n\n2 1 1"] | 1 second | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case, we can choose $$$l=2$$$ and $$$r=5$$$ to form $$$[1, 3, 3, 2, 2]$$$.In the second test case, we can choose $$$l=2$$$ and $$$r=4$$$ to form $$$[1, 4, 2, 2, 1]$$$. Then, we can choose $$$l=1$$$ and $$$r=5$$$ to form $$$[4, 2, 2, 1, 1]$$$.In the third test case, it can be proven that it is not possible to transform array $$$a$$$ to $$$b$$$ using the operation. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"implementation",
"two pointers"
] | 158bd0ab8f4319f5fd1e6062caddad4e | 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$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — elements of the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le n$$$) — elements of the array $$$b$$$. It is guaranteed that $$$b$$$ is a permutation of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$ | 1,700 | For each test case, print "YES" (without quotes) if it is possible to transform array $$$a$$$ to $$$b$$$, 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 | 57ed9e3dbe419315c57ce0f4fd51c81a | train_108.jsonl | 1650722700 | There is an array $$$a$$$ of length $$$n$$$. You may perform the following operation any number of times: Choose two indices $$$l$$$ and $$$r$$$ where $$$1 \le l < r \le n$$$ and $$$a_l = a_r$$$. Then, set $$$a[l \ldots r] = [a_{l+1}, a_{l+2}, \ldots, a_r, a_l]$$$. You are also given another array $$$b$$$ of length $$$n$$$ which is a permutation of $$$a$$$. Determine whether it is possible to transform array $$$a$$$ into an array $$$b$$$ using the above operation some number of times. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
InputReader in = new InputReader(inputStream);
// for(int i=4;i<=4;i++) {
// InputStream uinputStream = new FileInputStream("timeline.in");
//String f = i+".in";
//InputStream uinputStream = new FileInputStream(f);
// InputReader in = new InputReader(uinputStream);
// PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("timeline.out")));
// }
Task t = new Task();
t.solve(in, out);
out.close();
}
static class Task{
public void solve(InputReader in, PrintWriter out) throws IOException {
int c = in.nextInt();
int o = c;
int oo = 0;
while(c-->0) {
int n = in.nextInt();
oo++;
int arr[] = in.readIntArray(n);
int brr[] = in.readIntArray(n);
int[] t = new int[n+1];
int p = n-1;
int q = n-1;
int cnt = 0;
while(p>=0) {
if(q>0&&brr[q]==brr[q-1]){
t[brr[q]]++;
q--;
cnt++;
}else if(q>=0&&arr[p]==brr[q]) {
q--;p--;
}else if(t[arr[p]]>0){
t[arr[p]]--;p--;
cnt--;
}else {
break;
}
}
out.println((cnt==0&&q<0)?"YES":"NO");
}
}
public class edge implements Comparable<edge>{
int f, t, len;
public edge(int a, int b, int c) {
f = a; t = b; len = c;
}
@Override
public int compareTo(Main.Task.edge o) {
// TODO Auto-generated method stub
return this.len - o.len;
}
}
public Set<Integer> get_factor(int number) {
int n = number;
Set<Integer> primeFactors = new HashSet<Integer>();
for (int i = 2; i <= n/i; i++) { while (n % i == 0) { primeFactors.add(i); n /= i; } } if(n>1)
primeFactors.add(n);
return primeFactors;
}
private static int combx(int n, int k) {
int comb[][] = new int[n+1][n+1];
for(int i = 0; i <=n; i ++){
comb[i][0] = comb[i][i] = 1;
for(int j = 1; j < i; j++){
comb[i][j] = comb[i-1][j] + comb[i-1][j-1];
comb[i][j] %= 1000000007;
}
}
return comb[n][k];
}
class trie_node{
boolean end;
int val;
int lvl;
trie_node zero;
trie_node one;
public trie_node() {
zero = null;
one = null;
end = false;
val = -1;
lvl = -1;
}
}
class trie{
trie_node root = new trie_node();
public void build(int x, int sz) {
trie_node cur = root;
for(int i=sz;i>=0;i--) {
int v = (x&(1<<i))==0?0:1;
if(v==0&&cur.zero==null) {
cur.zero = new trie_node();
}
if(v==1&&cur.one==null) {
cur.one = new trie_node();
}
cur.lvl = i;
if(i==0) {
cur.end = true;
cur.val = x;
}else {
if(v==0)
cur = cur.zero;
else
cur = cur.one;
cur.val = v;
cur.lvl = i;
}
}
}
int search(int num, int limit, trie_node r, int lvl) {
if(r==null) return -1;
if(r.end) return r.val;
int f = -1;
int num_val = (num&1<<lvl)==0?0:1;
int limit_val = (limit&1<<lvl)==0?0:1;
if(limit_val==1) {
if(num_val==0) {
int t = search(num,limit,r.one,lvl-1);
if(t>f) return t;
t = search(num,limit,r.zero,lvl-1);
if(t>f) return t;
}else {
int t = search(num,limit,r.zero,lvl-1);
if(t>f) return t;
t = search(num,limit,r.one,lvl-1);
if(t>f) return t;
}
}else {
int t = search(num,limit,r.zero,lvl-1);
if(t>f) return t;
}
return f;
}
}
public int[] maximizeXor(int[] nums, int[][] queries) {
int m = queries.length;
int ret[] = new int[m];
trie t = new trie();
int sz = 5;
for(int i:nums) t.build(i,sz);
int p = 0;
for(int x[]:queries) {
if(p==1) {
Dumper.print("here");
}
ret[p++] = t.search(x[0], x[1], t.root, sz);
}
return ret;
}
/*
* class edge implements Comparable<edge>{ int id,f,t; int len;int short_len;
* public edge(int a, int b, int c, int d, int e) { f=a;t=b;len=c;id=d;short_len
* = e; }
*
* @Override public int compareTo(edge t) { if(this.len-t.len>0) return 1; else
* if(this.len-t.len<0) return -1; return 0; } }
*/
static class lca_naive{
int n;
ArrayList<edge>[] g;
int lvl[];
int pare[];
int dist[];
public lca_naive(int t, ArrayList<edge>[] x) {
n=t;
g=x;
lvl = new int[n];
pare = new int[n];
dist = new int[n];
}
void pre_process() {
dfs(0,-1,g,lvl,pare,dist);
}
void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[]) {
for(edge nxt_edge:g[cur]) {
if(nxt_edge.t!=pre) {
lvl[nxt_edge.t] = lvl[cur]+1;
dist[nxt_edge.t] = dist[cur]+nxt_edge.len;
pare[nxt_edge.t] = cur;
dfs(nxt_edge.t,cur,g,lvl,pare,dist);
}
}
}
public int work(int p, int q) {
int a = p;
int b = q;
while(lvl[p]<lvl[q]) q = pare[q];
while(lvl[p]>lvl[q]) p = pare[p];
while(p!=q){p = pare[p]; q = pare[q];}
int c = p;
return dist[a]+dist[b]-dist[c]*2;
}
}
static class lca_binary_lifting{
int n;
ArrayList<edge>[] g;
int lvl[];
int pare[];
int dist[];
int table[][];
public lca_binary_lifting(int a, ArrayList<edge>[] t) {
n = a;
g = t;
lvl = new int[n];
pare = new int[n];
dist = new int[n];
table = new int[20][n];
}
void pre_process() {
dfs(0,-1,g,lvl,pare,dist);
for(int i=0;i<20;i++) {
for(int j=0;j<n;j++) {
if(i==0) table[0][j] = pare[j];
else table[i][j] = table[i-1][table[i-1][j]];
}
}
}
void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[]) {
for(edge nxt_edge:g[cur]) {
if(nxt_edge.t!=pre) {
lvl[nxt_edge.t] = lvl[cur]+1;
dist[nxt_edge.t] = dist[cur]+nxt_edge.len;
pare[nxt_edge.t] = cur;
dfs(nxt_edge.t,cur,g,lvl,pare,dist);
}
}
}
public int work(int p, int q) {
int a = p;
int b = q;
if(lvl[p]>lvl[q]) {
int tmp = p;
p=q;
q=tmp;
}
for(int i=19;i>=0;i--) {
if(lvl[table[i][q]]>=lvl[p]) q=table[i][q];
}
if(p==q) return p;// return dist[a]+dist[b]-dist[p]*2;
for(int i=19;i>=0;i--) {
if(table[i][p]!=table[i][q]) {
p = table[i][p];
q = table[i][q];
}
}
return table[0][p];
//return dist[a]+dist[b]-dist[table[0][p]]*2;
}
}
static class lca_sqrt_root{
int n;
ArrayList<edge>[] g;
int lvl[];
int pare[];
int dist[];
int jump[];
int sz;
public lca_sqrt_root(int a, ArrayList<edge>[] b) {
n=a;
g=b;
lvl = new int[n];
pare = new int[n];
dist = new int[n];
jump = new int[n];
sz = (int) Math.sqrt(n);
}
void pre_process() {
dfs(0,-1,g,lvl,pare,dist,jump);
}
void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[], int[] jump) {
for(edge nxt_edge:g[cur]) {
if(nxt_edge.t!=pre) {
lvl[nxt_edge.t] = lvl[cur]+1;
dist[nxt_edge.t] = dist[cur]+nxt_edge.len;
pare[nxt_edge.t] = cur;
if(lvl[nxt_edge.t]%sz==0) {
jump[nxt_edge.t] = cur;
}else {
jump[nxt_edge.t] = jump[cur];
}
dfs(nxt_edge.t,cur,g,lvl,pare,dist,jump);
}
}
}
int work(int p, int q) {
int a = p; int b = q;
if(lvl[p]>lvl[q]) {
int tmp = p;
p=q;
q=tmp;
}
while(jump[p]!=jump[q]) {
if(lvl[p]>lvl[q]) p = jump[p];
else q = jump[q];
}
while(p!=q) {
if(lvl[p]>lvl[q]) p = pare[p];
else q = pare[q];
}
return dist[a]+dist[b]-dist[p]*2;
}
}
// class edge implements Comparable<edge>{
// int f,t,len;
// public edge(int a, int b, int c) {
// f=a;t=b;len=c;
// }
// @Override
// public int compareTo(edge t) {
// return t.len-this.len;
// }
// }
class pair implements Comparable<pair>{
int idx,lvl;
public pair(int a, int b) {
idx = a;
lvl = b;
}
@Override
public int compareTo(pair t) {
return t.lvl-this.lvl;
}
}
static class lca_RMQ{
int n;
ArrayList<edge>[] g;
int lvl[];
int dist[];
int tour[];
int tour_rank[];
int first_occ[];
int c;
sgt s;
public lca_RMQ(int a, ArrayList<edge>[] b) {
n=a;
g=b;
c=0;
lvl = new int[n];
dist = new int[n];
tour = new int[2*n];
tour_rank = new int[2*n];
first_occ = new int[n];
Arrays.fill(first_occ, -1);
}
void pre_process() {
tour[c++] = 0;
dfs(0,-1);
for(int i=0;i<2*n;i++) {
tour_rank[i] = lvl[tour[i]];
if(first_occ[tour[i]]==-1) first_occ[tour[i]] = i;
}
s = new sgt(0,2*n,tour_rank);
}
void dfs(int cur, int pre) {
for(edge nxt_edge:g[cur]) {
if(nxt_edge.t!=pre) {
lvl[nxt_edge.t] = lvl[cur]+1;
dist[nxt_edge.t] = dist[cur]+nxt_edge.len;
tour[c++] = nxt_edge.t;
dfs(nxt_edge.t,cur);
tour[c++] = cur;
}
}
}
int work(int p,int q) {
int a = Math.max(first_occ[p], first_occ[q]);
int b = Math.min(first_occ[p], first_occ[q]);
int idx = s.query_min_idx(b, a+1);
//Dumper.print(a+" "+b+" "+idx);
int c = tour[idx];
return dist[p]+dist[q]-dist[c]*2;
}
}
static class sgt{
sgt lt;
sgt rt;
int l,r;
int sum, max, min, lazy;
int min_idx;
public sgt(int L, int R, int arr[]) {
l=L;r=R;
if(l==r-1) {
sum = max = min = arr[l];
lazy = 0;
min_idx = l;
return;
}
lt = new sgt(l, l+r>>1, arr);
rt = new sgt(l+r>>1, r, arr);
pop_up();
}
void pop_up() {
this.sum = lt.sum + rt.sum;
this.max = Math.max(lt.max, rt.max);
this.min = Math.min(lt.min, rt.min);
if(lt.min<rt.min)
this.min_idx = lt.min_idx;
else if(lt.min>rt.min) this.min_idx = rt.min_idx;
else this.min = Math.min(lt.min_idx, rt.min_idx);
}
void push_down() {
if(this.lazy!=0) {
lt.sum+=lazy;
rt.sum+=lazy;
lt.max+=lazy;
lt.min+=lazy;
rt.max+=lazy;
rt.min+=lazy;
lt.lazy+=this.lazy;
rt.lazy+=this.lazy;
this.lazy = 0;
}
}
void change(int L, int R, int v) {
if(R<=l||r<=L) return;
if(L<=l&&r<=R) {
this.max+=v;
this.min+=v;
this.sum+=v*(r-l);
this.lazy+=v;
return;
}
push_down();
lt.change(L, R, v);
rt.change(L, R, v);
pop_up();
}
int query_max(int L, int R) {
if(L<=l&&r<=R) return this.max;
if(r<=L||R<=l) return Integer.MIN_VALUE;
push_down();
return Math.max(lt.query_max(L, R), rt.query_max(L, R));
}
int query_min(int L, int R) {
if(L<=l&&r<=R) return this.min;
if(r<=L||R<=l) return Integer.MAX_VALUE;
push_down();
return Math.min(lt.query_min(L, R), rt.query_min(L, R));
}
int query_sum(int L, int R) {
if(L<=l&&r<=R) return this.sum;
if(r<=L||R<=l) return 0;
push_down();
return lt.query_sum(L, R) + rt.query_sum(L, R);
}
int query_min_idx(int L, int R) {
if(L<=l&&r<=R) return this.min_idx;
if(r<=L||R<=l) return Integer.MAX_VALUE;
int a = lt.query_min_idx(L, R);
int b = rt.query_min_idx(L, R);
int aa = lt.query_min(L, R);
int bb = rt.query_min(L, R);
if(aa<bb) return a;
else if(aa>bb) return b;
return Math.min(a,b);
}
}
List<List<Integer>> convert(int arr[][]){
int n = arr.length;
List<List<Integer>> ret = new ArrayList<>();
for(int i=0;i<n;i++) {
ArrayList<Integer> tmp = new ArrayList<Integer>();
for(int j=0;j<arr[i].length;j++) tmp.add(arr[i][j]);
ret.add(tmp);
}
return ret;
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public int GCD(int a, int b) {
if (b==0) return a;
return GCD(b,a%b);
}
public long GCD(long a, long b) {
if (b==0) return a;
return GCD(b,a%b);
}
}
static class ArrayUtils {
static final long seed = System.nanoTime();
static final Random rand = new Random(seed);
public static void sort(int[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void shuffle(int[] a) {
for (int i = 0; i < a.length; i++) {
int j = rand.nextInt(i + 1);
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
public static void sort(long[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void shuffle(long[] a) {
for (int i = 0; i < a.length; i++) {
int j = rand.nextInt(i + 1);
long t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
static class BIT{
int arr[];
int n;
public BIT(int a) {
n=a;
arr = new int[n];
}
int sum(int p) {
int s=0;
while(p>0) {
s+=arr[p];
p-=p&(-p);
}
return s;
}
void add(int p, int v) {
while(p<n) {
arr[p]+=v;
p+=p&(-p);
}
}
}
static class DSU{
int[] arr;
int[] sz;
public DSU(int n) {
arr = new int[n];
sz = new int[n];
for(int i=0;i<n;i++) arr[i] = i;
Arrays.fill(sz, 1);
}
public int find(int a) {
if(arr[a]!=a) arr[a] = find(arr[a]);
return arr[a];
}
public void union(int a, int b) {
int x = find(a);
int y = find(b);
if(x==y) return;
arr[y] = x;
sz[x] += sz[y];
}
public int size(int x) {
return sz[find(x)];
}
}
static class MinHeap<Key> implements Iterable<Key> {
private int maxN;
private int n;
private int[] pq;
private int[] qp;
private Key[] keys;
private Comparator<Key> comparator;
public MinHeap(int capacity){
if (capacity < 0) throw new IllegalArgumentException();
this.maxN = capacity;
n=0;
pq = new int[maxN+1];
qp = new int[maxN+1];
keys = (Key[]) new Object[capacity+1];
Arrays.fill(qp, -1);
}
public MinHeap(int capacity, Comparator<Key> c){
if (capacity < 0) throw new IllegalArgumentException();
this.maxN = capacity;
n=0;
pq = new int[maxN+1];
qp = new int[maxN+1];
keys = (Key[]) new Object[capacity+1];
Arrays.fill(qp, -1);
comparator = c;
}
public boolean isEmpty() { return n==0; }
public int size() { return n; }
public boolean contains(int i) {
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
return qp[i] != -1;
}
public int peekIdx() {
if (n == 0) throw new NoSuchElementException("Priority queue underflow");
return pq[1];
}
public Key peek(){
if(isEmpty()) throw new NoSuchElementException("Priority queue underflow");
return keys[pq[1]];
}
public int poll(){
if(isEmpty()) throw new NoSuchElementException("Priority queue underflow");
int min = pq[1];
exch(1,n--);
down(1);
assert min==pq[n+1];
qp[min] = -1;
keys[min] = null;
pq[n+1] = -1;
return min;
}
public void update(int i, Key key) {
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
if (!contains(i)) {
this.add(i, key);
}else {
keys[i] = key;
up(qp[i]);
down(qp[i]);
}
}
private void add(int i, Key x){
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue");
n++;
qp[i] = n;
pq[n] = i;
keys[i] = x;
up(n);
}
private void up(int k){
while(k>1&&less(k,k/2)){
exch(k,k/2);
k/=2;
}
}
private void down(int k){
while(2*k<=n){
int j=2*k;
if(j<n&&less(j+1,j)) j++;
if(less(k,j)) break;
exch(k,j);
k=j;
}
}
public boolean less(int i, int j){
if (comparator == null) {
return ((Comparable<Key>) keys[pq[i]]).compareTo(keys[pq[j]]) < 0;
}
else {
return comparator.compare(keys[pq[i]], keys[pq[j]]) < 0;
}
}
public void exch(int i, int j){
int swap = pq[i];
pq[i] = pq[j];
pq[j] = swap;
qp[pq[i]] = i;
qp[pq[j]] = j;
}
@Override
public Iterator<Key> iterator() {
// TODO Auto-generated method stub
return null;
}
}
private static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int zcurChar;
private int znumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (znumChars == -1)
throw new InputMismatchException();
if (zcurChar >= znumChars)
{
zcurChar = 0;
try
{
znumChars = stream.read(buf);
} catch (IOException e)
{
throw new InputMismatchException();
}
if (znumChars <= 0)
return -1;
}
return buf[zcurChar++];
}
public int nextInt()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public 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 double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public 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 boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return nextString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
public int[] readIntArray(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = nextInt();
}
return ret;
}
}
static class Dumper {
static void print_int_arr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_char_arr(char[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_double_arr(double[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_2d_arr(int[][] arr, int x, int y) {
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("---------------------");
}
static void print_2d_arr(boolean[][] arr, int x, int y) {
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("---------------------");
}
static void print(Object o) {
System.out.println(o.toString());
}
static void getc() {
System.out.println("here");
}
}
} | Java | ["5\n\n5\n\n1 2 3 3 2\n\n1 3 3 2 2\n\n5\n\n1 2 4 2 1\n\n4 2 2 1 1\n\n5\n\n2 4 5 5 2\n\n2 2 4 5 5\n\n3\n\n1 2 3\n\n1 2 3\n\n3\n\n1 1 2\n\n2 1 1"] | 1 second | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case, we can choose $$$l=2$$$ and $$$r=5$$$ to form $$$[1, 3, 3, 2, 2]$$$.In the second test case, we can choose $$$l=2$$$ and $$$r=4$$$ to form $$$[1, 4, 2, 2, 1]$$$. Then, we can choose $$$l=1$$$ and $$$r=5$$$ to form $$$[4, 2, 2, 1, 1]$$$.In the third test case, it can be proven that it is not possible to transform array $$$a$$$ to $$$b$$$ using the operation. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"implementation",
"two pointers"
] | 158bd0ab8f4319f5fd1e6062caddad4e | 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$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — elements of the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le n$$$) — elements of the array $$$b$$$. It is guaranteed that $$$b$$$ is a permutation of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$ | 1,700 | For each test case, print "YES" (without quotes) if it is possible to transform array $$$a$$$ to $$$b$$$, 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 | a1e9c022fe39c28b7e5c247eb40f6e60 | train_108.jsonl | 1650722700 | There is an array $$$a$$$ of length $$$n$$$. You may perform the following operation any number of times: Choose two indices $$$l$$$ and $$$r$$$ where $$$1 \le l < r \le n$$$ and $$$a_l = a_r$$$. Then, set $$$a[l \ldots r] = [a_{l+1}, a_{l+2}, \ldots, a_r, a_l]$$$. You are also given another array $$$b$$$ of length $$$n$$$ which is a permutation of $$$a$$$. Determine whether it is possible to transform array $$$a$$$ into an array $$$b$$$ using the above operation some number of times. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF1672D extends PrintWriter {
CF1672D() { super(System.out); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1672D o = new CF1672D(); o.main(); o.flush();
}
void main() {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] aa = new int[n];
for (int i = 0; i < n; i++)
aa[i] = sc.nextInt();
int[] bb = new int[n];
for (int i = 0; i < n; i++)
bb[i] = sc.nextInt();
int[] kk = new int[n + 1];
boolean yes = true;
out:
for (int i = n - 1, j = n - 1; j >= 0; ) {
int a = aa[i];
int b = bb[j];
if (a != b) {
if (kk[a] > 0) {
kk[a]--;
i--;
continue;
}
yes = false;
break;
}
// a == b
while (j > 0 && bb[j - 1] == b) {
if (i > 0 && aa[i - 1] == a)
i--;
else
kk[a]++;
j--;
}
i--; j--;
}
println(yes ? "YES" : "NO");
}
}
}
| Java | ["5\n\n5\n\n1 2 3 3 2\n\n1 3 3 2 2\n\n5\n\n1 2 4 2 1\n\n4 2 2 1 1\n\n5\n\n2 4 5 5 2\n\n2 2 4 5 5\n\n3\n\n1 2 3\n\n1 2 3\n\n3\n\n1 1 2\n\n2 1 1"] | 1 second | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case, we can choose $$$l=2$$$ and $$$r=5$$$ to form $$$[1, 3, 3, 2, 2]$$$.In the second test case, we can choose $$$l=2$$$ and $$$r=4$$$ to form $$$[1, 4, 2, 2, 1]$$$. Then, we can choose $$$l=1$$$ and $$$r=5$$$ to form $$$[4, 2, 2, 1, 1]$$$.In the third test case, it can be proven that it is not possible to transform array $$$a$$$ to $$$b$$$ using the operation. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"implementation",
"two pointers"
] | 158bd0ab8f4319f5fd1e6062caddad4e | 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$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — elements of the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le n$$$) — elements of the array $$$b$$$. It is guaranteed that $$$b$$$ is a permutation of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$ | 1,700 | For each test case, print "YES" (without quotes) if it is possible to transform array $$$a$$$ to $$$b$$$, 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 | 6b110a58be219c6a65773776edee5ea0 | train_108.jsonl | 1650722700 | There is an array $$$a$$$ of length $$$n$$$. You may perform the following operation any number of times: Choose two indices $$$l$$$ and $$$r$$$ where $$$1 \le l < r \le n$$$ and $$$a_l = a_r$$$. Then, set $$$a[l \ldots r] = [a_{l+1}, a_{l+2}, \ldots, a_r, a_l]$$$. You are also given another array $$$b$$$ of length $$$n$$$ which is a permutation of $$$a$$$. Determine whether it is possible to transform array $$$a$$$ into an array $$$b$$$ using the above operation some number of times. | 256 megabytes | /*
Rating: 1378
Date: 24-04-2022
Time: 10-47-22
Author: Kartik Papney
Linkedin: https://www.linkedin.com/in/kartik-papney-4951161a6/
Leetcode: https://leetcode.com/kartikpapney/
Codechef: https://www.codechef.com/users/kartikpapney
----------------------------Jai Shree Ram----------------------------
*/
import java.util.*;
import javax.lang.model.util.ElementScanner6;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class D_Cyclic_Rotation {
public static void s() {
int n = sc.nextInt();
int[] a = sc.readArray(n);
int[] b = sc.readArray(n);
HashMap<Integer, Integer> map = new HashMap<>();
HashMap<Integer, Integer> nmap = new HashMap<>();
for(int i=a.length-1; i>=0; i--) {
map.put(a[i], i);
}
// i
// a -> 1 2 3 3 2
// b -> 1 3 3 2 2
// j
int i = a.length-1;
for(int j=b.length-1; j>=0; j--) {
if(a[i] != b[j]) {
if(i == a.length-1) {
p.writeln("NO");
return;
} else if(b[j] == b[j+1]) {
if(map.get(b[j])<i) {
nmap.put(b[j], nmap.getOrDefault(b[j], 0) + 1);
} else {
p.writeln("NO");
return;
}
} else if(nmap.containsKey(a[i]) && nmap.get(a[i]) > 0){
nmap.put(a[i], nmap.get(a[i]) - 1);
i--;
j++;
} else {
p.writeln("NO");
return;
}
} else {
i--;
}
}
p.writeln("YES");
}
public static void main(String[] args) {
int t = 1;
t = sc.nextInt();
while (t-- != 0) {
s();
}
p.print();
}
public static boolean debug = false;
static void debug(String st) {
if(debug) p.writeln(st);
}
static final Integer MOD = (int) 1e9 + 7;
static final FastReader sc = new FastReader();
static final Print p = new Print();
static class Functions {
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 max(int... a) {
int max = Integer.MIN_VALUE;
for (int val : a) max = Math.max(val, max);
return max;
}
static int min(int... a) {
int min = Integer.MAX_VALUE;
for (int val : a) min = Math.min(val, min);
return min;
}
static long min(long... a) {
long min = Long.MAX_VALUE;
for (long val : a) min = Math.min(val, min);
return min;
}
static long max(long... a) {
long max = Long.MIN_VALUE;
for (long val : a) max = Math.max(val, max);
return max;
}
static long sum(long... a) {
long sum = 0;
for (long val : a) sum += val;
return sum;
}
static int sum(int... a) {
int sum = 0;
for (int val : a) sum += val;
return sum;
}
public static long mod_add(long a, long b) {
return (a % MOD + b % MOD + MOD) % MOD;
}
public static long pow(long a, long b) {
long res = 1;
while (b > 0) {
if ((b & 1) != 0)
res = mod_mul(res, a);
a = mod_mul(a, a);
b >>= 1;
}
return res;
}
public static long mod_mul(long a, long b) {
long res = 0;
a %= MOD;
while (b > 0) {
if ((b & 1) > 0) {
res = mod_add(res, a);
}
a = (2 * a) % MOD;
b >>= 1;
}
return res;
}
public static long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
public static long factorial(long n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res = (i % MOD * res % MOD) % MOD;
}
return res;
}
public static int count(int[] arr, int x) {
int count = 0;
for (int val : arr) if (val == x) count++;
return count;
}
public static ArrayList<Integer> generatePrimes(int n) {
boolean[] primes = new boolean[n];
for (int i = 2; i < primes.length; i++) primes[i] = true;
for (int i = 2; i < primes.length; i++) {
if (primes[i]) {
for (int j = i * i; j < primes.length; j += i) {
primes[j] = false;
}
}
}
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < primes.length; i++) {
if (primes[i]) arr.add(i);
}
return arr;
}
}
static class Print {
StringBuffer strb = new StringBuffer();
public void write(Object str) {
strb.append(str);
}
public void writes(Object str) {
char c = ' ';
strb.append(str).append(c);
}
public void writeln(Object str) {
char c = '\n';
strb.append(str).append(c);
}
public void yes() {
char c = '\n';
writeln("YES");
}
public void no() {
writeln("NO");
}
public void writeln() {
char c = '\n';
strb.append(c);
}
public void writes(int... arr) {
for (int val : arr) {
write(val);
write(' ');
}
}
public void writes(long... arr) {
for (long val : arr) {
write(val);
write(' ');
}
}
public void writeln(int... arr) {
for (int val : arr) {
writeln(val);
}
}
public void print() {
System.out.print(strb);
}
public void println() {
System.out.println(strb);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] 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;
}
double[] readArrayDouble(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
String nextLine() {
String str = new String();
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["5\n\n5\n\n1 2 3 3 2\n\n1 3 3 2 2\n\n5\n\n1 2 4 2 1\n\n4 2 2 1 1\n\n5\n\n2 4 5 5 2\n\n2 2 4 5 5\n\n3\n\n1 2 3\n\n1 2 3\n\n3\n\n1 1 2\n\n2 1 1"] | 1 second | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case, we can choose $$$l=2$$$ and $$$r=5$$$ to form $$$[1, 3, 3, 2, 2]$$$.In the second test case, we can choose $$$l=2$$$ and $$$r=4$$$ to form $$$[1, 4, 2, 2, 1]$$$. Then, we can choose $$$l=1$$$ and $$$r=5$$$ to form $$$[4, 2, 2, 1, 1]$$$.In the third test case, it can be proven that it is not possible to transform array $$$a$$$ to $$$b$$$ using the operation. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"implementation",
"two pointers"
] | 158bd0ab8f4319f5fd1e6062caddad4e | 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$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — elements of the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le n$$$) — elements of the array $$$b$$$. It is guaranteed that $$$b$$$ is a permutation of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$ | 1,700 | For each test case, print "YES" (without quotes) if it is possible to transform array $$$a$$$ to $$$b$$$, 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 | 74d2e9d8b3dd8b6732315e40d1e3baee | train_108.jsonl | 1650722700 | There is an array $$$a$$$ of length $$$n$$$. You may perform the following operation any number of times: Choose two indices $$$l$$$ and $$$r$$$ where $$$1 \le l < r \le n$$$ and $$$a_l = a_r$$$. Then, set $$$a[l \ldots r] = [a_{l+1}, a_{l+2}, \ldots, a_r, a_l]$$$. You are also given another array $$$b$$$ of length $$$n$$$ which is a permutation of $$$a$$$. Determine whether it is possible to transform array $$$a$$$ into an array $$$b$$$ using the above operation some number of times. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class D {
//java -Xss515m Solution.java < input.txt
private static final String SPACE = "\\s+";
private static final int MOD = 1_000_000_007;
private static final Reader in = new Reader();
public static void main(String[] args) throws IOException {
int tt = in.nextInt();
while (tt-- > 0) {
int n = in.nextInt();
int[] A = in.nextInts();
int[] B = in.nextInts();
System.out.println(solve(n, A, B) ? "YES" : "NO");
}
}
private static boolean solve(int n, int[] A, int[] B) throws IOException {
Map<Integer, Integer> count = new HashMap<>();
for (int i = n - 1, j = n - 1; i >= 0; i--) {
while (j > 0 && B[j] == B[j - 1]) {
count.put(B[j], count.getOrDefault(B[j], 0) + 1);
j--;
}
if (j >= 0 && A[i] == B[j]) {
j--;
} else {
if (count.getOrDefault(A[i], 0) == 0) {
return false;
} else {
count.put(A[i], count.get(A[i]) - 1);
}
}
}
return true;
}
// Utility functions
private static void shuffleSort(int[] nums) {
shuffle(nums);
Arrays.sort(nums);
}
private static void shuffleSort(long[] nums) {
shuffle(nums);
Arrays.sort(nums);
}
private static void swap(int a, int b, int[] nums) {
int temp = nums[a];
nums[a] = nums[b];
nums[b] = temp;
}
private static void swap(int a, int b, long[] nums) {
long temp = nums[a];
nums[a] = nums[b];
nums[b] = temp;
}
private static void shuffle(int[] nums) {
Random random = ThreadLocalRandom.current();
for (int i = nums.length - 1; i > 0; i--) {
swap(random.nextInt(i), i, nums);
}
}
private static void shuffle(long[] nums) {
Random random = ThreadLocalRandom.current();
for (int i = nums.length - 1; i > 0; i--) {
swap(random.nextInt(i), i, nums);
}
}
private static long pow(int base, int exp) {
long res = 1;
while (exp-- > 0) {
res *= base;
}
return res;
}
private static long pow(int base, int exp, Long[][] memo) {
if (exp == 0) return 1;
if (memo[base][exp] != null) return memo[base][exp];
return memo[base][exp] = base * pow(base, exp - 1, memo) % MOD;
}
private static class Reader {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int index = 0;
String[] tokens = {};
public int nextInt() throws IOException {
read();
return Integer.parseInt(tokens[index++]);
}
public long nextLong() throws IOException {
read();
return Long.parseLong(tokens[index++]);
}
public String nextString() throws IOException {
read();
return tokens[index++];
}
public int[] nextInts() throws IOException {
return Arrays.stream(in.readLine().split(SPACE)).mapToInt(Integer::parseInt).toArray();
}
public long[] nextLongs() throws IOException {
return Arrays.stream(in.readLine().split(SPACE)).mapToLong(Long::parseLong).toArray();
}
public String[] nextStrings() throws IOException {
return in.readLine().split(SPACE);
}
private void read() throws IOException {
if (index >= tokens.length) {
tokens = in.readLine().split(SPACE);
index = 0;
}
}
}
}
| Java | ["5\n\n5\n\n1 2 3 3 2\n\n1 3 3 2 2\n\n5\n\n1 2 4 2 1\n\n4 2 2 1 1\n\n5\n\n2 4 5 5 2\n\n2 2 4 5 5\n\n3\n\n1 2 3\n\n1 2 3\n\n3\n\n1 1 2\n\n2 1 1"] | 1 second | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case, we can choose $$$l=2$$$ and $$$r=5$$$ to form $$$[1, 3, 3, 2, 2]$$$.In the second test case, we can choose $$$l=2$$$ and $$$r=4$$$ to form $$$[1, 4, 2, 2, 1]$$$. Then, we can choose $$$l=1$$$ and $$$r=5$$$ to form $$$[4, 2, 2, 1, 1]$$$.In the third test case, it can be proven that it is not possible to transform array $$$a$$$ to $$$b$$$ using the operation. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"implementation",
"two pointers"
] | 158bd0ab8f4319f5fd1e6062caddad4e | 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$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — elements of the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le n$$$) — elements of the array $$$b$$$. It is guaranteed that $$$b$$$ is a permutation of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$ | 1,700 | For each test case, print "YES" (without quotes) if it is possible to transform array $$$a$$$ to $$$b$$$, 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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.