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
56ca7d6b26115533bca1903b6ae14fed
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.Arrays; public class B { static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer in = new StreamTokenizer(ins); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static int[] a = new int[200000]; static int n,m; public static void main(String[] args)throws IOException { int t =Integer.parseInt(ins.readLine()); while(t-->0){ Arrays.fill(a,0); int n = Integer.parseInt(ins.readLine()); String ss = ins.readLine(); int res=0,ai=0; int l=-1; a[0]=1; for (int i = 1; i <n; i++) { if(ss.charAt(i)==ss.charAt(i-1))a[ai]++; else{ ai++; a[ai]=1; } } for (int i = 0; i <= ai; i++) { if(a[i]%2!=0){ if(l!=-1){ res = res+i-l; l=-1; } else{ l=i; } } } out.println(res); } out.flush(); } public static int in()throws IOException{ in.nextToken(); return (int)in.nval; } public static long inl()throws IOException{ in.nextToken(); return (long)in.nval; } public static double ind()throws IOException{ in.nextToken(); return in.nval; } public static String ins()throws IOException{ in.nextToken(); return in.sval; } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
8d47413d8d13d0a1bad20121bacaf550
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class B1_Tokitsukaze_and_Good_01_String_easy_version { static Scanner in = new Scanner(); static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans = new StringBuilder(); static int testCases, n; static char a[]; /* 10 1110011000 -> 11 00 11 11 00 */ static void solve(int index, ArrayList<Integer> list, int len) { if (index >= n) { return; } len = 1; while (index + 1 < n && a[index] == a[index + 1]) { ++len; ++index; } list.add(len); solve(index + 1, list, len); } static void solve(int t) { ArrayList<Integer> list = new ArrayList<>(); solve(0, list, 1); long arr[] = new long[list.size()]; int index = 0; while (!list.isEmpty()) { arr[index++] = list.get(0); list.popFront(); } int ans1 = 0, m = arr.length; for (int i = 0; i < m - 1; i++) { if (arr[i] % 2 == arr[i + 1] % 2 && arr[i] % 2 == 1) { arr[i]--; arr[i + 1]++; ans1++; } else if (arr[i] % 2 != arr[i + 1] % 2 && arr[i + 1] % 2 == 0) { arr[i]--; arr[i + 1]++; ans1++; } } ans.append(ans1); if (t != testCases) { ans.append("\n"); } } public static void main(String[] amit) throws IOException { testCases = in.nextInt(); for (int t = 0; t < testCases; ++t) { n = in.nextInt(); a = in.next().toCharArray(); 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(); } if(index == 0) { return head.getData(); } 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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
7e29430449b976b3dfb01c0d534c0a27
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; import java.io.*; public class Solution{ public static void main(String[] args) throws IOException { BufferedReader re = new BufferedReader(new InputStreamReader(System.in)); int testCase = Integer.parseInt(re.readLine()); StringBuilder ans = new StringBuilder(); for(int t = 1; t <= testCase; t++){ int n = Integer.parseInt(re.readLine()); String s = re.readLine(); char prev = s.charAt(0); int answer = 0; int count = 1; for(int i = 1; i < n; i++){ if(prev != s.charAt(i)){ if(count % 2 == 0){ count = 1; }else{ answer++; count = 2; } }else { count++; } prev = s.charAt(i); } ans.append(answer).append('\n'); } System.out.print(ans); re.close(); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
f10b37b391bc1dbd7932e1e03319b932
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; public class Main { static long cr[][]=new long[1001][1001]; //static double EPS = 1e-7; static long mod=998244353; static long val=0; static boolean flag=true; public static void main(String[] args) { FScanner sc = new FScanner(); //Arrays.fill(prime, true); //sieve(); //ncr(); int t=sc.nextInt(); StringBuilder sb = new StringBuilder(); while(t-->0) { int n=sc.nextInt(); String s=sc.next(); s=s+"#"; char c[]=s.toCharArray(); int count=1; int ans=0; ArrayList<Integer> A=new ArrayList<>(); for(int i=0;i<n;i++) { if(c[i]!=c[i+1]) { if(count%2==1) A.add(ans); ans++; count=1; } else count++; } int res=0; for(int i=1;i<A.size();i=i+2) { res=res+A.get(i)-A.get(i-1); } sb.append(res); sb.append("\n"); } System.out.println(sb.toString()); } public static int solve(char c[],int i,int n,char prev,int dp[][]) { if(i>=n) return 0; if(dp[i][prev-'a']!=-1) return dp[i][prev-'a']; int res=0; if(c[i]==prev) { if(i+1>=n) {res=2; } else res= 2+solve(c,i+2,n,c[i+1],dp); } else { res= Math.max(solve(c,i+1,n,prev,dp),solve(c,i+1,n,c[i],dp)); } dp[i][prev-'a']=res; return res; } public static long powerLL(long x, long n) { long result = 1; while (n > 0) { if (n % 2 == 1) { result = result * x % mod; } n = n / 2; x = x * x % mod; } return result; } public static long gcd(long a,long b) { return b==0 ? a:gcd(b,a%b); } /* public static void sieve() { prime[0]=prime[1]=false; int n=1000000; 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 void ncr() { cr[0][0]=1; for(int i=1;i<=1000;i++) { cr[i][0]=1; for(int j=1;j<i;j++) { cr[i][j]=(cr[i-1][j-1]+cr[i-1][j])%mod; } cr[i][i]=1; } } } class pair //implements Comparable<pair> { long a;long b; pair(long a,long b) { this.b=b; this.a=a; } } class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(next()); } double nextDouble(){ return Double.parseDouble(next()); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
acde84c7ef989fcadb04268da7521ef0
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.math.BigInteger; 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; } 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); } } 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; } 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 = 0; //out.print(String.format("Case #%d: ", tidx)); n=in.nextInt(); String s=in.next(); List<Integer>list=new ArrayList<>(); int cnt=0; char p=s.charAt(0); for(i=0;i<s.length();i++){ if(s.charAt(i)==p){ cnt++; } else { p=s.charAt(i); list.add(cnt); cnt=1; } } list.add(cnt); Integer a[]=list.toArray(new Integer[0]); for(i=0;i<a.length-1;i++){ if(a[i]%2==1){ a[i]--; a[i+1]++; ans++; } } out.println(ans); } /*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()); commit(); } 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 printIntArr(int []arr) throws IOException { for (int i = 0; i < arr.length - 1; i++) { print(arr[i] + " "); } println(arr[arr.length - 1]); } 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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
0937055a4b6f4a98b0ad5203bf5e61e2
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
// Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.*; public class HelloWorld { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t--!=0){ int n=sc.nextInt(); sc.nextLine(); String val = sc.nextLine(); char ch='*'; int ans=0,cnt=0; for(int i=0;i<n;i++){ if(val.charAt(i)!=ch){ if(ch=='*'){ ch=val.charAt(i); cnt++; }else{ if(cnt%2!=0){ ans++; ch='*'; cnt=0; }else{ cnt=1; ch=val.charAt(i); } } }else{ cnt++; } } System.out.println(ans); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
1ec11a0c0404fcd05576f2b3e42cf086
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Random; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; // B -> CodeForcesProblemSet public final class B { static FastReader fr = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static final int gigamod = 1000000007; static int t = 1; static double epsilon = 0.00000001; static boolean[] isPrime; static int[] smallestFactorOf; @SuppressWarnings("unused") public static void main(String[] args) { t = fr.nextInt(); OUTER: for (int tc = 0; tc < t; tc++) { int n = fr.nextInt(); char[] s = fr.next().toCharArray(); char[] sorg = s.clone(); // determine two things: // // a. Min opers required to make 's' good // // b. Min segments that can be formed in // these opers // if it's a single element, we'd prefer // to convert it to the other int numOpers = 0; for (int i = 0; i < n; i++) { int j = i; while (j < n - 1 && s[j + 1] == s[i]) j++; // [i, j] is the segment int len = j + 1 - i; if (len % 2 == 0) { i = j; continue; } assert len % 2 != 0; // we'll prefer changing the last element s[j] -= '0'; s[j] ^= 1; s[j] += '0'; // [i, j - 1] is the last segment, // which has even length numOpers++; i = j - 1; } // in these opers, out.println(numOpers); } out.close(); } static class Query { int type, idx; long val; Query(int tt, int ii, long xx) { type = tt; idx = ii; val = xx; } Query(int tt, long xx) { type = tt; val = xx; } } static class Tuple { int val, idx, numPairs; Tuple(int vv, int ii, int np) { val = vv; idx = ii; numPairs = np; } } static class UnionFind { // Uses weighted quick-union with path compression. private int[] parent; // parent[i] = parent of i private int[] size; // size[i] = number of sites in tree rooted at i // Note: not necessarily correct if i is not a root node private int count; // number of components public UnionFind(int n) { count = n; parent = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } // Number of connected components. public int count() { return count; } // Find the root of p. public int find(int p) { while (p != parent[p]) p = parent[p]; return p; } public boolean connected(int p, int q) { return find(p) == find(q); } public int numConnectedTo(int node) { return size[find(node)]; } // Weighted union. public void union(int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return; // make smaller root point to larger one if (size[rootP] < size[rootQ]) { parent[rootP] = rootQ; size[rootQ] += size[rootP]; } else { parent[rootQ] = rootP; size[rootP] += size[rootQ]; } count--; } public static int[] connectedComponents(UnionFind uf) { // We can do this in nlogn. int n = uf.size.length; int[] compoColors = new int[n]; for (int i = 0; i < n; i++) compoColors[i] = uf.find(i); HashMap<Integer, Integer> oldToNew = new HashMap<>(); int newCtr = 0; for (int i = 0; i < n; i++) { int thisOldColor = compoColors[i]; Integer thisNewColor = oldToNew.get(thisOldColor); if (thisNewColor == null) thisNewColor = newCtr++; oldToNew.put(thisOldColor, thisNewColor); compoColors[i] = thisNewColor; } return compoColors; } } static class UGraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; @SuppressWarnings("unchecked") public UGraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); adj[to].add(from); } public HashSet<Integer> adj(int from) { return adj[from]; } public int degree(int v) { return adj[v].size(); } public int V() { return adj.length; } public int E() { return E; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static void dfsMark(int current, boolean[] marked, UGraph g) { if (marked[current]) return; marked[current] = true; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, marked, g); } public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) { if (marked[current]) return; marked[current] = true; if (from != -1) distTo[current] = distTo[from] + 1; HashSet<Integer> adj = g.adj(current); int alreadyMarkedCtr = 0; for (int adjc : adj) { if (marked[adjc]) alreadyMarkedCtr++; dfsMark(adjc, current, distTo, marked, g, endPoints); } if (alreadyMarkedCtr == adj.size()) endPoints.add(current); } public static void bfsOrder(int current, UGraph g) { } public static void dfsMark(int current, int[] colorIds, int color, UGraph g) { if (colorIds[current] != -1) return; colorIds[current] = color; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, colorIds, color, g); } public static int[] connectedComponents(UGraph g) { int n = g.V(); int[] componentId = new int[n]; Arrays.fill(componentId, -1); int colorCtr = 0; for (int i = 0; i < n; i++) { if (componentId[i] != -1) continue; dfsMark(i, componentId, colorCtr, g); colorCtr++; } return componentId; } public static boolean hasCycle(UGraph ug) { int n = ug.V(); boolean[] marked = new boolean[n]; boolean[] hasCycleFirst = new boolean[1]; for (int i = 0; i < n; i++) { if (marked[i]) continue; hcDfsMark(i, ug, marked, hasCycleFirst, -1); } return hasCycleFirst[0]; } // Helper for hasCycle. private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) { if (marked[current]) return; if (hasCycleFirst[0]) return; marked[current] = true; HashSet<Integer> adjc = ug.adj(current); for (int adj : adjc) { if (marked[adj] && adj != parent && parent != -1) { hasCycleFirst[0] = true; return; } hcDfsMark(adj, ug, marked, hasCycleFirst, current); } } } static class Digraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; @SuppressWarnings("unchecked") public Digraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); } public HashSet<Integer> adj(int from) { return adj[from]; } public int V() { return adj.length; } public int E() { return E; } public Digraph reversed() { Digraph dg = new Digraph(V()); for (int i = 0; i < V(); i++) for (int adjVert : adj(i)) dg.addEdge(adjVert, i); return dg; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static int[] KosarajuSharirSCC(Digraph dg) { int[] id = new int[dg.V()]; Digraph reversed = dg.reversed(); // Gotta perform topological sort on this one to get the stack. Stack<Integer> revStack = Digraph.topologicalSort(reversed); // Initializing id and idCtr. id = new int[dg.V()]; int idCtr = -1; // Creating a 'marked' array. boolean[] marked = new boolean[dg.V()]; while (!revStack.isEmpty()) { int vertex = revStack.pop(); if (!marked[vertex]) sccDFS(dg, vertex, marked, ++idCtr, id); } return id; } private static void sccDFS(Digraph dg, int source, boolean[] marked, int idCtr, int[] id) { marked[source] = true; id[source] = idCtr; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) sccDFS(dg, adjVertex, marked, idCtr, id); } public static Stack<Integer> topologicalSort(Digraph dg) { // dg has to be a directed acyclic graph. // We'll have to run dfs on the digraph and push the deepest nodes on stack first. // We'll need a Stack<Integer> and a int[] marked. Stack<Integer> topologicalStack = new Stack<Integer>(); boolean[] marked = new boolean[dg.V()]; // Calling dfs for (int i = 0; i < dg.V(); i++) if (!marked[i]) runDfs(dg, topologicalStack, marked, i); return topologicalStack; } static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) { marked[source] = true; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) runDfs(dg, topologicalStack, marked, adjVertex); topologicalStack.add(source); } } static class FastReader { private BufferedReader bfr; private StringTokenizer st; public FastReader() { bfr = new BufferedReader(new InputStreamReader(System.in)); } String next() { if (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bfr.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()); } char nextChar() { return next().toCharArray()[0]; } String nextString() { return next(); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } double[] nextDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextDouble(); return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } int[][] nextIntGrid(int n, int m) { int[][] grid = new int[n][m]; for (int i = 0; i < n; i++) { char[] line = fr.next().toCharArray(); for (int j = 0; j < m; j++) grid[i][j] = line[j] - 48; } return grid; } } static class LCA { int[] height, first, segtree; ArrayList<Integer> euler; boolean[] visited; int n; LCA(ArrayList<Point>[] outFrom, int root) { n = outFrom.length; height = new int[n]; first = new int[n]; euler = new ArrayList<>(); visited = new boolean[n]; dfs(outFrom, root, 0); int m = euler.size(); segtree = new int[m * 4]; build(1, 0, m - 1); } void dfs(ArrayList<Point>[] outFrom, int node, int h) { visited[node] = true; height[node] = h; first[node] = euler.size(); euler.add(node); for (Point to : outFrom[node]) { if (!visited[(int) to.x]) { dfs(outFrom, (int) to.x, h + 1); euler.add(node); } } } void build(int node, int b, int e) { if (b == e) { segtree[node] = euler.get(b); } else { int mid = (b + e) / 2; build(node << 1, b, mid); build(node << 1 | 1, mid + 1, e); int l = segtree[node << 1], r = segtree[node << 1 | 1]; segtree[node] = (height[l] < height[r]) ? l : r; } } int query(int node, int b, int e, int L, int R) { if (b > R || e < L) return -1; if (b >= L && e <= R) return segtree[node]; int mid = (b + e) >> 1; int left = query(node << 1, b, mid, L, R); int right = query(node << 1 | 1, mid + 1, e, L, R); if (left == -1) return right; if (right == -1) return left; return height[left] < height[right] ? left : right; } int lca(int u, int v) { int left = first[u], right = first[v]; if (left > right) { int temp = left; left = right; right = temp; } return query(1, 0, euler.size() - 1, left, right); } } static class FenwickTree { long[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates public FenwickTree(int size) { array = new long[size + 1]; } public long rsq(int ind) { assert ind > 0; long sum = 0; while (ind > 0) { sum += array[ind]; //Extracting the portion up to the first significant one of the binary representation of 'ind' and decrementing ind by that number ind -= ind & (-ind); } return sum; } public long rsq(int a, int b) { assert b >= a && a > 0 && b > 0; return rsq(b) - rsq(a - 1); } public void update(int ind, long value) { assert ind > 0; while (ind < array.length) { array[ind] = value; //Extracting the portion up to the first significant one of the binary representation of 'ind' and incrementing ind by that number ind += ind & (-ind); } } public int size() { return array.length - 1; } } static final class RedBlackCountSet { // Manual implementation of RB-BST for C++ PBDS functionality. // Modification required according to requirements. // Colors for Nodes: private static final boolean RED = true; private static final boolean BLACK = false; // Pointer to the root: private Node root; // Public constructor: public RedBlackCountSet() { root = null; } // Node class for storing key value pairs: private class Node { long key; long value; Node left, right; boolean color; long size; // Size of the subtree rooted at that node. public Node(long key, long value, boolean color) { this.key = key; this.value = value; this.left = this.right = null; this.color = color; this.size = value; } } /***** Invariant Maintenance functions: *****/ // When a temporary 4 node is formed: private Node flipColors(Node node) { node.left.color = node.right.color = BLACK; node.color = RED; return node; } // When there's a right leaning red link and it is not a 3 node: private Node rotateLeft(Node node) { Node rn = node.right; node.right = rn.left; rn.left = node; rn.color = node.color; node.color = RED; return rn; } // When there are 2 red links in a row: private Node rotateRight(Node node) { Node ln = node.left; node.left = ln.right; ln.right = node; ln.color = node.color; node.color = RED; return ln; } /***** Invariant Maintenance functions end *****/ // Public functions: public void put(long key) { root = put(root, key); root.color = BLACK; // One of the invariants. } private Node put(Node node, long key) { // Standard insertion procedure: if (node == null) return new Node(key, 1, RED); // Recursing: int cmp = Long.compare(key, node.key); if (cmp < 0) node.left = put(node.left, key); else if (cmp > 0) node.right = put(node.right, key); else node.value++; // Invariant maintenance: if (node.left != null && node.right != null) { if (node.right.color = RED && node.left.color == BLACK) node = rotateLeft(node); if (node.left.color == RED && node.left.left.color == RED) node = rotateRight(node); if (node.left.color == RED && node.right.color == RED) node = flipColors(node); } // Property maintenance: node.size = node.value + size(node.left) + size(node.right); return node; } private long size(Node node) { if (node == null) return 0; else return node.size; } public long rank(long key) { return rank(root, key); } // Modify according to requirements. private long rank(Node node, long key) { if (node == null) return 0; int cmp = Long.compare(key, node.key); if (cmp < 0) return rank(node.left, key); else if (cmp > 0) return node.value + size(node.left) + rank(node.right, key); else return node.value + size(node.left); } } static class SegmentTree { private Node[] heap; private int[] array; private int size; public SegmentTree(int[] array) { this.array = Arrays.copyOf(array, array.length); //The max size of this array is about 2 * 2 ^ log2(n) + 1 size = (int) (2 * Math.pow(2.0, Math.floor((Math.log((double) array.length) / Math.log(2.0)) + 1))); heap = new Node[size]; build(1, 0, array.length); } public int size() { return array.length; } //Initialize the Nodes of the Segment tree private void build(int v, int from, int size) { heap[v] = new Node(); heap[v].from = from; heap[v].to = from + size - 1; if (size == 1) { heap[v].sum = array[from]; heap[v].min = array[from]; } else { //Build childs build(2 * v, from, size / 2); build(2 * v + 1, from + size / 2, size - size / 2); heap[v].sum = heap[2 * v].sum + heap[2 * v + 1].sum; //min = min of the children heap[v].min = Math.min(heap[2 * v].min, heap[2 * v + 1].min); } } public int rsq(int from, int to) { return rsq(1, from, to); } private int rsq(int v, int from, int to) { Node n = heap[v]; //If you did a range update that contained this node, you can infer the Sum without going down the tree if (n.pendingVal != null && contains(n.from, n.to, from, to)) { return (to - from + 1) * n.pendingVal; } if (contains(from, to, n.from, n.to)) { return heap[v].sum; } if (intersects(from, to, n.from, n.to)) { propagate(v); int leftSum = rsq(2 * v, from, to); int rightSum = rsq(2 * v + 1, from, to); return leftSum + rightSum; } return 0; } public int rMinQ(int from, int to) { return rMinQ(1, from, to); } private int rMinQ(int v, int from, int to) { Node n = heap[v]; //If you did a range update that contained this node, you can infer the Min value without going down the tree if (n.pendingVal != null && contains(n.from, n.to, from, to)) { return n.pendingVal; } if (contains(from, to, n.from, n.to)) { return heap[v].min; } if (intersects(from, to, n.from, n.to)) { propagate(v); int leftMin = rMinQ(2 * v, from, to); int rightMin = rMinQ(2 * v + 1, from, to); return Math.min(leftMin, rightMin); } return Integer.MAX_VALUE; } public void update(int from, int to, int value) { update(1, from, to, value); } private void update(int v, int from, int to, int value) { //The Node of the heap tree represents a range of the array with bounds: [n.from, n.to] Node n = heap[v]; if (contains(from, to, n.from, n.to)) { change(n, value); } if (n.size() == 1) return; if (intersects(from, to, n.from, n.to)) { propagate(v); update(2 * v, from, to, value); update(2 * v + 1, from, to, value); n.sum = heap[2 * v].sum + heap[2 * v + 1].sum; n.min = Math.min(heap[2 * v].min, heap[2 * v + 1].min); } } //Propagate temporal values to children private void propagate(int v) { Node n = heap[v]; if (n.pendingVal != null) { change(heap[2 * v], n.pendingVal); change(heap[2 * v + 1], n.pendingVal); n.pendingVal = null; //unset the pending propagation value } } //Save the temporal values that will be propagated lazily private void change(Node n, int value) { n.pendingVal = value; n.sum = n.size() * value; n.min = value; array[n.from] = value; } //Test if the range1 contains range2 private boolean contains(int from1, int to1, int from2, int to2) { return from2 >= from1 && to2 <= to1; } //check inclusive intersection, test if range1[from1, to1] intersects range2[from2, to2] private boolean intersects(int from1, int to1, int from2, int to2) { return from1 <= from2 && to1 >= from2 // (.[..)..] or (.[...]..) || from1 >= from2 && from1 <= to2; // [.(..]..) or [..(..).. } //The Node class represents a partition range of the array. static class Node { int sum; int min; //Here We store the value that will be propagated lazily Integer pendingVal = null; int from; int to; int size() { return to - from + 1; } } } @SuppressWarnings("serial") static class CountMap<T> extends TreeMap<T, Integer>{ CountMap() { } CountMap(T[] arr) { this.putCM(arr); } public Integer putCM(T key) { if (super.containsKey(key)) { return super.put(key, super.get(key) + 1); } else { return super.put(key, 1); } } public Integer removeCM(T key) { Integer count = super.get(key); if (count == null) return -1; if (count == 1) return super.remove(key); else return super.put(key, super.get(key) - 1); } public Integer getCM(T key) { Integer count = super.get(key); if (count == null) return 0; return count; } public void putCM(T[] arr) { for (T l : arr) this.putCM(l); } } static class Point implements Comparable<Point> { long x; long y; long id; Point() { x = y = id = 0; } Point(Point p) { this.x = p.x; this.y = p.y; this.id = p.id; } Point(long a, long b, long id) { this.x = a; this.y = b; this.id = id; } Point(long a, long b) { this.x = a; this.y = b; } @Override public int compareTo(Point o) { if (this.x < o.x) return -1; if (this.x > o.x) return 1; if (this.y < o.y) return -1; if (this.y > o.y) return 1; return 0; } public boolean equals(Point that) { return this.compareTo(that) == 0; } } static int longestPrefixPalindromeLen(char[] s) { int n = s.length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) sb.append(s[i]); StringBuilder sb2 = new StringBuilder(sb); sb.append('^'); sb.append(sb2.reverse()); // out.println(sb.toString()); char[] newS = sb.toString().toCharArray(); int m = newS.length; int[] pi = piCalcKMP(newS); return pi[m - 1]; } static int KMPNumOcc(char[] text, char[] pat) { int n = text.length; int m = pat.length; char[] patPlusText = new char[n + m + 1]; for (int i = 0; i < m; i++) patPlusText[i] = pat[i]; patPlusText[m] = '^'; // Seperator for (int i = 0; i < n; i++) patPlusText[m + i] = text[i]; int[] fullPi = piCalcKMP(patPlusText); int answer = 0; for (int i = 0; i < n + m + 1; i++) if (fullPi[i] == m) answer++; return answer; } static int[] piCalcKMP(char[] s) { int 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 String toBinaryString(long num, int bits) { StringBuilder sb = new StringBuilder(Long.toBinaryString(num)); sb.reverse(); for (int i = sb.length(); i < bits; i++) sb.append('0'); return sb.reverse().toString(); } static long modDiv(long a, long b) { return mod(a * power(b, gigamod - 2, gigamod)); } static void coprimeGenerator(int m, int n, ArrayList<Point> coprimes, int limit, int numCoprimes) { if (m > limit) return; if (m <= limit && n <= limit) coprimes.add(new Point(m, n)); if (coprimes.size() > numCoprimes) return; coprimeGenerator(2 * m - n, m, coprimes, limit, numCoprimes); coprimeGenerator(2 * m + n, m, coprimes, limit, numCoprimes); coprimeGenerator(m + 2 * n, n, coprimes, limit, numCoprimes); } static long hash(long i) { return (i * 2654435761L % gigamod); } static long hash2(long key) { long h = Long.hashCode(key); h ^= (h >>> 20) ^ (h >>> 12) ^ (h >>> 7) ^ (h >>> 4); return h & (gigamod-1); } static int mapTo1D(int row, int col, int n, int m) { // Maps elements in a 2D matrix serially to elements in // a 1D array. return row * m + col; } static int[] mapTo2D(int idx, int n, int m) { // Inverse of what the one above does. int[] rnc = new int[2]; rnc[0] = idx / m; rnc[1] = idx % m; return rnc; } static double distance(Point p1, Point p2) { return Math.sqrt(Math.pow(p2.y-p1.y, 2) + Math.pow(p2.x-p1.x, 2)); } static Map<Integer, Integer> smolNumPrimeFactorization(int num) { if (smallestFactorOf == null) primeGenerator(200001); CountMap<Integer> fnps = new CountMap<>(); while (num != 1) { fnps.putCM(smallestFactorOf[num]); num /= smallestFactorOf[num]; } return fnps; } static HashMap<Long, Integer> primeFactorization(long num) { // Returns map of factor and its power in the number. HashMap<Long, Integer> map = new HashMap<>(); while (num % 2 == 0) { num /= 2; Integer pwrCnt = map.get(2L); map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1); } for (long i = 3; i * i <= num; i += 2) { while (num % i == 0) { num /= i; Integer pwrCnt = map.get(i); map.put(i, pwrCnt != null ? pwrCnt + 1 : 1); } } // If the number is prime, we have to add it to the // map. if (num != 1) map.put(num, 1); return map; } static HashSet<Long> divisors(long num) { HashSet<Long> divisors = new HashSet<Long>(); divisors.add(1L); divisors.add(num); for (long i = 2; i * i <= num; i++) { if (num % i == 0) { divisors.add(num/i); divisors.add(i); } } return divisors; } static int bsearch(int[] arr, int val, int lo, int hi) { // Returns the index of the first element // larger than or equal to val. int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { idx = mid; hi = mid - 1; } else lo = mid + 1; } return idx; } static int bsearch(long[] arr, long val, int lo, int hi) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { idx = mid; hi = mid - 1; } else lo = mid + 1; } return idx; } static int bsearch(long[] arr, long val, int lo, int hi, boolean sMode) { // Returns the index of the last element // smaller than or equal to val. int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { hi = mid - 1; } else { idx = mid; lo = mid + 1; } } return idx; } static int bsearch(int[] arr, long val, int lo, int hi, boolean sMode) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] > val) { hi = mid - 1; } else { idx = mid; lo = mid + 1; } } return idx; } static long factorial(long n) { if (n <= 1) return 1; long factorial = 1; for (int i = 1; i <= n; i++) factorial = mod(factorial * i); return factorial; } static long factorialInDivision(long a, long b) { if (a == b) return 1; if (b < a) { long temp = a; a = b; b = temp; } long factorial = 1; for (long i = a + 1; i <= b; i++) factorial = mod(factorial * i); return factorial; } static long nCr(long n, long r, long[] fac) { long p = 998244353; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r /*long fac[] = new long[(int)n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p;*/ return (fac[(int)n] * modInverse(fac[(int)r], p) % p * modInverse(fac[(int)n - (int)r], p) % p) % p; } static long modInverse(long n, long p) { return power(n, p - 2, p); } static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if ((y & 1)==1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long nPr(long n, long r) { return factorialInDivision(n, n - r); } static int logk(long n, long k) { return (int)(Math.log(n) / Math.log(k)); } static boolean[] primeGenerator(int upto) { // Sieve of Eratosthenes: isPrime = new boolean[upto + 1]; smallestFactorOf = new int[upto + 1]; Arrays.fill(smallestFactorOf, 1); Arrays.fill(isPrime, true); isPrime[1] = isPrime[0] = false; for (long i = 2; i < upto + 1; i++) if (isPrime[(int) i]) { smallestFactorOf[(int) i] = (int) i; // Mark all the multiples greater than or equal // to the square of i to be false. for (long j = i; j * i < upto + 1; j++) { if (isPrime[(int) j * (int) i]) { isPrime[(int) j * (int) i] = false; smallestFactorOf[(int) j * (int) i] = (int) i; } } } return isPrime; } static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static long gcd(long[] arr) { int n = arr.length; long gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static int gcd(int[] arr) { int n = arr.length; int gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long lcm(long[] arr) { long lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(long a, long b) { return (a * b)/gcd(a, b); } static boolean less(int a, int b) { return a < b ? true : false; } static boolean isSorted(int[] a) { for (int i = 1; i < a.length; i++) { if (less(a[i], a[i - 1])) return false; } return true; } static boolean isSorted(long[] a) { for (int i = 1; i < a.length; i++) { if (a[i] <= a[i - 1]) return false; } return true; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(double[] a, int i, int j) { double temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(char[] a, int i, int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; } static void sort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void reverseSort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverse(char[] arr) { int n = arr.length; for (int i = 0; i < n / 2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void shuffleArray(long[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(int[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(double[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { double tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(char[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { char tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } 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 (long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static String toString(int[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(boolean[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(long[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(char[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + ""); return sb.toString(); } static String toString(int[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(long[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(double[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(char[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static long mod(long a, long m) { return (a%m + m) % m; } static long mod(long num) { return (num % gigamod + gigamod) % gigamod; } } // NOTES: // ASCII VALUE OF 'A': 65 // ASCII VALUE OF 'a': 97 // Range of long: 9 * 10^18 // ASCII VALUE OF '0': 48 // Primes upto 'n' can be given by (n / (logn)).
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
ab454c594abfefd319cad0876c6aaed8
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; public final class Main { //int 2e9 - long 9e18 static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(0, 1), new Pair(1, 0), new Pair(0, -1)}; static Pair[] movesDiagonal = new Pair[]{new Pair(-1, -1), new Pair(-1, 1), new Pair(1, -1), new Pair(1, 1)}; static int mod = (int) (1e9 + 7); static int mod2 = 998244353; public static void main(String[] args) { int tt = i(); while (tt-- > 0) { solve(); } out.flush(); } public static void solve() { int n = i(); String s = s(); int ans = 0; for (int i = 0; i < n; i += 2) { if (s.charAt(i) != s.charAt(i + 1)) { ans++; } } out.println(ans); } // (10,5) = 2 ,(11,5) = 3 static long upperDiv(long a, long b) { return (a / b) + ((a % b == 0) ? 0 : 1); } static long sum(int[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static int[] preint(int[] a) { int[] pre = new int[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] pre(int[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] post(int[] a) { long[] post = new long[a.length + 1]; post[0] = 0; for (int i = 0; i < a.length; i++) { post[i + 1] = post[i] + a[a.length - 1 - i]; } return post; } static long[] pre(long[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static void print(char A[]) { for (char c : A) { out.print(c); } out.println(); } static void print(boolean A[]) { for (boolean c : A) { out.print(c + " "); } out.println(); } static void print(int A[]) { for (int c : A) { out.print(c + " "); } out.println(); } static void print(long A[]) { for (long i : A) { out.print(i + " "); } out.println(); } static void print(List<Integer> A) { for (int a : A) { out.print(a + " "); } } 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 String c() { return in.next(); } static int[][] inputWithIdx(int N) { int A[][] = new int[N][2]; for (int i = 0; i < N; i++) { A[i] = new int[]{i, in.nextInt()}; } return A; } 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 int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long GCD(long a, long b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long LCM(int a, int b) { return (long) a / GCD(a, b) * b; } static long LCM(long a, long b) { return a / GCD(a, b) * b; } // find highest i which satisfy a[i]<=x static int lowerbound(int[] a, int x) { int l = 0; int r = a.length - 1; while (l < r) { int m = (l + r + 1) / 2; if (a[m] <= x) { l = m; } else { r = m - 1; } } return l; } static void shuffle(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } } static void shuffleAndSort(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static void shuffleAndSort(int[][] arr, Comparator<? super int[]> comparator) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int[] temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr, comparator); } static void shuffleAndSort(long[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); long temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static boolean isPerfectSquare(double number) { double sqrt = Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static void swap(int A[], int a, int b) { int t = A[a]; A[a] = A[b]; A[b] = t; } static void swap(char A[], int a, int b) { char t = A[a]; A[a] = A[b]; A[b] = t; } static long pow(long a, long b, int mod) { 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 pow(long a, long b) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow *= x; } x = x * x; b /= 2; } return pow; } static long modInverse(long x, int mod) { return pow(x, mod - 2, mod); } 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; } public static String reverse(String str) { if (str == null) { return null; } return new StringBuilder(str).reverse().toString(); } public static void reverse(int[] arr) { int l = 0; int r = arr.length - 1; while (l < r) { swap(arr, l, r); l++; r--; } } public static String repeat(char ch, int repeat) { if (repeat <= 0) { return ""; } final char[] buf = new char[repeat]; for (int i = repeat - 1; i >= 0; i--) { buf[i] = ch; } return new String(buf); } public static int[] manacher(String s) { char[] chars = s.toCharArray(); int n = s.length(); int[] d1 = new int[n]; for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1); while (0 <= i - k && i + k < n && chars[i - k] == chars[i + k]) { k++; } d1[i] = k--; if (i + k > r) { l = i - k; r = i + k; } } return d1; } public static int[] kmp(String s) { int n = s.length(); int[] res = new int[n]; for (int i = 1; i < n; ++i) { int j = res[i - 1]; while (j > 0 && s.charAt(i) != s.charAt(j)) { j = res[j - 1]; } if (s.charAt(i) == s.charAt(j)) { ++j; } res[i] = j; } return res; } } class Pair { int i; int j; Pair(int i, int j) { this.i = i; this.j = j; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Pair pair = (Pair) o; return i == pair.i && j == pair.j; } @Override public int hashCode() { return Objects.hash(i, j); } } class ThreePair { int i; int j; int k; ThreePair(int i, int j, int k) { this.i = i; this.j = j; this.k = k; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } ThreePair pair = (ThreePair) o; return i == pair.i && j == pair.j && k == pair.k; } @Override public int hashCode() { return Objects.hash(i, j); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } class Node { int val; public Node(int val) { this.val = val; } } class ST { int n; Node[] st; ST(int n) { this.n = n; st = new Node[4 * Integer.highestOneBit(n)]; } void build(Node[] nodes) { build(0, 0, n - 1, nodes); } private void build(int id, int l, int r, Node[] nodes) { if (l == r) { st[id] = nodes[l]; return; } int mid = (l + r) >> 1; build((id << 1) + 1, l, mid, nodes); build((id << 1) + 2, mid + 1, r, nodes); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } void update(int i, Node node) { update(0, 0, n - 1, i, node); } private void update(int id, int l, int r, int i, Node node) { if (i < l || r < i) { return; } if (l == r) { st[id] = node; return; } int mid = (l + r) >> 1; update((id << 1) + 1, l, mid, i, node); update((id << 1) + 2, mid + 1, r, i, node); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } Node get(int x, int y) { return get(0, 0, n - 1, x, y); } private Node get(int id, int l, int r, int x, int y) { if (x > r || y < l) { return new Node(0); } if (x <= l && r <= y) { return st[id]; } int mid = (l + r) >> 1; return comb(get((id << 1) + 1, l, mid, x, y), get((id << 1) + 2, mid + 1, r, x, y)); } Node comb(Node a, Node b) { if (a == null) { return b; } if (b == null) { return a; } return new Node(GCD(a.val, b.val)); } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
d145ec06eac06d170942e2301cc477e1
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
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 TokitsukazeandGood { static InputReader inputReader=new InputReader(System.in); static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static void solve() throws IOException { int count=0; int n=inputReader.nextInt(); String str=inputReader.readString(); for (int i=1;i<n;i+=2) { if (str.charAt(i)!=str.charAt(i-1)) { count++; } }out.println(count); } 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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
fbc7c40136af3ea32c6b31828662565c
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.stream.IntStream; import java.util.Arrays; import java.io.ByteArrayOutputStream; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; import java.util.InputMismatchException; import java.io.FileNotFoundException; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author tauros */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; RealFastReader in = new RealFastReader(inputStream); RealFastWriter out = new RealFastWriter(outputStream); CF789B1 solver = new CF789B1(); solver.solve(1, in, out); out.close(); } static class CF789B1 { public void solve(int testNumber, RealFastReader in, RealFastWriter out) { int cases = in.ni(); while (cases-- > 0) { int n = in.ni(); char[] chars = in.ns(n); int ans = 0; for (int i = 0; i < n; i += 2) { if (chars[i] != chars[i + 1]) { ans++; } } out.println(ans); } out.flush(); } } static class RealFastReader { InputStream is; private byte[] inbuf = new byte[1024]; public int lenbuf = 0; public int ptrbuf = 0; public RealFastReader(final InputStream is) { this.is = is; } public int readByte() { if (lenbuf == -1) { throw new InputMismatchException(); } if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) { return -1; } } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } public int ni() { return (int) nl(); } public long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } static class RealFastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private OutputStream out; private Writer writer; private int ptr = 0; private RealFastWriter() { out = null; } public RealFastWriter(Writer writer) { this.writer = new BufferedWriter(writer); out = new ByteArrayOutputStream(); } public RealFastWriter(OutputStream os) { this.out = os; } public RealFastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public RealFastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) { innerflush(); } return this; } public RealFastWriter 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 RealFastWriter 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 RealFastWriter 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 RealFastWriter writeln(int x) { return write(x).writeln(); } public RealFastWriter writeln() { return write((byte) '\n'); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { if (writer != null) { writer.write(((ByteArrayOutputStream) out).toString()); out = new ByteArrayOutputStream(); writer.flush(); } else { out.flush(); } } catch (IOException e) { throw new RuntimeException("flush"); } } public RealFastWriter println(int x) { return writeln(x); } public void close() { flush(); try { out.close(); } catch (Exception e) { } } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
29014fbf5f339cbd2956e67c486568a8
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.stream.IntStream; import java.util.Arrays; import java.io.ByteArrayOutputStream; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; import java.util.InputMismatchException; import java.io.FileNotFoundException; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author tauros */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; RealFastReader in = new RealFastReader(inputStream); RealFastWriter out = new RealFastWriter(outputStream); CF789B1 solver = new CF789B1(); solver.solve(1, in, out); out.close(); } static class CF789B1 { public void solve(int testNumber, RealFastReader in, RealFastWriter out) { int cases = in.ni(); while (cases-- > 0) { int n = in.ni(); char[] chars = in.ns(n); int len = 0, eCnt = 0; boolean hasO = false; int ans = 0; for (int i = 0; i < n; i++) { len++; if (i == n - 1 || chars[i + 1] != chars[i]) { if (len % 2 == 1) { if (!hasO) { eCnt = 0; hasO = true; } else { ans += eCnt + 1; hasO = false; } } else { eCnt++; } len = 0; } } out.println(ans); } out.flush(); } } static class RealFastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private OutputStream out; private Writer writer; private int ptr = 0; private RealFastWriter() { out = null; } public RealFastWriter(Writer writer) { this.writer = new BufferedWriter(writer); out = new ByteArrayOutputStream(); } public RealFastWriter(OutputStream os) { this.out = os; } public RealFastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public RealFastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) { innerflush(); } return this; } public RealFastWriter 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 RealFastWriter 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 RealFastWriter 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 RealFastWriter writeln(int x) { return write(x).writeln(); } public RealFastWriter writeln() { return write((byte) '\n'); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { if (writer != null) { writer.write(((ByteArrayOutputStream) out).toString()); out = new ByteArrayOutputStream(); writer.flush(); } else { out.flush(); } } catch (IOException e) { throw new RuntimeException("flush"); } } public RealFastWriter println(int x) { return writeln(x); } public void close() { flush(); try { out.close(); } catch (Exception e) { } } } static class RealFastReader { InputStream is; private byte[] inbuf = new byte[1024]; public int lenbuf = 0; public int ptrbuf = 0; public RealFastReader(final InputStream is) { this.is = is; } public int readByte() { if (lenbuf == -1) { throw new InputMismatchException(); } if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) { return -1; } } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } public int ni() { return (int) nl(); } public long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
56901968c25d07c0f81fa774f364b399
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; public class k { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int res = 0; String s = sc.next(); for (int i = 0;i<n;i+=2) { if (s.charAt(i) != s.charAt(i+1)) { res+=1; } }out.println(res); } out.close(); } static boolean inc(int[] a) { for (int i = 0; i < a.length-1; i++) if (a[i] >= a[i+1]) return false; return true; } static boolean decOrEq(int[] a) { for (int i = 0; i < a.length-1; i++) if (a[i] < a[i+1]) return false; return true; } 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 void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class 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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
9c367bffe7bd6aa56a3ab1214ec80a82
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.Scanner; public class main2{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int count = 0;count<t;count++){ int n = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); boolean h = true; int length = 1; char c = s.charAt(0); int total = 0; for(int i = 1;i<s.length();i++){ if(c == s.charAt(i)){ length++; }else{ if(length%2!=0){ if(total==0){ length = 2; h = false; c = s.charAt(i); }else{ length++; } total++; }else{ c = s.charAt(i); length = 1; } } } if(h==true){ System.out.println(0); }else{ System.out.println(total); } } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
f1410578f60ef129877a7af601304adb
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; public class B { static PrintWriter pw; static Scanner sc; static pair[][] memo; static int end; static ArrayList<Integer> size; static int inf = (int) 1e9; static int get(int st) { return st == 2 ? -1 : st; } static pair dp(int st, int i) { if (i == end) { return st == 0 ? new pair(0, 0) : new pair(inf, inf); } if (memo[st][i] != null) return new pair(memo[st][i].x, memo[st][i].y); int cur = size.get(i) + get(st); if (cur % 2 == 0) { pair p = dp(0, i + 1); pair ans=new pair(p.x, p.y); if (cur == 0) ans.y--; else ans.y++; // pw.println(st+" "+i+" "+ p); return memo[st][i] = ans; } else { // pw.println(st + " " + i + " " + cur); int y = cur - 1; if (y == 0) { pair p1 = dp(1, i + 1); pair p2 = dp(2, i + 1); p2.y++; p1.x++; p2.x++; // pw.println(st+" "+i+" "+p1 + " " + p2); return memo[st][i] = p1.merge(p2); } else { pair p1 = dp(1, i + 1); pair p2 = dp(2, i + 1); p1.y++; p2.y++; p1.x++; p2.x++; // pw.println(st+" "+i+" "+ p1 + " " + p2); return memo[st][i] = p1.merge(p2); } } } public static void main(String[] args) throws Exception { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); char[] arr = sc.next().toCharArray(); char first = arr[0]; int cnt = 0; size = new ArrayList<>(); for (int i = 0; i < n;) { while (i < n && arr[i] == first) { i++; cnt++; } size.add(cnt); if (i == n) break; first = arr[i]; cnt = 0; } end = size.size(); memo = new pair[3][end]; pw.println(dp(0, 0).x); // pw.println(Arrays.deepToString(memo)); } pw.flush(); } static class pair { int x, y; public pair(int a, int b) { x = a; y = b; } public pair merge(pair p) { if (x < p.x) { return new pair(x, y); } else if (p.x < x) { return new pair(p.x, p.y); } else if (y < p.y) { return new pair(x, y); } return new pair(p.x, p.y); } public String toString() { return x + " " + y; } } 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 Scanner(String s) throws IOException { br = new BufferedReader(new FileReader(new File(s))); } 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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
3e6184c2869814e7cebcbf6af6159b1e
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; public class Main { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(); String str = sc.ns(); char[] strr = str.toCharArray(); int ct = 1; char prev = strr[0]; int moves = 0; for(int i = 1; i < n; i++) { if(strr[i] == prev) ct++; else { if(ct%2==0) { ct = 1; prev = strr[i]; continue; } else { moves++; ct = 0; continue; } } } w.p(moves); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
e4d1547a130727c7bb3fde9fd29b49f1
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; public class Main { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(); String str = sc.ns(); char[] strr = str.toCharArray(); int ct = 1; char prev = strr[0]; int odd = 0; int moves = 0; for(int i = 1; i < n; i++) { if(strr[i] == prev) { ct++; } else { if(ct%2==0) { prev = strr[i]; ct = 1; continue; } moves++; ct = 0; } } if(ct%2==1) { moves++; } w.p(moves); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
e753860d84ea1a105125f91fda2b1777
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
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= 10000_0000_7; static int dp[][]; 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; // int n=1000000000; outer:for(int time=1;time<=t;time++) { int n=fs.nextInt(); char arr[]=fs.next().toCharArray(); List<Integer> list=new ArrayList<>(); int cnta[]=new int[n+1], cntb[]=new int[n+1]; for(int i=1;i<=n;i++) { cnta[i]=cnta[i-1]; cntb[i]=cntb[i-1]; if(arr[i-1]=='0') { cnta[i]++; } else cntb[i]++; } int x=0; while(x<n) { int j=x+1; int cnt=1; while(j<n&&arr[j]==arr[x]) { j++; cnt++; } list.add(cnt); x=j; } // List<Integer> ind=new ArrayList<>(); // for(int i=0;i<list.size();i++) { // int ele=list.get(i); // if(ele%2==0) { // } // ind.add(i); // } int ans=0; int i=0; int sum=0; // System.out.println(list); while(i<list.size()) { while(i<list.size()&&list.get(i)%2==0) { sum+=list.get(i); i++; } if(i==list.size()) break; sum+=list.get(i); i++; int cnt=0; int a=sum; while(i<list.size()&&list.get(i)%2==0) { sum+=list.get(i); i++; cnt++; } int b=sum; sum+=list.get(i); i++; // System.out.println(a+" "+b); ans+= cnt+1; } out.println(ans); } out.close(); } static int find(int i,int j,int cnta[],int cntb[]) { return 1+Math.min(cnta[j+1]-cnta[i], cntb[j+1]-cntb[i]); } 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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
cfdad86591e613156b16d3012d894833
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
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 B { public static void main(String[] args){ int b, t; char a[]; String str; FastReader in = new FastReader(); t = in.nextInt(); while(t-- > 0) { boolean ok = true; int cnt = 0; int n = in.nextInt(); a = in.next().toCharArray(); for(int i = 1; i < n;i+=2) { if(a[i] != a[i - 1]) { cnt++; } } System.out.println(cnt); } } 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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
355ff5d6cd3221e05cbf4217936329ca
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import static java.lang.System.out; public class pre54 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]){ FastReader obj = new FastReader(); int tc = obj.nextInt(); while(tc--!=0){ int n = obj.nextInt(); char str[] = obj.next().toCharArray(); int count = 0; ArrayList<Integer> con = new ArrayList<>(); char c = str[0]; count = 1; for(int i=1;i<n;i++){ if(c!=str[i]){ con.add(count); count = 1; c = str[i]; }else count++; } con.add(count); // out.println(con); int ans = 0; for(int i=0;i<con.size()-1;i++){ if(con.get(i)%2!=0){ con.set(i,con.get(i)-1); con.set(i+1,con.get(i+1)+1); ans++; } } out.println(ans); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
af0fa38d2fa2406a977e870eac235462
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.lang.*; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.File; import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; public class Main { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair ob) { return (int)(first - ob.first); } } static class Tuple implements Comparable<Tuple> { long first, second,third; public Tuple(long first, long second, long third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int count = 0; int[] parent; int[] rank; public DSU(int n) { count = n; parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public void union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return; if(rank[a] < rank[b]) { parent[a] = b; } else if(rank[a] > rank[b]) { parent[b] = a; } else { parent[b] = a; rank[a] = 1 + rank[a]; } count--; } public int countConnected() { return count; } } static class Reader { 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) throws IOException { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] longReadArray(int n) throws IOException { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static long lcm(long a, long b) { return (a / LongGCD(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long powMod(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return powMod(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)(n)] * modInverse(fac[(int)(r)], p) % p * modInverse(fac[(int)(n - r)], p) % p) % p; } public static long fact(long n) { long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (long i = 1; i <= n; i++) fac[(int)(i)] = fac[(int)(i - 1)] * i; return fac[(int)(n)]; } public static long nCr(long n, long k) { long ans = 1; for(long i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } //Modular Operations for Addition and Multiplication. public static long perfomMod(long x) { return ((x%M + M)%M); } public static long addMod(long a, long b) { return perfomMod(perfomMod(a)+perfomMod(b)); } public static long subMod(long a, long b) { return perfomMod(perfomMod(a)-perfomMod(b)); } public static long mulMod(long a, long b) { return perfomMod(perfomMod(a)*perfomMod(b)); } public static boolean isPrime(long n) { if(n == 1) { return false; } //check only for sqrt of the number as the divisors //keep repeating so only half of them are required. So,sqrt. for(int i = 2;i*i<=n;i++) { if(n%i == 0) { return false; } } return true; } public static List<Long> SieveList(int n) { boolean prime[] = new boolean[(int)(n+1)]; Arrays.fill(prime, true); List<Long> l = new ArrayList<>(); 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; } } } for (long p = 2; p<=n; p++) { if (prime[(int)(p)] == true) { l.add(p); } } return l; } public static int countDivisors(int x) { int c = 0; for(int i = 1;i*i<=x;i++) { if(x%i == 0) { if(x/i != i) { c+=2; } else { c++; } } } return c; } public static long log2(long n) { long ans = (long)(log(n)/log(2)); return ans; } public static boolean isPow2(long n) { return (n != 0 && ((n & (n-1))) == 0); } public static boolean isSq(int x) { long s = (long)Math.round(Math.sqrt(x)); return s*s==x; } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(int a[], int x) { int l=-1, r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void Sort(int[] a) { List<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); // Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void ssort(char[] a) { List<Character> l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int tt = sc.nextInt(); fr:while(tt-- > 0) { int n = sc.nextInt(); char[] s = sc.next().toCharArray(); long ans = 0; for(int i = 0;i<n;i+=2) { if(s[i] != s[i+1]) ans++; } fout.println(ans); } fout.close(); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
c043fe4375b6569549b93d00d2cfe4a7
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.lang.*; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.File; import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; public class Main { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair ob) { return (int)(first - ob.first); } } static class Tuple implements Comparable<Tuple> { long first, second,third; public Tuple(long first, long second, long third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int count = 0; int[] parent; int[] rank; public DSU(int n) { count = n; parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public void union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return; if(rank[a] < rank[b]) { parent[a] = b; } else if(rank[a] > rank[b]) { parent[b] = a; } else { parent[b] = a; rank[a] = 1 + rank[a]; } count--; } public int countConnected() { return count; } } static class Reader { 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) throws IOException { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] longReadArray(int n) throws IOException { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static long lcm(long a, long b) { return (a / LongGCD(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long powMod(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return powMod(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)(n)] * modInverse(fac[(int)(r)], p) % p * modInverse(fac[(int)(n - r)], p) % p) % p; } public static long fact(long n) { long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (long i = 1; i <= n; i++) fac[(int)(i)] = fac[(int)(i - 1)] * i; return fac[(int)(n)]; } public static long nCr(long n, long k) { long ans = 1; for(long i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } //Modular Operations for Addition and Multiplication. public static long perfomMod(long x) { return ((x%M + M)%M); } public static long addMod(long a, long b) { return perfomMod(perfomMod(a)+perfomMod(b)); } public static long subMod(long a, long b) { return perfomMod(perfomMod(a)-perfomMod(b)); } public static long mulMod(long a, long b) { return perfomMod(perfomMod(a)*perfomMod(b)); } public static boolean isPrime(long n) { if(n == 1) { return false; } //check only for sqrt of the number as the divisors //keep repeating so only half of them are required. So,sqrt. for(int i = 2;i*i<=n;i++) { if(n%i == 0) { return false; } } return true; } public static List<Long> SieveList(int n) { boolean prime[] = new boolean[(int)(n+1)]; Arrays.fill(prime, true); List<Long> l = new ArrayList<>(); 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; } } } for (long p = 2; p<=n; p++) { if (prime[(int)(p)] == true) { l.add(p); } } return l; } public static int countDivisors(int x) { int c = 0; for(int i = 1;i*i<=x;i++) { if(x%i == 0) { if(x/i != i) { c+=2; } else { c++; } } } return c; } public static long log2(long n) { long ans = (long)(log(n)/log(2)); return ans; } public static boolean isPow2(long n) { return (n != 0 && ((n & (n-1))) == 0); } public static boolean isSq(int x) { long s = (long)Math.round(Math.sqrt(x)); return s*s==x; } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(int a[], int x) { int l=-1, r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void Sort(int[] a) { List<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); // Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void ssort(char[] a) { List<Character> l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int tt = sc.nextInt(); fr:while(tt-- > 0) { int n = sc.nextInt(); char[] s = sc.next().toCharArray(); long ans = 0, c = 1; for(int i = 1;i<n;i++) { if(s[i] != s[i-1]) { if(c%2 == 0) { c = 1; } else if(c % 2 != 0) { ans++; c = 0; } } else { c++; } } fout.println(ans); } fout.close(); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
9a11af0c12b3cfbf2da69bcce5edd5aa
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class Template { private static StringTokenizer st; private static BufferedReader br; private static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int test = nextInt(); while (test -- > 0) { int n = nextInt(); char a[] = next().toCharArray(); int cnt = 0; boolean zeroOrOne = false; char start = a[0]; List<Integer> part = new ArrayList<>(); for(int i = 0 ; i < n;i ++){ if(start == a[i]) cnt++; else{ part.add(cnt); cnt = 1; start = a[i]; } } part.add(cnt); int ans = 0; for(int i = 0 ; i < part.size() -1 ;i ++){ if(part.get(i) % 2 == 0) continue; if(part.get(i + 1) >= 1){ part.set(i,part.get(i) + 1); part.set(i + 1,part.get(i + 1) - 1); ans ++; } } System.out.println(ans); } } private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static double nextDouble() throws IOException { return Double.parseDouble(next()); } private static String next() throws IOException { while (st==null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
4003c42343dab03d063dc6b08ac3adcb
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; public class B1_1678 { public static void main(String[] args) { /* * 1 14 10011011001001 */ FastReader sc = new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); char[] ch=sc.next().toCharArray(); int[] arr=new int[n]; int c=1; int ind=0; for(int i=0;i<n-1;i++) { if(ch[i]==ch[i+1]) { c++; continue; } arr[ind++]=c; c=1; } arr[ind]=c; int no=0; int add=0; int ans=0; boolean check=true; for(int i=0;i<=ind;i++) { if(no>0) { add++; if(arr[i]%2!=0) { no=0; ans+=add; add=0; check=false; } } if(arr[i]%2!=0 && check) no++; check=true; } System.out.println(ans); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return (str); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
872041138e6dc08fb8d8908ad1e15999
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; public class TokitsukazeAndGood01StringEasyVersion { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; public static void main(String[] args) throws IOException { int t = readInt(); while (t-- > 0) { int n = readInt(); String str = readLine(); int[] s = new int[n]; for (int i = 0; i < n; i ++) s[i] = Character.getNumericValue(str.charAt(i)); ArrayList<Integer> segments = new ArrayList<Integer>(); int cur = s[0], len = 1; for (int i = 1; i < n; i ++) { if (s[i] != cur) { segments.add(len); len = 1; cur = s[i]; } else len ++; } segments.add(len); int ans = 0, sum = 0; boolean odd = false; for (int i = 0; i < segments.size(); i ++) { if (!odd) { if (segments.get(i) % 2 == 1) { odd = true; } } else { if (segments.get(i) % 2 == 1) { odd = false; ans ++; sum = 0; } else { sum += segments.get(i); ans ++; } } } if (odd) System.out.println(ans + sum); else System.out.println(ans); } } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readLine() throws IOException { return br.readLine().trim(); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
76decf1812b0b139b6ce722609d72a4e
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
// letsgoooooooooooooooooooooooooooooooo import java.util.*; import java.io.*; public class Solution{ static int MOD=1000000007; static PrintWriter pw; static FastReader sc; 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());} public char nextChar() throws IOException {return next().charAt(0);} String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int[] intArr(int a,int b,int n) throws IOException {int[]in=new int[n];for(int i=a;i<b;i++)in[i]=nextInt();return in;} } static void Solve() throws Exception{ int n =sc.nextInt(); String s =sc.next(); StringBuilder sb = new StringBuilder(s); int count=0,ans=0; for(int i=0;i<s.length();i++){ count=0; char ch = s.charAt(i); while(i<n && s.charAt(i)==ch){ i++; count++; } if(count%2==1){ ans++; }else{ i--; } // pw.println(s); } pw.println(ans); } public static void main(String[] args) throws Exception{ try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } sc= new FastReader(); pw = new PrintWriter(System.out); int t=1; t=sc.nextInt(); for(int ii=1;ii<=t;ii++) { Solve(); } pw.flush(); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
77a7753571f78f6c780fb88d80e1cc6f
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
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(); String s = scn.next(); solve(n, s); tc--; } } public static void solve(int n, String s) { if (n == 2) { if (s.charAt(0) == s.charAt(1)) { System.out.println("0"); return; } else { System.out.println("1"); return; } } ArrayList<Integer> dividedSegmentsSizeList = new ArrayList<>(); for (int i = 0 ; i < n ; ) { int currentContinuousSegementSize = 1; while (i < n - 1 && s.charAt(i) == s.charAt(i + 1)) { currentContinuousSegementSize++; i++; } i++; dividedSegmentsSizeList.add(currentContinuousSegementSize); } int[] arr = new int[dividedSegmentsSizeList.size()]; for (int i = 0 ; i < arr.length ; i++) { arr[i] = dividedSegmentsSizeList.get(i); } int minimumCount = 0; for (int i = 0 ; i < arr.length - 1 ; i++) { if (arr[i] % 2 != 0 && arr[i + 1] % 2 != 0) { arr[i]--; arr[i + 1]++; minimumCount++; } else if (arr[i] % 2 != 0 && arr[i + 1] % 2 == 0) { arr[i]--; arr[i + 1]++; minimumCount++; } } System.out.println(minimumCount); return; } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
bbffb6173a69d7323914f3a958e0c183
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
//package com.company; import java.util.*; public class Practice { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); String s = sc.next(); char[] arr = s.toCharArray(); int cnt = 1; int flag = 0; for(int i = 0;i < n - 1;i++) { if(s.charAt(i) == s.charAt(i + 1)) cnt++; else { if(cnt % 2 != 0) flag = 1; else cnt = 1; } } if(flag == 0) System.out.println("0"); else { int count = 0; cnt = 1; for(int i = 0;i < n - 1;i++) { if(arr[i] == arr[i + 1]) cnt++; if(arr[i] != arr[i + 1] && cnt % 2 == 0) cnt = 1; else if(arr[i] != arr[i + 1]) { count++; arr[i + 1] = arr[i]; i++; cnt = 1; } } System.out.println(count); } } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
3b2f46293126959609bb315142358ef2
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class B2 { public static void main(String[] args) throws IOException { BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out); int t=Integer.parseInt(bf.readLine()); for(int i=0;i<t;i++) { int n=Integer.parseInt(bf.readLine()); String s=bf.readLine(); int sol=0; for(int j=0;j<n;j+=2) { char x1=s.charAt(j); char x2=s.charAt(j+1); if((x1=='0'&&x2=='0')||(x1=='1'&&x2=='1')) { continue; }else { sol++; } } pw.println(sol); } pw.close(); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
30b6996f1eee12a07a971b65839f2f1c
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; import static java.lang.System.out; public class app { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n=sc.nextInt(); String s=sc.next(); ArrayList<Integer> aa=new ArrayList<>(); char a1=s.charAt(0); int con=1; for(int i=1;i<n;i++) { if(a1==s.charAt(i)){ con++; if(i==n-1&&a1==s.charAt(i)){ aa.add(con); } } else{ aa.add(con); con=1; if(i==n-1&&a1!=s.charAt(i)){ aa.add(1); } a1=s.charAt(i); } } int cons=0; if(aa.size()==1) { out.println(0); } if(aa.size()>1) { int sss = aa.get(0); for (int i = 1; i < aa.size(); i++) { if (sss % 2 == 1) { cons++; sss = aa.get(i) - 1; } else{ sss=aa.get(i); } } out.println(cons); } } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
408d5585c2d44039aeb6360eb8c39803
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class tr1 { static PrintWriter out; static StringBuilder sb; static long mod = 998244353; static int m, id, c; static ArrayList<int[]>[] ad; static HashMap<Integer, Integer>[] going; static long inf = Long.MAX_VALUE; static char[][] g; static boolean[][] vis; static int[] ans, arr[], per; static int h, w; static HashMap<Integer, int[]> hmC, hmG; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); char[] s = sc.nextLine().toCharArray(); ArrayList<Integer> seg = new ArrayList<>(); ArrayList<Integer> type = new ArrayList<>(); for (int i = 0; i < n; i++) { int c = 1; while (i < n - 1 && s[i] == s[i + 1]) { i++; c++; } seg.add(c); type.add(s[i] - '0'); } int ans = 0; int carry = 0; for (int i = 0; i < seg.size(); i++) { int sum = carry + seg.get(i); if (sum % 2 == 1) ans++; carry = sum % 2; } out.println(ans); } out.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public int[] nextArrInt(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextArrLong(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextArrIntSorted(int n) throws IOException { int[] a = new int[n]; Integer[] a1 = new Integer[n]; for (int i = 0; i < n; i++) a1[i] = nextInt(); Arrays.sort(a1); for (int i = 0; i < n; i++) a[i] = a1[i].intValue(); return a; } public long[] nextArrLongSorted(int n) throws IOException { long[] a = new long[n]; Long[] a1 = new Long[n]; for (int i = 0; i < n; i++) a1[i] = nextLong(); Arrays.sort(a1); for (int i = 0; i < n; i++) a[i] = a1[i].longValue(); return a; } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
07866ea355803194bb9cb2b037a2e05b
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
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 Practice1 { // // 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 int[] sortrev(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,Collections.reverseOrder()); 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(); String str=sc.nextLine(); int count=1; ArrayList<Integer> al=new ArrayList<>(); int one=0,zero=0; for(int i=0;i<n;i++) { if(str.charAt(i)=='1') { one++; }else { zero++; } if(i==n-1) { al.add(count); continue; } if(str.charAt(i)!=str.charAt(i+1)) { al.add(count); count=1; }else { count++; } } int n1=al.size(); int[] arr=new int[n1]; for(int i=0;i<n1;i++) { arr[i]=al.get(i); } count=0; for(int i=0;i<n1-1;i++) { if(arr[i]==0||arr[i]%2==0) { continue; } count++; arr[i+1]--; } if(arr[n1-1]%2==0) { out.println(count); }else { out.println(Math.min(one,zero)); } } 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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
d44cc55d11d09efe6e8f03a4802ddbcc
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
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()); } int[] readIntArray(int n) throws IOException { int [] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = readInt(); } return a; } long[] readLongArray(int n) throws IOException { long [] a = new long[n]; for(int i = 0; i < n; i++) { a[i] = readLong(); } return a; } 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(); String s = readString(); char curChar = s.charAt(0); int curCharCount = 1; int ops = 0; for(int i=1;i<n;i++) { char c = s.charAt(i); if(c == curChar) { curCharCount++; continue; } if(curCharCount % 2 == 1) { ops++; curChar = curChar == '0' ? '1' : '0'; curCharCount = 2; } else { curChar = curChar == '0' ? '1' : '0'; curCharCount = 1; } } if(curCharCount % 2 == 1) { ops++; } out.println(ops); } void solve() throws IOException { int numTests = readInt(); while(numTests-->0) { solveTest(); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
5d6aea65adca80bb8edb619b466fce72
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Map.*; import static java.util.Arrays.*; import static java.util.Collections.*; import static java.lang.System.*; public class Main { public void tq() throws Exception { st=new StringTokenizer(bq.readLine()); int tq=i(); sb=new StringBuilder(2000000); o: while(tq-->0) { int n=i(); char ar[]=s().toCharArray(); LinkedList<Integer> l=new LinkedList<>(); char a=ar[0]; int c=0; for(char aa:ar) { if(aa==a)c++; else { l.add(c); c=1; a=aa; } } l.add(c); c=0; int p=l.remove(); while(l.size()>0) { int e=l.remove(); if(p%2!=0) { if(e%2!=0) { c++; if(l.size()==0)break; p=l.remove(); } else { c++; p=e-1; } continue; } p=e; } sl(c); } p(sb); } long mod=1000000007l; int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE;long maxl=Long.MAX_VALUE,minl=Long.MIN_VALUE; BufferedReader bq = new BufferedReader(new InputStreamReader(in));StringTokenizer st;StringBuilder sb; public static void main(String[] a)throws Exception{new Main().tq();} int di[][]={{-1,0},{1,0},{0,-1},{0,1}}; int de[][]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}}; void f1(int ar[],int v){fill(ar,v);} void f2(int ar[][],int v){for(int a[]:ar)fill(a,v);} void f3(int ar[][][],int v){for(int a[][]:ar)for(int b[]:a)fill(b,v);} void f1(long ar[],long v){fill(ar,v);} void f2(long ar[][],int v){for(long a[]:ar)fill(a,v);} void f3(long ar[][][],int v){for(long a[][]:ar)for(long b[]:a)fill(b,v);} void f(){out.flush();} int p(int i,int p[]){return p[i]<0?i:(p[i]=p(p[i],p));} boolean c(int x,int y,int n,int m){return x>=0&&x<n&&y>=0&&y<m;} int[] so(int ar[]) { Integer r[] = new Integer[ar.length]; for (int x = 0; x < ar.length; x++) r[x] = ar[x]; sort(r); for (int x = 0; x < ar.length; x++) ar[x] = r[x]; return ar; } long[] so(long ar[]) { Long r[] = new Long[ar.length]; for (int x = 0; x < ar.length; x++) r[x] = ar[x]; sort(r); for (int x = 0; x < ar.length; x++) ar[x] = r[x]; return ar; } char[] so(char ar[]) { Character r[] = new Character[ar.length]; for (int x = 0; x < ar.length; x++) r[x] = ar[x]; sort(r); for (int x = 0; x < ar.length; x++) ar[x] = r[x]; return ar; } void p(Object p) {out.print(p);}void pl(Object p) {out.println(p);}void pl() {out.println();} void s(String s) {sb.append(s);} void s(int s) {sb.append(s);} void s(long s) {sb.append(s);} void s(char s) {sb.append(s);} void s(double s) {sb.append(s);} void ss() {sb.append(' ');} void sl(String s) {s(s);sb.append("\n");} void sl(int s) {s(s);sb.append("\n");} void sl(long s) {s(s);sb.append("\n");} void sl(char s) {s(s);sb.append("\n");} void sl(double s) {s(s);sb.append("\n");} void sl() {sb.append("\n");} int l(int v) {return 31 - Integer.numberOfLeadingZeros(v);} long l(long v) {return 63 - Long.numberOfLeadingZeros(v);} int sq(int a) {return (int) sqrt(a);} long sq(long a) {return (long) sqrt(a);} int gcd(int a, int b) { while (b > 0) { int c = a % b; a = b; b = c; } return a; } long gcd(long a, long b) { while (b > 0l) { long c = a % b; a = b; b = c; } return a; } boolean p(String s, int i, int j) { while (i < j) if (s.charAt(i++) != s.charAt(j--)) return false; return true; } boolean[] si(int n) { boolean bo[] = new boolean[n + 1]; bo[0] = bo[1] = true; for (int x = 4; x <= n; x += 2) bo[x] = true; for (int x = 3; x * x <= n; x += 2) { if (!bo[x]) { int vv = (x << 1); for (int y = x * x; y <= n; y += vv) bo[y] = true; } } return bo; } long mul(long a, long b, long m) { long r = 1l; a %= m; while (b > 0) { if ((b & 1) == 1) r = (r * a) % m; b >>= 1; a = (a * a) % m; } return r; } int i() throws IOException { if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); return Integer.parseInt(st.nextToken()); } long l() throws IOException { if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); return Long.parseLong(st.nextToken()); } String s() throws IOException { if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); return st.nextToken(); } double d() throws IOException { if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); return Double.parseDouble(st.nextToken()); } void s(int a[]) { for (int e : a){sb.append(e);sb.append(' ');} sb.append("\n"); } void s(long a[]) { for (long e : a){sb.append(e);sb.append(' ');} sb.append("\n"); } void s(char a[]) { for (char e : a){sb.append(e);sb.append(' ');} sb.append("\n"); } void s(int ar[][]) {for (int a[] : ar) s(a);} void s(long ar[][]) {for (long a[] : ar) s(a);} void s(char ar[][]) {for (char a[] : ar) s(a);} int[] ari(int n) throws IOException { int ar[] = new int[n]; if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); for (int x = 0; x < n; x++) ar[x] = Integer.parseInt(st.nextToken()); return ar; } long[] arl(int n) throws IOException { long ar[] = new long[n]; if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); for (int x = 0; x < n; x++) ar[x] = Long.parseLong(st.nextToken()); return ar; } char[] arc(int n) throws IOException { char ar[] = new char[n]; if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); for (int x = 0; x < n; x++) ar[x] = st.nextToken().charAt(0); return ar; } double[] ard(int n) throws IOException { double ar[] = new double[n]; if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); for (int x = 0; x < n; x++) ar[x] = Double.parseDouble(st.nextToken()); return ar; } String[] ars(int n) throws IOException { String ar[] = new String[n]; if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); for (int x = 0; x < n; x++) ar[x] = st.nextToken(); return ar; } int[][] ari(int n, int m) throws IOException { int ar[][] = new int[n][m]; for (int x = 0; x < n; x++)ar[x]=ari(m); return ar; } long[][] arl(int n, int m) throws IOException { long ar[][] = new long[n][m]; for (int x = 0; x < n; x++)ar[x]=arl(m); return ar; } char[][] arc(int n, int m) throws IOException { char ar[][] = new char[n][m]; for (int x = 0; x < n; x++)ar[x]=arc(m); return ar; } double[][] ard(int n, int m) throws IOException { double ar[][] = new double[n][m]; for (int x = 0; x < n; x++)ar[x]=ard(m); return ar; } void p(int ar[]) { sb = new StringBuilder(11 * ar.length); for (int a : ar) {sb.append(a);sb.append(' ');} out.println(sb); } void p(long ar[]) { StringBuilder sb = new StringBuilder(20 * ar.length); for (long a : ar){sb.append(a);sb.append(' ');} out.println(sb); } void p(double ar[]) { StringBuilder sb = new StringBuilder(22 * ar.length); for (double a : ar){sb.append(a);sb.append(' ');} out.println(sb); } void p(char ar[]) { StringBuilder sb = new StringBuilder(2 * ar.length); for (char aa : ar){sb.append(aa);sb.append(' ');} out.println(sb); } void p(String ar[]) { int c = 0; for (String s : ar) c += s.length() + 1; StringBuilder sb = new StringBuilder(c); for (String a : ar){sb.append(a);sb.append(' ');} out.println(sb); } void p(int ar[][]) { StringBuilder sb = new StringBuilder(11 * ar.length * ar[0].length); for (int a[] : ar) { for (int aa : a){sb.append(aa);sb.append(' ');} sb.append("\n"); } p(sb); } void p(long ar[][]) { StringBuilder sb = new StringBuilder(20 * ar.length * ar[0].length); for (long a[] : ar) { for (long aa : a){sb.append(aa);sb.append(' ');} sb.append("\n"); } p(sb); } void p(double ar[][]) { StringBuilder sb = new StringBuilder(22 * ar.length * ar[0].length); for (double a[] : ar) { for (double aa : a){sb.append(aa);sb.append(' ');} sb.append("\n"); } p(sb); } void p(char ar[][]) { StringBuilder sb = new StringBuilder(2 * ar.length * ar[0].length); for (char a[] : ar) { for (char aa : a){sb.append(aa);sb.append(' ');} sb.append("\n"); } p(sb); } void pl(Object... ar) {for (Object e : ar) p(e + " ");pl();} }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
ab31cb0ac2eacc667f9f6ab6694bf358
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; public class Solution { 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 = Integer.parseInt(bf.readLine()); while(t-->0){ solve(); } } public static void solve()throws IOException{ int n = nextInt(); String[]arr = bf.readLine().split(""); String[]arr2 = arr.clone(); int count = 1; int ans = 0; for(int i =1;i<n;i++){ if(arr[i].equals(arr[i-1])){ count++; } else{ if(count%2 != 0){ arr[i] = arr[i-1].charAt(0)+""; ans++; count++; } else{ count = 1; } } } int ans2 = 0; count = 1; for(int i = n-2;i>=0;i--){ if(arr2[i].equals(arr2[i+1])){ count++; } else{ if(count%2 != 0){ arr2[i] = arr2[i+1].charAt(0)+""; ans2++; count++; } else{ count = 1; } } } println(Math.min(ans,ans2)); } public static int check(long mid,int []arr,long count){ for(int i = 0;i<arr.length;i++){ if(arr[i] != 0){ count = count - (mid - arr[i]); } } if(count == 0)return 0; if(count < 0)return -1; return 1; } // code for input public static void print(String s ){ System.out.print(s); } public static void print(int num ){ System.out.print(num); } public static void print(long num ){ System.out.print(num); } public static void println(String s){ System.out.println(s); } public static void println(int num){ System.out.println(num); } public static void println(long num){ System.out.println(num); } public static void println(){ System.out.println(); } public static int 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; } static class pair{ int one; int two; pair(int one,int two){ this.one = one ; this.two =two; } } public static long gcd(long a,long b){ if(b == 0)return a; return gcd(b,a%b); } public static long lcm(long a,long b){ return (a*b)/(gcd(a,b)); } public static boolean isPalindrome(String s){ int i = 0; int j = s.length()-1; while(i<=j){ if(s.charAt(i) != s.charAt(j)){ return false; } i++; j--; } return true; } } // use some math tricks it might help // sometimes just try to think in straightforward plan in A and B problems don't always complecate the questions with thinking too much differently // always use long number to do 10^9+7 modulo // if a problem is related to binary string it could also be related to parenthesis // *****try to use binary search(it is a very beautiful thing it can work in some of the very unexpected problems ) in the question it might work****** // try sorting // try to think in opposite direction of question it might work in your way // if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+d or a,b,c,d in general // if the question is to much related to left and/or right side of any element in an array then try monotonic stack it could work. // in range query sums try to do binary search it could work // analyse the time complexity of program thoroughly // anylyse the test cases properly // if we divide any number by 2 till it gets 1 then there will be (number - 1) operation required // try to do the opposite operation of what is given in the problem //think about the base cases properly //If a question is related to numbers try prime factorisation or something related to number theory // keep in mind unique strings
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
d5d79a3163d28a01af5d2a65235c48bb
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.List; public class MainB { public static void main(String[] args) throws IOException { // System.setIn(new FileInputStream("input.txt")); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); for (int test_case = 0; test_case < T; test_case++) { int n = Integer.parseInt(br.readLine()); String s = br.readLine(); int cnt = 1; char cur = s.charAt(0); List<Integer> list = new ArrayList<>(); for (int i = 1; i < n; i++) { if(s.charAt(i) == cur){ cnt++; }else{ cur = s.charAt(i); list.add(cnt); cnt = 1; } } list.add(cnt); int left = -1, ans = 0; for (int i = 0; i < list.size(); i++) { if(list.get(i) % 2 == 1){ if(left == -1) { left = i; }else{ ans += i - left; left = -1; } } } System.out.println(ans); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
20b409eedeb86298bc4d921cde9a57a9
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.StringTokenizer; public class Main { 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()); String s = in.readLine(); int n = Integer.parseInt(st1.nextToken()); int[] a = new int[n]; for (int i=0; i<n; i++) { char c = s.charAt(i); if (c == '1') a[i] = 1; } // List<Integer> temp = new ArrayList<>(); int zero = 0, one = 0, ans = 0; int prev = a[0] == '0' ? 0 : 1; for (int i=0; i<n; i++) { if (prev != a[i]) { // if (zero > 0) // temp.add(zero); // else // temp.add(one); if (zero%2 !=0 || one%2 != 0) { ans++; prev = a[i]; zero = 0; one = 0; if (a[i] == 0) zero++; else one++; } } if (a[i] == 0) zero++; else one++; prev = a[i]; } out.println(ans); } in.close(); out.close(); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
b9a263b28d07d9da9453e504ae0cf1a1
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; // import java.lang.invoke.ConstantBootstraps; // import java.math.BigInteger; // import java.beans.IndexedPropertyChangeEvent; import java.io.*; @SuppressWarnings("unchecked") public class Main implements Runnable { 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 * i <= 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 = 2 * i; j <= n; j += i) { prime[j] = false; } } } } static long gcd(long a, long b) { if (a < b) { long temp = a; a = b; b = temp; } if (b == 0) return a; return gcd(b, a % b); } static HashMap<Long, Long> map_prime_factors(long n) { HashMap<Long, Long> map = new HashMap<>(); while (n % 2 == 0) { map.put(2L, map.getOrDefault(2L, 0L) + 1L); n /= 2L; } for (long i = 3; i * i <= n; i++) { while (n % i == 0) { map.put(i, map.getOrDefault(i, 0L) + 1L); n /= i; } } if (n > 2) { map.put(n, map.getOrDefault(n, 0L) + 1L); } return map; } static List<Long> divisor(long n) { List<Long> ans = new ArrayList<>(); ans.add(1L); long count = 0; for (long i = 2L; i * i <= n; i++) { if (n % i == 0) { if (i == n / i) ans.add(i); else { ans.add(i); ans.add(n / i); } } } return ans; } static void sum_of_divisors(int n) { int[] dp = new int[n + 1]; for (int i = 1; i <= n; i++) { dp[i] += i; for (int j = i + i; j <= n; j += i) { dp[j] += i; } } } static void prime_factorization_using_sieve(int n) { int[] dp = new int[n + 1]; Arrays.fill(dp, Integer.MAX_VALUE); for (int i = 2; i <= n; i++) { dp[i] = Math.min(dp[i], i);// dp[i] stores smallest prime number which divides number i for (int j = 2 * i; j <= n; j++) {// can calculate prime factorization in O(logn) time by dividing // val/=dp[val]; till 1 is obtained dp[j] = Math.min(dp[j], i); } } } /* * ----------------------------------------------------Sorting------------------ * ------------------------------------------------ */ 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 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) * */ // ---------------------------------------------------Segment_Tree----------------------------------------------------------------// // static class comparator implements Comparator<node> { // public int compare(node a, node b) { // return a.a - b.a > 0 ? 1 : -1; // } // } static class Segment_Tree { 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 class min_Segment_Tree { private long[] segment_tree; public min_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] = Math.min(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 Integer.MAX_VALUE; if (left >= l && r >= right) { return segment_tree[index]; } if (l > right || left > r) return Integer.MAX_VALUE; int mid = (left + right) / 2; return Math.min(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] = Math.min(segment_tree[2 * index + 1], segment_tree[2 * index + 2]); } } static class max_Segment_Tree { public long[] segment_tree; public max_Segment_Tree(int n) { this.segment_tree = new long[4 * n + 1]; } void build(int index, int left, int right, long[] a) { // pn(index+" "+left+" "+right); 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] = Math.max(segment_tree[2 * index + 1], segment_tree[2 * index + 2]); } long query(int index, int left, int right, int l, int r) { if (left >= l && r >= right) { return segment_tree[index]; } if (l > right || left > r) return Long.MIN_VALUE; int mid = (left + right) / 2; long ans1=query(2 * index + 1, left, mid, l, r); long ans2=query(2 * index + 2, mid + 1, right, l, r); long max=Math.max(ans1,ans2); // pn(index+" "+left+" "+right+" "+max+" "+l+" "+r+" "+ans1+" "+ans2); return max; } 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] = Math.max(segment_tree[2 * index + 1], segment_tree[2 * index + 2]); } } // // ------------------------------------------------------ DSU // // --------------------------------------------------------------------// static class dsu { private int[] parent; private int[] rank; private int[] size; public dsu(int n) { this.parent = new int[n + 1]; this.rank = new int[n + 1]; this.size = new int[n + 1]; for (int i = 0; i <= n; i++) { parent[i] = i; rank[i] = 1; size[i] = 1; } } int findParent(int a) { if (parent[a] == a) return a; else return parent[a] = findParent(parent[a]); } void join(int a, int b) { int parent_a = findParent(a); int parent_b = findParent(b); if (parent_a == parent_b) return; if (rank[parent_a] > rank[parent_b]) { parent[parent_b] = parent_a; size[parent_a] += size[parent_b]; } else if (rank[parent_a] < rank[parent_b]) { parent[parent_a] = parent_b; size[parent_b] += size[parent_a]; } else { parent[parent_a] = parent_b; size[parent_b] += size[parent_a]; rank[parent_b]++; } } } // ------------------------------------------------Comparable---------------------------------------------------------------------// public static class rectangle { int x1, x3, y1, y3;// lower left and upper rigth coordinates int x2, y2, x4, y4;// remaining coordinates /* * (x4,y4) (x3,y3) * ____________ * | | * |____________| * * (x1,y1) (x2,y2) */ public rectangle(int x1, int y1, int x3, int y3) { this.x1 = x1; this.y1 = y1; this.x3 = x3; this.y3 = y3; this.x2 = x3; this.y2 = y1; this.x4 = x1; this.y4 = y3; } public long area() { if (x3 < x1 || y3 < y1) return 0; return (long) Math.abs(x1 - x3) * (long) Math.abs(y1 - y3); } } static long intersection(rectangle a, rectangle b) { if (a.x3 < a.x1 || a.y3 < a.y1 || b.x3 < b.x1 || b.y3 < b.y1) return 0; long l1 = ((long) Math.min(a.x3, b.x3) - (long) Math.max(a.x1, b.x1)); long l2 = ((long) Math.min(a.y3, b.y3) - (long) Math.max(a.y1, b.y1)); if (l1 < 0 || l2 < 0) return 0; long area = ((long) Math.min(a.x3, b.x3) - (long) Math.max(a.x1, b.x1)) * ((long) Math.min(a.y3, b.y3) - (long) Math.max(a.y1, b.y1)); if (area < 0) return 0; return area; } // --------------------------------------------------------------Multiset---------------------------------------------------------------// public static class multiset { public TreeMap<Integer, Integer> map; public int size = 0; public multiset() { map = new TreeMap<>(); } public multiset(int[] a) { map = new TreeMap<>(); size = a.length; for (int i = 0; i < a.length; i++) { map.put(a[i], map.getOrDefault(a[i], 0) + 1); } } void add(int a) { size++; map.put(a, map.getOrDefault(a, 0) + 1); } void remove(int a) { size--; int val = map.get(a); map.put(a, val - 1); if (val == 1) map.remove(a); } void removeAll(int a) { if (map.containsKey(a)) { size -= map.get(a); map.remove(a); } } int ceiling(int a) { if (map.ceilingKey(a) != null) { int find = map.ceilingKey(a); return find; } else return Integer.MIN_VALUE; } int floor(int a) { if (map.floorKey(a) != null) { int find = map.floorKey(a); return find; } else return Integer.MAX_VALUE; } int lower(int a) { if (map.lowerKey(a) != null) { int find = map.lowerKey(a); return find; } else return Integer.MAX_VALUE; } int higher(int a) { if (map.higherKey(a) != null) { int find = map.higherKey(a); return find; } else return Integer.MIN_VALUE; } int first() { return map.firstKey(); } int last() { return map.lastKey(); } boolean contains(int a) { if (map.containsKey(a)) return true; return false; } int size() { return size; } void clear() { map.clear(); } int poll() { if (map.size() == 0) { return Integer.MAX_VALUE; } size--; int first = map.firstKey(); if (map.get(first) == 1) { map.pollFirstEntry(); } else map.put(first, map.get(first) - 1); return first; } int polllast() { if (map.size() == 0) { return Integer.MAX_VALUE; } size--; int last = map.lastKey(); if (map.get(last) == 1) { map.pollLastEntry(); } else map.put(last, map.get(last) - 1); return last; } } static class pair implements Comparable<pair> { int a; int b; int dir; public pair(int a, int b, int dir) { this.a = a; this.b = b; this.dir = dir; } 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 implements Comparable<node> { int l; int r; public node(int l, int r) { this.l = l; this.r = r; } public int compareTo(node a) { if (this.l == a.l) { return this.r - a.r; } return (int) (this.l - a.l); } } static long ans = 0; static int leaf = 0; static boolean poss = true; static long mod = 1000000007L; static int[] dx = { -1, 0, 0, 1, -1, -1, 1, 1 }; static int[] dy = { 0, -1, 1, 0, -1, 1, -1, 1 }; static boolean cycle; int count = 0; public static void main(String[] args) throws Exception { // new Thread(null,new Main(), "1", 1 << 26).start(); long start = System.nanoTime(); in = new FastReader(); out = new PrintWriter(System.out, false); int tc = ni(); while (tc-- > 0) { int n=ni(); String s=n(); int ans=0; List<Integer> arr=new ArrayList<>(); int index=0; int minus=0; Map<Integer,Integer> map=new HashMap<>(); for(int i=0;i<n;i++){ char c=s.charAt(i); int j=i; while(j<n && s.charAt(j)==c){ j++; } if(((j-i)%2)==1){ arr.add(index); map.put(index, j-i); } index++; i=j-1; } for(int i=1;i<arr.size();i+=2){ ans+=arr.get(i)-arr.get(i-1); if(map.get(arr.get(i))==1 || map.get(arr.get(i-1))==1)minus--; } pn(ans); } long end = System.nanoTime(); // pn((end-start)*1.0/1000000000); out.flush(); out.close(); } static boolean KMPSearch(String pat, char[] txt,int[] lps,int m) { int M = pat.length(); int N = m; int j = 0; int i = 0; // index for txt[] while ((N - i) >= (M - j)) { if (pat.charAt(j) == txt[i] || txt[i]=='#') { j++; i++; } if (j == M) { return true; // j = lps[j - 1]; } // mismatch after j matches else if (i < N && pat.charAt(j) != txt[i]) { // Do not match lps[0..lps[j-1]] characters, // they will match anyway if (j != 0) j = lps[j - 1]; else i = i + 1; } } return false; } // static boolean find(int i, int length, int[] a, int n) { // if (i == n) { // if (length == 0) // return true; // return false; // } // if (i > n) // return false; // boolean ans = false; // ans = find(i + 1, length + 1, a, n); // if (length == 0) { // ans = ans || find(i + a[i] + 1, 0, a, n); // } // if (a[i] == length) { // ans = ans || (find(i + 1, 0, a, n)); // } // return ans; // } static int find(int val, int[][] pre) { int l = 0; int r = pre.length - 1; int ans = -1; while (l <= r) { int mid = l + (r - l) / 2; if (pre[mid][1] >= val) { ans = mid; l = mid + 1; } else r = mid - 1; } if (ans == -1) { return -1; } else return pre[ans][0]; } static long nc(long[] fact,int a,int b){ return (fact[a])/(fact[a-b]*fact[b]); } static long ncm(long[] fact, long[] fact_inv, int n, int m) { if (n < m) return 0L; long a = fact[n]; long b = fact_inv[n - m]; long c = fact_inv[m]; a = (a * b) % mod; return (a * c) % mod; } public void run() { try { in = new FastReader(); out = new PrintWriter(System.out); int tc = ni(); while (tc-- > 0) { } out.flush(); } catch (Exception e) { } } static void dfs(int i, int p, boolean[] visited, List<List<Integer>> arr, int length) { visited[i] = true; for (int nei : arr.get(i)) { if (visited[nei] || nei == p) continue; dfs(nei, i, visited, arr, length + 1); } } static boolean inside(int i, int j, int n, int m) { if (i >= 0 && j >= 0 && i < n && j < m) return true; return false; } static int binary_search(int[] a, int val) { int l = 0; int r = a.length - 1; int ans = 0; 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 int[] longest_common_prefix(String s) { int m = s.length(); int[] lcs = new int[m]; int len = 0; int i = 1; lcs[0] = 0; while (i < m) { if (s.charAt(i) == s.charAt(len)) { lcs[i++] = ++len; } else { if (len == 0) { lcs[i] = 0; i++; } else len = lcs[len - 1]; } } return lcs; } static void swap(char[] a, char[] b, int i, int j) { char temp = a[i]; a[i] = b[j]; b[j] = temp; } 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 } // (a^(b^c))%m is equal to, let res=(b^c)%(m-1) then (a^res)%m // https://www.geeksforgeeks.org/find-power-power-mod-prime/?ref=rp } 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 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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
2df021ec0b2783bc4cd8b6a0c4371521
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; import java.io.*; public class Main { static Scanner sc; static PrintWriter pw; public static void main(String[] args) throws IOException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); char[] arr = sc.next().toCharArray(); int res = 0; for (int i = 0; i < n; i++) { char curr = arr[i]; int j = i,len = 0; while(j < n && arr[j] == curr) { j++; len++; } if(len % 2 == 1) { i = j; res++; }else { i = j - 1; } } pw.println(res); } pw.close(); } 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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
b3f82bba206f20563a2777d6cca364ee
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.math.BigInteger; import java.util.*; import java.io.*; public class Vaibhav { static long bit[]; static boolean prime[]; static class Pair implements Comparable<Pair> { long x; int y; Pair(long x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o){ return (int)(this.x-o.x); } } static int st[]; //array to store segment tree // A utility function to get minimum of two numbers static int minVal(int x, int y) { return (x < y) ? x : y; } // A utility function to get the middle index from corner // indexes. static int getMid(int s, int e) { return s + (e - s) / 2; } /* A recursive function to get the minimum value in a given range of array indexes. The following are parameters for this function. st --> Pointer to segment tree index --> Index of current node in the segment tree. Initially 0 is passed as root is always at index 0 ss & se --> Starting and ending indexes of the segment represented by current node, i.e., st[index] qs & qe --> Starting and ending indexes of query range */ static int RMQUtil(int ss, int se, int qs, int qe, int index) { // If segment of this node is a part of given range, then // return the min of the segment if (qs <= ss && qe >= se) return st[index]; // If segment of this node is outside the given range if (se < qs || ss > qe) return Integer.MAX_VALUE; // If a part of this segment overlaps with the given range int mid = getMid(ss, se); return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1), RMQUtil(mid + 1, se, qs, qe, 2 * index + 2)); } // Return minimum of elements in range from index qs (query // start) to qe (query end). It mainly uses RMQUtil() static int RMQ(int n, int qs, int qe) { // Check for erroneous input values if (qs < 0 || qe > n - 1 || qs > qe) { // System.out.println("Invalid Input"); return -1; } return RMQUtil(0, n - 1, qs, qe, 0); } // A recursive function that constructs Segment Tree for // array[ss..se]. si is index of current node in segment tree st static int constructSTUtil(int arr[], int ss, int se, int si) { // If there is one element in array, store it in current // node of segment tree and return if (ss == se) { st[si] = arr[ss]; return arr[ss]; } // If there are more than one elements, then recur for left and // right subtrees and store the minimum of two values in this node int mid = getMid(ss, se); st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1), constructSTUtil(arr, mid + 1, se, si * 2 + 2)); return st[si]; } /* Function to construct segment tree from given array. This function allocates memory for segment tree and calls constructSTUtil() to fill the allocated memory */ static void constructST(int arr[], int n) { // Allocate memory for segment tree //Height of segment tree int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); //Maximum size of segment tree int max_size = 2 * (int) Math.pow(2, x) - 1; st = new int[max_size]; // allocate memory // Fill the allocated memory st constructSTUtil(arr, 0, n - 1, 0); } 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()); } 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()); } } 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)); } static int binomialCoeff(int n, int r) { if (r > n) return 0; long m = 1000000007; long inv[] = new long[r + 1]; inv[0] = 1; if(r+1>=2) inv[1] = 1; // Getting the modular inversion // for all the numbers // from 2 to r with respect to m // here m = 1000000007 for (int i = 2; i <= r; i++) { inv[i] = m - (m / i) * inv[(int) (m % i)] % m; } int ans = 1; // for 1/(r!) part for (int i = 2; i <= r; i++) { ans = (int) (((ans % m) * (inv[i] % m)) % m); } // for (n)*(n-1)*(n-2)*...*(n-r+1) part for (int i = n; i >= (n - r + 1); i--) { ans = (int) (((ans % m) * (i % m)) % m); } return ans; } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static BigInteger bi(String str) { return new BigInteger(str); } // Author - vaibhav_1710 static FastScanner fs = null; static long ans; static long mod=998244353; static ArrayList<Integer> al[]; static int dp[][][]; static int cntbig; static int cntless; static HashSet<Long> h; static int a[]; 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(); String s = fs.next(); ArrayList<Integer> al = new ArrayList<>(); int i=0; int cnt=0; int cnto=0; int cnt1=0; int cnt0=0; for(i=0;i<s.length();i++){ if(s.charAt(i)=='0') cnt0++; else cnt1++; } i=0; while(i<s.length()){ char c = s.charAt(i); cnt=0; while(i<n && s.charAt(i)==c){ cnt++; i++; } if(cnt%2!=0) cnto++; al.add(cnt); } if(cnto%2!=0){ out.println(Math.min(cnt0,cnt1)); continue outer; } int f=-1; int ss=-1; int ans=0; for(i=0;i<al.size();i++){ if(al.get(i)%2!=0){ if(f==-1){ f = i; }else{ ans += (i-f); f = -1; } } } out.println(ans); } out.close(); } public static long query(int a,int b){ System.out.println("? "+a+" "+b); System.out.flush(); long v = fs.nextLong(); return v; } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
e2de77926808afb528767ba0d4caf170
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
//package kg.my_algorithms.Codeforces; import java.io.*; import java.util.*; /* */ public class Solution { public static void main(String[] args) throws IOException { BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); FastReader fr = new FastReader(); StringBuilder sb = new StringBuilder(); int testCases = fr.nextInt(); for(int test=1;test<=testCases;test++){ List<Integer> groups = new ArrayList<>(); int n = fr.nextInt(); String s = fr.next(); char prev = s.charAt(0); int groupSize = 1; for(int i=1;i<s.length();i++){ if(prev!=s.charAt(i)){ groups.add(groupSize); prev = s.charAt(i); groupSize = 1; } else groupSize++; } groups.add(groupSize); int changes = 0; for(int i=0;i<groups.size()-1;i++){ if(groups.get(i)%2==1){ if(groups.get(i+1)%2==1){ changes++; i++; } else{ changes++; groups.set(i+1,groups.get(i+1)+1); } } } sb.append(changes).append("\n"); } output.write(sb.toString()); output.flush(); } } //Fast Input 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; } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
58c18afdc0051470041750c7a40612a0
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.StringTokenizer; public class Tokitsukaze_and_Good_01String_easy_version { 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 void main(String Args[]) throws java.lang.Exception { try { Scanner obj = new Scanner (System.in); int t = obj.nextInt(); while (t > 0) { t--; int n = obj.nextInt(); String str = obj.next(); int i = 0; ArrayList<Integer> arr = new ArrayList<Integer>(); while (i < n) { char chi = str.charAt(i); int j = i; while (j < n) { char chj = str.charAt(j); if (chi == chj) { j++; } else { break; } } if ((j - i) % 2 == 0) { arr.add (0); } else { arr.add (1); } i = j; } int l = arr.size(), c = 0; for (i=0;i<l-1;i++) { if (arr.get (i) == 1) { c++; int v = arr.get (i+1); int ele = arr.set (i+1, (v ^ 1)); } } System.out.println (c); } }catch(Exception e){ return; } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
97bfe76565cdb1699f9eaa55d41599f4
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; import java.util.Arrays; /* 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 Scanner scan = new Scanner(System.in); int T = scan.nextInt(); for(int i = 0; i < T; i++) { int N = scan.nextInt(); String s = scan.next(); char[] ch = s.toCharArray(); int ans = 0; int count = 1; for(int j = 1; j < N; j++){ if(ch[j] == ch[j-1]) { count++; }else{ if(count%2 == 0) { count = 1; }else{ ch[j] = ch[j-1]; ans++; j++; count = 1; } } } System.out.println(ans); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
57fa633ea63c55d557cba4ecc66b7b27
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { public static int[] arr = new int[1000000]; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t>0) { int n = scanner.nextInt(); scanner.nextLine(); String s = scanner.nextLine(); char prev='2'; int count=0; int ans=0; //System.out.println(s); for(int i=0;i<n;i++){ if(prev!='2'){ if(prev!=s.charAt(i)){ if(count%2 == 1){ ans++; } } } count++; prev = s.charAt(i); } if(count%2==1){ ans++; } System.out.println(ans); t--; } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
a43a763f7973d5a9d56d23ecf3d4a4a3
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testCount = Integer.parseInt(sc.nextLine()); int[] result = new int[testCount]; for (int i = 0; i < testCount; i++) { int stringLen = Integer.parseInt(sc.nextLine()); String str = sc.nextLine(); int primMin = 0; if (str.charAt(0) == str.charAt(1)) { primMin = 0; } else { primMin = 1; } for (int j = 3; j < str.length(); j += 2) { if (str.charAt(j - 1) == str.charAt(j)) { primMin = primMin; } else { primMin = primMin + 1; } } result[i] = primMin; } for (int i = 0; i < result.length; i++) { System.out.println(result[i]); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
6c8a917119cdb0eb61f79950cd69cb89
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.Scanner; public class main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt();//用例个数 while(n!=0){ int ans = 0; int len=sc.nextInt();//s长度 String s=sc.next(); for (int i = 0; i < len-1; i++) { if (s.charAt(i)!=s.charAt(i+1)){ ans++; } i++; } System.out.println(ans); n--; } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
18c8952c640152b823782b1659da68b1
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.Scanner; public class GoodString { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-->0){ int n = in.nextInt(); String s = in.next(); int count = 0; for (int i = 1; i < n; i+=2) { if(s.charAt(i-1) != s.charAt(i)) count++; } System.out.println(count); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
565a5f2b775411e212b910d655f697c5
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
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 x1678B { static final int INF = Integer.MAX_VALUE/2; public static void main(String[] followthekkathyoninsta) 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()); String line = infile.readLine().trim(); int[] arr = new int[N]; for(int i=0; i < N; i++) arr[i] = line.charAt(i)-'0'; Pair[][] dp = new Pair[2][2]; fill(dp); dp[1][arr[0]] = new Pair(0, 0); dp[1][arr[0]^1] = new Pair(1, 0); for(int t=1; t < N; t++) { Pair[][] next = new Pair[2][2]; fill(next); for(int a=0; a < 2; a++) for(int b=0; b < 2; b++) if(dp[a][b].moves != INF) { //extend from block int na = a^1; int nb = b; int cost = b^arr[t]; Pair temp = new Pair(dp[a][b].moves+cost, dp[a][b].blocks); if(next[na][nb].compareTo(temp) > 0) next[na][nb] = temp; //create new block if(a == 0) { na = 1; nb = b^1; cost ^= 1; temp = new Pair(dp[a][b].moves+cost, dp[a][b].blocks+1); if(next[na][nb].compareTo(temp) > 0) next[na][nb] = temp; } } dp = next; } Pair res = dp[0][0]; if(res.compareTo(dp[0][1]) > 0) res = dp[0][1]; sb.append(res.moves); sb.append("\n"); } System.out.print(sb); } public static void fill(Pair[][] dp) { for(int a=0; a < 2; a++) for(int b=0; b < 2; b++) dp[a][b] = new Pair(INF, INF); } } /* dp[i][odd/even][tag] = minimum moves such that for first i elements, the current block = tag and it's length is odd or even block variable won't include itself can also store the hard version easily */ class Pair { public int moves; public int blocks; public Pair(int a, int b) { moves = a; blocks = b; } public int compareTo(Pair oth) { if(moves != oth.moves) return moves-oth.moves; return blocks-oth.blocks; } } /* 1 8 11001111 */
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
18ddf94e5dfeea972528ea8b6a77d47f
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; import java.io.*; import java.lang.reflect.Array; public class CodeForces { class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader(new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { 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; } } void shuffle(int a[], int n) { for (int i = 0; i < n; i++) { int t = (int) Math.random() * a.length; int x = a[t]; a[t] = a[i]; a[i] = x; } } long lcm(long a, long b) { long val = a * b; return val / gcd(a, b); } void swap(long a[], long[] b, int i) { long temp = a[i]; a[i] = b[i]; b[i] = temp; } long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } long mod = (long) 1e9 + 7, c; FastReader in; PrintWriter o; public static void main(String[] args) throws IOException { new CodeForces().run(); } void run() throws IOException { in = new FastReader(); OutputStream op = System.out; o = new PrintWriter(op); int t; t = 1; t = in.nextInt(); for (int i = 1; i <= t; i++) { helper(); o.println(); } o.flush(); o.close(); } public void helper() { int n=in.nextInt(); char a[]=in.nextLine().toCharArray(); int ans=0; for(int i=0;i<n;i++) { int j=i; while(i+1<n && a[i+1]==a[j]) i++; if((i-j+1)%2==0)continue; ans++; i++; } o.print(ans); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
1967c93b8cd1bc001dea0e6f70ecc750
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args)throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 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(); ArrayList<Integer> al=new ArrayList<>(); int ct=1; for(int i=0;i<n-1;i++) { if(s.charAt(i+1)==s.charAt(i)) { ct++; } else { al.add(ct); ct=1; } } al.add(ct); int ans=0; for(int i=0;i<al.size()-1;i++) { if(al.get(i)%2==1) { al.set(i,al.get(i)-1); al.set(i+1,al.get(i+1)+1); ans++; } } pw.println(ans); } pw.flush(); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
ee197a80f6bf975773952da3cd81ca67
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String args[]) { FastScanner input = new FastScanner(); int tc = input.nextInt(); work: while (tc-- > 0) { int n = input.nextInt(); char a[] = input.next().toCharArray(); int ans=0; for (int i = 0; i <n; i++) { if(a[i]=='0') { int count=1; while(i+1<n&&a[i+1]=='0') { count++; i++; } if(count%2==1) { ans++; i++; } } else{ int count=1; while(i+1<n&&a[i+1]=='1') { count++; i++; } if(count%2==1) { ans++; i++; } } } System.out.println(ans); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { 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() throws IOException { return br.readLine(); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
9c646fdbaa169e93adc295b655168d42
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
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.util.List; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Roy */ 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); B1TokitsukazeAndGood01StringEasyVersion solver = new B1TokitsukazeAndGood01StringEasyVersion(); solver.solve(1, in, out); out.close(); } static class B1TokitsukazeAndGood01StringEasyVersion { public void solve(int testNumber, InputReader in, OutputWriter out) { int tCases = in.readInteger(); for (int cs = 1; cs <= tCases; ++cs) { int n = in.readInteger(); String s = in.readString(); int segLength = 1; List<Integer> segList = new ArrayList<>(); for (int i = 1; i < n; i++) { if (s.charAt(i) == s.charAt(i - 1)) segLength++; else { segList.add(segLength); segLength = 1; } } segList.add(segLength); int ans = 0; for (int i = 0, sz = segList.size() - 1; i < sz; i++) { int x = segList.get(i); if (MathUtility.isOdd(x)) { ans++; segList.set(i, x + 1); segList.set(i + 1, segList.get(i + 1) - 1); } } out.printLine(ans); } } } static class InputReader { private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; private final InputStream stream; private final byte[] buf = new byte[1024]; public InputReader(InputStream stream) { this.stream = stream; } private long readWholeNumber(int c) { long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res; } 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 readInteger() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = (int) readWholeNumber(c); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { this.print(objects); writer.println(); } public void close() { writer.flush(); writer.close(); } } static class MathUtility { public static <T> boolean isOdd(T number) { long longNumber = Long.parseLong(number.toString()); return ((longNumber & 1) > 0); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
9b36c7f60b0d811c38b89cf8af70e175
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
//package com.company; import java.util.*; import java.io.*; public class TokitsukazeEasy { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(--t >= 0) { int len = Integer.parseInt(br.readLine()); String binS = br.readLine(); int num = 0; int c = 0; ArrayList<Boolean> conS = new ArrayList<>(); for(int i = 0; i < len; i++) { if(i == 0) { num = binS.charAt(i) - '0'; c++; } else { if(binS.charAt(i) - '0' == num) { c++; if(i == len - 1) { conS.add(c % 2 == 1); } } else { conS.add(c % 2 == 1); num = binS.charAt(i) - '0'; c = 1; } } } int ans = 0; for(int i = 0; i < conS.size(); i++) { if(conS.get(i)) { ans++; conS.set(i, !conS.get(i)); if(i != conS.size() - 1) conS.set(i+1, !conS.get(i+1)); } } System.out.println(ans); } br.close(); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
05e0e58ac8230e2c525d5dcb49e08781
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Sanket Makani */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); B1TokitsukazeAndGood01StringEasyVersion solver = new B1TokitsukazeAndGood01StringEasyVersion(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class B1TokitsukazeAndGood01StringEasyVersion { public void solve(int testNumber, InputReader sc, PrintWriter w) { int n = sc.nextInt(); char c[] = sc.next().toCharArray(); List<Integer> list = new ArrayList<>(); int current = 1; for (int i = 1; i < n; i++) { if (c[i] == c[i - 1]) { current++; } else { list.add(current); current = 1; } } list.add(current); int answer = 0; for (int i = 0; i < list.size() - 1; i++) { int currentElem = list.get(i); int nextElem = list.get(i + 1); if (currentElem % 2 == 0) { continue; } nextElem--; list.set(i + 1, nextElem); answer++; } w.println(answer); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int 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 readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
1869bd5642a8f2eecef91b32e803ef57
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class B { private static FastReader fr; private static OutputStream out; private static int mod = (int)(1e9+7); private void solve() { int t = fr.nextInt(); while(t-- > 0) { int n = fr.nextInt(); char arr[] = fr.next().toCharArray(); int ans = 0; int ones = arr[0] == '1' ? 1 : 0; int zeros = 1 - ones; for(int i=1;i<n;i++){ if(arr[i] == arr[i-1]){ ones += arr[i] == '1' ? 1 : 0; zeros += arr[i] == '0' ? 1 : 0; }else if(arr[i-1] == '0'){ ones = 1; if(zeros % 2 != 0){ ans ++; ones ++; } zeros = 0; }else{ zeros = 1; if(ones % 2 != 0){ ans ++; zeros ++; } ones = 0; } } println(ans); } } public static void main(String args[]) throws IOException{ new B().run(); } private static int modInverse(int a) { int m0 = mod; int y = 0, x = 1; if (mod == 1) return 0; while (a > 1) { int q = (int)(a / mod); int t = mod; mod = a % mod; a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } private ArrayList<Integer> factors(int n, boolean include){ ArrayList<Integer> factors = new ArrayList<>(); if(n < 0) return factors; if(include) { factors.add(1); if(n > 1) factors.add(n); } int i = 2; for(;i*i<n;i++) { if(n % i == 0) { factors.add(i); factors.add(n / i); } } if(i * i == n) { factors.add(i); } return factors; } private ArrayList<Integer> PrimeFactors(int n) { ArrayList<Integer> primes = new ArrayList<>(); int i = 2; while (i * i <= n) { if (n % i == 0) { primes.add(i); n /= i; while (n % i == 0) { primes.add(i); n /= i; } } i++; } if(n > 1) primes.add(i); return primes; } private boolean isPrime(int n) { if(n == 0 || n == 1) { return false; } if(n % 2 == 0) { return false; } for(int i=3;i*i<=n;i+=2) { if(n % i == 0) { return false; } } return true; } private ArrayList<Integer> Sieve(int n){ boolean bool[] = new boolean[n+1]; Arrays.fill(bool, true); bool[0] = bool[1] = false; for(int i=2;i*i<=n;i++) { if(bool[i]) { int j = 2; while(i*j <= n) { bool[i*j] = false; j++; } } } ArrayList<Integer> primes = new ArrayList<>(); for(int i=2;i<=n;i++) { if(bool[i]) primes.add(i); } return primes; } private HashMap<Integer, Integer> addToHashMap(HashMap<Integer, Integer> map, int arr[]){ for(int val: arr) { if(!map.containsKey(val)) { map.put(val, 1); }else { map.put(val, map.get(val) + 1); } } return map; } private int factorial(int n) { long fac = 1; for(int i=2;i<=n;i++) { fac *= i; fac %= mod; } return (int)(fac % mod); } // private static int pow(int base,int exp){ // if(exp == 0){ // return 1; // }else if(exp == 1){ // return base; // } // int a = pow(base,exp/2); // a = ((a % mod) * (a % mod)) % mod; // if(exp % 2 == 1) { // a = ((a % mod) * (base % mod)); // } // return a; // } private static int gcd(int a,int b){ if(a == 0){ return b; } return gcd(b%a,a); } private static int lcm(int a,int b){ return (a * b)/gcd(a,b); } private void run() throws IOException{ fr = new FastReader(); out = new BufferedOutputStream(System.out); solve(); out.flush(); out.close(); } private static class FastReader{ private static BufferedReader br; private static StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedOutputStream(System.out); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public char[] nextCharArray() { return next().toCharArray(); } 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 String[] nextStringArray(int n) { String arr[] = new String[n]; for(int i=0;i<n;i++) { arr[i] = next(); } return arr; } } public static void print(Object str) { try { out.write(str.toString().getBytes()); } catch (IOException e) { e.printStackTrace(); } } public static void println(Object str) { try { out.write((str.toString() + "\n").getBytes()); } catch (IOException e) { e.printStackTrace(); } } public static void println() { println(""); } public static void printArray(Object str[]){ for(Object s : str) { try { out.write(str.toString().getBytes()); } catch (IOException e) { e.printStackTrace(); } } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
ea40962a1489dfe06d00008e1ff8d3d5
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; public class GoodString{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = Integer.parseInt(sc.nextLine()); for(int i = 0 ; i < t ; i++) { int len = Integer.parseInt(sc.nextLine()); String num = sc.nextLine(); int count = 1; int moves = 0; for(int j = 0 ; j < num.length() ; j++) { if(j > 0) { if(num.charAt(j) == num.charAt(j-1)) { count ++; } else if(num.charAt(j) != num.charAt(j-1)) { if(count % 2 ==1) { moves ++; count = 0; } else { count ++; } } } } System.out.println(moves); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
767641e19093fe6d34768f18fdb4b9a4
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
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(), cnt = 0; char c[] = sc.next().toCharArray(); for(int i=0; i<c.length; i+=2) if(c[i] != c[i+1]) cnt++; System.out.println(cnt); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
a2f008a7efba09fd28c496abde8fca66
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.Scanner; //1。Tokitsukaze and Good 01-String (简易版) //1110111000 //(3 1) (2 0) (2 1) (3 0) public class Tokit { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); for (int i = 0; i < n; i++) { int x=scan.nextInt(); String s=scan.next(); int change=0; int count=0; for (int j = 0; j <s.length(); j++) { if(count!=0) { if(s.charAt(j)!=s.charAt(j-1)) { if(count%2!=0) { change++; j++; } count=0; } } count++; } System.out.println(change); } scan.close(); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
9b266579ea22c4d4a0926a4ed09c2d02
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; public class _1678B1 { FastScanner scn; PrintWriter w; PrintStream fs; int MOD = 1000000007; int MAX = 200005; long mul(long x, long y) {long res = x * y; return (res >= MOD ? res % MOD : res);} long power(long x, long y) {if (y < 0) return 1; long res = 1; x %= MOD; while (y!=0) {if ((y & 1)==1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;} 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);} void reverseSort(int[] arr){List<Integer> list = new ArrayList<>();for (int i=0; i<arr.length; i++){list.add(arr[i]);}Collections.sort(list, Collections.reverseOrder());for (int i = 0; i < arr.length; i++){arr[i] = list.get(i);}} boolean LOCAL; void debug(Object... o){if(LOCAL)System.err.println(Arrays.deepToString(o));} //SPEED IS NOT THE CRITERIA, CODE SHOULD BE A NO BRAINER, CMP KILLS, MOCIM void solve(){ int t=scn.nextInt(); while(t-->0) { int n= scn.nextInt(); String s = scn.next(); int ans = 0; for(int i=n-1;i>=0;i--){ int j = i; int val = s.charAt(i)-'0'; int ct= 0; while(j>=0&&(s.charAt(j)-'0')==val){ ct++; j--; } j++; i=j; if((ct&1)==0){ continue; }else{ ans++; i--; } } w.println(ans); } } void run() { try { long ct = System.currentTimeMillis(); scn = new FastScanner(new File("input.txt")); w = new PrintWriter(new File("output.txt")); fs=new PrintStream("error.txt"); System.setErr(fs); LOCAL=true; solve(); w.close(); System.err.println(System.currentTimeMillis() - ct); } catch (FileNotFoundException e) { e.printStackTrace(); } } void runIO() { scn = new FastScanner(System.in); w = new PrintWriter(System.out); LOCAL=false; solve(); w.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long[] nextLongArray(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()); } double nextDouble() { return Double.parseDouble(next()); } } int lowerBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid-1 >= 0 && arr[mid-1] == arr[mid]){ei = mid-1;}else{return mid;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; } int upperBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid+1 < n && arr[mid+1] == arr[mid]){si = mid+1;}else{return mid + 1;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; } int upperBound(ArrayList<Integer> list, int x){int n = list.size(), si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(list.get(mid) == x){if(mid+1 < n && list.get(mid+1) == list.get(mid)){si = mid+1;}else{return mid + 1;}}else if(list.get(mid) > x){ei = mid - 1; }else{si = mid+1;}}return si; } void swap(int[] arr, int i, int j){int temp = arr[i];arr[i] = arr[j];arr[j] = temp;} long nextPowerOf2(long v){if (v == 0) return 1;v--;v |= v >> 1;v |= v >> 2;v |= v >> 4;v |= v >> 8;v |= v >> 16;v |= v >> 32;v++;return v;} int gcd(int a, int b) {if(a == 0){return b;}return gcd(b%a, a);} // TC- O(logmax(a,b)) boolean nextPermutation(int[] arr) {if(arr == null || 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;}if(last >= 0){int nextGreater = arr.length-1;for(int i=arr.length-1; i>last; i--){if(arr[i] > arr[last]){nextGreater = i;break;}}swap(arr, last, nextGreater);}int i = last + 1, j = arr.length - 1;while(i < j){swap(arr, i++, j--);}return true;} public static void main(String[] args) { new _1678B1().runIO(); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
6c770f6cd9cf3f8af1fe9e59b08b804b
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
// Problem: B1. Tokitsukaze and Good 01-String (easy version) // Contest: Codeforces - Codeforces Round #789 (Div. 2) // URL: https://codeforces.com/contest/1678/problem/B1 // Memory Limit: 256 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) import java.util.*; public class My_template { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { solve(sc); } } private static void solve(Scanner sc) { int n=sc.nextInt(); String s=sc.next(); int c=1; int ans=0; char[] ar=s.toCharArray(); int i=0; char curr=ar[0]; for( i=1;i<s.length();i++){ if(ar[i]==curr){ c++; continue; } if(c%2==0){ curr=ar[i]; c=1; }else { ans++; c++; } } System.out.println(ans); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
acbe5f60690d121f9fec715976ba6ba9
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
// * * * 2100 * * * // import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class A { static class Pair { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } } static class Edge implements Comparable<Edge> { int u; int v; int w; Edge(int u, int v, int w) { this.u = u; this.v = v; this.w = w; } public int compareTo(Edge o) { return this.w - o.w; } } public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); char ca[] = (sc.nextLine()).toCharArray(); boolean can = true; int prev = 0; int count = 0; for (int i = 1; i < n; i++) { if (ca[i] != ca[i - 1]) { if ((i - prev) % 2 != 0) { count++; } else prev = i; } } System.out.println(count); } } static void sort(int[] a) { // ruffle 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; } // then sort Arrays.sort(a); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } String str = ""; String nextLine() { try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] 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 long gcd(long n, long l) { if (l == 0) return n; return gcd(l, n % l); } static void sieveOfEratosthenes(int n, ArrayList<Integer> al) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) al.get(i); } } static final int mod = 100000000 + 7; static final int max_val = 2147483647; static final int min_val = max_val + 1; static long fastPow(long base, long exp) { if (exp == 0) return 1; long half = fastPow(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long mul(long a, long b) { return a * b % mod; } static int nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } static int fact(int n) { int res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } // a -> z == 97 -> 122 // String.format("%.9f", ans) ,--> to get upto 9 decimal places , (ans is // double) }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
76f81f6a0dc44b8800b59fc2c6459662
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.PriorityQueue; import java.util.StringTokenizer; public class B { public static void main(String[]args) throws IOException { Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int t=sc.nextInt(); a:while(t-->0) { int n=sc.nextInt(); String s=sc.next(); ArrayList<Integer>arr=new ArrayList<Integer>(); int ans=0; for(int i=0;i<n;) { int cnt=0; while(i<n&&s.charAt(i)=='0') { cnt++; i++; } if(cnt>0) {arr.add(cnt);cnt=0;} while(i<n&&s.charAt(i)=='1') { cnt++; i++; } if(cnt>0) arr.add(cnt); } boolean odd=false; int lst=-1; for(int i=0;i<arr.size();i++) { if(arr.get(i)%2==0)continue; if(odd) { ans+=i-lst; odd=false; }else { odd=true; lst=i; } } out.println(ans); } out.close(); } 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 boolean hasNext() {return st.hasMoreTokens();} public int nextInt() throws IOException {return Integer.parseInt(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready(); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
76eddbf16d5dee442b316cdde0cfee44
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc=new FastReader(); // static long dp[][]; // static boolean v[][][]; // static int mod=998244353;; // static int mod=1000000007; static long oset[]; static int oset_p; static long mod=1000000007; // static int max; static int bit[]; //static long fact[]; //static HashMap<Long,Long> mp; //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(); long cnt=0; String s=s(); int r=0; int l=0; while(r<n) { int ch=s.charAt(r); while(r<n&&s.charAt(r)==ch) r++; if((r-l)%2!=0) { cnt++; r++; } l=r; } out.println(cnt); } out.close(); } 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 long summation(long A[],int si,int ei) { long ans=0; for(int i=si;i<=ei;i++) ans+=A[i]; return ans; } static void add(long v,Map<Long,Long>mp) { if(!mp.containsKey(v)) { mp.put(v, (long)1); } else { mp.put(v, mp.get(v)+(long)1); } } static void remove(long v,Map<Long,Long>mp) { if(mp.containsKey(v)) { mp.put(v, mp.get(v)-(long)1); if(mp.get(v)==0) mp.remove(v); } } public static int upper(long A[],long 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(long A[],long 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 long[][] input(int n,int m){ long A[][]=new long[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=l(); } } 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,Long> hash(int A[]){ HashMap<Integer,Long> map=new HashMap<Integer, Long>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+(long)1); } else { map.put(i, (long)1); } } return map; } static HashMap<Long,Long> hash(long A[]){ HashMap<Long,Long> map=new HashMap<Long, Long>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+(long)1); } else { map.put(i, (long)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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
2775928fb412b67ef6dbe009b69f5627
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
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 B { public static void process() throws IOException { int n = sc.nextInt(); String s = sc.next(); int prev = s.charAt(0); int cc = 1; int ans = 0; for(int i = 1; i<n; i++) { int curr = s.charAt(i); if(curr == prev) { cc++; continue; } if(cc%2 == 0) { prev = curr; cc = 1; continue; } cc++; ans++; } System.out.println(ans); } //============================================================================= //--------------------------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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
765a1d582d45ab95e0dd032fa4f9be49
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; import java.util.*; public class Main { public static void main(String[] args){ InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); solve(in, out); out.close(); } static String reverse(String s) { return (new StringBuilder(s)).reverse().toString(); } static void sieveOfEratosthenes(int n, int factors[], ArrayList<Integer> ar) { factors[1]=1; int p; for(p = 2; p*p <=n; p++) { if(factors[p] == 0) { ar.add(p); factors[p]=p; for(int i = p*p; i <= n; i += p) if(factors[i]==0) factors[i] = p; } } for(;p<=n;p++){ if(factors[p] == 0) { factors[p] = p; ar.add(p); } } } static void sort(int ar[]) { int n = ar.length; ArrayList<Integer> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static void sort1(long ar[]) { int n = ar.length; ArrayList<Long> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static long ncr(long n, long r, long mod) { if (r == 0) return 1; long val = ncr(n - 1, r - 1, mod); val = (n * val) % mod; val = (val * modInverse(r, mod)) % mod; return val; } static int findMax(int a[], int n, int vis[], int i, int d){ if(i>=n) return 0; if(vis[i]==1) return findMax(a, n, vis, i+1, d); int max = 0; for(int j=i+1;j<n;j++){ if(Math.abs(a[i]-a[j])>d||vis[j]==1) continue; vis[j] = 1; max = Math.max(max, 1 + findMax(a, n, vis, i+1, d)); vis[j] = 0; } return max; } static void findSub(ArrayList<ArrayList<Integer>> ar, int n, ArrayList<Integer> a, int i){ if(i==n){ ArrayList<Integer> b = new ArrayList<Integer>(); for(int y:a){ b.add(y); } ar.add(b); return; } for(int j=0;j<n;j++){ if(j==i) continue; a.set(i,j); findSub(ar, n, a, i+1); } } // *-------------------code starts here--------------------------------------------------* static HashMap<Long,Integer>map; public static void solve(InputReader sc, PrintWriter pw){ long mod=(long)1e9+7; int t=sc.nextInt(); // int t=1; L : while(--t>=0){ int n=sc.nextInt(); char ch[]=sc.next().toCharArray(); int cnt=0; for(int i=0;i<n;i+=2){ if(ch[i]!=ch[i+1]){ cnt++; } } pw.println(cnt); } } // *-------------------code ends here-----------------------------------------------------* static void assignAnc(ArrayList<Integer> ar[],int sz[], int pa[] ,int curr, int par){ sz[curr] = 1; pa[curr] = par; for(int v:ar[curr]){ if(par==v) continue; assignAnc(ar, sz, pa, v, curr); sz[curr] += sz[v]; } } static int findLCA(int a, int b, int par[][], int depth[]){ if(depth[a]>depth[b]){ a = a^b; b = a^b; a = a^b; } int diff = depth[b] - depth[a]; for(int i=19;i>=0;i--){ if((diff&(1<<i))>0){ b = par[b][i]; } } if(a==b) return a; for(int i=19;i>=0;i--){ if(par[b][i]!=par[a][i]){ b = par[b][i]; a = par[a][i]; } } return par[a][0]; } static void formArrayForBinaryLifting(int n, int par[][]){ for(int j=1;j<20;j++){ for(int i=0;i<n;i++){ if(par[i][j-1]==-1) continue; par[i][j] = par[par[i][j-1]][j-1]; } } } static long lcm(int a, int b){ return a*b/gcd(a,b); } static class Pair1 { long a; long b; Pair1(long a, long b) { this.a = a; this.b = b; } } static class Pair implements Comparable<Pair> { int a; int b; // int c; Pair(int a, int b) { this.a = a; this.b = b; // this.c = c; } public int compareTo(Pair p) { return Integer.compare(a,p.a); } } static boolean isPrime(int 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 long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long fast_pow(long base, long n, long M) { if (n == 0) return 1; if (n == 1) return base % M; long halfn = fast_pow(base, n / 2, M); if (n % 2 == 0) return (halfn * halfn) % M; else return (((halfn * halfn) % M) * base) % M; } static long modInverse(long n, long M) { return fast_pow(n, M - 2, M); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 9992768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } public long[] readarray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
8d4244912c49c8f6d1def41e0242b89d
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class GoodStringEasy { public static void main(String[] args) { FastReader in = new FastReader(); int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); String s = in.nextLine(); int c0 = 0, c1 = 0, total = 0; for (int j = 0; j < n; j++) { char c = s.charAt(j); if (j + 1 == n) { if (c == '0') c0++; else c1++; if (c0 % 2 != 0 || c1 % 2 != 0) total++; break; } if (c == '0') { if (c1 % 2 == 0) { c0++; } else { total++; } c1 = 0; } else { if (c0 % 2 == 0) { c1++; } else { total++; } c0 = 0; } } System.out.println(total); } } 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; } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
79d19cb6ced429aac665f221215bf18f
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.Scanner; /** * @Author LWX * @date 2022/5/11 21:43 */ public class Tokitsukaze_and_Good_01_String_easy_version { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); for (int i = 0; i < n; i++) { int len = input.nextInt(); String line = input.next(); System.out.println(minimumPos(len, line)); } } private static int minimumPos(int len, String line) { if (len == 0) return 0; int current = -1; int count = 0; int ans = 0; for (char c : line.toCharArray()) { int cur = c - '0'; if (current == -1) { current = cur; count = 1; } else { if (current == cur) { count++; } else { if (count % 2 == 0) { current = cur; count = 1; } else { count++; ans++; } } } } return ans; } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
0c5c047ed129b8c28ca5f008071be695
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.Scanner; public class B1_1678 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for (int t=0; t<T; t++) { in.nextInt(); char[] S = (in.next() + ' ').toCharArray(); int answer = 0; boolean unpairedOdd = false; int count = 0; char last = ' '; for (char c : S) { if (c == last) { count++; } else { if (unpairedOdd) { answer++; } if (count%2 == 1) { unpairedOdd = !unpairedOdd; } count = 1; last = c; } } System.out.println(answer); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
3c6ec352e9ec93fec4775a96455c246d
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
//package december; import java.util.*; import java.io.*; public class codeforces1678B { public static void main (String [] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int numCases = Integer.parseInt(br.readLine()); for (int rep = 0; rep<numCases;rep++) { int length = Integer.parseInt(br.readLine()); String str = br.readLine(); int count = 0; for (int i = 0; i<length;i+=2) { if (str.charAt(i)!=str.charAt(i+1)) { count++; } } System.out.println(count); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
42d5d8efb4019766274a67643a9cc241
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for(int i = 0; i < t; i++){ int n = scanner.nextInt(); String s = scanner.next(); System.out.println(solve(s)); } } public static int solve(String s){ int counter = 0; for(int i = 0; i < s.length(); i+=2){ if(s.charAt(i) != s.charAt(i + 1)) counter++; } return counter; } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
a0cd5729beb992c833c3ee02147dbcd8
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; public class Five { private static int find(String s) { List<Integer> res = new ArrayList<>(); int cnt=1; for(int i=1;i<s.length();i++) { if(s.charAt(i)==s.charAt(i-1)) cnt++; else { res.add(cnt);cnt=1; } } cnt=0; int arr[] = new int[res.size()]; for(int i=0;i< res.size();i++) { arr[i]=res.get(i); } for(int i=0;i< res.size();i++) { if(arr[i]%2!=0) { arr[i]-=1; if(i+1< res.size()) arr[i+1]+=1; cnt++; } } return cnt; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-->0) { int n = sc.nextInt(); String input = sc.next(); System.out.println(find(input)); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
78dc027ff5783242b329823fa4c6e79e
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
//package com.company; import java.awt.*; import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.net.InetAddress; import java.text.CollationKey; import java.util.*; public class Main implements Runnable { int size[]; static int e9 = 1000000007; int max = Integer.MIN_VALUE; long max1 = Long.MIN_VALUE; long min1 = Long.MAX_VALUE; long fib[][]; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } int parent[]; int rank[]; int sie=1000000; boolean sieve[]=new boolean[sie+1]; ArrayList<ArrayList<Integer>> arrayLists; HashMap<String,Integer> hashMap; long fib1[][]; long r[]; int n; int m; long co=0; long v[]; int dp[][][]; boolean check=false; int k; int il[]; String s; ArrayList<String> arrayList; long ans=0; Set<String> set; public static void main(String[] args) throws IOException { new Thread(null,new Main(),"Main",1<<28).start(); } public void run() { FastReader scanner = new FastReader(); // Scanner scanner = new Scanner(System.in); // BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in)); PrintWriter printWriter = new PrintWriter(System.out); // System.out.println(-3%23); long t1=scanner.nextLong(); high:while (t1-- > 0) { int n=scanner.nextInt(); String s=scanner.next(); char arr[]=s.toCharArray(); int c=1; int ans=0; for (int i = 1; i < n; i++) { if(arr[i]==arr[i-1]){ c++; } else{ // System.out.println(c+" "+i); if(c%2!=0){ ans++; arr[i]=arr[i-1]; i++; } c=1; } } printWriter.println(ans); } printWriter.flush(); } static long mod=1_000_000_007; public void sieve(){ for (long p = 2; p<=sie; p++) { if (sieve[(int)p] == true) { for (long i = p * p; i <=sie; i += p) { // long o=i; //// System.out.println(o+" "+p); // if(o<0){ // return; // } sieve[(int) (i)] = false; } } } } public long lcm(long a,long b){ return (a*b)/gcd(a,b); } public long mul(long a, long b) { // System.out.println(a+" "+b); return ((a)*(b))%mod; } public long fact(int x) { long ans=1; for (int i=2; i<=x; i++) ans=mul(ans, i); return ans; } public long fastPow(long base, long exp) { if (exp==0) return 1; long half=fastPow(base, exp/2); if (exp%2==0) return mul(half, half)%mod; return mul(half, mul(half, base))%mod; } public long modInv(long x) { return fastPow(x, mod-2); } public long nc2(int n,int k){ return n*(n-1)/2; } public long nCk(int n, int k) { return mul(fact(n), mul(modInv(fact(k)), modInv(fact(n-k)))); } public void parent(int n){ parent=new int[n+1]; size=new int[n+1]; rank=new int[n+1]; for (int i = 0; i <=n; i++) { parent[i]=i; rank[i]=0; // size[i]=1; } } public void union(int i,int j){ int root1=find(i); int root2=find(j); // if(root1 != root2) { // parent[root2] = root1; //// sz[a] += sz[b]; // } if(root1==root2){ return; } if(rank[root1]>rank[root2]){ parent[root2]=root1; size[root1]+=size[root2]; } else if(rank[root1]<rank[root2]){ parent[root1]=root2; size[root2]+=size[root1]; } else{ parent[root2]=root1; rank[root1]+=1; size[root1]+=size[root2]; } } public int find(int p){ if(parent[p]!=p){ if(parent[p]==-1){ return parent[p]; } parent[p]=find(parent[p]); } return parent[p]; } public double dist(double x1,double y1,double x2,double y2){ double e=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1); double e1=Math.sqrt(e); return e1; } public void make(int p){ parent[p]=p; rank[p]=1; } Random rand = new Random(); public void sort(int[] a, int n) { for (int i = 0; i < n; i++) { int j = rand.nextInt(i + 1); int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } Arrays.sort(a, 0, n); } public long gcd(long a,long b){ if(b==0){ return a; } return gcd(b,a%b); } // public void dfs(ArrayList<Integer> arrayList){ // for (int i = 0; i < arrayList.size(); i++) { // if(v1[arrayList.get(i)]==false){ // v1[arrayList.get(i)]=true; // dfs(arrayLists.get(i)); // } // } // } public double fact(double h){ double sum=1; while(h>=1){ sum=(sum%e9)*(h%e9); h--; } return sum%e9; } public long primef(double r){ long c=0; long ans=1; long g=0; while(r%2==0){ c++; r=r/2; } if(c>0){ ans*=2; } c=0; // System.out.println(ans+" "+r); for (int i = 3; i <=Math.sqrt(r) ;i+=2) { g=0; while(r%i==0){ // System.out.println(i); c++; g++; r=r/i; } if(c>0){ ans*=i; } c=0; } if(r>2){ ans*=r; } return ans; } public long divisor(double r){ long c=0; // if(n==3) // System.out.println(r+" "+n+" "+k+" "+"l"); for (int i =2; i <=Math.sqrt(r); i++) { if(r%i==0){ if(r/i==i){ c++; } else{ if(r/i<n){ c+=2; }else{ c++; } } } } return c; } } class Pair { int x; int y; int z; public Pair(int x,int y) { this.x = x; this.y = y; // this.z=z; } // @Override // public int hashCode() { // int hash = 37; // return this.x * hash + this.y; // } // // @Override // public boolean equals(Object o1) { // if (o1 == null || o1.getClass() != this.getClass()) { // return false; // } // Pair o = (Pair) o1; // if (o.x == this.x && o.y == this.y) { // return true; // } // return false; // } } class Sorting implements Comparator<Pair> { public int compare(Pair p1, Pair p2) { if(p1.x==p2.x){ return Long.compare(p1.y,p2.y); }else { return Long.compare(p1.x, p2.x); } } } class Edge{ int s=0; int d=0; int c=0; public Edge(int s,int d,int c){ this.s=s; this.d=d; this.c=c; } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
328edaab4929a2945ad2239256a1f585
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class ProblemB { public static void solve(Scanner sc) { int n = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); List<Integer> list = new ArrayList<>(); int cntOfContiStr = 0; for (int i = 0; i < n - 1; i++) { if (s.charAt(i) == s.charAt(i + 1)) { cntOfContiStr++; } else { list.add(cntOfContiStr + 1); cntOfContiStr = 0; } } list.add(cntOfContiStr + 1); int ans = 0; int temp = 0; boolean flag = false; for (int i = 0; i < list.size(); i++) { if (list.get(i) % 2 == 1) { if (!flag) { temp = i; } else if (flag) { ans += i - temp; temp = 0; } flag = !flag; } } System.out.println(ans); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t > 0) { solve(sc); t--; } sc.close(); } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
44b15243abedadf758dd0cbcce89a6fb
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
/* !!!!Hello World,Prakhar here!!!! codechef handle prakhar_3011 codeforces handle prakhar_30 trying to get good at CP PEACE OUT......... */ /* 11 10 01 10 00 */ import java.io.*; import java.util.*; public class goodstring { public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); String s=sc.next();int ans=0; Stack<Character> c=new Stack<>(); for(int i=0;i<s.length();i++){ if(c.empty()==false&&c.peek()==s.charAt(i)){ c.pop(); } else if(c.empty()==false&&c.peek()!=s.charAt(i)){ ans++;c.pop(); } else{ c.push(s.charAt(i)); } } System.out.println(ans); } } 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 revsort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l, Collections.reverseOrder()); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } /* ......FAST SCANNER template taken from secondthread...... */ 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()); } 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 nextLong() { return Long.parseLong(next()); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
41bed42171a990fbb47b83c56f9e7f7b
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; public class B { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); char[] c = sc.next().toCharArray(); int ans = 0; for (int i = 0; i < n; i+=2) { if (c[i] != c[i+1]) ans++; } out.println(ans); } out.close(); } static boolean inc(int[] a) { for (int i = 0; i < a.length-1; i++) if (a[i] >= a[i+1]) return false; return true; } static boolean decOrEq(int[] a) { for (int i = 0; i < a.length-1; i++) if (a[i] < a[i+1]) return false; return true; } 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 void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class 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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
eaba3c2907e820d9efb474fe76aa611c
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
//package Div2.B; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class TokitsukazeAndGood01StringEasy { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t>0){ int n=Integer.parseInt(br.readLine()); String s=br.readLine(); List<Integer> list=new ArrayList<>(); for(int i=0;i<n;i++){ int cnt=1; while(i+1<n && s.charAt(i)==s.charAt(i+1)){ cnt++; i++; } list.add(cnt); } int operations = 0; for(int i=0;i<list.size()-1;){ int cnt1=list.get(i); int cnt2=list.get(i+1); if(cnt1%2==0 && cnt2%2==0){ i+=2; }else if(cnt1%2!=0 && cnt2%2!=0){ i+=2; operations+=1; }else if(cnt1%2==0 && cnt2%2!=0){ i+=1; }else { operations+=1; list.set(i+1, cnt2-1); i+=1; } } System.out.println(operations); t--; } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
78d6bda0827386a464778fb2a2de0306
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; public class B { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for(int ttt = 1; ttt <= T; ttt++) { int n = in.nextInt(); String str = in.next(); char[] s = str.toCharArray(); List<Integer> list = new ArrayList<>(); int cur = 1, cnt = 1; while(cur < s.length) { if(s[cur] == s[cur-1]) cnt++; else { list.add(cnt); cnt = 1; } cur++; } list.add(cnt); int ans = 0; for(int i = 0; i < list.size(); i++) { if(list.get(i)%2==1) { ans++; list.set(i+1, list.get(i+1)+1); } } out.println(ans); // out.println(list); } 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; } int[] readInt(int size) { int[] arr = new int[size]; for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(next()); return arr; } long[] readLong(int size) { long[] arr = new long[size]; for(int i = 0; i < size; i++) arr[i] = Long.parseLong(next()); return arr; } int[][] read2dArray(int rows, int cols) { int[][] arr = new int[rows][cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) arr[i][j] = Integer.parseInt(next()); } return arr; } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
69aa220b27cfc3106f9506a4ff2b8b57
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.awt.datatransfer.StringSelection; import java.util.*; import javax.management.Query; import java.io.*; public class practice { static String s; static int n,k; static int[] a; static long[][] memo; 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(); String s=sc.next(); int ones=0,zeros=0; for (int i = 0; i <n ; i++) { if(s.charAt(i)=='1') ones++; else zeros++; } if(zeros==0||ones==0) pw.println(0); else{ int res=0; Stack<Integer>st=new Stack<>(); for (int i = 0; i < n; i++) { if(st.isEmpty()) st.push(s.charAt(i)-'0'); else if(st.peek().equals(s.charAt(i)-'0')) st.pop(); else{ res++; st.pop(); } } pw.println(res); } } pw.close(); } static long nCr(int n, int r) { return 1l*fact(n) / 1l*(fact(r) * fact(n - r)); } static long fact(int n) { int res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } private static double customLog(double base, double logNumber) { return Math.log(logNumber) / Math.log(base); } 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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
9d8da834da3786c9194c45aabec92d36
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class B1_Tokitsukaze_and_Good_01_String_easy_version { static Scanner in = new Scanner(); static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans = new StringBuilder(); static int testCases, n, iterations = 0; static char a[]; /* 10 1110011000 -> 11 00 11 11 00 */ static void solve(int index, ArrayList<Integer> list, int len) { if (index >= n) { return; } len = 1; while (index + 1 < n && a[index] == a[index + 1]) { ++len; ++index; } list.add(len); ++iterations; solve(index + 1, list, len); } static void solve(int t) { iterations = 0; ArrayList<Integer> list = new ArrayList<>(); solve(0, list, 1); long arr[] = new long[list.size()]; int index = 0; while (!list.isEmpty()) { arr[index++] = list.get(0); list.popFront(); } int ans1 = 0, m = arr.length; iterations = 0; for (int i = 0; i < m - 1; i++) { if (arr[i] % 2 == arr[i + 1] % 2 && arr[i] % 2 == 1) { arr[i]--; arr[i + 1]++; ans1++; //++iterations; } else if (arr[i] % 2 != arr[i + 1] % 2 && arr[i + 1] % 2 == 0) { arr[i]--; arr[i + 1]++; ans1++; } } ans.append(ans1); if (t != testCases) { ans.append("\n"); } } public static void main(String[] amit) throws IOException { testCases = in.nextInt(); for (int t = 0; t < testCases; ++t) { n = in.nextInt(); a = in.next().toCharArray(); 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(); } if (index == 0) { return head.getData(); } 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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
47bba199bff717ca57539fb93b647805
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.Scanner; public class CF_1678B1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); char s[] = sc.next().toCharArray(); int ans =0; for(int i = 0;i<n;i+=2){ if(s[i]!=s[i+1]){ ans+=1; } } System.out.println(ans); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
24a20a6510138e3213f533656642c52a
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.util.*; import static java.util.Arrays.*; public class CodeforcesTemp { public static void main(String[] args) throws IOException { Reader scan = new Reader(); FastPrinter out = new FastPrinter(); int t = scan.nextInt(); for (int tc = 1; tc <= t; tc++) { int n=scan.nextInt(); String s=scan.next(); int[] cnt=new int[1]; cnt[0]=0; int[] segment=new int[cnt[0]]; doWork(s,cnt,false,segment); segment=new int[cnt[0]]; doWork(s,cnt,true,segment); int op=0; for (int i = 0; i < cnt[0]/2; i++) { if(segment[i]%2==1){ segment[i]++; segment[i+1]--; op++; } else{ continue; } } out.println(op); out.flush(); } out.close(); } private static void doWork(String s,int[] cnt,boolean flag,int[] segment) { int curr_cnt=1; int p=0; int n=s.length(); for (int i = 0; i < s.length(); i++) { if((i+1)<n && s.charAt(i)==s.charAt(i+1)){ curr_cnt++; continue; }else{ cnt[0]++; if(flag){ segment[p++]=curr_cnt; } curr_cnt=1; } } } static class Reader { private final InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public Reader(InputStream in) { this.in = in; } public Reader() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("not number")); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("not number")); } } } } throw new ArithmeticException( String.format(" overflows long.")); } n = n * 10 + digit; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public int[][] nextIntArrayMulti(int length, int width) { int[][] arrays = new int[width][length]; for (int i = 0; i < length; i++) { for (int j = 0; j < width; j++) arrays[j][i] = this.nextInt(); } return arrays; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width) { long[][] mat = new long[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width) { int[][] mat = new int[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width) { double[][] mat = new double[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width) { char[][] mat = new char[height][width]; for (int h = 0; h < height; h++) { String s = this.next(); for (int w = 0; w < width; w++) { mat[h][w] = s.charAt(w); } } return mat; } } static class FastPrinter extends PrintWriter { public FastPrinter(PrintStream stream) { super(stream); } public FastPrinter() { super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if (x < 0) { sb.append('-'); x = -x; } x += Math.pow(10, -n) / 2; sb.append((long) x); sb.append("."); x -= (long) x; for (int i = 0; i < n; i++) { x *= 10; sb.append((int) x); x -= (int) x; } return sb.toString(); } @Override public void print(float f) { super.print(dtos(f, 20)); } @Override public void println(float f) { super.println(dtos(f, 20)); } @Override public void print(double d) { super.print(dtos(d, 20)); } @Override public void println(double d) { super.println(dtos(d, 20)); } public void printArray(int[] array, String separator) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(int[] array) { this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n - 1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(long[] array, String separator) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(long[] array) { this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n - 1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map) { this.printArray(array, " ", map); } public void printMatrix(int[][] arr) { for (int i = 0; i < arr.length; i++) { this.printArray(arr[i]); } } } static Random __r = new Random(); static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
3a3c0c0f236533064bd2b53e5d39f083
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
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(); String s = sc.next(); List<Integer> list = new ArrayList<>(); int count = 0; for (int i = 0; i < n - 1; i++) { count++; if (s.charAt(i) != s.charAt(i + 1)) { list.add(count); count = 0; } } list.add(count + 1); int res = 0; for (int i = 0; i < list.size() - 1; i++) { if (list.get(i) % 2 != 0) { res++; list.set(i + 1, list.get(i + 1) - 1); } } System.out.println(res); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
5d3e6d947f2b573ed3d5be24a5ae9c37
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class Main { public static int inf = (int)1e9+7; public static int mod = (int)1e9+7; public static void main(String[] args) throws IOException, InterruptedException { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); MyScanner sc = new MyScanner(); //code part int t = sc.nextInt(); while (t-- > 0){ int n = sc.nextInt(); String s = sc.next(); int cnt = 0; int ans = 0; for (int i = 0; i < s.length(); i++){ if(i == 0 || s.charAt(i) == s.charAt(i - 1)){ cnt++; }else{ if(cnt % 2 == 0){ cnt = 1; }else{ cnt = 2; ans++; } } } writer.write(ans + "\n"); } //code part writer.flush(); writer.close(); } //快读模板 public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
f4bb70b3443abb1cc1c08f1e8b117786
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.*; import java.util.*; public class codeMaster { 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(); char[] arr = sc.next().toCharArray(); ArrayList<Integer> seq = new ArrayList<>(); int cnt = 1; for(int i = 0; i<n-1; i++){ if(arr[i] == arr[i+1]){ cnt++; }else{ seq.add(cnt); cnt = 1; } } seq.add(cnt); int carry = 0; int ans = 0; for(int x:seq){ x+=carry; carry = 0; if(x%2 != 0){ ans++; carry++; } } pw.println(ans); } 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 low = 0; 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 - 1; }else{ low = mid + 1; } } 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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
5d86be94dc404bddf240a20b6be77a58
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main{ static int mod = (int)1e9+7; static PrintWriter out; static String[] memo; static int n; static String line; public static void main(String[] args) throws IOException { //BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); Scanner sc=new Scanner(System.in); int t = sc.nextInt(); while(t-- !=0) { int n = sc.nextInt(); char [] m = sc.next().toCharArray(); //System.out.println(Arrays.toString(m)); int ans = 0; for(int i=0;i<n;) { int tmp=i; while(i<n && m[i]==m[tmp])i++; // out.println(tmp+" "+n+" "+i); if((i-tmp)%2!=0) { i++; ans++; } } out.println(ans); } out.flush(); } public static int log2(int N) { // calculate log2 N indirectly // using log() method int result = (int)(Math.log(N) / Math.log(2)); if((int)(Math.log(N) % Math.log(2))!=0) result++; return result; } 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> { 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 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; } } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
a25b6dcb79cebc4bcc7700883d8e2779
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
/** * @Jai_Bajrang_Bali * @Har_Har_Mahadev */ /* Created by IntelliJ IDEA. * Author: Sanat Kumar Dubey (sanat04) * Date: 02-05-2022 * Time: 19:43 * File: B.java */ import java.util.Scanner; public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n=sc.nextInt(); String str=sc.next(); char[] ch=str.toCharArray(); int count=0; for (int i = 0; i < n; i+=2) { if(ch[i]!=ch[i+1]) count++; } System.out.println(count); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
39e759b193fb54984e1228d7f0dab186
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
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 long[]a; 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(); while(tt-->0) { // int n=s.nextInt(); // int[]a=new int[n]; // for(int i=0;i<n;i++)a[i]=s.nextInt(); // Arrays.sort(a); // solver(a,n); int n=s.nextInt(); String str=s.next(); char[]ch=str.toCharArray(); ArrayList<Integer>a=new ArrayList<Integer>(); for(int i=0;i<ch.length;i++) { if(ch[i]=='0') { int cnt1=0; while(i<n&&ch[i]=='0') { i++; cnt1++; } i--; a.add(cnt1); } else { int cnt1=0; while(i<n&&ch[i]=='1') { i++; cnt1++; } i--; a.add(cnt1); } } int max=-1; int ans=0; for(int i=0;i<a.size();i++) { if(a.get(i)%2!=0) { ++i; int len=1; while(i<a.size()&&a.get(i)%2==0) { len++; i++; } ans+=len; } } max=ans; ans=0; for(int i=a.size()-1;i>=0;i--) { if(a.get(i)%2!=0) { --i; int len=1; while(i>=0&&a.get(i)%2==0) { len++; i--; } ans+=len; } } max=Math.min(max,ans); System.out.println(max); } } static void solver(int[]a,int n) { boolean f=false; int cnt=0; for(int i=0;i<n-1;i++) { if(a[i]==a[i+1]) {a[i]=0;cnt++;f=true;} } int cntt=0; for(int i:a) { if(i!=0)cntt++; else f=true; } if(f) { int ans=cnt+cntt; System.out.println(ans); } else System.out.println(n+1); } 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 Power(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
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
1d5c05f53563e2db20ec52808dc1bce7
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; public class B1{ public static int solve(String str){ ArrayList<Integer> arr=new ArrayList<>(); int count=1; for(int i=0;i<str.length()-1;i++){ char ch1=str.charAt(i); char ch2=str.charAt(i+1); if(ch1!=ch2){ arr.add(count); count=1; }else{ count++; } } char ch1=str.charAt(str.length()-1); char ch2=str.charAt(str.length()-2); if(ch1!=ch2){ arr.add(count); arr.add(1); }else{ count++; arr.add(count); } count=0; for(int i=0;i<arr.size();i++){ if(arr.get(i)%2==1&& i+1!=arr.size()){ int no=arr.get(i); arr.set(i,no-1); arr.set(i+1,arr.get(i+1)+1); count++; }else{ continue; } } return count; } public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); for(int i=0;i<n;i++){ int len=sc.nextInt(); String str=sc.next(); int op=solve(str); System.out.println(op); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
54a95b0614423cab91315a29c3522ca4
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; public class test { public static void main(String[] args) { Scanner scn=new Scanner(System.in); int n=scn.nextInt(); while(n>0){ int len=scn.nextInt(); String str=scn.next(); char[] arr=new char[len]; for(int i=0;i<len;i++){ arr[i]=str.charAt(i); } int count=0; int odd=0; int even=0; if(arr[0]=='0')even++; else{ odd++; } for(int i=1;i<len;i++){ if(arr[i]=='0'){ if(arr[i-1]=='1' && odd%2==1){ count+=1; arr[i]='1'; odd=0; }else{ even+=1; } }else{ if(arr[i-1]=='0' && even%2==1){ count+=1; arr[i]='0'; even=0; }else{ odd+=1; } } } System.out.println(count); n--; } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
cf9fb66f7322cd437136c5a377cfeea4
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code here Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); for (int i = 0; i < n; i++) { int m = in.nextInt(); String a = in.next(); String b[] = a.split(""); int s = 1; int sum = 0; for (int j = 1; j < m; j++) { if (b[j - 1].equals(b[j])) s++; else if (!b[j - 1].equals(b[j]) && s % 2 != 0) { b[j] = b[j - 1]; sum++; s ++; }else s = 1; } System.out.println(sum); } } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
01e3730305113d170b08e8c7c3c92996
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; public class Solution{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String str; int t, row, ops; char last; for(int i = 0; i < n; i++){ t = sc.nextInt(); sc.nextLine(); str = sc.nextLine(); row = 1; ops = 0; last = str.charAt(0); for(int j = 1; j < str.length(); j++){ if(row != 0){ if(str.charAt(j) != last){ if(row % 2 != 0){ ops++; row = 0; }else{ row = 1; last = str.charAt(j); } }else{ row++; } }else{ last = str.charAt(j); row = 1; } } System.out.println(ops); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
45c0fe0f5e6b677dd6d4b2a56637a51d
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; public class forces { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int flag = 1; // int n = sc.nextInt(); int n = sc.nextInt(); String str = sc.next(); char a[] = new char[n + 1]; for (int i = 0; i < n; i++) { a[i] = str.charAt(i); } ArrayList<Integer> list = new ArrayList<>(); int c = 1; for (int i = 1; i < n + 1; i++) { if (a[i - 1] == a[i]) { c++; } else { list.add(c); c = 1; } } // list.add(c); int l = list.size(); int b[] = new int[l]; int m = 0; for (int x : list) { b[m++] = x; } int ans = 0; for (int i = 0; i < l; i++) { if (b[i] % 2 == 1) { ans++; if (i + 1 < n) { b[i + 1]++; } } } // System.out.println(list); System.out.println(ans); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
fb1993d73a822f06715b87a162e72d7d
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template */ import java.lang.invoke.MethodHandles; import java.util.Scanner; import javax.swing.text.PasswordView; import java.util.*; /** * * @author HauPC */ public class JavaApplication4 { /** * @param args the command line arguments * */ public static void main(String[] args) { // TODO code application logic here // 6 9 15 Scanner input = new Scanner(System.in); int t=Integer.parseInt(input.nextLine()); while(t-->0) { int n; int res=0; n=Integer.parseInt(input.nextLine()); String binaryString=input.nextLine(); int count =1; for(int i=1;i<n;i++) { if(binaryString.charAt(i-1)!=binaryString.charAt(i)&&(count%2)!=0) { res++; i++; count=1; } else{ count++; } } System.out.println(res); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
81cea9a08fe16dfcb0a7466075b21547
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; import java.io.*; public class Main { static long startTime = System.currentTimeMillis(); // for global initializations and methods starts here // global initialisations and methods end here static void run() { boolean tc = true; //AdityaFastIO r = new AdityaFastIO(); FastReader r = new FastReader(); try (OutputStream out = new BufferedOutputStream(System.out)) { //long startTime = System.currentTimeMillis(); int testcases = tc ? r.ni() : 1; int tcCounter = 1; // Hold Here Sparky------------------->>> // Solution Starts Here start: while (testcases-- > 0) { int n = r.ni(); char[] s = r.word().toCharArray(); int count = 0; int res = 0; char charc = '1'; { int i = 0; while (i < n) { if (s[i] == s[i + 1]) { charc = s[i + 1]; break; } i += 2; } } { int i = 0; while (i < s.length) { if (s[i] == s[i + 1]) { charc = s[i + 1]; } else { count++; s[i] = charc; s[i + 1] = charc; } i += 2; } } charc = 'b'; int i = 0; while (i < n) { if (s[i] != charc) { res++; charc = s[i]; } i++; } out.write((count + " ").getBytes()); //out.write((res + " ").getBytes()); out.write(("\n").getBytes()); } // Solution Ends Here } catch (IOException e) { e.printStackTrace(); } } static class AdityaFastIO { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public BufferedReader br; public StringTokenizer st; public AdityaFastIO() { br = new BufferedReader(new InputStreamReader(System.in)); din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public AdityaFastIO(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String word() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String line() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public String readLine() throws IOException { byte[] buf = new byte[100000001]; // 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 ni() 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 nl() 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 nd() 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 void main(String[] args) throws Exception { run(); } static int[] readIntArr(int n, AdityaFastIO r) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = r.ni(); return arr; } static long[] readLongArr(int n, AdityaFastIO r) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = r.nl(); return arr; } static List<Integer> readIntList(int n, AdityaFastIO r) throws IOException { List<Integer> al = new ArrayList<>(); for (int i = 0; i < n; i++) al.add(r.ni()); return al; } static List<Long> readLongList(int n, AdityaFastIO r) throws IOException { List<Long> al = new ArrayList<>(); for (int i = 0; i < n; i++) al.add(r.nl()); return al; } static long mod = 998244353; static long modInv(long base, long e) { long result = 1; base %= mod; while (e > 0) { if ((e & 1) > 0) result = result * base % mod; base = base * base % mod; e >>= 1; } return result; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String word() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String line() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int ni() { return Integer.parseInt(word()); } long nl() { return Long.parseLong(word()); } double nd() { return Double.parseDouble(word()); } } static int MOD = (int) (1e9 + 7); static long powerLL(long x, long n) { long result = 1; while (n > 0) { if (n % 2 == 1) result = result * x % MOD; n = n / 2; x = x * x % MOD; } return result; } static long powerStrings(int i1, int i2) { String sa = String.valueOf(i1); String sb = String.valueOf(i2); long a = 0, b = 0; for (int i = 0; i < sa.length(); i++) a = (a * 10 + (sa.charAt(i) - '0')) % MOD; for (int i = 0; i < sb.length(); i++) b = (b * 10 + (sb.charAt(i) - '0')) % (MOD - 1); return powerLL(a, b); } static long gcd(long a, long b) { if (a == 0) return b; else return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } static long lower_bound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] >= x) r = m; else l = m; } return r; } static int upper_bound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] <= x) l = m; else r = m; } return l + 1; } static void addEdge(ArrayList<ArrayList<Integer>> graph, int edge1, int edge2) { graph.get(edge1).add(edge2); graph.get(edge2).add(edge1); } public static class Pair implements Comparable<Pair> { int first; int second; public Pair(int first, int second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(Pair o) { // TODO Auto-generated method stub if (this.first != o.first) return (int) (this.first - o.first); else return (int) (this.second - o.second); } } public static class PairC<X, Y> implements Comparable<PairC> { X first; Y second; public PairC(X first, Y second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(PairC o) { // TODO Auto-generated method stub return o.compareTo((PairC) first); } } static boolean isCollectionsSorted(List<Long> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) <= list.get(i - 1)) return false; return true; } static boolean isCollectionsSortedReverseOrder(List<Long> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) >= list.get(i - 1)) return false; return true; } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
c096b1b8c6d96c6397703288035aabe4
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import static java.lang.Math.*; import static java.lang.System.out; public class Main { static class Reader{ BufferedReader br; StringTokenizer st; public Reader(boolean f) throws IOException{ if(f) { br = new BufferedReader(new FileReader("input.txt")); }else{ br=new BufferedReader(new InputStreamReader(System.in)); } } String next(){ while(st==null || !st.hasMoreTokens()){ try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class Writer { private final BufferedWriter bw; public Writer(boolean f) throws IOException { if(f) { this.bw = new BufferedWriter(new FileWriter("output.txt")); }else{ 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 { Reader in=new Reader(false); Writer out =new Writer(false); int t = 1; t=in.nextInt(); while(t-- > 0){ // write code here int n = in.nextInt(); String s = in.nextLine(); int count = 0; for (int i = 0; i <=n-1; i++) { if((s.charAt(i)=='1'&&i%2==0&&s.charAt(i)!=s.charAt(i+1))||(s.charAt(i)=='0'&&i%2==0&&s.charAt(i)!=s.charAt(i+1))){ count++; } } out.println(count); } out.close(); } catch (Exception e) { return; } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
f08ae876ec05b0553b42631cea1a1039
train_108.jsonl
1652020500
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?
256 megabytes
//CP-MASS import java.util.*; import java.io.*; public class Codechef{ static FastReader in=new FastReader(); static final Random random=new Random(); static long mod=1000000007L; static HashMap<String,Integer>map=new HashMap<>(); public static void main(String args[]) throws IOException { try{ int t=in.nextInt(); int cse=1; loop: while(t-->0) { StringBuilder res=new StringBuilder(); //res.append("Hello"+"\n"); int n=in.nextInt(); ArrayList<Integer> al=new ArrayList<>(); String s=in.next(); int c=1; int f=0; for(int i=1;i<n;i++) { if(s.charAt(i)==s.charAt(i-1)) { c++; } else{ al.add(c); c=1; } } if(s.charAt(n-1)==s.charAt(n-2)) { al.add(c); } else{ al.add(1); } for(int i=0;i<al.size();i++) { if(((int)al.get(i))%2!=0) { f=1; break; } } if(f==0) { println("0"); } else{ int op=0; for(int i=0;i<al.size()-1;i++) { if((int)al.get(i)%2==1 && (int)al.get(i+1)%2==1) { op++; al.set(i,(int)al.get(i)+1); al.set(i+1,(int)al.get(i+1)-1); } else if((int)al.get(i)%2==1 && (int)al.get(i+1)%2==0) { op++; al.set(i,(int)al.get(i)+1); al.set(i+1,(int)al.get(i+1)-1); } } println(op); } //println(op); } }catch(Exception e) { return; } } static int max(int a, int b) { if(a<b) return b; return a; } 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); } static < E > void print(E res) { System.out.print(res); } static < E > void println(E res) { System.out.println(res); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static int abs(int a) { if(a<0) return -1*a; return a; } static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers 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; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int [] readintarray(int n) { int res [] = new int [n]; for(int i = 0; i<n; i++)res[i] = nextInt(); return res; } long [] readlongarray(int n) { long res [] = new long [n]; for(int i = 0; i<n; i++)res[i] = nextLong(); return res; } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3\n0\n0\n0\n3"]
NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
Java 8
standard input
[ "implementation" ]
aded139ff4d3f313f8e4d81559760f12
The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.
standard output
PASSED
71fd48abacae88905aa6194c5a091ea4
train_108.jsonl
1652020500
Tokitsukaze has a sequence $$$a$$$ of length $$$n$$$. For each operation, she selects two numbers $$$a_i$$$ and $$$a_j$$$ ($$$i \ne j$$$; $$$1 \leq i,j \leq n$$$). If $$$a_i = a_j$$$, change one of them to $$$0$$$. Otherwise change both of them to $$$\min(a_i, a_j)$$$. Tokitsukaze wants to know the minimum number of operations to change all numbers in the sequence to $$$0$$$. It can be proved that the answer always exists.
256 megabytes
import java.util.*; import java.io.*; public class G { static FastScanner fs = new FastScanner(); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(""); static Scanner scn = new Scanner(System.in); public static void main(String[] args) { int t = fs.nextInt(); //int t = 1; for (int tt = 0; tt < t; tt++) { int n = fs.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = fs.nextInt(); sort(a); int cnt = 0; int z = 0; for (int i = 0; i < n-1; i++) { if (a[i] == 0) z++; if (a[i] == a[i+1] && a[i] != 0) cnt++; } if (a[n-1] == 0) z++; if (z > 0) sb.append(n-z + "\n"); else if (cnt > 0) sb.append(n + "\n"); else sb.append(n+1 + "\n"); } pw.print(sb.toString()); pw.close(); } static void sort(int[] a) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) al.add(i); Collections.sort(al); for (int i = 0; i < a.length; i++) a[i] = al.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() {while (!st.hasMoreTokens())try {st = new StringTokenizer(br.readLine());} catch (IOException e) {}return st.nextToken();} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() {return Double.parseDouble(next());} int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) {a[i] = nextInt();} return a;} } }
Java
["3\n3\n1 2 3\n3\n1 2 2\n3\n1 2 0"]
1 second
["4\n3\n2"]
NoteIn the first test case, one of the possible ways to change all numbers in the sequence to $$$0$$$:In the $$$1$$$-st operation, $$$a_1 &lt; a_2$$$, after the operation, $$$a_2 = a_1 = 1$$$. Now the sequence $$$a$$$ is $$$[1,1,3]$$$.In the $$$2$$$-nd operation, $$$a_1 = a_2 = 1$$$, after the operation, $$$a_1 = 0$$$. Now the sequence $$$a$$$ is $$$[0,1,3]$$$.In the $$$3$$$-rd operation, $$$a_1 &lt; a_2$$$, after the operation, $$$a_2 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,3]$$$.In the $$$4$$$-th operation, $$$a_2 &lt; a_3$$$, after the operation, $$$a_3 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,0]$$$.So the minimum number of operations is $$$4$$$.
Java 11
standard input
[ "implementation" ]
ad242f98f1c8eb8d30789ec672fc95a0
The first line contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the length of the sequence $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 100$$$) — the sequence $$$a$$$.
800
For each test case, print a single integer — the minimum number of operations to change all numbers in the sequence to $$$0$$$.
standard output
PASSED
51b46caa66a0398d07c4e44f3a2f2ae2
train_108.jsonl
1652020500
Tokitsukaze has a sequence $$$a$$$ of length $$$n$$$. For each operation, she selects two numbers $$$a_i$$$ and $$$a_j$$$ ($$$i \ne j$$$; $$$1 \leq i,j \leq n$$$). If $$$a_i = a_j$$$, change one of them to $$$0$$$. Otherwise change both of them to $$$\min(a_i, a_j)$$$. Tokitsukaze wants to know the minimum number of operations to change all numbers in the sequence to $$$0$$$. It can be proved that the answer always exists.
256 megabytes
import java.util.*; import java.lang.*; public class Main{ public static String s; public static int n; public static void main(String args[]){ Scanner inp=new Scanner(System.in); int t=inp.nextInt(); for(int i=0;i<t;i++){ int count=0,a=0,b=0,coun=0; /*n=inp.nextInt(); comeback2 obj[] = new comeback2[n]; int x=inp.nextInt(); inp.nextLine(); for(int j=0;j<n;j++){ obj[j].s=inp.nextLine(); System.out.println(obj[j].s);*/ n=inp.nextInt(); int arr[]=new int[n]; for(int j=0;j<n;j++){ arr[j]=inp.nextInt(); if(arr[j]==0) count++; } Arrays.sort(arr); if(arr[0]==0 && arr[n-1]==0){ System.out.println(0); continue; } for(int j=0;j<n;j++){ if(arr[j]==0){ System.out.println(n-count); a++; break; } } if(a!=0) continue; for(int j=0;j<n-1;j++){ if(arr[j]==arr[j+1]){ coun=1; coun=coun+(n-1); System.out.println(coun); b++; break; } } if(b!=0) continue; int run=2+(n-1); System.out.println(run); } /* for(int j=0;j<n;j++){ for(int k=0;k<x;k++){ char first = 'e'; char last = 'o'; int diff = last-first; } }*/ //System.out.println(diff); } }
Java
["3\n3\n1 2 3\n3\n1 2 2\n3\n1 2 0"]
1 second
["4\n3\n2"]
NoteIn the first test case, one of the possible ways to change all numbers in the sequence to $$$0$$$:In the $$$1$$$-st operation, $$$a_1 &lt; a_2$$$, after the operation, $$$a_2 = a_1 = 1$$$. Now the sequence $$$a$$$ is $$$[1,1,3]$$$.In the $$$2$$$-nd operation, $$$a_1 = a_2 = 1$$$, after the operation, $$$a_1 = 0$$$. Now the sequence $$$a$$$ is $$$[0,1,3]$$$.In the $$$3$$$-rd operation, $$$a_1 &lt; a_2$$$, after the operation, $$$a_2 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,3]$$$.In the $$$4$$$-th operation, $$$a_2 &lt; a_3$$$, after the operation, $$$a_3 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,0]$$$.So the minimum number of operations is $$$4$$$.
Java 11
standard input
[ "implementation" ]
ad242f98f1c8eb8d30789ec672fc95a0
The first line contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the length of the sequence $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 100$$$) — the sequence $$$a$$$.
800
For each test case, print a single integer — the minimum number of operations to change all numbers in the sequence to $$$0$$$.
standard output
PASSED
713995cf2b5cef522cb67ed03492a860
train_108.jsonl
1652020500
Tokitsukaze has a sequence $$$a$$$ of length $$$n$$$. For each operation, she selects two numbers $$$a_i$$$ and $$$a_j$$$ ($$$i \ne j$$$; $$$1 \leq i,j \leq n$$$). If $$$a_i = a_j$$$, change one of them to $$$0$$$. Otherwise change both of them to $$$\min(a_i, a_j)$$$. Tokitsukaze wants to know the minimum number of operations to change all numbers in the sequence to $$$0$$$. It can be proved that the answer always exists.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class ContestAns{ public static void main(String[] args){ Scanner scin = new Scanner(System.in); int t = scin.nextInt(); while (t-->0){ int times = scin.nextInt(); int[] a = new int[times]; for (int i = 0 ; i < times ; i++){ a[i] = scin.nextInt(); } Arrays.sort(a); int count = 0; for (int i = 0 ; i < times ; i++){ if (a[i] == 0){ count++; } } if (count > 0){ System.out.println(times - count); continue; } boolean x = false; for (int i = 0 ; i < times - 1 ; i++){ if (a[i] == a[i+1]){ x = true; break; } } if (x){ System.out.println(times); } else{ System.out.println(times + 1); } } } }
Java
["3\n3\n1 2 3\n3\n1 2 2\n3\n1 2 0"]
1 second
["4\n3\n2"]
NoteIn the first test case, one of the possible ways to change all numbers in the sequence to $$$0$$$:In the $$$1$$$-st operation, $$$a_1 &lt; a_2$$$, after the operation, $$$a_2 = a_1 = 1$$$. Now the sequence $$$a$$$ is $$$[1,1,3]$$$.In the $$$2$$$-nd operation, $$$a_1 = a_2 = 1$$$, after the operation, $$$a_1 = 0$$$. Now the sequence $$$a$$$ is $$$[0,1,3]$$$.In the $$$3$$$-rd operation, $$$a_1 &lt; a_2$$$, after the operation, $$$a_2 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,3]$$$.In the $$$4$$$-th operation, $$$a_2 &lt; a_3$$$, after the operation, $$$a_3 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,0]$$$.So the minimum number of operations is $$$4$$$.
Java 11
standard input
[ "implementation" ]
ad242f98f1c8eb8d30789ec672fc95a0
The first line contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the length of the sequence $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 100$$$) — the sequence $$$a$$$.
800
For each test case, print a single integer — the minimum number of operations to change all numbers in the sequence to $$$0$$$.
standard output
PASSED
9727a0c10d5a0672a4fa455939c4bcfd
train_108.jsonl
1652020500
Tokitsukaze has a sequence $$$a$$$ of length $$$n$$$. For each operation, she selects two numbers $$$a_i$$$ and $$$a_j$$$ ($$$i \ne j$$$; $$$1 \leq i,j \leq n$$$). If $$$a_i = a_j$$$, change one of them to $$$0$$$. Otherwise change both of them to $$$\min(a_i, a_j)$$$. Tokitsukaze wants to know the minimum number of operations to change all numbers in the sequence to $$$0$$$. It can be proved that the answer always exists.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class ContestAns { public static void main(String[] koolProgrammer){ Scanner scin = new Scanner(System.in); int t = scin.nextInt(); while (t-->0){ int times = scin.nextInt(); int[] a = new int[times]; for (int i = 0 ; i < times ; i++){ a[i] = scin.nextInt(); } Arrays.sort(a); int count = 0; for (int i = 0 ; i < times ; i++){ if (a[i] == 0){ count++; } } if (count > 0){ System.out.println(times - count); continue; } boolean x = false; for (int i = 1 ; i < times ; i++){ if (a[i] == a[i - 1]){ x = true; } } if (x){ System.out.println(times); } else{ System.out.println(times + 1); } } } }
Java
["3\n3\n1 2 3\n3\n1 2 2\n3\n1 2 0"]
1 second
["4\n3\n2"]
NoteIn the first test case, one of the possible ways to change all numbers in the sequence to $$$0$$$:In the $$$1$$$-st operation, $$$a_1 &lt; a_2$$$, after the operation, $$$a_2 = a_1 = 1$$$. Now the sequence $$$a$$$ is $$$[1,1,3]$$$.In the $$$2$$$-nd operation, $$$a_1 = a_2 = 1$$$, after the operation, $$$a_1 = 0$$$. Now the sequence $$$a$$$ is $$$[0,1,3]$$$.In the $$$3$$$-rd operation, $$$a_1 &lt; a_2$$$, after the operation, $$$a_2 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,3]$$$.In the $$$4$$$-th operation, $$$a_2 &lt; a_3$$$, after the operation, $$$a_3 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,0]$$$.So the minimum number of operations is $$$4$$$.
Java 11
standard input
[ "implementation" ]
ad242f98f1c8eb8d30789ec672fc95a0
The first line contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the length of the sequence $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 100$$$) — the sequence $$$a$$$.
800
For each test case, print a single integer — the minimum number of operations to change all numbers in the sequence to $$$0$$$.
standard output
PASSED
9d5ae94fe93ac1f1e636be1b49377851
train_108.jsonl
1652020500
Tokitsukaze has a sequence $$$a$$$ of length $$$n$$$. For each operation, she selects two numbers $$$a_i$$$ and $$$a_j$$$ ($$$i \ne j$$$; $$$1 \leq i,j \leq n$$$). If $$$a_i = a_j$$$, change one of them to $$$0$$$. Otherwise change both of them to $$$\min(a_i, a_j)$$$. Tokitsukaze wants to know the minimum number of operations to change all numbers in the sequence to $$$0$$$. It can be proved that the answer always exists.
256 megabytes
//package javaapplication1; import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc. ArrayList<Integer> A = new ArrayList<Integer>(); for (int i = 1; i <= t; i++) { int a=in.nextInt(); int ans=0; for(int j=0;j<a;j++){ A.add(in.nextInt()); } for(int j=0;j<A.size();j++){ int b=Collections.frequency(A, A.get(j)); if(b>=2){ ans=A.size()-Collections.frequency(A, 0); j=A.size(); } } if(ans==0){ if(A.contains(0)==true){ ans=(A.size())-Collections.frequency(A, 0); }else{ ans=(A.size()+1); } } System.out.println(ans); A = new ArrayList<Integer>(); } } }
Java
["3\n3\n1 2 3\n3\n1 2 2\n3\n1 2 0"]
1 second
["4\n3\n2"]
NoteIn the first test case, one of the possible ways to change all numbers in the sequence to $$$0$$$:In the $$$1$$$-st operation, $$$a_1 &lt; a_2$$$, after the operation, $$$a_2 = a_1 = 1$$$. Now the sequence $$$a$$$ is $$$[1,1,3]$$$.In the $$$2$$$-nd operation, $$$a_1 = a_2 = 1$$$, after the operation, $$$a_1 = 0$$$. Now the sequence $$$a$$$ is $$$[0,1,3]$$$.In the $$$3$$$-rd operation, $$$a_1 &lt; a_2$$$, after the operation, $$$a_2 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,3]$$$.In the $$$4$$$-th operation, $$$a_2 &lt; a_3$$$, after the operation, $$$a_3 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,0]$$$.So the minimum number of operations is $$$4$$$.
Java 11
standard input
[ "implementation" ]
ad242f98f1c8eb8d30789ec672fc95a0
The first line contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the length of the sequence $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 100$$$) — the sequence $$$a$$$.
800
For each test case, print a single integer — the minimum number of operations to change all numbers in the sequence to $$$0$$$.
standard output