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 | 25d179fa4fa68a3cb680563a9067337b | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.*;
public class cf {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int t=s.nextInt();
for(int i=0;i<t;i++) {
int n=s.nextInt();
String st=s.next();
char[] a=st.toCharArray();
int pref=-1;
int seg=0;
int count=0;
for(int j=0;j<n;j=j+2) {
if(a[j]==a[j+1]) {
if(a[j]-'0' != pref) {
pref=a[j]-'0';
seg++;
}
}
else {
count++;
}
}
System.out.println(count+" "+Math.max(seg,1));
}
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | a0813ac9be7c57cfe484fa2207e67f5f | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
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.Iterator;
public class k
{
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();
}
}
//---------------------MATHS--------------------
public static ArrayList<Long> divisor(long n)
{
ArrayList<Long> arr=new ArrayList<Long>();
long x=(long) Math.sqrt(n);
for (long i=1; i<=x; i++)
{
if (n%i == 0)
{
if (n/i == i)
arr.add(i);
else
arr.add( i);
arr.add( (n/i));
}
}
return arr;
}
public static ArrayList<Long> Factors(long n)
{
ArrayList<Long> arr=new ArrayList<Long>();
while (n%2l==0)
{
n /=2;
arr.add(2l);
}
long p=(long) Math.sqrt(n);
for (long i = 3; i <=p; i+= 2)
{ if(n==1)break;
while (n%i == 0)
{
arr.add(i);
n /= i;
}
}
if (n > 2)
{
arr.add(n);
}
return arr;
}
static long hcf(long a,long b)
{
while (b > 0)
{
long temp = b;
b = a % b;
a = temp;
}
return a;
}
public static long gcd(long x, long p)
{
if (x == 0)
return p;
return gcd(p%x, x);
}
public static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
public static int biggestFactor(int num) {
int result = 1;
for(int i=2; i*i <=num; i++){
if(num%i==0){
result = num/i;
break;
}
}
return result;
}
static int sieve = 1000000 ;
static boolean[] prime = new boolean[sieve + 1] ;
static ArrayList<Integer> pr=new ArrayList<Integer>();
public static void sieveOfEratosthenes()
{
// FALSE == prime and 1 // TRUE == COMPOSITE
// time complexity = 0(NlogLogN)== o(N)
// gives prime nos bw 1 to N // size - 1e7(at max)
for(int i = 4; i<= sieve ; i++)
{
prime[i] = true ; i++ ;
}
for(int p = 3; p*p <= sieve; p++)
{
if(prime[p] == false)
{
pr.add(p);
for(int i = p*p; i <= sieve; i += p)
prime[i] = true;
}
p++ ;
}
}
static long isPrime(long x)
{
if (x >= 0) {
long sr = (long)Math.sqrt(x);
long k=sr*sr;
if(k == x)return sr;
}
return -1;
}
public static long pwmd(long a, long n,long mod) {
if (n == 0)
return 1;
long pt = pwmd(a, n / 2,mod);
pt *= pt;
pt %= mod;
if ((n & 1) > 0) {
pt *= a;
pt %= mod;
}
return pt;
}
static long nCr(long n, long r)
{
return (long)fact(n) / (long)(fact(r) *
fact(n - r));
}
// Returns factorial of n
static long fact(long n)
{
long res = 1;
for (int i = 2; i <= n; i++)
res = res * (long)(i);
return res;
}
//-------------------------BINARY SEARCHES--------------------------------------
//if present - return the first occurrence of the no
//not present- return the index of next greater value
//if greater than all the values return N(taking high=N-1)
//if smaller than all the values return 0(taking low =0)
static int lower_bound(long arr[], int low,int high, long X)
{
if (low > high) {
return low;
}
int mid = low + (high - low) / 2;
if (arr[mid] >= X) {
return lower_bound(arr, low,
mid - 1, X);
}
return lower_bound(arr, mid + 1,
high, X);
}
//if present - return the index of next greater value
//not present- return the index of next greater value
//if greater than all the values return N(taking high=N-1)
//if smaller than all the values return 0(taking low =0)\
static int upper_bound(long arr[], int low, int high, long X)
{
if (low > high)
return low;
int mid = low + (high - low) / 2;
if (arr[mid] <= X) {
return upper_bound(arr, mid + 1,high, X);
}
return upper_bound(arr, low,mid - 1, X);
}
public static class Pair {// comparator with class
long x;
long y;
public Pair(long x, long y)
{
this.x = x;
this.y = y;
}
}
//---------------UTIL---------------------
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
public static void printArr(int[] arr)
{
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static int[] decSort(int[] arr)
{
int[] arr1 = Arrays.stream(arr).boxed().sorted(Collections.reverseOrder()).mapToInt(Integer::intValue).toArray();
return arr1;
}
public static void sortbyColumn(int arr[][], int col) // send 2d array and col no
{
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else if (entry1[col] < entry2[col])
return -1;
else return 0;
}
});
}
public static void print2D(long[][] dp)
{
for (int i = 0; i < dp.length; i++)
{
{
for (int j = 0; j < dp[i].length; j++)
System.out.print(dp[i][j] + " ");
}
System.out.println();
}
}
public static int knapsack(int[] weights,int[] price, int totW)
{
int[] dp1=new int[totW+1];
int[] dp2=new int[totW+1];
int N=totW;
int ans=0;
for(int i=0;i<price.length;i++)
{
for(int j=0;j<=N;j++)
{
if(weights[i]>j)
{
if(i%2==0)
{
dp1[j]=dp2[j];
}
else
{
dp2[j]=dp1[j];
}
}
else
{
if(i%2==0)
{
dp1[j]=Math.max(dp2[j],dp2[j-weights[i]]+price[i]);
}
else
{
dp2[j]=Math.max(dp1[j], dp1[j-weights[i]]+price[i]);
}
}
}
if(i%2==0)ans=dp1[N];
else ans=dp2[N];
}
return ans;
}
public static class p
{
long no;
long h;
public p(long no, long h)
{
this.no=no;
this.h= h;
}
}
static class com implements Comparator<p>{
public int compare(p s1, p s2) {
if (s1.h > s2.h)
return -1;
else if (s1.h < s2.h)
return 1;
else if(s1.h==s2.h)
{
if(s1.no>s2.no)return -1;
else return 1;
}
return 0;
}
}
static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {
SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
new Comparator<Map.Entry<K,V>>() {
@Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
int res = e1.getValue().compareTo(e2.getValue());
return res != 0 ? res : 1; // Special fix to preserve items with equal values
}
}
);
sortedEntries.addAll(map.entrySet());
return sortedEntries;
}
public static void floodFill1(int[][] image, int sr, int sc) {
image[sr][sc]=2;
if((sr-1>=0) && image[sr-1][sc]==1)
{
floodFill1(image,sr-1,sc);
}
if((sr+1<image.length) && image[sr+1][sc]==1)
{
floodFill1(image,sr+1,sc);
}
if((sc-1>=0) && image[sr][sc-1]==1 )
{
floodFill1(image,sr,sc-1);
}
if((sc+1<image[0].length) && image[sr][sc+1]==1)
{
floodFill1(image,sr,sc+1);
}
}
// ---------------SEGMENT TREE----------
public static void buildTree(long[] arr,long[][] tree,int st, int en,int ind)
{
// int x = (int) (Math.ceil(Math.log(arr.length) / Math.log(2)));
// int size = 2 * (int) Math.pow(2, x) - 1;
// int[] tree=new int[size];
if(st==en) {tree[ind][0]=arr[st];tree[ind][1]=1;return;}
int mid=(st+en)/2;
buildTree(arr,tree,st,mid,2*ind);
buildTree(arr,tree,mid+1,en,(2*ind)+1);
if(tree[2*ind][0]<tree[(2*ind)+1][0])
{
tree[ind][0]=tree[2*ind][0];
tree[ind][1]=tree[2*ind][1];
}
if(tree[2*ind][0]>tree[(2*ind)+1][0])
{
tree[ind][0]=tree[2*ind+1][0];
tree[ind][1]=tree[2*ind+1][1];
}
if(tree[2*ind][0]==tree[(2*ind)+1][0])
{
tree[ind][0]=tree[2*ind][0];
tree[ind][1]=tree[2*ind][1]+tree[2*ind+1][1];
}
return;
}
public static int k=0;
public static long query(long[][] tree,int st, int en, int qs, int qe, int ind)
{
if(st>=qs && en<=qe)
{
return tree[ind][0];
}
if(st>qe || en<qs)return Integer.MAX_VALUE;
int mid=(st+en)/2;
long l=query(tree,st,mid,qs,qe,2*ind);
long r=query(tree,mid+1,en,qs,qe,2*ind+1);
return Math.min(l, r);
}
public static p query1(long[][] tree,int st, int en, int qs, int qe, int ind)
{
if(st>qe || en<qs)
{
p k=new p(1000000007,-1);
return k;
}
if(st>=qs && en<=qe)
{
return new p(tree[ind][0],tree[ind][1]);
}
int mid=(st+en)/2;
p l=query1(tree,st,mid,qs,qe,2*ind);
p r=query1(tree,mid+1,en,qs,qe,2*ind+1);
p fin;
if(l.no<r.no)
{
return l;
}
if(l.no>r.no)
{
return r;
}
return new p(l.no,l.h+r.h);
}
public static void update(long[][] tree,int st, int en, int qs, int qe, int ind,long inc)
{
if(st>qe || en<qs)return ;
if(st==en)
{
tree[ind][0]=inc;
return;
}
int mid=(st+en)/2;
update(tree,st,mid,qs,qe,2*ind,inc);
update(tree,mid+1,en,qs,qe,2*ind+1,inc);
if(tree[2*ind][0]<tree[(2*ind)+1][0])
{
tree[ind][0]=tree[2*ind][0];
tree[ind][1]=tree[2*ind][1];
}
if(tree[2*ind][0]>tree[(2*ind)+1][0])
{
tree[ind][0]=tree[2*ind+1][0];
tree[ind][1]=tree[2*ind+1][1];
}
if(tree[2*ind][0]==tree[(2*ind)+1][0])
{
tree[ind][0]=tree[2*ind][0];
tree[ind][1]=tree[2*ind][1]+tree[2*ind+1][1];
}
return ;
}
//--------------------------------------------------------------
public static void main(String args[]) throws NumberFormatException, IOException ,java.lang.Exception
{
Reader reader = new Reader();
long mod= 1000000007;
// long mod= 998244353;
// long[] pow2 =new long[64];
// pow2[0]=1l;
// for(int i=1;i<64;i++)
//// {
////// pow2[i]=(int) Math.pow(2, i);
// pow2[i]=((long)(2)*pow2[i-1]);
////// System.out.println(pow2[i]);
//// }
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int cases=Integer.parseInt(br.readLine());
// int cases=1;
// int cases=reader.nextInt();
while (cases-->0){
//
// long N=reader.nextLong();
// long B=reader.nextLong();
// long X=reader.nextLong();
// long Y=reader.nextLong();
// long D2=reader.nextLong();
// long C=reader.nextLong();
// long W=reader.nextLong();
// long A=reader.nextLong();
// long N=reader.nextLong();
// int N=reader.nextInt();
// int M=reader.nextInt();
// int D=reader.nextInt();
// int A=reader.nextInt();
// int B=reader.nextInt();
// int K=reader.nextInt();
String[] first=br.readLine().split(" ");
int N=Integer.parseInt(first[0]);
String p=br.readLine();
int c=0;
int last=-1;
int k=0;
for(int i=0;i<N;i=i+2)
{
if(p.charAt(i)!=p.charAt(i+1))c++;
if(p.charAt(i)==p.charAt(i+1))
{
if(p.charAt(i)=='1' && (last!=1))
{
k++;
last=1;
}
else if(p.charAt(i)=='0' && (last!=0))
{
k++;
last=0;
}
}
}
System.out.println(c+" "+(k>1?k:1));
// int X=Integer.parseInt(first[1]);
// int Y=Integer.parseInt(first[3]);
// String[] first2=br.readLine().split(" ");
// String[] first3=br.readLine().split(" ");
// int M=Integer.parseInt(first1[1]);
// long K=Long.parseLong(first1[0]);
// long X=Long.parseLong(first1[1]);
// String s3=br.readLine();String s4=br.readLine();
// char[] s11=s2.toCharArray();
// char[] s12=new char[s11.length];
// long max=Long.MIN_VALUE;
// long max=10000000000001l;
// int min=Integer.MAX_VALUE;
// long min=Inteeg.MAX_VALUE;
// int min1=Integer.MAX_VALUE;
// int min2=Integer.MAX_VALUE;
// HashMap<Integer, Integer> map=new HashMap<Integer,Integer>();
// PriorityQueue<Integer> q = new PriorityQueue<Integer>();
// PriorityQueue<Long> q = new PriorityQueue<Long>(Collections.reverseOrder());
// HashMap<Integer,TreeSet<Integer>> map=new HashMap<Integer,TreeSet<Integer>>();
// HashMap<Long,Long> map=new HashMap<Long,Long>();
// HashMap<String,String> map1=new HashMap<String,String>();
// TreeMap<Long,Integer> map1=new TreeMap<Long,Integer>();
// List<TreeMap<Integer,Integer>> map = new ArrayList<TreeMap<Integer,Integer>>();
// HashSet<Character> set =new HashSet<Character>();
// HashSet<String> set1 =new HashSet<String>();
// HashSet<Integer> map =new HashSet<Integer>();
// HashSet<Long> map =new HashSet<Long>();
// TreeSet<Integer> a =new TreeSet<Integer>();
// TreeSet<Long> b =new TreeSet<Long>();
// TreeSet<Integer> map=new TreeSet<Integer>();
// int[] arr=new int[(int)M];
// Integer[] arr=new Integer[N];
// Integer[] arr2=new Integer[N];
// int[] arr2=new int[64];
// int[] ev=new int[N];
// int[] od=new int[N];
// boolean[] s=new boolean[K+1];
// int[] arr=new int[32];// i00nt[] odd=new int[100001];
// Integer[] arr=new Integer[N];
// Integer[] arr1=new Integer[N];
// long[] b=new long[N+1];
// long[] suf=new long[N+1];
// Integer[] arr=new Integer[N];
// Long[] arr=new Long[N];
// long[][] dp=new long[N][M+1];
// ArrayList<String> l=new ArrayList<String>();
}
output.flush();
}
}
//
// output.flush();
// output.flush();
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | a588404de7bc1b96536b7f91848f4c9d | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
public class Round_789 {
static BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
static int mod = 998244353;
static String toReturn = "";
static int steps = Integer.MAX_VALUE;
static int maxlen = 1000005;
/*MATHEMATICS FUNCTIONS START HERE
MATHS
MATHS
MATHS
MATHS*/
static long gcd(long a, long b) {
if(b == 0) return a;
else return gcd(b, a % b);
}
static long powerMod(long x, long y, int mod) {
if(y == 0) return 1;
long temp = powerMod(x, y / 2, mod);
temp = ((temp % mod) * (temp % mod)) % mod;
if(y % 2 == 0) return temp;
else return ((x % mod) * (temp % mod)) % mod;
}
static long modInverse(long n, int p) {
return powerMod(n, p - 2, p);
}
static long nCr(int n, int r, int mod, long [] fact, long [] ifact) {
return ((fact[n] % mod) * ((ifact[r] * ifact[n - r]) % mod)) % mod;
}
static boolean isPrime(long a) {
if(a == 1) return false;
else if(a == 2 || a == 3 || a== 5) return true;
else if(a % 2 == 0 || a % 3 == 0) return false;
for(int i = 5; i * i <= a; i = i + 6) {
if(a % i == 0 || a % (i + 2) == 0) return false;
}
return true;
}
static int [] seive(int a) {
int [] toReturn = new int [a + 1];
for(int i = 0; i < a; i++) toReturn[i] = 1;
toReturn[0] = 0;
toReturn[1] = 0;
toReturn[2] = 1;
for(int i = 2; i * i <= a; i++) {
if(toReturn[i] == 0) continue;
for(int j = 2 * i; j <= a; j += i) toReturn[j] = 0;
}
return toReturn;
}
static long [] fact(int a) {
long [] arr = new long[a + 1];
arr[0] = 1;
for(int i = 1; i < a + 1; i++) {
arr[i] = (arr[i - 1] * i) % mod;
}
return arr;
}
/*MATHS
MATHS
MATHS
MATHS
MATHEMATICS FUNCTIONS END HERE */
/*SWAP FUNCTION START HERE
SWAP
SWAP
SWAP
SWAP
*/
static void swap(int i, int j, int [] arr) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/*SWAP
SWAP
SWAP
SWAP
SWAP FUNCTION END HERE*/
/*BINARY SEARCH METHODS START HERE
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
*/
static boolean BinaryCheck(long test, long [] arr, long health) {
for(int i = 0; i <= arr.length - 1; i++) {
if(i == arr.length - 1) health -= test;
else if(arr[i + 1] - arr[i] > test) {
health = health - test;
}else {
health = health - (arr[i + 1] - arr[i]);
}
if(health <= 0) return true;
}
return false;
}
static long binarySearchModified(long start1, long n, ArrayList<Long> arr, int a, long r) {
long start = start1, end = n, ans = -1;
while(start < end) {
long mid = (start + end) / 2;
//System.out.println(mid);
if(arr.get((int)mid) + arr.get(a) <= r && mid != start1) {
ans = mid;
start = mid + 1;
}else{
end = mid;
}
}
//System.out.println();
return ans;
}
static long binarySearchModified1(int n, ArrayList<Long> arr, long poi) {
long start = 0, end = n, ans = -1;
while(start < end) {
long mid = (start + end) / 2;
//System.out.println(mid);
if(arr.get((int)mid) <= poi) {
ans = mid;
start = mid + 1;
} else {
end = mid;
}
}
//System.out.println();
return ans;
}
/*BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
BINARY SEARCH METHODS END HERE*/
/*RECURSIVE FUNCTION START HERE
* RECURSIVE
* RECURSIVE
* RECURSIVE
* RECURSIVE
*/
static int recurse(int x, int y, int n, int steps1, Integer [][] dp) {
if(x > n || y > n) return 0;
if(dp[x][y] != null) {
return dp[x][y];
}
else if(x == n || y == n) {
return steps1;
}
return dp[x][y] = Math.max(recurse(x + y, y, n, steps1 + 1, dp), recurse(x, x + y, n, steps1 + 1, dp));
}
/*RECURSIVE
* RECURSIVE
* RECURSIVE
* RECURSIVE
* RECURSIVE
RECURSIVE FUNCTION END HERE*/
/*GRAPH FUNCTIONS START HERE
* GRAPH
* GRAPH
* GRAPH
* GRAPH
* */
static class edge{
int from, to;
long weight;
public edge(int x, int y, long weight2) {
this.from = x;
this.to = y;
this.weight = weight2;
}
}
static class sort implements Comparator<pair>{
@Override
public int compare(pair o1, pair o2) {
// TODO Auto-generated method stub
if(o1.b > o2.b) return 1;
else return -1;
}
}
static void addEdge(ArrayList<ArrayList<edge>> graph, int from, int to, long weight) {
edge temp = new edge(from, to, weight);
edge temp1 = new edge(to, from, weight);
graph.get(from).add(temp);
//graph.get(to).add(temp1);
}
static int ans = 0;
static void dfs(ArrayList<ArrayList<edge>> graph, int vertex, boolean [] visited, int dest) {
//System.out.println(graph.get(vertex).size());
if(ans == 2) return;
if(vertex == dest) {
ans++;
return;
}
if(visited[vertex]) return;
visited[vertex] = true;
for(int i = 0; i < graph.get(vertex).size(); i++) {
edge temp = graph.get(vertex).get(i);
if(!visited[temp.to]) {
//System.out.println(temp.to);
//toReturn[(int) temp.weight] = weight;
dfs(graph, temp.to, visited, dest);
//weight = 5 - weight;
}
}
}
static void bfs(ArrayList<ArrayList<edge>> graph, int vertex, boolean [] visited, int [] toReturn, Queue<Integer> q, int weight) {
if(visited[vertex]) return;
visited[vertex] = true;
if(graph.get(vertex).size() > 2) return;
int first = weight;
for(int i = 0; i < graph.get(vertex).size(); i++) {
edge temp = graph.get(vertex).get(i);
if(!visited[temp.to]) {
q.add(temp.to);
toReturn[(int) temp.weight] = weight;
weight = 5 - weight;
}
}
if(!q.isEmpty())bfs(graph, q.poll(), visited, toReturn, q, 5 - first);
}
static void topoSort(ArrayList<ArrayList<Integer>> graph, int vertex, boolean [] visited, ArrayList<Integer> toReturn) {
if(visited[vertex]) return;
visited[vertex] = true;
for(int i = 0; i < graph.get(vertex).size(); i++) {
if(!visited[graph.get(vertex).get(i)]) topoSort(graph, graph.get(vertex).get(i), visited, toReturn);
}
toReturn.add(vertex);
}
static boolean isCyclicDirected(ArrayList<ArrayList<Integer>> graph, int vertex, boolean [] visited, boolean [] reStack) {
if(reStack[vertex]) return true;
if(visited[vertex]) return false;
reStack[vertex] = true;
visited[vertex] = true;
for(int i = 0; i < graph.get(vertex).size(); i++) {
if(isCyclicDirected(graph, graph.get(vertex).get(i), visited, reStack)) return true;
}
reStack[vertex] = false;
return false;
}
static int e = 0;
static long mst(PriorityQueue<edge> pq, int nodes) {
long weight = 0;
while(!pq.isEmpty()) {
edge temp = pq.poll();
int x = parent(parent, temp.to);
int y = parent(parent, temp.from);
if(x != y) {
//System.out.println(temp.weight);
union(x, y, rank, parent);
weight += temp.weight;
e++;
}
}
return weight;
}
static void floyd(long [][] dist) { // to find min distance between two nodes
for(int k = 0; k < dist.length; k++) {
for(int i = 0; i < dist.length; i++) {
for(int j = 0; j < dist.length; j++) {
if(dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
}
static void dijkstra(ArrayList<ArrayList<edge>> graph, long [] dist, int src) {
for(int i = 0; i < dist.length; i++) dist[i] = Long.MAX_VALUE / 2;
dist[src] = 0;
boolean visited[] = new boolean[dist.length];
PriorityQueue<pair> pq = new PriorityQueue<>(new sort());
pq.add(new pair(src, 0));
while(!pq.isEmpty()) {
pair temp = pq.poll();
int index = temp.a;
for(int i = 0; i < graph.get(index).size(); i++) {
if(dist[graph.get(index).get(i).to] > dist[index] + graph.get(index).get(i).weight) {
dist[graph.get(index).get(i).to] = dist[index] + graph.get(index).get(i).weight;
pq.add(new pair(graph.get(index).get(i).to, graph.get(index).get(i).weight));
}
}
}
}
static int parent1 = -1;
static boolean ford(ArrayList<ArrayList<edge>> graph1, ArrayList<edge> graph, long [] dist, int src, int [] parent) {
for(int i = 0; i < dist.length; i++) dist[i] = Long.MIN_VALUE / 2;
dist[src] = 0;
boolean hasNeg = false;
for(int i = 0; i < dist.length - 1; i++) {
for(int j = 0; j < graph.size(); j++) {
int from = graph.get(j).from;
int to = graph.get(j).to;
long weight = graph.get(j).weight;
if(dist[to] < dist[from] + weight) {
dist[to] = dist[from] + weight;
parent[to] = from;
}
}
}
for(int i = 0; i < graph.size(); i++) {
int from = graph.get(i).from;
int to = graph.get(i).to;
long weight = graph.get(i).weight;
if(dist[to] < dist[from] + weight) {
parent1 = from;
hasNeg = true;
dfs(graph1, parent1, new boolean[dist.length], dist.length - 1);
//System.out.println(ans);
dfs(graph1, 0, new boolean[dist.length], parent1);
//System.out.println(ans);
if(ans == 2) break;
else ans = 0;
}
}
return hasNeg;
}
/*GRAPH FUNCTIONS END HERE
* GRAPH
* GRAPH
* GRAPH
* GRAPH
*/
/*disjoint Set START HERE
* disjoint Set
* disjoint Set
* disjoint Set
* disjoint Set
*/
static int [] rank;
static int [] parent;
static int parent(int [] parent, int x) {
if(parent[x] == x) return x;
else return parent[x] = parent(parent, parent[x]);
}
static boolean union(int x, int y, int [] rank, int [] parent) {
if(parent(parent, x) == parent(parent, y)) {
return true;
}
if(rank[x] > rank[y]) {
swap(x, y, rank);
}
rank[x] += rank[y];
parent[y] = x;
return false;
}
/*disjoint Set END HERE
* disjoint Set
* disjoint Set
* disjoint Set
* disjoint Set
*/
/*INPUT START HERE
* INPUT
* INPUT
* INPUT
* INPUT
* INPUT
*/
static int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(sc.readLine());
}
static long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(sc.readLine());
}
static long [] inputLongArr() throws NumberFormatException, IOException{
String [] s = sc.readLine().split(" ");
long [] toReturn = new long[s.length];
for(int i = 0; i < s.length; i++) {
toReturn[i] = Long.parseLong(s[i]);
}
return toReturn;
}
static int max = 0;
static int [] inputIntArr() throws NumberFormatException, IOException{
String [] s = sc.readLine().split(" ");
//System.out.println(s.length);
int [] toReturn = new int[s.length];
for(int i = 0; i < s.length; i++) {
toReturn[i] = Integer.parseInt(s[i]);
}
return toReturn;
}
/*INPUT
* INPUT
* INPUT
* INPUT
* INPUT
* INPUT END HERE
*/
static long [] preCompute(int level) {
long [] toReturn = new long[level];
toReturn[0] = 1;
toReturn[1] = 16;
for(int i = 2; i < level; i++) {
toReturn[i] = ((toReturn[i - 1] % mod) * (toReturn[i - 1] % mod)) % mod;
}
return toReturn;
}
static class pair{
int a;
long b;
public pair(int x, long y) {
this.a = x;
this.b = y;
}
}
static long smallestFactor(long a) {
for(long i = 2; i * i <= a; i++) {
if(a % i == 0) {
return i;
}
}
return a;
}
static int recurseRow(int [][] mat, int i, int j, int prev, int ans) {
if(j >= mat[0].length) return ans;
if(mat[i][j] == prev) {
return recurseRow(mat, i, j + 1, prev, ans + 1);
}
else return ans;
}
static int recurseCol(int [][] mat, int i, int j, int prev, int ans) {
if(i >= mat.length) return ans;
if(mat[i][j] == prev) {
return recurseCol(mat, i + 1, j, prev, ans + 1);
}
else return ans;
}
static int diff(char [] a, char [] b) {
int sum = 0;
for(int i = 0; i < a.length; i++) {
sum += Math.abs((int)a[i] - (int)b[i]);
}
return sum;
}
static void solve() throws IOException {
int n = nextInt();
String s = sc.readLine();
char [] c = s.toCharArray();
char [] c1 = s.toCharArray();
int count = 0;
char prev = 0;
for(int i = 0; i < c.length; i+=2) {
if(i > 0) prev = c[i - 1];
if(c[i] != c[i + 1]) {
if(i == 0) {
c[i] = '0';
c[i + 1] = '0';
}else {
c[i] = prev;
c[i + 1] = prev;
}
count++;
}
}
//System.out.println(String.valueOf(c));
int sum1 = count(c);
count = 0;
for(int i = 0; i < c1.length; i+=2) {
if(i > 0) prev = c1[i - 1];
if(c1[i] != c1[i + 1]) {
if(i == 0) {
c1[i] = '1';
c1[i + 1] = '1';
}else {
c1[i] = prev;
c1[i + 1] = prev;
}
count++;
}
}
//System.out.println(String.valueOf(c1));
int sum2 = count(c1);
System.out.println(count + " " + Math.min(sum1, sum2));
return;
}
static int count(char [] c) {
int one = 0, zero = 0;
int sum1 = 0, sum2 = 0;
for(int i = 0; i < c.length; i++) {
if(c[i] == '1') {
sum1++;
if(sum2 != 0) zero++;
sum2 = 0;
}else {
sum2++;
if(sum1 != 0) one++;
sum1 = 0;
}
}
if(sum1 != 0) one++;
else if(sum2 != 0) zero++;
return one + zero;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int t = Integer.parseInt(sc.readLine()); for(int i = 0; i < t; i++)
solve();
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 792fbaa17c5d683c797289cdabb99dd7 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | // Working program using Reader Class
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.Collections;
import java.util.*;
public class BaseClassPublic
{
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[1000000]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
static final int MAXN = 100001;
static int spf[] = new int[MAXN];
public static void sieve()
{
spf[1] = 1;
for (int i=2; i<MAXN; i++)
spf[i] = i;
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
if (spf[i] == i)
{
for (int j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}
public static Vector<Integer> getFactorization(int x)
{
Vector<Integer> ret = new Vector<>();
while (x != 1)
{
ret.add(spf[x]);
x = x / spf[x];
}
return ret;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
//Reader sc=new Reader();
int t=sc.nextInt();
PrintWriter writer = new PrintWriter(System.out);
while(t-- > 0) {
int n = sc.nextInt();
String str = sc.next();
int ans = 0;
int[] arr = new int[n/2];
for(int i = 0; i < n/2; i++) arr[i] = -1;
int k = 0;
for(int i = 0; i < n-1; i++) {
if (str.charAt(i) != str.charAt(i+1)) ans += 1;
else if (str.charAt(i) == '0') arr[k] = 0;
else arr[k] = 1;
i += 1;
k += 1;
}
int segAns = 0;
int prev = -2;
for(int i = 0; i < n/2; i++) {
if (arr[i] == prev || arr[i] == -1) continue;
else {
segAns += 1;
prev = arr[i];
}
}
if (segAns == 0) segAns = 1;
writer.write(ans + " " + segAns+ "\n");
}
writer.flush();
writer.close();
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 38a1ec378fa78f1826bfa21214e0482a | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | /*
5
10
1110011000
8
11001111
2
00
2
11
6
100110
Can divide each string into n/2 segments of size two each, every segment that looks like '10' or '01' needs to be flipped to become '11' or '00'
*/
import java.util.*;
import java.io.*;
public class Main{
public static int n;
public static int[] str;
public static Pair[][] dp;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int nc = Integer.parseInt(br.readLine());
for(int cc = 0; cc < nc; cc++){
n = Integer.parseInt(br.readLine());
//dp[i][j] = min cost, then # of subsegments, given the first i segments of length two, and the value of the last segment is j
int[] st = new int[n/2 + 1]; //type of segment: 0, 1 means that the segment is set (two numbers of the same type) and of that value, 2 means that the segment can be edited (01 or 10)
str = new int[n];
String s = br.readLine();
for(int a = 0; a < n; a++){
if(s.charAt(a) == '1') str[a] = 1;
if(a % 2 == 1){
if(str[a] != str[a - 1]) st[a/2 + 1] = 2;
else st[a/2 + 1] = str[a];
}
}
//System.out.println(Arrays.toString(st));
dp = new Pair[n/2 + 1][2];
for(int a = 1; a <= n/2; a++) for(int b = 0; b < 2; b++) dp[a][b] = new Pair(1000000000, 1000000000);
dp[0][0] = new Pair(1,0);
dp[0][1] = new Pair(1,0);
for(int sb = 1; sb <= n/2; sb++){
for(int type = 0; type < 2; type++){
if(st[sb] == 2){
dp[sb][type] = min(new Pair(dp[sb-1][type].ni, dp[sb-1][type].cost + 1), new Pair(dp[sb-1][1 - type].ni + 1, dp[sb-1][1 - type].cost + 1));
}
else if(type == st[sb]){
//fixed
dp[sb][type] = min(dp[sb-1][type], new Pair(dp[sb-1][1 - type].ni + 1, dp[sb-1][1 - type].cost));
}
//System.out.println(sb + " segments, type is " + type + " and cost, number of intervals is " + dp[sb][type].cost + " " + dp[sb][type].ni);
}
}
System.out.println(min(dp[n/2][0], dp[n/2][1]));
}
br.close();
}
public static Pair min(Pair a, Pair b){
if(a.compareTo(b) < 0) return a;
return b;
}
}
class Pair{
int ni; //# of intervals
int cost; //# of flips
public Pair(int n, int c){
ni = n;
cost = c;
}
public String toString(){
return cost + " " + ni;
}
public int compareTo(Pair p){
if(p.cost != cost) return cost - p.cost;
return ni - p.ni;
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | f3c1327418cc10f34a123e8d87124aae | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.lang.*;
public class Solution{
static int mod=(int)1e9+7;
static int mod1=998244353;
static FastScanner sc = new FastScanner();
static StringBuffer ans;
static long ct;
public static void solve(){
int n=sc.nextInt();
String s=sc.next();
int ans=0;
for (int i=0;i<n;i+=2) {
if (s.charAt(i)!=s.charAt(i+1)) {
ans++;
}
}
// System.out.print(ans);
int[][] dp=new int[n/2][2];
dp[0][0]=1;
dp[0][1]=1;
int ct=1;
for (int i=2;i<n;i+=2){
dp[ct][0]=ct+1;
dp[ct][1]=ct+1;
if (s.charAt(i)==s.charAt(i+1)) {
if (s.charAt(i-1)==s.charAt(i-2)) {
if (s.charAt(i-1)=='0' && s.charAt(i)=='0') {
dp[ct][0]=dp[ct-1][0];
}else if(s.charAt(i-1)=='0' && s.charAt(i)=='1'){
dp[ct][1]=dp[ct-1][0]+1;
}else if(s.charAt(i-1)=='1' && s.charAt(i)=='0'){
dp[ct][0]=dp[ct-1][1]+1;
}else if(s.charAt(i-1)=='1' && s.charAt(i)=='1'){
dp[ct][1]=dp[ct-1][1];
}
}else{
if (s.charAt(i)=='0') {
dp[ct][0]=min(dp[ct-1][0],dp[ct][0]);
dp[ct][0]=min(dp[ct-1][1]+1,dp[ct][0]);
}else{
dp[ct][1]=min(dp[ct-1][1],dp[ct][1]);
dp[ct][1]=min(dp[ct-1][0]+1,dp[ct][1]);
}
}
}else{
if (s.charAt(i-1)==s.charAt(i-2)) {
if (s.charAt(i-1)=='0') {
dp[ct][0]=dp[ct-1][0];
dp[ct][1]=dp[ct-1][0]+1;
}else{
dp[ct][1]=dp[ct-1][1];
dp[ct][0]=dp[ct-1][1]+1;
}
}else{
dp[ct][0]=min(dp[ct-1][0],dp[ct][0]);
dp[ct][0]=min(dp[ct-1][1]+1,dp[ct][0]);
dp[ct][1]=min(dp[ct-1][1],dp[ct][1]);
dp[ct][1]=min(dp[ct-1][0]+1,dp[ct][1]);
}
// dp[ct][0]=min(dp[ct-1][0],dp[ct][0]);
// dp[ct][0]=min(dp[ct-1][1]+1,dp[ct][0]);
// dp[ct][1]=min(dp[ct-1][1],dp[ct][1]);
// dp[ct][1]=min(dp[ct-1][0]+1,dp[ct][1]);
}
ct++;
}
System.out.println(ans+" "+min(dp[ct-1][0],dp[ct-1][1]));
}
public static void main(String[] args) {
// ans=new StringBuffer("");
int t = sc.nextInt();
outer: for (int tt = 0; tt < t; tt++) {
solve();
}
// System.out.println(ans);
}
static boolean isSubSequence(String str1, String str2,
int m, int n)
{
int j = 0;
for (int i = 0; i < n && j < m; i++)
if (str1.charAt(j) == str2.charAt(i))
j++;
return (j == m);
}
static int reverseDigits(int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
/* Function to check if n is Palindrome*/
static boolean isPalindrome(int n)
{
// get the reverse of n
int rev_n = reverseDigits(n);
// Check if rev_n and n are same or not.
if (rev_n == n)
return true;
else
return false;
}
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;
}
public static int[] LPS(String s){
int[] lps=new int[s.length()];
int i=0,j=1;
while (j<s.length()) {
if(s.charAt(i)==s.charAt(j)){
lps[j]=i+1;
i++;
j++;
continue;
}else{
if (i==0) {
j++;
continue;
}
i=lps[i-1];
while(s.charAt(i)!=s.charAt(j) && i!=0) {
i=lps[i-1];
}
if(s.charAt(i)==s.charAt(j)){
lps[j]=i+1;
i++;
}
j++;
}
}
return lps;
}
static long getPairsCount(int n, double sum,int[] arr)
{
HashMap<Double, Integer> hm = new HashMap<>();
for (int i = 0; i < n; i++) {
if (!hm.containsKey((double)arr[i]))
hm.put((double)arr[i], 0);
hm.put((double)arr[i], hm.get((double)arr[i]) + 1);
}
long twice_count = 0;
for (int i = 0; i < n; i++) {
if (hm.get(sum - arr[i]) != null)
twice_count += hm.get(sum - arr[i]);
if (sum - (double)arr[i] == (double)arr[i])
twice_count--;
}
return twice_count / 2l;
}
static boolean[] sieveOfEratosthenes(int n)
{
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
prime[1]=false;
return prime;
}
static long power(long x, long y, long p)
{
long res = 1l;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y>>=1;
x = (x * x) % p;
}
return res;
}
public static int log2(int N)
{
int result = (int)(Math.log(N) / Math.log(2));
return result;
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////DO NOT READ AFTER THIS LINE //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
static long modFact(int n,
int p)
{
if (n >= p)
return 0;
long result = 1l;
for (int i = 2; i <= n; i++)
result = (result * i) % p;
return result;
}
static boolean isPalindrom(char[] arr, int i, int j) {
boolean ok = true;
while (i <= j) {
if (arr[i] != arr[j]) {
ok = false;
break;
}
i++;
j--;
}
return ok;
}
static int max(int a, int b) {
return Math.max(a, b);
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long max(long a, long b) {
return Math.max(a, b);
}
static long min(long a, long b) {
return Math.min(a, b);
}
static int abs(int a) {
return Math.abs(a);
}
static long abs(long a) {
return Math.abs(a);
}
static void swap(long arr[], int i, int j) {
long temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static int maxArr(int arr[]) {
int maxi = Integer.MIN_VALUE;
for (int x : arr)
maxi = max(maxi, x);
return maxi;
}
static int minArr(int arr[]) {
int mini = Integer.MAX_VALUE;
for (int x : arr)
mini = min(mini, x);
return mini;
}
static long maxArr(long arr[]) {
long maxi = Long.MIN_VALUE;
for (long x : arr)
maxi = max(maxi, x);
return maxi;
}
static long minArr(long arr[]) {
long mini = Long.MAX_VALUE;
for (long x : arr)
mini = min(mini, x);
return mini;
}
static int lcm(int a,int b){
return (int)(((long)a*b)/(long)gcd(a,b));
}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static 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);
int temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
public static int binarySearch(int a[], int target) {
int left = 0;
int right = a.length - 1;
int mid = (left + right) / 2;
int i = 0;
while (left <= right) {
if (a[mid] <= target) {
i = mid + 1;
left = mid + 1;
} else {
right = mid - 1;
}
mid = (left + right) / 2;
}
return i-1;
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
int[][] read2dArray(int n, int m) {
int arr[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = nextInt();
}
}
return arr;
}
ArrayList<Integer> readArrayList(int n) {
ArrayList<Integer> arr = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
int a = nextInt();
arr.add(a);
}
return arr;
}
long nextLong() {
return Long.parseLong(next());
}
}
static class Pair {
int val;
long coins;
Pair(int val,long coins) {
this.val=val;
this.coins=coins;
}
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 33512f6a0e90fc1e6a31ba5049ebf456 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | /*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = sc.nextInt();
for (int t = 1; t <= test; t++) {
solve();
}
out.close();
}
private static void solve() {
int n = sc.nextInt();
char[] arr = sc.next().toCharArray();
int operations = 0, segments = 0, lastSegment = -1;
for (int i = 0; i < n; i += 2) {
if (arr[i] != arr[i + 1]) {
operations++;
}else {
// create new segment or join with previous segment
if (lastSegment != arr[i]) {
segments++;
}
lastSegment = arr[i];
}
}
out.println(operations + " " + Math.max(1, segments));
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 2799d1ae3df5f60483192faf9283ccb9 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.*;
public class Solution {
static Scanner sc = new Scanner(System.in);
public static void solver() {
int n = sc.nextInt();
String s = sc.next();
int oddSeg= 0, evenSeg = 0, l = -1;
for(int i = 0; i < n; i+=2){
if(s.charAt(i) != s.charAt(i+1)){
oddSeg++;
}
else{
if(l != s.charAt(i)){
evenSeg++;
}
l = s.charAt(i);
}
}
System.out.println(oddSeg + " "+ Math.max(evenSeg, 1));
}
public static void main(String[] args) {
int t = sc.nextInt();
while (t-- > 0) {
solver();
}
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | ee19300543887fdee07ae889205a5463 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | // Java is like Alzheimer's, it starts off slow, but eventually, your memory is gone.
import java.io.*;
import java.util.*;
public class Aqueous {
static MyScanner sc = new MyScanner();
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) {
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
String s = sc.next();
StringBuilder sb = new StringBuilder();
int cnt = 0;
for(int i = 0; i<s.length()-1; i=i+2) {
if(s.charAt(i)!=s.charAt(i+1)) {
cnt++;
}
else {
sb.append(s.charAt(i));
}
}
int ans = 1;
for(int i =1; i<sb.length(); i++) {
if(sb.charAt(i)!=sb.charAt(i-1)) {
ans++;
}
}
pw.println(cnt+" "+ans);
}
pw.close();
}
static void ruffleSort(int a[]) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < n; i++) {
int oi = r.nextInt(n);
int temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long a[]) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < n; i++) {
int oi = r.nextInt(n);
long temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 1f3b0611394714e0a9b427cd40c0755c | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Good01String {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static String next() throws IOException {
while(st == null||!st.hasMoreTokens()){
st = new StringTokenizer(br.readLine().trim());
}
return st.nextToken();
}
static int readInt() throws IOException{
return Integer.parseInt(next());
}
public static void main(String[] args) throws IOException {
int T = readInt(), n, ans, segment, n1, n2, lastS;
while (T-->0){
n = readInt()/2;
ans = 0;
lastS = -1;
segment = 0;
for (int i = 0; i < n; i++) {
n1 = br.read(); n2 = br.read();
if (n1!=n2)ans++;
else if (n1!=lastS){
segment++;
lastS = n1;
}
}segment = Math.max(segment, 1);
System.out.println(ans+" "+segment);
}
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | ccbf5d8de6f1c1203102b59ed5713def | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
FastReader fr=new FastReader();
PrintWriter pw=new PrintWriter(System.out);
int tt=fr.nextInt();
while(tt-->0){
int n=fr.nextInt();
String str=fr.next();
int [] arr=new int[n/2];
int count=0,idx=0;
boolean flag=true;
for(int i=0;i<n;i+=2){
if(str.charAt(i)==str.charAt(i+1)) {
arr[i/2]=str.charAt(i)-'0';
idx=i/2;
flag=false;
}
else {
arr[i / 2] = -1;
count++;
}
}
if(flag)
pw.println((n/2)+" "+1);
else {
int diff=1;
for (int i = idx - 1; i >= 0; i--) {
if (arr[i] == -1)
arr[i] = arr[i + 1];
if(arr[i]!=arr[i+1])
diff++;
}
for(int i=idx+1;i<n/2;i++){
if(arr[i]==-1)
arr[i]=arr[i-1];
if(arr[i]!=arr[i-1])
diff++;
}
pw.println(count+" "+diff);
}
}
pw.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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 852ded1dbacbafe37ba4c379ea31846d | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static int mod = (int)1e9+7;
static boolean[] prime = new boolean[1];
static int[][] dir1 = new int[][] {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
static int[][] dir2 = new int[][] {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
static {
for (int i = 2; i < prime.length; i++)
prime[i] = true;
for (int i = 2; i < prime.length; i++) {
if (prime[i]) {
for (int k = 2; i * k < prime.length; k++) {
prime[i * k] = false;
}
}
}
}
static class JoinSet {
int[] fa;
JoinSet(int n) {
fa = new int[n];
for (int i = 0; i < n; i++)
fa[i] = i;
}
int find(int t) {
if (t != fa[t])
fa[t] = find(fa[t]);
return fa[t];
}
void join(int x, int y) {
x = find(x);
y = find(y);
fa[x] = y;
}
}
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static int get() throws Exception {
String ss = bf.readLine();
if (ss.contains(" "))
ss = ss.substring(0, ss.indexOf(" "));
return Integer.parseInt(ss);
}
static long getx() throws Exception {
String ss = bf.readLine();
if (ss.contains(" "))
ss = ss.substring(0, ss.indexOf(" "));
return Long.parseLong(ss);
}
static int[] getint() throws Exception {
String[] s = bf.readLine().split(" ");
int[] a = new int[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(s[i]);
}
return a;
}
static long[] getlong() throws Exception {
String[] s = bf.readLine().split(" ");
long[] a = new long[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Long.parseLong(s[i]);
}
return a;
}
static String getstr() throws Exception {
return bf.readLine();
}
static void println() throws Exception {
bw.write("\n");
}
static void print(int a) throws Exception {
bw.write(a + "\n");
}
static void print(long a) throws Exception {
bw.write(a + "\n");
}
static void print(String a) throws Exception {
bw.write(a + "\n");
}
static void print(int[] a) throws Exception {
for (int i : a) {
bw.write(i + " ");
}
println();
}
static void print(long[] a) throws Exception {
for (long i : a) {
bw.write(i + " ");
}
println();
}
static void print(int[][] a) throws Exception {
for (int i[] : a)
print(i);
}
static void print(long[][] a) throws Exception {
for (long[] i : a)
print(i);
}
static void print(char[] a) throws Exception {
for (char i : a) {
bw.write(i + "");
}
println();
}
static long pow(long a, long b) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans *= a;
}
a *= a;
b >>= 1;
}
return ans;
}
static int powmod(long a, long b, int mod) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return (int)ans;
}
static void sort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[i];
}
static void sort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[i];
}
static void resort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[n - 1 - i];
}
static void resort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[n - 1 - i];
}
static int max(int a, int b) {
return Math.max(a, b);
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long max(long a, long b) {
return Math.max(a, b);
}
static long min(long a, long b) {
return Math.min(a, b);
}
static int max(int[] a) {
int max = a[0];
for (int i : a)
max = max(max, i);
return max;
}
static int min(int[] a) {
int min = a[0];
for (int i : a)
min = min(min, i);
return min;
}
static long max(long[] a) {
long max = a[0];
for (long i : a)
max = max(max, i);
return max;
}
static long min(long[] a) {
long min = a[0];
for (long i : a)
min = min(min, i);
return min;
}
static int abs(int a) {
return Math.abs(a);
}
static long abs(long a) {
return Math.abs(a);
}
static void yes() throws Exception {
print("Yes");
}
static void no() throws Exception {
print("No");
}
static int[] getarr(List<Integer> list) {
int n = list.size();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = list.get(i);
return a;
}
public static void main(String[] args) throws Exception {
int t = get();
while (t-- > 0){
int n = get();
String s = getstr();
int ans = 0, min = 1, cur = -1;
for(int i = 0;i < n;i += 2){
if(s.charAt(i) != s.charAt(i+1)) ans ++;
else if(s.charAt(i)== '0'){
if(cur == 1) min++;
cur = 0;
} else{
if(cur == 0) min++;
cur = 1;
}
}
print(ans+" "+min);
}
bw.flush();
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 8c1b9ce73d9ecc605c9b6a08374bafe2 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | ///package solution;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Solution implements Runnable {
public void solve() throws Exception {
int testCase = sc.nextInt();
while (testCase-- > 0) {
int n = sc.nextInt();
String str = in.readLine();
int ans = 0, mn = 0;
char prev = '*';
for (int i = 0; i < n; i+=2) {
if (str.charAt(i) != str.charAt(i+1)) {
ans++;
} else {
if (str.charAt(i) != prev) {
prev = str.charAt(i);
mn++;
}
}
}
if (mn == 0) {
mn = 1;
}
out.println(ans + " " + mn);
}
}
static Throwable uncaught;
BufferedReader in;
FastScanner sc;
PrintWriter out;
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
sc = new FastScanner(in);
solve();
} catch (Throwable uncaught) {
Solution.uncaught = uncaught;
} finally {
out.close();
}
}
public static void main(String[] args) throws Throwable {
Thread thread = new Thread(null, new Solution(), "", (1 << 26));
thread.start();
thread.join();
if (Solution.uncaught != null) {
throw Solution.uncaught;
}
}
}
class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | cd3089471f71caef1f7e0fe790a653fb | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 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();
String s = fs.next();
int ans = 0;
char prev = 0;
int cnt = 1;
for (int i = 0; i < n; i+=2) {
if (s.charAt(i) == s.charAt(i+1)) {
if (prev == (s.charAt(i) ^ 1)) cnt++;
prev = s.charAt(i);
} else ans++;
}
sb.append(ans + " " + cnt + "\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 | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 053cb72cab77b67a29ac35a5105f6da1 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static long mod = (int)1e9+7;
// static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
int t=sc.nextInt();
// int t=1;
while(t-->0)
{
int n = sc.nextInt();
char arr[] = sc.next().toCharArray();
ArrayList<Pair> list = new ArrayList<>();
char curr = arr[0];
int count = 0;
for(int i=0;i<n;i++)
{
if(curr != arr[i])
{
list.add(new Pair(count , curr));
count = 1;
curr = arr[i];
}
else
{
count++;
}
}
list.add(new Pair(count , curr));
boolean flag = true;
for(Pair x:list)
{
if(x.first%2==1)
{
flag = false;
break;
}
}
if(flag)
{
System.out.println(0+" "+list.size());
continue;
}
for(int i=list.size()-1;i>=1;i--)
{
if(list.get(i).first%2==1)
{
list.get(i).first -= 1;
list.get(i - 1).first += 1;
}
}
int index = 0;
char brr[] = new char[n];
for(int i=0;i<list.size();i++)
{
if(list.get(i).second == '1')
{
for(int j=0;j<list.get(i).first;j++)
{
brr[index++] = '1';
}
}
else
{
for(int j=0;j<list.get(i).first;j++)
{
brr[index++] = '0';
}
}
}
/*for(char x:brr)
{
System.out.print(x);
}
System.out.println();*/
int ans = 0;
for(int i=0;i<n;i++)
{
if(arr[i] != brr[i])
{
ans++;
}
}
char possible = '*';
int seg = 0;
for(int i=0;i<n-1;i+=2)
{
if(arr[i] == arr[i + 1])
{
if(possible != arr[i])
{
seg++;
}
possible = arr[i];
}
}
if(seg == 0)
{
seg++;
}
System.out.println(ans+" "+seg);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
static void my_sort(long[] arr)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list);
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static void reverse_sorted(int[] arr)
{
ArrayList<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);
}
}
static int LowerBound(int a[], int x) { // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value
int l=-1,r=list.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(list.get(m)<=x) l=m;
else r=m;
}
return l+1;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static class Queue_Pair implements Comparable<Queue_Pair> {
int first , second;
public Queue_Pair(int first, int second) {
this.first=first;
this.second=second;
}
public int compareTo(Queue_Pair o) {
return Integer.compare(o.first, first);
}
}
static void leftRotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
static void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n-1] = temp;
}
static boolean isPalindrome(String str)
{
// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;
// While there are characters to compare
while (i < j) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
static boolean palindrome_array(char arr[], int n)
{
// Initialise flag to zero.
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
return false;
else
return true;
}
static boolean allElementsEqual(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
static boolean allElementsDistinct(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]!=arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
public static void reverse_Long(long[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
long temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
static boolean isSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static boolean isReverseSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] < a[i + 1]) {
return false;
}
}
return true;
}
static int[] rearrangeEvenAndOdd(int arr[], int n)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
int[] array = list.stream().mapToInt(i->i).toArray();
return array;
}
static long[] rearrangeEvenAndOddLong(long arr[], int n)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
long[] array = list.stream().mapToLong(i->i).toArray();
return array;
}
static boolean isPrime(long n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (long i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static long getSum(long n)
{
long sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcdLong(long a, long b)
{
if (b == 0)
return a;
return gcdLong(b, a % b);
}
static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static int countDigit(int n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
}
class Pair
{
int first;
char second;
Pair(int first , char second)
{
this.first = first;
this.second = second;
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | e81a05082feca350cd734cf06ad75b5e | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.*;
public class E {
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o2.getValue()).compareTo(o1.getValue());
}
});
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static int binarySearch(ArrayList<Integer> arr, int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr.get(m) == x)
return m;
// If x greater, ignore left half
if (arr.get(m) < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// if we reach here, then element was
// not present
return l;
}
static int binarySearch1(ArrayList<Integer> arr, int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr.get(m) == x)
return m;
// If x greater, ignore left half
if (arr.get(m) < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// if we reach here, then element was
// not present
return l-1;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
String s = sc.next();
int ans = 0,count = 1;
String st = "";
for(int i=0;i<s.length()-1;i+=2) {
if(s.charAt(i)!=s.charAt(i+1))
ans++;
else
st+=s.charAt(i);
}
for(int i=0;i<st.length()-1;i++) {
if(st.charAt(i)!=st.charAt(i+1))
count++;
}
sb.append(ans+" "+count+"\n");
}
System.out.print(sb.toString());
sc.close();
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 3060abc1daa022a301cfdec472708a0f | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.*;
import java.io.*;
public class TokitsukazeAndGood01StringHardVersion{
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t=sc.nextInt();
while (t-->0){
int n=sc.nextInt(),ans=0,sub=1,rem=-1;
String s=sc.next();
for(int i=0;i<n-1;i+=2){
if(s.charAt(i)!=s.charAt(i+1)){
ans++;
}else{
if(rem==-1)rem=s.charAt(i)-'0';
else if(rem!=s.charAt(i)-'0'){
sub++;
rem=s.charAt(i)-'0';
}
}
}
out.println(ans+" "+sub);
}
out.flush();
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int 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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | dc210f7fde7b0b142ed0072833663486 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | // import static java.lang.System.out;
import static java.lang.Math.*;
import static java.lang.System.*;
import java.lang.reflect.Array;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
static FastReader sc;
static long mod = ((long) 1e9) + 7;
static long lmax=Long.MAX_VALUE;
static long lmin=Long.MIN_VALUE;
static int imax=Integer.MAX_VALUE;
static int imin=Integer.MIN_VALUE;
// static FastWriter out;
static FastWriter out;
public static void main(String hi[]) throws IOException {
// initializeIO();
out = new FastWriter();
sc = new FastReader();
long startTimeProg = System.currentTimeMillis();
long endTimeProg = Long.MAX_VALUE;
int t = sc.nextInt();
// int t=1;
// boolean[] seave=sieveOfEratosthenes((int)(1e5));
// int[] seave2=sieveOfEratosthenesInt((long)sqrt(1e9));
// debug(seave2);
while (t-- != 0) {
int n=sc.nextInt();
String s=sc.next();
char[] a=s.toCharArray();
int last_num=-1;
int sum=0,cnt=0;
for (int i=0;i<n;i+=2)
{
if (a[i]==a[i+1])
{
if (a[i]-'0' != last_num)
{
last_num = a[i]-'0';
sum++;
}
}
else
{
cnt++;
}
}
print(cnt+" "+max(sum,1));
}
endTimeProg = System.currentTimeMillis();
debug("[finished : " + (endTimeProg - startTimeProg) + ".ms ]");
// System.out.println(String.format("%.9f", max));
}
private static boolean isPeak(int[] arr,int i,int l,int r){
if(i==l||i==r)return false;
return (arr[i+1]<arr[i]&&arr[i]>arr[i-1]);
}
private static boolean isPeak(long[] arr,int i,int l,int r){
if(i==l||i==r)return false;
return (arr[i+1]<arr[i]&&arr[i]>arr[i-1]);
}
private static long kadens(List<Long> li, int l, int r) {
long max = Long.MIN_VALUE;
long ans = 0;
for (int i = l; i <= r; i++) {
ans += li.get(i);
max = max(ans, max);
if (ans < 0) ans = 0;
}
return max;
}
public static boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
double d = Double.parseDouble(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
private static boolean isSorted(List<Integer> li) {
int n = li.size();
if (n <= 1) return true;
for (int i = 0; i < n - 1; i++) {
if (li.get(i) > li.get(i + 1)) return false;
}
return true;
}
static boolean isPowerOfTwo(long x) {
return x != 0 && ((x & (x - 1)) == 0);
}
private static boolean ispallindromeList(List<Integer> res) {
int l = 0, r = res.size() - 1;
while (l < r) {
if (res.get(l) != res.get(r)) return false;
l++;
r--;
}
return true;
}
private static class Pair {
int first = 0;
int sec = 0;
int[] arr;
char ch;
String s;
Map<Integer, Integer> map;
Pair(int first, int sec) {
this.first = first;
this.sec = sec;
}
Pair(int[] arr) {
this.map = new HashMap<>();
for (int x : arr) this.map.put(x, map.getOrDefault(x, 0) + 1);
this.arr = arr;
}
Pair(char ch, int first) {
this.ch = ch;
this.first = first;
}
Pair(String s, int first) {
this.s = s;
this.first = first;
}
}
private static int sizeOfSubstring(int st, int e) {
int s = e - st + 1;
return (s * (s + 1)) / 2;
}
private static Set<Long> factors(long n) {
Set<Long> res = new HashSet<>();
// res.add(n);
for (long i = 1; i * i <= (n); i++) {
if (n % i == 0) {
res.add(i);
if (n / i != i) {
res.add(n / i);
}
}
}
return res;
}
private static long fact(long n) {
if (n <= 2) return n;
return n * fact(n - 1);
}
private static long ncr(long n, long r) {
return fact(n) / (fact(r) * fact(n - r));
}
private static int lessThen(long[] nums, long val, boolean work, int l, int r) {
int i = -1;
if (work) i = 0;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums[mid] <= val) {
i = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return i;
}
private static int lessThen(List<Long> nums, long val, boolean work, int l, int r) {
int i = -1;
if (work) i = 0;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums.get(mid) <= val) {
i = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return i;
}
private static int lessThen(List<Integer> nums, int val, boolean work, int l, int r) {
int i = -1;
if (work) i = 0;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums.get(mid) <= val) {
i = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return i;
}
private static int greaterThen(List<Long> nums, long val, boolean work, int l, int r) {
int i = -1;
if (work) i = r;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums.get(mid) >= val) {
i = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return i;
}
private static int greaterThen(List<Integer> nums, int val, boolean work) {
int i = -1, l = 0, r = nums.size() - 1;
if (work) i = r;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums.get(mid) >= val) {
i = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return i;
}
private static int greaterThen(long[] nums, long val, boolean work, int l, int r) {
int i = -1;
if (work) i = r;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums[mid] >= val) {
i = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return i;
}
private static long gcd(long[] arr) {
long ans = 0;
for (long x : arr) {
ans = gcd(x, ans);
}
return ans;
}
private static int gcd(int[] arr) {
int ans = 0;
for (int x : arr) {
ans = gcd(x, ans);
}
return ans;
}
private static long sumOfAp(long a, long n, long d) {
long val = (n * (2 * a + ((n - 1) * d)));
return val / 2;
}
//geometrics
private static double areaOftriangle(double x1, double y1, double x2, double y2, double x3, double y3) {
double[] mid_point = midOfaLine(x1, y1, x2, y2);
// debug(Arrays.toString(mid_point)+" "+x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+" "+y3);
double height = distanceBetweenPoints(mid_point[0], mid_point[1], x3, y3);
double wight = distanceBetweenPoints(x1, y1, x2, y2);
// debug(height+" "+wight);
return (height * wight) / 2;
}
private static double distanceBetweenPoints(double x1, double y1, double x2, double y2) {
double x = x2 - x1;
double y = y2 - y1;
return (Math.pow(x, 2) + Math.pow(y, 2));
}
public static boolean isPerfectSquareByUsingSqrt(long n) {
if (n <= 0) {
return false;
}
double squareRoot = Math.sqrt(n);
long tst = (long) (squareRoot + 0.5);
return tst * tst == n;
}
private static double[] midOfaLine(double x1, double y1, double x2, double y2) {
double[] mid = new double[2];
mid[0] = (x1 + x2) / 2;
mid[1] = (y1 + y2) / 2;
return mid;
}
private static long sumOfN(long n) {
return (n * (n + 1)) / 2;
}
private static long power(long x, long y, long p) {
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0) {
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
/* Function to calculate x raised to the power y in O(logn)*/
static long power(long x, long y) {
long temp;
if (y == 0)
return 1l;
temp = power(x, y / 2);
if (y % 2 == 0)
return (temp * temp);
else
return (x * temp * temp);
}
private static StringBuilder reverseString(String s) {
StringBuilder sb = new StringBuilder(s);
int l = 0, r = sb.length() - 1;
while (l <= r) {
char ch = sb.charAt(l);
sb.setCharAt(l, sb.charAt(r));
sb.setCharAt(r, ch);
l++;
r--;
}
return sb;
}
private static void swap(List<Integer> li, int i, int j) {
int t = li.get(i);
li.set(i, li.get(j));
li.set(j, t);
}
static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static String decimalToString(int x) {
return Integer.toBinaryString(x);
}
private static String decimalToString(long x) {
return Long.toBinaryString(x);
}
private static boolean isPallindrome(String s, int l, int r) {
while (l < r) {
if (s.charAt(l) != s.charAt(r)) return false;
l++;
r--;
}
return true;
}
private static boolean isSubsequence(String s, String t) {
if (s == null || t == null) return false;
Map<Character, List<Integer>> map = new HashMap<>(); //<character, index>
//preprocess t
for (int i = 0; i < t.length(); i++) {
char curr = t.charAt(i);
if (!map.containsKey(curr)) {
map.put(curr, new ArrayList<Integer>());
}
map.get(curr).add(i);
}
int prev = -1; //index of previous character
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map.get(c) == null) {
return false;
} else {
List<Integer> list = map.get(c);
prev = lessThen(list, prev, false, 0, list.size() - 1);
if (prev == -1) {
return false;
}
prev++;
}
}
return true;
}
private static StringBuilder removeLeadingZero(StringBuilder sb) {
int i = 0;
while (i < sb.length() && sb.charAt(i) == '0') i++;
// debug("remove "+i);
if (i == sb.length()) return new StringBuilder();
return new StringBuilder(sb.substring(i, sb.length()));
}
private static StringBuilder removeEndingZero(StringBuilder sb) {
int i = sb.length() - 1;
while (i >= 0 && sb.charAt(i) == '0') i--;
// debug("remove "+i);
if (i < 0) return new StringBuilder();
return new StringBuilder(sb.substring(0, i + 1));
}
private static int stringToDecimal(String binaryString) {
// debug(decimalToString(n<<1));
return Integer.parseInt(binaryString, 2);
}
private static int stringToInt(String s) {
return Integer.parseInt(s);
}
private static String toString(long val) {
return String.valueOf(val);
}
private static void debug(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(long[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(List<int[]> arr) {
for (int[] a : arr) {
err.println(Arrays.toString(a));
}
}
private static void debug(float[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(double[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(boolean[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void print(String s) throws IOException {
out.println(s);
}
private static void debug(String s) throws IOException {
err.println(s);
}
private static int charToIntS(char c) {
return ((((int) (c - '0')) % 48));
}
private static void print(double s) throws IOException {
out.println(s);
}
private static void debug(char[] s1) throws IOException {
debug(Arrays.toString(s1));
}
private static void print(float s) throws IOException {
out.println(s);
}
private static void print(long s) throws IOException {
out.println(s);
}
private static void print(int s) throws IOException {
out.println(s);
}
private static void debug(double s) throws IOException {
err.println(s);
}
private static void debug(float s) throws IOException {
err.println(s);
}
private static void debug(long s) {
err.println(s);
}
private static void debug(int s) {
err.println(s);
}
private static boolean isPrime(long n) {
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return false;
}
return true;
}
private static List<List<Integer>> readUndirectedGraph(int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
graph.get(x).add(y);
graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readUndirectedGraph(int[][] intervals, int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < intervals.length; i++) {
int x = intervals[i][0];
int y = intervals[i][1];
graph.get(x).add(y);
graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readDirectedGraph(int[][] intervals, int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < intervals.length; i++) {
int x = intervals[i][0];
int y = intervals[i][1];
graph.get(x).add(y);
// graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readDirectedGraph(int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
graph.get(x).add(y);
// graph.get(y).add(x);
}
return graph;
}
static String[] readStringArray(int n) {
String[] arr = new String[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.next();
}
return arr;
}
private static Map<Character, Integer> freq(String s) {
Map<Character, Integer> map = new HashMap<>();
for (char c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
return map;
}
private static Map<Long, Integer> freq(long[] arr) {
Map<Long, Integer> map = new HashMap<>();
for (long x : arr) {
map.put(x, map.getOrDefault(x, 0) + 1);
}
return map;
}
private static Map<Integer, Integer> freq(int[] arr) {
Map<Integer, Integer> map = new HashMap<>();
for (int x : arr) {
map.put(x, map.getOrDefault(x, 0) + 1);
}
return map;
}
static boolean[] sieveOfEratosthenes(long n) {
boolean prime[] = new boolean[(int) n + 1];
for (int i = 2; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
static int[] sieveOfEratosthenesInt(long n) {
boolean prime[] = new boolean[(int) n + 1];
Set<Integer> li = new HashSet<>();
for (int i = 1; i <= n; i++) {
prime[i] = true;
li.add(i);
}
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p) {
li.remove(i);
prime[i] = false;
}
}
}
int[] arr = new int[li.size()];
int i = 0;
for (int x : li) {
arr[i++] = x;
}
return arr;
}
public static long Kadens(List<Long> prices) {
long sofar = 0;
long max_v = 0;
for (int i = 0; i < prices.size(); i++) {
sofar += prices.get(i);
if (sofar < 0) {
sofar = 0;
}
max_v = Math.max(max_v, sofar);
}
return max_v;
}
public static int Kadens(int[] prices) {
int sofar = 0;
int max_v = 0;
for (int i = 0; i < prices.length; i++) {
sofar += prices[i];
if (sofar < 0) {
sofar = 0;
}
max_v = Math.max(max_v, sofar);
}
return max_v;
}
static boolean isMemberAC(int a, int d, int x) {
// If difference is 0, then x must
// be same as a.
if (d == 0)
return (x == a);
// Else difference between x and a
// must be divisible by d.
return ((x - a) % d == 0 && (x - a) / d >= 0);
}
static boolean isMemberAC(long a, long d, long x) {
// If difference is 0, then x must
// be same as a.
if (d == 0)
return (x == a);
// Else difference between x and a
// must be divisible by d.
return ((x - a) % d == 0 && (x - a) / d >= 0);
}
private static void sort(int[] arr) {
int n = arr.length;
List<Integer> li = new ArrayList<>();
for (int x : arr) {
li.add(x);
}
Collections.sort(li);
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sortReverse(int[] arr) {
int n = arr.length;
List<Integer> li = new ArrayList<>();
for (int x : arr) {
li.add(x);
}
Collections.sort(li, Collections.reverseOrder());
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sort(double[] arr) {
int n = arr.length;
List<Double> li = new ArrayList<>();
for (double x : arr) {
li.add(x);
}
Collections.sort(li);
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sortReverse(double[] arr) {
int n = arr.length;
List<Double> li = new ArrayList<>();
for (double x : arr) {
li.add(x);
}
Collections.sort(li, Collections.reverseOrder());
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sortReverse(long[] arr) {
int n = arr.length;
List<Long> li = new ArrayList<>();
for (long x : arr) {
li.add(x);
}
Collections.sort(li, Collections.reverseOrder());
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sort(long[] arr) {
int n = arr.length;
List<Long> li = new ArrayList<>();
for (long x : arr) {
li.add(x);
}
Collections.sort(li);
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static long sum(int[] arr) {
long sum = 0;
for (int x : arr) {
sum += x;
}
return sum;
}
private static long sum(long[] arr) {
long sum = 0;
for (long x : arr) {
sum += x;
}
return sum;
}
private static long evenSumFibo(long n) {
long l1 = 0, l2 = 2;
long sum = 0;
while (l2 < n) {
long l3 = (4 * l2) + l1;
sum += l2;
if (l3 > n) break;
l1 = l2;
l2 = l3;
}
return sum;
}
private static void initializeIO() {
try {
System.setIn(new FileInputStream("input"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
System.setErr(new PrintStream(new FileOutputStream("error.txt")));
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
private static int maxOfArray(int[] arr) {
int max = Integer.MIN_VALUE;
for (int x : arr) {
max = Math.max(max, x);
}
return max;
}
private static long maxOfArray(long[] arr) {
long max = Long.MIN_VALUE;
for (long x : arr) {
max = Math.max(max, x);
}
return max;
}
private static int[][] readIntIntervals(int n, int m) {
int[][] arr = new int[n][m];
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++) {
arr[j][i] = sc.nextInt();
}
}
return arr;
}
private static long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
private static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
private static int[] readIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
return arr;
}
private static double[] readDoubleArray(int n) {
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextDouble();
}
return arr;
}
private static long[] readLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
}
return arr;
}
private static void print(int[] arr) throws IOException {
out.println(Arrays.toString(arr));
}
private static void print(long[] arr) throws IOException {
out.println(Arrays.toString(arr));
}
private static void print(String[] arr) throws IOException {
out.println(Arrays.toString(arr));
}
private static void print(double[] arr) throws IOException {
out.println(Arrays.toString(arr));
}
private static void debug(String[] arr) {
err.println(Arrays.toString(arr));
}
private static void debug(Boolean[][] arr) {
for (int i = 0; i < arr.length; i++) err.println(Arrays.toString(arr[i]));
}
private static void debug(int[] arr) {
err.println(Arrays.toString(arr));
}
private static void debug(long[] arr) {
err.println(Arrays.toString(arr));
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
BufferedWriter bw;
List<String> list = new ArrayList<>();
Set<String> set = new HashSet<>();
FastWriter() {
bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
<T> void print(T obj) throws IOException {
bw.write(obj.toString());
bw.flush();
}
void println() throws IOException {
print("\n");
}
<T> void println(T obj) throws IOException {
print(obj.toString() + "\n");
}
void print(int[] arr) throws IOException {
for (int x : arr) {
print(x + " ");
}
println();
}
void print(long[] arr) throws IOException {
for (long x : arr) {
print(x + " ");
}
println();
}
void print(double[] arr) throws IOException {
for (double x : arr) {
print(x + " ");
}
println();
}
void printCharN(char c, int n) throws IOException {
for (int i = 0; i < n; i++) {
print(c);
}
}
}
static class Dsu {
int[] parent, size;
Dsu(int n) {
parent = new int[n + 1];
size = new int[n + 1];
for (int i = 0; i <= n; i++) {
parent[i] = i;
size[i] = 1;
}
}
private int findParent(int u) {
if (parent[u] == u) return u;
return parent[u] = findParent(parent[u]);
}
private boolean union(int u, int v) {
// System.out.println("uf "+u+" "+v);
int pu = findParent(u);
// System.out.println("uf2 "+pu+" "+v);
int pv = findParent(v);
// System.out.println("uf3 " + u + " " + pv);
if (pu == pv) return false;
if (size[pu] <= size[pv]) {
parent[pu] = pv;
size[pv] += size[pu];
} else {
parent[pv] = pu;
size[pu] += size[pv];
}
return true;
}
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 27d4e813c6ed1eb54071c384d7b4bc7d | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
in = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
try {
int t = in.nextInt(); while(t-- > 0) { solve(); out.println();}
// solve();
} finally {
out.close();
}
return;
}
public static void solve() {
int n = in.nextInt();
char[] s = fillArray();
int ans = 0;
int cnt = 0;
char prev = 0;
for(int i = 0; i + 1 < n; i +=2) {
if(s[i] != s[i+1]) {
ans++;
if(prev == 0) {
prev = 'A';
}
continue;
}
if(prev != s[i]) {
prev = s[i];
cnt++;
}
}
if(prev == 'A') {
cnt = 1;
}
out.print(ans + " " + cnt);
}
//-------------- Helper methods-------------------
public static int[] fillArray(int n) {
int[] array = new int[n];
for(int i = 0; i < n; i++) {
array[i] = in.nextInt();
}
return array;
}
public static char[] fillArray() {
char[] array = in.next().toCharArray();
return array;
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
public static MyScanner in;
//-----------MyScanner class for faster input----------x§x
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;
}
}
static void shuffleArray(int[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
//--------------------------------------------------------
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 3837c353be6e201d1df59a6125e2b2b3 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class codeforces_789_B2 {
private static void solve(FastIOAdapter in, PrintWriter out) {
int n = in.nextInt();
char[] s = in.next().toCharArray();
int moves = 0;
int ranges = 0;
char prevRangeChar = '?';
for (int i = 0; i < n; i += 2) {
if (s[i] != s[i + 1]) moves++;
else {
if (s[i] != prevRangeChar) ranges++;
prevRangeChar = s[i];
}
}
out.println(moves + " " + Math.max(1, ranges));
}
public static void main(String[] args) throws Exception {
try (FastIOAdapter ioAdapter = new FastIOAdapter()) {
int count = 1;
count = ioAdapter.nextInt();
while (count-- > 0) {
solve(ioAdapter, ioAdapter.out);
}
}
}
static void ruffleSort(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
static class FastIOAdapter implements AutoCloseable {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out))));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
@Override
public void close() throws Exception {
out.flush();
out.close();
br.close();
}
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 8cf89f2ed250a79f5b185ccdb7995d97 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | //package com.company;
import java.math.BigInteger;
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static FastScanner fs = new FastScanner();
public static Scanner sc = new Scanner(System.in);
public static PrintWriter out = new PrintWriter(System.out);
public static int inf = 1000000007;
public static BigInteger infb = new BigInteger("1000000007");
public static long lmax= (long) 1e18;
public static boolean flag=false;
public static StringBuilder sb=new StringBuilder();
/////// For printing elements in 1D-array
public static void print1d(long[] arr) {
for (long x : arr) {
out.print(x + " ");
}
out.println();
}
/////// For printing elements in 2D-array
public static void print2d(long[][] arr) {
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[0].length;j++){
out.print(arr[i][j]+" ");
}
out.println();
}
out.println();
}
/////// For freq of elements in array
public static long[] freq(long[] freq_arr, long[] arr) {
for (long j : arr) {
freq_arr[(int) j]++;
}
return freq_arr;
}
/////// For sum elements in array
public static long sum(long[] arr) {
long sum = 0;
for (int i=0;i<arr.length;i++) {
sum += arr[i];
}
return sum;
}
/////// For storing elements in array 1D
public static long[] scan1d(long[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] = fs.nextLong();
}
return arr;
}
/////// For storing elements in array 2D
public static long[][] scan2d(long[][] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i][0] = fs.nextLong();
arr[i][1] = fs.nextLong();
}
return arr;
}
/////// For copying elements in array
public static long[] copy_arr(long[] arr, long[] arr1) {
for (int i = 0; i < arr.length; i++) {
arr1[i] = arr[i];
}
return arr;
}
/////// For GCD
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
/// gcd of array
static long gcd_arr(long arr[], long n)
{
long result = 0;
for (long element: arr){
result = gcd(result, element);
if(result == 1)
{
return 1;
}
}
return result;
}
//////// Forprime
public static boolean isPrime(long n) {
if (n < 2)
return false;
if (n == 2 || n == 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
long sqrtN = (long) Math.sqrt(n) + 1;
for (long i = 6L; i <= sqrtN; i += 6) {
if (n % (i - 1) == 0 || n % (i + 1) == 0)
return false;
}
return true;
}
//////Using array as Set
public static boolean[] arr_set(boolean[] arr, int n) {
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
arr[x] = true;
}
return arr;
}
/// Fidning min in array
public static long min_arr(long[] arr,int l,int r){
long min=arr[l];
for(int i=l;i<r;i++){
min=Math.min(min,arr[i]);
}
return min;
}
/// Fidning max in array
public static long max_arr(long[] arr,int l,int r){
long max=arr[l];
for(int i=l;i<r;i++){
max=Math.max(max,arr[i]);
}
return max;
}
///prefix sum
public static long[] pre_sum(long[] arr){
long[] prefix_sum=new long[arr.length];
prefix_sum[0]=arr[0];
for(int i=1;i<arr.length;i++){
prefix_sum[i]=prefix_sum[i-1]+arr[i];
}
return prefix_sum;
}
///sorted_arr
public static boolean sorted_arr(long[] arr,long[] arr_sorted){
for(int i=0;i<arr.length;i++){
if(arr[i]!=arr_sorted[i]){
return false;
}
}
return true;
}
public static boolean sorted_arr_increasing(long ar[],long n){
for(long i=0;i<n-1;i++){
if(ar[(int) i]>ar[(int) (i+1)])
return false;
}
return true;
}
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[] readArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
public static long fact(long n) {
long fact = 1;
for (long i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
public static long nCr(long n, long r) {
long res = 1;
for (long i = 0; i < r; i++) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
public static long pCr(long n, long r)
{
return fact(n) / (fact(n-r));
}
static boolean isSubSequence(String str1, String strm,
int m, int n)
{
int j = 0;
for (int i = 0; i < n && j < m; i++)
if (str1.charAt(j) == strm.charAt(i))
j++;
return (j == m);
}
public static boolean cmp(int a, int b)
{
return a > b;
}
static boolean palindrome(String x){
String s="";
for(int i=x.length()-1;i>=0;i--){
s+=x.charAt(i);
}
if(s.equals(x)){
return true;
}
return false;
}
public static class pair {
long a;
long b;
pair(long a, long b){
this.a=a;
this.b=b;
}
}
static String toBinary(int x, int len)
{
if (len > 0)
{
return String.format("%" + len + "s",
Integer.toBinaryString(x)).replaceAll(" ", "0");
}
return null;
}
static long maxSubArraySum(long[] a)
{
int size = a.length;
long max_so_far = Long.MIN_VALUE, max_ending_here = 0;
for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
public static List<Integer> primeNumbers = new LinkedList<>();
public static void primeChecker(int n) {
boolean prime[] = new boolean[n + 1];
Arrays.fill(prime, true);
for (int p = 2; p * p <= n; p++) {
if (prime[p]) {
for (int i = p * 2; i <= n; i += p) {
prime[i] = false;
}
}
}
for (int i = 2; i <= n; i++) {
if (prime[i]) {
primeNumbers.add(i);
}
}
}
public static void main(String[] args) throws IOException {
// (fact(n)/(fact(i)*fact(n-i)))
// for(Map.Entry<String,Integer> entry : tree Map.entrySet()) {
// String key = entry.getKey();
// Integer value = entry.getValue();
//
// }
//File in = new File("input.txt");
//Writer wr= new FileWriter("output.txt");
//BigInteger n = new BigInteger(String.valueOf(sc.nextInt()));
//StringBuilder sb= new StringBuilder();
//Arrays.sort(myArr, (a, b) -> a[0] - b[0]); for index at 0
long t=fs.nextLong();
while (t-->0){
long n=fs.nextLong();
String s=fs.next();
char[] arr=s.toCharArray();
char cur='1';
for (int i=0;i<n;i+=2){
if (arr[i]==arr[i+1]) {
cur=arr[i+1];
break;
}
}
long ans=0;
for (int i=0;i<n;i+=2){
if (arr[i]==arr[i+1]) {
cur=arr[i+1];
}
else {
ans++;
arr[i]=cur;
arr[i+1]=cur;
}
}
cur='b';
long c=0;
for (int i=0;i<n;i++){
if (arr[i]!=cur){
c++;
cur=arr[i];
}
}
out.println(ans+" "+c);
}
out.flush();
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | d90a0aa3a0bf6df614728df64797f0eb | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0)
{
int n = sc.nextInt();
sc.nextLine();
String s = sc.nextLine();
int i = 0;
int last = -1;
long ans = 0;
int curr = 1;
while(i < n)
{
int j = i;
while(j+1 < n && s.charAt(j) == s.charAt(j+1))
{
j++;
}
int len = j-i+1;
if(len%2 == 1)
{
if(last == -1)
last = curr;
else
{
ans += curr-last;
last = -1;
}
}
i = j+1;
curr++;
}
int c = 0;
// int l = n/2;
// int a[] = new int[l];
int tot = 1;
last = -1;
for( i = 0 ; i < n ; i += 2)
{
if(s.charAt(i) != s.charAt(i+1))
c++;
else
{
if(last == -1)
{
last = s.charAt(i)-'0';
}
else
{
int xx = s.charAt(i)-'0';
if(xx != last)
{
tot++;
last = xx;
}
}
}
}
// System.out.println(ans + " " + tot + " " + c);
System.out.println(ans + " " + (tot+c-ans));
}
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | e41f78cb8acfcf0bb3d0753088885455 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.*;
import java.io.*;
//need to be careful about negative sum resulting negative mod.
public class Solution {
static long mod = 1000000007;
static long inv(long a, long b) {return 1 < a ? b - inv(b % a, a) * b / a : 1;}
static long mi(long a) {return inv(a, mod);}
static InputReader sc = new InputReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
//setUp("input.txt", "output.txt");
//setUp("maxcross.in", "maxcross.out");
int T = 1; T = sc.nextInt();
while(T-- > 0){
int n = sc.nextInt();
String s = sc.next();
int[] c = new int[n];
for(int i = 0 ; i < s.length(); i++){
if(s.charAt(i) == '1') c[i] = 1;
}
int num = 0;
int count = 1;
for(int i = 1; i < n; i++){
if(c[i - 1] != c[i]){
if(count % 2 != 0){
num++;
count = 2;
}else{
count = 1;
}
}else{
count++;
}
}
int[][] dp = new int[n][2];
for(int i = n - 2; i >= 0 ; i-= 2){
if(i == n - 2){
if(c[i] == c[i + 1]){
if(c[i] == 0){
dp[i][0] = 1;
dp[i][1] = n;
}else{
dp[i][0] = n;
dp[i][1] = 1;
}
}else{
dp[i][0] = 1;
dp[i][1] = 1;
}
}else{
if(c[i] == c[i + 1]){
if(c[i] == 0){
dp[i][0] = Math.min(dp[i + 2][0], 1 + dp[i + 2][1]);
dp[i][1] = n;
}else{
dp[i][0] = n;
dp[i][1] = Math.min(1 + dp[i + 2][0], dp[i + 2][1]);
}
}else{
dp[i][0] = Math.min(dp[i + 2][0], 1 + dp[i + 2][1]);
dp[i][1] = Math.min(1 + dp[i + 2][0], dp[i + 2][1]);
}
}
}
out.println(num + " " + Math.min(dp[0][0], dp[0][1]));
}
out.flush();
out.close();
}
public static void solve(){
}
private static boolean isPalindrome(int x){
int v = 0;
int k = x;
while(k > 0){
v = v * 10 + k % 10;
k = k / 10;
}
return x == v;
}
private static int lowerBound (int A, int[] pwrOfTwos){
int lo = 0, hi = pwrOfTwos.length - 1;
while(lo < hi){
int mid = lo + (hi - lo) / 2;
if(mid >= A){
hi = mid;
}else{
lo = mid + 1;
}
}
return pwrOfTwos[lo] >= A ? lo : lo + 1;
}
static void setUp(String input, String output) throws IOException {
sc = new InputReader(new FileInputStream(input));
out = new PrintWriter(new BufferedWriter(new FileWriter(output)));
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.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 = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return str;
}
int[] readArray(int N){
int[] arr = new int[N];
for(int i = 0 ; i < N; i++){
arr[i] = sc.nextInt();
}
return arr;
}
long[] readLongArray(int N){
long[] arr = new long[N];
for(int i = 0 ; i < N; i++){
arr[i] = sc.nextLong();
}
return arr;
}
}
public static void shuffle(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;
}
}
public static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static class Pair<K,V> {
K key;
V val;
public Pair(K key, V val){
this.key = key;
this.val = val;
}
}
public static int gcd(int a, int b){
if(b == 0) return a;
return gcd(b, a % b);
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | a0d38adbc567ecfde473045c39d45aff | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Practise{
static boolean palindrome(String s,int i,int j) {
if(j==s.length())return false;
while(i<j) {
if(s.charAt(i)!=s.charAt(j))return false;
i++;j--;
}
return true;
}
static int valid(String s,int i) {
Stack<Character>st=new Stack<>();
int n=s.length();
return -1;
}
static int msb(long num){
int c=0;
while(num!=0) {
num>>=1;
c++;
}
return c-1;
}
static boolean isPrime(long n, int k)
{
// Corner cases
if (n <= 1 || n == 4) return false;
if (n <= 3) return true;
// Try k times
while (k > 0)
{
// Pick a random number in [2..n-2]
// Above corner cases make sure that n > 4
int a = 2 + (int)(Math.random() % (n - 4));
// Fermat's little theorem
if (Math.pow(a,n - 1) != 1)
return false;
k--;
}
return true;
}
static void swap(int a[],int i,int j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
static boolean collinear(int x1, int y1, int x2,
int y2, int x3, int y3)
{
int a = x1 * (y2 - y3) +
x2 * (y3 - y1) +
x3 * (y1 - y2);
if (a == 0)return true;
else return false;
}
public static void main(String[] args) throws ArithmeticException{
FastScanner fs=new FastScanner();
int t=fs.nextInt();
while(t-->0){
int n=fs.nextInt();
String s=fs.next();
int a[]=new int[n];
int c=0;
for(int i=1;i<n;i+=2) {
if(s.charAt(i)!=s.charAt(i-1))c++;
}
int ans=0;
char p='2';
for(int i=1;i<n;i+=2) {
if(s.charAt(i)==s.charAt(i-1) && s.charAt(i)!=p) {
ans++;
p=s.charAt(i);
}
}
if(ans==0)ans++;
System.out.println(c+" "+ans);
}
}
static boolean dfs(int src,int a[],boolean visit[],Set<Integer>hs,Map<Integer,Integer>hm) {
if(hs.contains(src))return false;
if(visit[src])return true;
visit[src]=true;
return dfs(hm.get(src),a,visit,hs,hm);
}
static BigInteger factorial(int N)
{
// Initialize result
BigInteger f
= new BigInteger("1"); // Or BigInteger.ONE
// Multiply f with 2, 3, ...N
for (int i = 2; i <= N; i++)
f = f.multiply(BigInteger.valueOf(i));
return f;
}
}
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());
}
char nextChar() {
return next().charAt(0);
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | b805ea558d0036b8cda977c911c4d11c | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
import static java.lang.Math.*;
public class Main {
static double pi = 3.141592653589;
static int mod = (int) 1e9 + 7;
public static void main(String[] args) throws IOException {
int qwe = in.nextInt();
while (qwe-- > 0)
{
solve();
}
out.close();
}
public static void solve() {
int n = in.nextInt();
char[] s = in.next().toCharArray();
char[] ans = new char[n];
int idx = 0;
int res = 0;
for (int i = 0; i < n; i+=2) {
if (s[i]==s[i+1]){
ans[idx++]=s[i];
ans[idx++]=s[i];
}else {
res++;
}
}
int res1 = 1;
for (int i = 0; i < idx-1; i++) {
if (ans[i]!=ans[i+1]){
res1++;
}
}
System.out.println(res + " "+res1);
}
/*
3
3 114514 5
*/
// static int N = 1010;
// static int n,m;
// static int INF = 0x3f3f3f3f;
// static int[][] g = new int[N][N]; // 存储每条边 邻接矩阵
// static int[] dist = new int[N]; // 存储1号点到每个点的最短距离
// static boolean[] st = new boolean[N]; // 存储每个点的最短路是否已经确定
// static void init(){
// Arrays.fill(dist, INF);
// for (int i = 0; i <= n; i++) Arrays.fill(g[i],0x3f3f3f3f);
// }
// // 求x号点到y号点的最短路,如果不存在则返回-1
// static int dijkstra(int x, int y) {
// dist[x] = 0;
// for (int i = 1; i < n; i++) {
// int t = -1; // 在还未确定最短路的点中,寻找距离最小的点
// for (int j = 1; j <= n; j++)
// if (!st[j] && (t == -1 || dist[t] > dist[j]))
// t = j;
//
// st[t] = true;
// // 用t更新其他点的距离
// for (int j = 1; j <= n; j++) dist[j] = Math.min(dist[j], dist[t] + g[t][j]);
// }
// if (dist[y] == INF) return -1;
// return dist[y];
// }
// static int N = 1000 * 26;
// static int[][] trie = new int[N][26];;
// static int[] cnt = new int[N];
// static int idx;
// static void insert_trie(String s) {
// int p = 0;
// for (int i = 0; i < s.length(); i++) {
// int u = s.charAt(i) - 'a'; //如果是大写字母,则需要改成大写A
// if (trie[p][u] == 0) trie[p][u] = ++idx;
// p = trie[p][u];
// }
// cnt[p]++;
// }
// static int search_trie(String s){
// int p = 0;
// for (int i = 0; i < s.length(); i ++ ){
// int u = s.charAt(i) - 'a';
// if (trie[p][u] == 0) return 0;
// p = trie[p][u];
// }
// return cnt[p];
// }
static void kmp(String s1, String s2) { //短字符串、长字符串
int n = s1.length(); //短字符串
int m = s2.length();
char[] p = (" " + s1).toCharArray();//短字符串
char[] s = (" " + s2).toCharArray();
// 构造ne数组
int[] ne = new int[n + 1];
for (int i = 2, j = 0; i <= n; i++) {
while (j != 0 && p[i] != p[j + 1]) j = ne[j];
if (p[i] == p[j + 1]) j++;
ne[i] = j;
}
// kmp匹配
for (int i = 1, j = 0; i <= m; i++) {
while (j != 0 && s[i] != p[j + 1]) j = ne[j];
if (s[i] == p[j + 1]) j++;
if (j == n) { // 匹配了n字符了即代表完全匹配了
out.print(i - n + " "); // 输出在s串中p出现的位置
j = ne[j]; // 完全匹配后继续搜索
}
out.flush();
}
}
// static int N = 100010;
// static int idx = 0; //节点位置
// static char[] str = new char[N];
// static int[] cnt = new int[N];
// static int[][] son = new int[N][26];
// public static void insert(char[] str) {
// int p = 0; //从根节点出发
// for (char i : str) {
// int u = i - 'a';
// if (son[p][u] == 0) son[p][u] = ++idx;
// p = son[p][u];
// }
// cnt[p]++;
// }
// public static int query(char[] str) {
// int p = 0;
// for (char i : str) {
// int u = i - 'a';
// if (son[p][u] == 0) return 0;
// p = son[p][u];
// }
// return cnt[p];
// }
// static int bsearch_l(int l, int r, long target) {
// while (l < r) {
// int mid = l + r >> 1;
// if (num[mid] >= target) r = mid;
// else l = mid + 1;
// }
// return l;
// }
//
// static int bsearch_r(int l, int r, long target) {
// while (l < r) {
// int mid = l + r + 1 >> 1;
// if (num[mid] <= target) l = mid;
// else r = mid - 1;
// }
// return l;
// }
// static double bsearch_d(double l, double r) {
// double eps = 1e-8;
// while (Math.abs(r - l) > eps) {
// double mid = (l + r) / 2;
// if (check(mid)) r = mid;
// else l = mid;
// }
// return l;
// }
// static boolean check(int mid) {
// return true;
// }
public static long ksm(long n, long m) {//本质为n^m
long res = 1, base = n;
while (m != 0) {
if ((m & 1) == 1) { //等价于m%2==1,也就是m为奇数时
res = mul(res, base) % mod;
}
base = mul(base, base) % mod;//m为偶数时,base自乘
m = m >> 1;//等价于m/2
}
return res % mod;
}
static long mul(long a, long b) {//本质为a*b
long ans = 0;
while (b != 0) {
if ((b & 1) == 1) {
ans = (ans + a) % mod;
}
a = (a + a) % mod;
b = b >> 1;
}
return ans % mod;
}
public static long cnm(int a, int b) {
long sum = 1;
int i = a, j = 1;
while (j <= b) {
sum = sum * i / j;
i--;
j++;
}
return sum;
}
public static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static void gbSort(int[] a, int l, int r) {
if (l < r) {
int m = (l + r) >> 1;
gbSort(a, l, m);
gbSort(a, m + 1, r);
int[] t = new int[r - l + 1];
int idx = 0, i = l, j = m + 1;
while (i <= m && j <= r)
if (a[i] <= a[j]) t[idx++] = a[i++];
else t[idx++] = a[j++];
while (i <= m) t[idx++] = a[i++];
while (j <= r) t[idx++] = a[j++];
for (int z = 0; z < t.length; z++) a[l + z] = t[z];
}
}
static InputReader in = new InputReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
boolean hasNext() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e) {
return false;
// TODO: handle exception
}
}
return true;
}
public String nextLine() {
String str = null;
try {
str = reader.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 BigInteger nextBigInteger() {
return new BigInteger(next());
}
public BigDecimal nextBigDecimal() {
return new BigDecimal(next());
}
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 3e6bbd87a35e805f62a0e0d228c64a00 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | /*----------- ---------------*
Author : Ryan Ranaut
__Hope is a big word, never lose it__
------------- --------------*/
import java.io.*;
import java.util.*;
public class Codeforces1 {
static PrintWriter out = new PrintWriter(System.out);
static final int mod = 1_000_000_007;
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
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;
}
}
/*--------------------------------------------------------------------------*/
//Try seeing general case
//Minimization Maximization - BS..... Connections - Graphs.....
//Greedy not worthy - Try DP
//Think edge cases
public static void main(String[] args)
{
FastReader s = new FastReader();
int t = s.nextInt();
while(t-->0)
{
int n = s.nextInt();
char[] str = s.nextLine().toCharArray();
find(n, str);
}
out.close();
}
public static void find(int n, char[] str)
{
int[] ans = find1(n, str);
if(ans[0] == 0)
{
int x = 1;
for(int i=2;i<n;i+=2)
{
if(str[i]!=str[i-1])
x++;
}
out.println(ans[0]+" "+(x));
return ;
}
out.println(ans[0]+" "+(ans[1]+1));
}
public static int[] find1(int n, char[] str)
{
int res = 0, cnt = 0;
for(int i=1;i<n;i+=2)
{
if(str[i] == str[i-1])
continue;
res++;
}
int prev = -1;
int i=1;
for(;i<n;i+=2)
{
if(str[i] == str[i-1])
{
prev = str[i]-'0';
break;
}
}
i+=2;
for(;i<n;i+=2)
{
if(str[i] == str[i-1])
{
if(str[i]-'0' != prev)
{
prev ^= 1;
cnt++;
}
}
}
return new int[]{res, cnt};
}
/*----------------------------------End of the road--------------------------------------*/
static class DSU {
int[] parent;
int[] ranks;
int[] groupSize;
int size;
public DSU(int n) {
size = n;
parent = new int[n];//0 based
ranks = new int[n];
groupSize = new int[n];//Size of each component
for (int i = 0; i < n; i++) {
parent[i] = i;
ranks[i] = 1;
groupSize[i] = 1;
}
}
public int find(int x)//Path Compression
{
if (parent[x] == x)
return x;
else
return parent[x] = find(parent[x]);
}
public void union(int x, int y)//Union by rank
{
int x_rep = find(x);
int y_rep = find(y);
if (x_rep == y_rep)
return;
if (ranks[x_rep] < ranks[y_rep]) {
parent[x_rep] = y_rep;
groupSize[y_rep] += groupSize[x_rep];
} else if (ranks[x_rep] > ranks[y_rep]) {
parent[y_rep] = x_rep;
groupSize[x_rep] += groupSize[y_rep];
} else {
parent[y_rep] = x_rep;
ranks[x_rep]++;
groupSize[x_rep] += groupSize[y_rep];
}
size--;//Total connected components
}
}
public static int gcd(int x, int y) {
return y == 0 ? x : gcd(y, x % y);
}
public static long gcd(long x, long y) {
return y == 0L ? x : gcd(y, x % y);
}
public static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static long pow(long a, long b) {
if (b == 0L)
return 1L;
long tmp = 1;
while (b > 1L) {
if ((b & 1L) == 1)
tmp *= a;
a *= a;
b >>= 1;
}
return (tmp * a);
}
public static long modPow(long a, long b, long mod) {
if (b == 0L)
return 1L;
long tmp = 1;
while (b > 1L) {
if ((b & 1L) == 1L)
tmp *= a;
a *= a;
a %= mod;
tmp %= mod;
b >>= 1;
}
return (tmp * a) % mod;
}
static long mul(long a, long b) {
return a * b;
}
static long fact(int n) {
long ans = 1;
for (int i = 2; i <= n; i++) ans = mul(ans, i);
return ans;
}
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 void debug(int... a) {
for (int x : a)
out.print(x + " ");
out.println();
}
static void debug(long... a) {
for (long x : a)
out.print(x + " ");
out.println();
}
static void debugMatrix(int[][] a) {
for (int[] x : a)
out.println(Arrays.toString(x));
}
static void debugMatrix(long[][] a) {
for (long[] x : a)
out.println(Arrays.toString(x));
}
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];
}
static void sort(int[] a) {
ArrayList<Integer> ls = new ArrayList<>();
for (int x : a) ls.add(x);
Collections.sort(ls);
for (int i = 0; i < a.length; i++)
a[i] = ls.get(i);
}
static void sort(long[] a) {
ArrayList<Long> ls = new ArrayList<>();
for (long x : a) ls.add(x);
Collections.sort(ls);
for (int i = 0; i < a.length; i++)
a[i] = ls.get(i);
}
static class Pair {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | e2b868bb9a2debf10fcc870cd6e65853 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes |
// Java is like Alzheimer's, it starts off slow, but eventually, your memory is gone.
import java.io.*;
import java.util.*;
public class Aqueous {
static MyScanner sc = new MyScanner();
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) {
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
String s = sc.next();
StringBuilder sb = new StringBuilder();
int cnt = 0;
for(int i = 0; i<s.length()-1; i=i+2) {
if(s.charAt(i)!=s.charAt(i+1)) {
cnt++;
}
else {
sb.append(s.charAt(i));
}
}
int ans = 1;
for(int i =1; i<sb.length(); i++) {
if(sb.charAt(i)!=sb.charAt(i-1)) {
ans++;
}
}
pw.println(cnt+" "+ans);
}
pw.close();
}
static void ruffleSort(int a[]) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < n; i++) {
int oi = r.nextInt(n);
int temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long a[]) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < n; i++) {
int oi = r.nextInt(n);
long temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 556c801eb50dcc86621b1127238f597e | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import static java.lang.Math.*;
import static java.lang.System.*;
import java.util.*;
public class Main {
static public void main(String[] args){
Read in = new Read(System.in);
int t =in.nextInt();
while(t>0){
t--;
solve(in);
}
}
static void solve(Read in){
int n = in.nextInt();
String s = in.next();
int id= 0;
int ans = 0;
int b = -1;
int c = 0;
for(int i=1;i<n;i+=2){
if(s.charAt(i-1)!=s.charAt(i)){
ans++;
}else {
int t = s.charAt(i) - '0';
if(b!=t){
c++;
}
b = t;
}
}
if(c==0) c=1;
out.println(ans+ " "+ c);
}
void shuffleArray(long[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static class Read {//自定义快读 Read
public BufferedReader reader;
public StringTokenizer tokenizer;
public Read(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String str = null;
try {
str = reader.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 BigInteger nextBigInteger() {
return new BigInteger(next());
}
}
static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
static long gcd(long a, long b) {
return (a % b == 0) ? b : gcd(b, a % b);
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | e42dcfd343f70d62c415cad977a6697b | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int T = sc.nextInt();
while (T-- > 0){
int n = sc.nextInt();
char[] a = sc.next().toCharArray();
int res = 0;
int cnt = 1;
char last = ' ';
for(int i = 0; i < n; i+= 2){
char left = a[i];
char right = a[i + 1];
if(left != right){
res++;
}else { // same in current two
if(last == ' '){
last = a[i];
}else if(last == a[i]){
continue;
}else { // last != a[i]
cnt++;
last = a[i];
}
}
}
System.out.println(res + " " + cnt);
}
}
static void solver() {
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 11 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 17ab59136de0a452023e897367b68623 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.util.*;
public class TokitsukazeAndGood01StringHardVersion {
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>();
segments.add(-1);
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 siz = segments.size() - 1;
boolean[] one = new boolean[siz + 1];
one[1] = s[0] == 0 ? false : true;
for (int i = 2; i <= siz; i ++) one[i] = !one[i - 1];
int[][] dp = new int[siz + 1][2];
dp[0][0] = 1; dp[0][1] = 1;
int ans = 0;
boolean odd = false;
for (int i = 1; i <= siz; i ++) {
dp[i][0] = n + 1; dp[i][1] = n + 1;
int idx = one[i] ? 1 : 0;
if (!odd) {
if (segments.get(i) % 2 == 1) {
ans ++;
odd = true;
dp[i][idx] = Math.min(dp[i - 1][idx], dp[i - 1][1 - idx] + 1);
if (segments.get(i) == 1) dp[i][1 - idx] = Math.min(dp[i - 1][1 - idx], dp[i - 1][idx] + 1);
} else {
dp[i][idx] = Math.min(dp[i - 1][idx], dp[i - 1][1 - idx] + 1);
}
} else {
if (segments.get(i) % 2 == 1) {
dp[i][idx] = Math.min(dp[i - 1][idx], dp[i - 1][1 - idx] + 1);
if (segments.get(i) == 1) dp[i][1 - idx] = Math.min(dp[i - 1][1 - idx], dp[i - 1][idx] + 1);
odd = false;
} else {
ans ++;
dp[i][idx] = Math.min(dp[i - 1][idx], dp[i - 1][1 - idx] + 1);
if (segments.get(i) == 2) dp[i][1 - idx] = Math.min(dp[i - 1][1 - idx], dp[i - 1][idx] + 1);
else dp[i][1 - idx] = Math.min(dp[i - 1][idx] + 1, dp[i - 1][1 - idx] + 2);
}
}
}
System.out.println(ans + " " + Math.min(dp[siz][0], dp[siz][1]));
}
}
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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | b937adf878d2aca1bcc549b79a64fdc5 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 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 = 0;
char prev = 'a';
boolean foundOdd = false;
int ans = 0;
for(int i = 0; i < n; i++) {
if(prev == strr[i]) {
ct++;
continue;
}
// odd
if(ct%2==1 && !foundOdd) {
foundOdd = true;
} else if(ct%2==1) {
ans++;
foundOdd = false;
} else if(foundOdd) {
ans++;
}
prev = strr[i];
ct = 1;
}
if(ct%2==1) ans++;
List<Integer> segments = new ArrayList<>();
ct = 0;
prev = 'a';
for(int i = 0; i < n; i++) {
if(strr[i] == prev) {
ct++;
continue;
}
if(ct != 0) segments.add(ct);
ct = 1;
prev = strr[i];
}
segments.add(ct);
foundOdd = false;
n = segments.size();
if(ans == 0) {
w.p(0+" "+n);
return;
}
int[] dp = new int[n];
for(int i = 0; i < n; i++) {
int currLen = segments.get(i);
if(currLen%2==0 && !foundOdd) {
dp[i] = i==0?n:dp[i-1];
}
if(currLen%2==1 || foundOdd) {
if(currLen != 1 && currLen != 2) dp[i] = i==0?n:dp[i-1];
else {
if(i==0) dp[i]=n-1;
else if(i==n-1) dp[i]=(i==1?n-1:dp[i-2]-1);
else if(i==1) dp[i]=n-2;
else dp[i] = dp[i-2]-2;
}
}
if(i!=0) dp[i]=Math.min(dp[i], dp[i-1]);
if(currLen%2==1) foundOdd = !foundOdd;
}
w.p(ans+" "+dp[n-1]);
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 16976122978508e8fba901660975c52f | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
/**
*
* @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++;
}
char vv = '0';
for(int i = 1; i<n; i+=2) {
if(s.charAt(i) == s.charAt(i-1)) {
vv = s.charAt(i);
break;
}
}
char arr[] = s.toCharArray();
for(int i = 1; i<n; i+=2) {
if(arr[i] != arr[i-1]) {
if(i-2>=0) {
arr[i] = arr[i-2];
arr[i-1] = arr[i-2];
}
else {
arr[i-1] = vv;
arr[i] = vv;
}
}
}
int change = 1;
for(int i = 1; i<n; i++)if(arr[i] != arr[i-1])change++;
System.out.println(ans+" "+change);
}
//=============================================================================
//--------------------------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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 9e172aa8a00ca74c0782c56cfef4d12d | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 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++;
}
}
boolean first = true;
int min =0;
int ans2 =ans;
char l ='2';
for(int i=0;i<n;i+=2)
{
if(m[i]==m[i+1])
{
if(m[i]!=l)
{
min++;
l=m[i];
}
}
}
out.println(ans+" "+(min==0?1:min));
}
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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 166a749b2964248977c36d3f15c16f55 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.util.*;
public class GoodStrHard {
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
for (int i = 1; i <= t; i++) {
int n = in.nextInt();
String s = in.next();
int numOps = 0;
int currentPair = -1;
int stuckIndex = -1;
char[] sArr = s.toCharArray();
for (int j = 0; j < n; j = j + 2) {
if ((sArr[j] == '0') && (sArr[j + 1] == '1')) {
numOps++;
if (currentPair == 0) {
sArr[j + 1] = '0';
}
else if (currentPair == 1) {
sArr[j] = '1';
}
else {
if (stuckIndex == -1) {
stuckIndex = j;
}
}
}
else if ((sArr[j] == '1') && (sArr[j + 1] == '0')) {
numOps++;
if (currentPair == 0) {
sArr[j] = '0';
}
else if (currentPair == 1) {
sArr[j + 1] = '1';
}
else {
if (stuckIndex == -1) {
stuckIndex = j;
}
}
}
else if ((sArr[j] == '1') && (sArr[j + 1] == '1')) {
currentPair = 1;
if (stuckIndex >= 0) {
for (int z = stuckIndex; z < j; z++) {
sArr[z] = '1';
}
stuckIndex = -1;
}
}
else if ((sArr[j] == '0') && (sArr[j + 1] == '0')) {
currentPair = 0;
if (stuckIndex >= 0) {
for (int z = stuckIndex; z < j; z++) {
sArr[z] = '0';
}
stuckIndex = -1;
}
}
}
int numSegs = 1;
if (currentPair >= 0) {
for (int j = 1; j < n; j++) {
if (sArr[j] != sArr[j - 1]) {
numSegs++;
}
}
}
out.println(numOps + " " + numSegs);
}
out.close();
}
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) {
// noop
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | e45a1a6b327a0f4a0a817f2c954c0457 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.lang.Math;
import java.util.*;
import java.util.regex.Pattern;
import javax.swing.text.DefaultStyledDocument.ElementSpec;
public final class Solution {
private static class FastScanner {
private final int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private FastScanner(boolean usingFile) throws IOException {
if (usingFile) din =
new DataInputStream(new FileInputStream("path")); else din =
new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private short nextShort() throws IOException {
short ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = (short) (ret * 10 + c - '0'); while (
(c = read()) >= '0' && c <= '9'
);
if (neg) return (short) -ret;
return ret;
}
private 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;
}
private char nextChar() throws IOException {
byte c = read();
while (c <= ' ') c = read();
return (char) c;
}
private String nextString() throws IOException {
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <= ' ') c = read();
do {
ret.append((char) c);
} while ((c = read()) > ' ');
return ret.toString();
}
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++];
}
}
static StringTokenizer st;
static BufferedReader br = new BufferedReader(
new InputStreamReader(System.in)
);
static BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(System.out)
);
static int mod = 1000000007;
static String is(int no) {
return Integer.toString(no);
}
static String ls(long no) {
return Long.toString(no);
}
static int pi(String s) {
return Integer.parseInt(s);
}
static long pl(String s) {
return Long.parseLong(s);
}
/*write your constructor and global variables here*/
static class Rec<f, s, t> {
f a;
s b;
t c;
Rec(f a, s b, t c) {
this.a = a;
this.b = b;
this.c = c;
}
}
static class Quad<f, s, t, fo> {
f a;
s b;
t c;
fo d;
Quad(f a, s b, t c, fo d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
}
static class Pair<f, s> {
f a;
s b;
Pair(f a, s b) {
this.a = a;
this.b = b;
}
}
static int findPow(int a, int b, int mod) {
int res = 1;
while (b > 0) {
if ((b & 1) != 0) {
res = modMul.mod(res, a, mod);
}
a = modMul.mod(a, a, mod);
b = b / 2;
}
return res;
}
interface modOperations {
int mod(int a, int b, int mod);
}
static modOperations modAdd = (int a, int b, int mod) -> {
return (a % mod + b % mod) % mod;
};
static modOperations modSub = (int a, int b, int mod) -> {
return ((a % mod - b % mod + mod) % mod);
};
static modOperations modMul = (int a, int b, int mod) -> {
return (int) ((1l * a % mod * b % mod)) % mod;
};
static modOperations modDiv = (int a, int b, int mod) -> {
return modMul.mod(a, findPow(b, mod - 2, mod), mod);
};
static long gcd(long a, long b) {
long div = b;
long rem = a % b;
while (rem != 0) {
long temp = rem;
rem = div % rem;
div = temp;
}
return div;
}
static HashSet<Integer> primeList(int MAXI) {
int[] prime = new int[MAXI + 1];
HashSet<Integer> list = new HashSet<>();
for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) {
if (prime[i] == 0) {
list.add(i);
for (int j = i * i; j <= MAXI; j += i) {
prime[j] = 1;
}
}
}
for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) {
if (prime[i] == 0) {
list.add(i);
}
}
return list;
}
static int[] factorialList(int MAXI, int mod) {
int[] factorial = new int[MAXI + 1];
factorial[0] = 1;
for (int i = 1; i < MAXI + 1; i++) {
factorial[i] = modMul.mod(factorial[i - 1], i, mod);
}
return factorial;
}
static void put(HashMap<Integer, Integer> cnt, int key) {
if (cnt.containsKey(key)) {
cnt.replace(key, cnt.get(key) + 1);
} else {
cnt.put(key, 1);
}
}
static ArrayList<Integer> getKeys(HashMap<Integer, Integer> maps) {
ArrayList<Integer> vals = new ArrayList<>();
for (Map.Entry<Integer, Integer> map : maps.entrySet()) {
vals.add(map.getKey());
}
return vals;
}
static ArrayList<Integer> getValues(HashMap<Integer, Integer> maps) {
ArrayList<Integer> vals = new ArrayList<>();
for (Map.Entry<Integer, Integer> map : maps.entrySet()) {
vals.add(map.getValue());
}
return vals;
}
static int getMax(ArrayList<Integer> arr) {
int max = arr.get(0);
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i) > max) {
max = arr.get(i);
}
}
return max;
}
static int getMin(ArrayList<Integer> arr) {
int max = arr.get(0);
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i) < max) {
max = arr.get(i);
}
}
return max;
}
static int[][] fill_arr(int m, int n, int fill) {
int arr[][] = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = fill;
}
}
return arr;
}
static class sortCond implements Comparator<Pair<Integer, Integer>> {
@Override
public int compare(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) {
return p2.a - p1.a;
}
}
static class sortCondRec
implements Comparator<Rec<Integer, Integer, Integer>> {
@Override
public int compare(
Rec<Integer, Integer, Integer> p1,
Rec<Integer, Integer, Integer> p2
) {
return p1.b - p2.b;
}
}
/*write your methods and classes here*/
static int cnt = 0;
static boolean dfs(int[] arr, int l, int h) {
if (l == h) {
return true;
}
int mid = l + (h - l) / 2;
int la = 262145, ma = 0;
int lb = 262144, mb = 0;
for (int i = l; i <= mid; i++) {
la = Math.min(la, arr[i]);
ma = Math.max(ma, arr[i]);
}
for (int i = mid + 1; i <= h; i++) {
lb = Math.min(lb, arr[i]);
mb = Math.max(mb, arr[i]);
}
if (ma <= lb || mb <= la) {
if (mb <= la) {
cnt++;
}
return dfs(arr, l, mid) && dfs(arr, mid + 1, h);
} else {
return false;
}
}
public static void main(String[] args) throws IOException {
FastScanner input = new FastScanner(false);
PrintWriter out = new PrintWriter(System.out);
int cases = input.nextInt(), n, i;
while (cases-- != 0) {
n = input.nextInt();
String s = input.nextString();
int arr[] = new int[n];
for (i = 0; i < n; i++) {
if (s.charAt(i) == '1') {
arr[i] = 1;
} else {
arr[i] = 0;
}
}
int no = -1, op = 0, seg = 0;
for (i = 0; i < n; i += 2) {
if (arr[i] != arr[i + 1]) {
op++;
} else {
if (arr[i] != no) {
seg++;
}
no = arr[i];
}
}
out.println(op + " " + Math.max(seg, 1));
}
out.flush();
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | d52970155a7f38042b66f81c85b8d915 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.lang.Math;
import java.util.*;
import java.util.regex.Pattern;
import javax.swing.text.DefaultStyledDocument.ElementSpec;
public final class Solution {
private static class FastScanner {
private final int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private FastScanner(boolean usingFile) throws IOException {
if (usingFile) din =
new DataInputStream(new FileInputStream("path")); else din =
new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private short nextShort() throws IOException {
short ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = (short) (ret * 10 + c - '0'); while (
(c = read()) >= '0' && c <= '9'
);
if (neg) return (short) -ret;
return ret;
}
private 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;
}
private char nextChar() throws IOException {
byte c = read();
while (c <= ' ') c = read();
return (char) c;
}
private String nextString() throws IOException {
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <= ' ') c = read();
do {
ret.append((char) c);
} while ((c = read()) > ' ');
return ret.toString();
}
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++];
}
}
static StringTokenizer st;
static BufferedReader br = new BufferedReader(
new InputStreamReader(System.in)
);
static BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(System.out)
);
static int mod = 1000000007;
static String is(int no) {
return Integer.toString(no);
}
static String ls(long no) {
return Long.toString(no);
}
static int pi(String s) {
return Integer.parseInt(s);
}
static long pl(String s) {
return Long.parseLong(s);
}
/*write your constructor and global variables here*/
static class Rec<f, s, t> {
f a;
s b;
t c;
Rec(f a, s b, t c) {
this.a = a;
this.b = b;
this.c = c;
}
}
static class Quad<f, s, t, fo> {
f a;
s b;
t c;
fo d;
Quad(f a, s b, t c, fo d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
}
static class Pair<f, s> {
f a;
s b;
Pair(f a, s b) {
this.a = a;
this.b = b;
}
}
static int findPow(int a, int b, int mod) {
int res = 1;
while (b > 0) {
if ((b & 1) != 0) {
res = modMul.mod(res, a, mod);
}
a = modMul.mod(a, a, mod);
b = b / 2;
}
return res;
}
interface modOperations {
int mod(int a, int b, int mod);
}
static modOperations modAdd = (int a, int b, int mod) -> {
return (a % mod + b % mod) % mod;
};
static modOperations modSub = (int a, int b, int mod) -> {
return ((a % mod - b % mod + mod) % mod);
};
static modOperations modMul = (int a, int b, int mod) -> {
return (int) ((1l * a % mod * b % mod)) % mod;
};
static modOperations modDiv = (int a, int b, int mod) -> {
return modMul.mod(a, findPow(b, mod - 2, mod), mod);
};
static long gcd(long a, long b) {
long div = b;
long rem = a % b;
while (rem != 0) {
long temp = rem;
rem = div % rem;
div = temp;
}
return div;
}
static HashSet<Integer> primeList(int MAXI) {
int[] prime = new int[MAXI + 1];
HashSet<Integer> list = new HashSet<>();
for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) {
if (prime[i] == 0) {
list.add(i);
for (int j = i * i; j <= MAXI; j += i) {
prime[j] = 1;
}
}
}
for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) {
if (prime[i] == 0) {
list.add(i);
}
}
return list;
}
static int[] factorialList(int MAXI, int mod) {
int[] factorial = new int[MAXI + 1];
factorial[0] = 1;
for (int i = 1; i < MAXI + 1; i++) {
factorial[i] = modMul.mod(factorial[i - 1], i, mod);
}
return factorial;
}
static void put(HashMap<Integer, Integer> cnt, int key) {
if (cnt.containsKey(key)) {
cnt.replace(key, cnt.get(key) + 1);
} else {
cnt.put(key, 1);
}
}
static ArrayList<Integer> getKeys(HashMap<Integer, Integer> maps) {
ArrayList<Integer> vals = new ArrayList<>();
for (Map.Entry<Integer, Integer> map : maps.entrySet()) {
vals.add(map.getKey());
}
return vals;
}
static ArrayList<Integer> getValues(HashMap<Integer, Integer> maps) {
ArrayList<Integer> vals = new ArrayList<>();
for (Map.Entry<Integer, Integer> map : maps.entrySet()) {
vals.add(map.getValue());
}
return vals;
}
static int getMax(ArrayList<Integer> arr) {
int max = arr.get(0);
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i) > max) {
max = arr.get(i);
}
}
return max;
}
static int getMin(ArrayList<Integer> arr) {
int max = arr.get(0);
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i) < max) {
max = arr.get(i);
}
}
return max;
}
static int[][] fill_arr(int m, int n, int fill) {
int arr[][] = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = fill;
}
}
return arr;
}
static class sortCond implements Comparator<Pair<Integer, Integer>> {
@Override
public int compare(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) {
return p2.a - p1.a;
}
}
static class sortCondRec
implements Comparator<Rec<Integer, Integer, Integer>> {
@Override
public int compare(
Rec<Integer, Integer, Integer> p1,
Rec<Integer, Integer, Integer> p2
) {
return p1.b - p2.b;
}
}
/*write your methods and classes here*/
static int cnt = 0;
static boolean dfs(int[] arr, int l, int h) {
if (l == h) {
return true;
}
int mid = l + (h - l) / 2;
int la = 262145, ma = 0;
int lb = 262144, mb = 0;
for (int i = l; i <= mid; i++) {
la = Math.min(la, arr[i]);
ma = Math.max(ma, arr[i]);
}
for (int i = mid + 1; i <= h; i++) {
lb = Math.min(lb, arr[i]);
mb = Math.max(mb, arr[i]);
}
if (ma <= lb || mb <= la) {
if (mb <= la) {
cnt++;
}
return dfs(arr, l, mid) && dfs(arr, mid + 1, h);
} else {
return false;
}
}
public static void main(String[] args) throws IOException {
FastScanner input = new FastScanner(false);
PrintWriter out = new PrintWriter(System.out);
int cases = input.nextInt(), n, i;
while (cases-- != 0) {
n = input.nextInt();
int arr[] = new int[n];
String s = input.nextString();
for (i = 0; i < n; i++) {
arr[i] = pi(s.substring(i, i + 1));
}
int L = -1;
int op = 0;
int seg = 0;
for (i = 0; i < n; i += 2) {
if (arr[i] != arr[i + 1]) {
op += 1;
} else {
if (arr[i] != L) {
seg++;
}
L = arr[i];
}
}
out.println(op + " " + Math.max(1, seg));
}
out.flush();
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 7a0784fcd7e31bf90b7277c052c2ab41 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t,n;
t = sc.nextInt();
for(int i = 0; i < t; i++){
n = sc.nextInt();
String A = sc.next();
boolean one = false;
boolean zero = false;
boolean both = false;
int op = 0;
int seg = 0;
for(int j = 0; j <n; j += 2){
String B = A.substring(j,j+2);
if(B.equals("11")){
one = true;
if(zero){
seg++;
zero = false;
}
}
else if(B.equals("00")){
zero = true;
if(one){
seg++;
one = false;
}
}
else{
op++;
}
}
seg++;
System.out.println(op+" "+seg);
}
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 8614169901bd085ef10b5510056a4518 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | //package december;
import java.util.*;
import java.io.*;
public class codeforces1678B2 {
public static void main(String[] args) throws Exception {
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());
char [] arr = br.readLine().toCharArray();
int currLeft = 2;
int count = 0;
int operations = 0;
for (int i = 0; i<arr.length;i+=2)
{
if (arr[i]==arr[i+1])
{
if (arr[i]!=currLeft)
{
count++;
currLeft = arr[i];
}
}
else
{
operations++;
}
}
if (count==0)
{
count++;
}
//count++;
System.out.println(operations+" "+ count);
}
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 0ebcaae053d30901d43a3655a3ae1124 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
InputReader s = new InputReader(System.in);
int t = s.nextInt();
while(t-->0){
int n = s.nextInt();
char[] c = s.next().toCharArray();
int cnt = 0;
for(int i=0;i<n;i+=2){
if(c[i]!=c[i+1]){
c[i]=c[i+1]='2';
cnt++;
}
}
int ans = 0;
char last = '2';
for(int i = 0;i < n;i ++)
{
if(c[i] == '2') continue;
if(c[i] != last){
ans++;
last = c[i];
}
}
System.out.println(cnt+" "+Math.max(1,ans));
}
}
static class InputReader {
private final static int BUF_SZ = 65536;
BufferedReader in;
StringTokenizer tokenizer;
public InputReader(InputStream in) {
super();
this.in = new BufferedReader(new InputStreamReader(in), BUF_SZ);
tokenizer = new StringTokenizer("");
}
private String next() {
while (!tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(in.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | dc91cae2b38d2a6c18d444688a6e7d4f | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
InputReader s = new InputReader(System.in);
int t = s.nextInt();
while(t-->0){
int n = s.nextInt();
char[] c = s.next().toCharArray();
int cnt = 0;
for(int i=0;i<n;i+=2){
if(c[i]!=c[i+1]){
c[i]=c[i+1]='2';
cnt++;
}
}
int ans = 0;
// char last = '2';
// for(int i=0;i<n;i++){
// if(c[i]=='2')continue;
// else{
// ans++;
// last = c[i];
// int j = i;
// while(j<n && c[j]==last)j++;
// i = j-1;
// }
// }
char last = '2';
for(int i = 0;i < n;i ++)
{
if(c[i] == '2') continue;
if(c[i] != last){
ans++;
last = c[i];
}
}
System.out.println(cnt+" "+Math.max(1,ans));
}
}
static class InputReader {
private final static int BUF_SZ = 65536;
BufferedReader in;
StringTokenizer tokenizer;
public InputReader(InputStream in) {
super();
this.in = new BufferedReader(new InputStreamReader(in), BUF_SZ);
tokenizer = new StringTokenizer("");
}
private String next() {
while (!tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(in.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | d79e9cd98f654f7a8e9db8643da0362a | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 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();
char last = '?';
int change = 0;
int seg = 0;
for(int i = 0; i < n; i += 2){
if(s.charAt(i) != s.charAt(i + 1)){
change++;
}else{
if(s.charAt(i) != last){
last = s.charAt(i);
seg++;
}
}
}
seg = Math.max(seg, 1);
writer.write(change + " " + seg + "\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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | cda45363a2c1a0c3b38ea6739a533fb9 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes |
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.lang.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class A_769 {
static PrintWriter out = new PrintWriter(System.out);
static ArrayList<ArrayList<Integer>> ans;
static ArrayList<Integer> [] adj;
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void sort(long[] arr) {
ArrayList<Long> a = new ArrayList<>();
for (long i : arr) {
a.add(i);
}
Collections.sort(a);
for (int i = 0; i < a.size(); i++) {
arr[i] = a.get(i);
}
}
static long highestPowerof2(long x) {
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
public static long fact(long number) {
if(number == 0 || number == 1) {
return 1;
}
else {
return number * fact(number - 1);
}
}
public static ArrayList<Long> primeFactors(long n)
{
ArrayList<Long> arr = new ArrayList<>();
while (n % 2 == 0) {
arr.add(2l);
n /= 2;
}
for (long i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
arr.add(i);
n /= i;
}
}
if (n > 2)
arr.add(n);
return arr;
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
int t = sc.nextInt();
test: while (t-- > 0) {
int n = sc.nextInt();
char[] ch = sc.next().toCharArray();
long ans = 0;
for(int i = 0; i < n-1; i+=2){
char a = ch[i];
char b = ch[i+1];
if(a != b){
ans++;
}
}
long seg = 0;
char c = '?';
for(int i = 0; i < n; i+=2){
if(ch[i]==ch[i+1]){
c = ch[i];
break;
}
}
for(int i = 0; i < n;i+=2) {
if(ch[i] == ch[i+1]){
c = ch[i];
}else{
ch[i] = c;
ch[i+1] = c;
}
}
c = '2';
for(int i = 0; i < n; i++){
if(ch[i] != c){
seg++;
c = ch[i];
}
}
if(seg == 0) seg = 1;
System.out.println(ans+" "+seg);
}
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 4e6bb54902e6f65eba911b1543fea526 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Codechef {
/* FastReader class to work with input */
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 [] iArray(int n) {
int res [] = new int [n];
for(int i = 0; i<n; i++)res[i] = nextInt();
return res;
}
long [] lArray(int n) {
long res [] = new long [n];
for(int i = 0; i<n; i++)res[i] = nextLong();
return res;
}
}
/* UnionFind class to work with disjoint set */
static class UnionFind {
int[] p, rank, setSize;
int numSets;
public UnionFind(int N) {
p = new int[numSets = N];
rank = new int[N];
setSize = new int[N];
for (int i = 0; i < N; i++) {
p[i] = i;
setSize[i] = 1;
}
}
public int findSet(int i) {
return p[i] == i ? i : (p[i] = findSet(p[i]));
}
public boolean isSameSet(int i, int j) {
return findSet(i) == findSet(j);
}
public void unionSet(int i, int j) {
if (isSameSet(i, j))
return;
numSets--;
int x = findSet(i), y = findSet(j);
if (rank[x] > rank[y]) {
p[y] = x;
setSize[x] += setSize[y];
}
else {
p[x] = y;
setSize[y] += setSize[x];
if (rank[x] == rank[y]) rank[y]++;
}
}
public int numDisjointSets() {
return numSets;
}
public int sizeOfSet(int i) {
return setSize[findSet(i)];
}
}
static class Pair {
long a,b,c;
public Pair(long a, long b, long c) {
this.a = a;
this.b = b;
this.c = c;
}
}
static class Compare {
static void compare(ArrayList<Pair> al, int n) {
Collections.sort(al, new Comparator<Pair>(){
@Override public int compare(Pair p1, Pair p2) {
return (int)(p1.b-p2.b);
}
});
}
}
static class Compare1 {
static void compare(ArrayList<Pair> al, int n) {
Collections.sort(al, new Comparator<Pair>(){
@Override public int compare(Pair p1, Pair p2) {
return (int)(p1.a-p2.a);
}
});
}
}
static FastReader in = new FastReader();
static final Random random = new Random();
// static int mod = (int) 1e9 + 7;
static long[] fact = new long[16];
/* function to calculate factorial */
static void init() {
fact[0] = 1;
for(int i=1; i<16; i++)
fact[i] = (i*fact[i-1]);
}
/* function to calculate modular exponentiation */
public static long pow(long a, long b, long mod) {
long res = 1;
while (b > 0) {
if ((b & 1) > 0)
res = (res * a) % mod;
b = b >> 1;
a = ((a % mod) * (a % mod)) % mod;
}
return (res % mod + mod) % mod;
}
/* function to check if string is palindrome or not */
public static boolean isPal(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i))
return false;
}
return true;
}
/* sieveOfEratosthenes algorithm will return ArrayList of prime numbers */
public static ArrayList<Integer> primeSieve(int n) {
ArrayList<Integer> arr=new ArrayList<>();
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)
arr.add(i);
}
return arr;
}
/* sieveOfEratosthenes algorithm*/
public static void sieveOfEratosthenes(int N , boolean[] prime) {
Arrays.fill(prime, true);
for(int p = 2; p*p <=N; p++) {
if(prime[p] == true) {
// Update all multiples of p
for(int i = p*p; i <= N; i += p)
prime[i] = false;
}
}
}
/* Eculidean algorithm to find gcd of two number */
static int gcd(int a,int b) {
if(b==0) {
return a;
}
return gcd(b,a%b);
}
/* function to calculate lcm of two number */
static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static long cntSetBit(long num) {
long ans = 0;
while(num>0) {
if(num%2==1)
ans++;
num /= 2;
}
return ans;
}
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.println(res);
}
/* function to check whether ArrayList a and b are equal or not */
static boolean isEqual(ArrayList<Integer> a, ArrayList<Integer> b, int n) {
HashSet<Integer> hs = new HashSet<>();
if (a.size() < (n / 2) || b.size() < (n / 2))
return false;
int s1 = 0;
for (int ele : a)
hs.add(ele);
for (int ele : b)
hs.add(ele);
if (hs.size() == n)
return true;
return false;
}
/* main method */
public static void main (String[] args) throws java.lang.Exception {
int t=in.nextInt();
while(t-->0)
solve();
/*
ArrayList<Pair> al = new ArrayList<>();
for(int i=0; i<5; i++) {
al.add(new Pair(in.nextLong(), in.nextLong()));
}
for(int i=0; i<5; i++) {
System.out.println(al.get(i).a + " " + al.get(i).b);
}
Compare obj = new Compare();
obj.compare(al, 5);
Arrays.sort(al, (p0, p1) -> Integer.compare(p0.a, p1.a));
for(int i=0; i<5; i++) {
System.out.println(al.get(i).a + " " + al.get(i).b);
}
*/
}
static void solve() {
int n = in.nextInt();
String s = in.nextLine();
char[] c = s.toCharArray();
int prev = s.charAt(0)-'0';
int prevIdx = 0;
int ans = 0;
for(int i=1; i<n; i++) {
if(s.charAt(i)-'0'!=prev) {
if((i-1-prevIdx)%2==0) {
ans++;
prev = s.charAt(i-1)-'0';
prevIdx = i-1;
}
else {
prev = s.charAt(i)-'0';
prevIdx = i;
}
}
}
char temp = '0';
if(c[0]!=c[1]) {
for(int i=2; i<c.length; i+=2) {
if(c[i]==c[i+1]) {
temp = c[i];
break;
}
}
c[0]=temp;
c[1]=temp;
}
for(int i=2; i<c.length; i+=2) {
if(c[i]!=c[i+1]) {
c[i] = c[i-1];
c[i+1] = c[i-1];
}
}
int cnt=0;
for(int i=0; i<c.length-1; i++) {
if(c[i]!=c[i+1])
cnt++;
}
System.out.println(ans + " " + (cnt+1));
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 3c66f5ae2ad1fee3f40ce2757a850c80 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 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[] arr=new int[n];
for(int i=0;i<n;i++) {
if(str.charAt(i)=='1') {
arr[i]=1;
}
}
int count1=0,count2=0,prev=-1;
for(int i=0;i<n;i+=2) {
if(arr[i]==arr[i+1]) {
if(arr[i]!=prev) {
count2++;
prev=arr[i];
}
}else {
count1++;
}
}
out.println(count1+" "+Math.max(count2,1));
}
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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | e5c742be95c221c32350137b1aae94b1 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.function.DoubleToLongFunction;
public class Codeforces789{
static long mod = 1000000007L;
static MyScanner sc = new MyScanner();
static void solve() {
int n = sc.nextInt();
int arr[] = sc.readIntArray(n);
int z = 0;
HashSet<Integer> set = new HashSet<>();
boolean f = false;
for(int i = 0;i<n;i++){
if(arr[i]==0) z++;
if(set.contains(arr[i])) f = true;
set.add(arr[i]);
}
if(z>0){
out.println(n-z);
}else{
if(f)
out.println(n);
else
out.println(n+1);
}
}
static void solve2(){
int n= sc.nextInt();
String str = sc.nextLine();
int count = 0;
for(int i = 0;i<n;i+=2){
if(str.charAt(i)!=str.charAt(i+1)){
count++;
}
}
out.println(count);
}
static void solve3(){
int n = sc.nextInt();
String str = sc.nextLine();
char arr[] = str.toCharArray();
int count = 0;
int res = 1;
ArrayList<Integer> ar = new ArrayList<>();
for(int i = 0;i<n;i+=2){
if(arr[i]!=arr[i+1]){
count++;
}else{
ar.add(arr[i]-'0');
}
}
for(int i = 0;i<ar.size()-1;i++){
if(ar.get(i)!=ar.get(i+1)) res++;
}
out.println(count+" "+res);
}
static void solve4(){
}
static void solve5(){
}
static void solve6(){
}
static void swap(char arr[][],int i,int j){
for(int k = j;k>0;k--){
if(arr[i][k]=='.'&& arr[i][k-1]=='*'){
char temp = arr[i][k];
arr[i][k] = arr[i][k-1];
arr[i][k-1] = temp;
}
}
}
static int search(int pre,int suf[],int i,int j){
while(i<=j){
int mid = (i+j)/2;
if(suf[mid]==pre) return mid;
else if(suf[mid]<pre) j = mid-1;
else i = mid+1;
}
return Integer.MIN_VALUE;
}
static long pow(long a, long b) {
if (b == 0) return 1;
long res = pow(a, b / 2);
res = (res * res) % 1_000_000_007;
if (b % 2 == 1) {
res = (res * a) % 1_000_000_007;
}
return res;
}
static int lis(int arr[],int n){
int lis[] = new int[n];
lis[0] = 1;
for(int i = 1;i<n;i++){
lis[i] = 1;
for(int j = 0;j<i;j++){
if(arr[i]>arr[j]){
lis[i] = Math.max(lis[i],lis[j]+1);
}
}
}
int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
max = Math.max(lis[i],max);
}
return max;
}
static boolean isPali(String str){
int i = 0;
int j = str.length()-1;
while(i<j){
if(str.charAt(i)!=str.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
static long gcd(long a,long b){
if(b==0) return a;
return gcd(b,a%b);
}
static String reverse(String str){
char arr[] = str.toCharArray();
int i = 0;
int j = arr.length-1;
while(i<j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
String st = new String(arr);
return st;
}
static boolean isprime(int n){
if(n==1) return false;
if(n==3 || n==2) return true;
if(n%2==0 || n%3==0) return false;
for(int i = 5;i*i<=n;i+= 6){
if(n%i== 0 || n%(i+2)==0){
return false;
}
}
return true;
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
// int t= 1;
while(t-- >0){
// solve();
// solve2();
solve3();
// solve4();
// solve5();
// solve6();
}
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
int[] reverse(int arr[]){
int n= arr.length;
int i = 0;
int j = n-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;i++;
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 1ba8d97065ab52f7db34ac0141fecde5 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
static long INF = 2000000000000000010l;
static int fx[][] = {{-1,0},{0,1},{1,0},{0,-1},{-1,-1},{1,1},{1,-1},{-1,1}};
public static void main(String[] args) throws IOException {
int t=1;
t = cin.nextInt();
while (t-- > 0)
{
// csh();
solve();
out.flush();
}
out.close();
}
public static void solve() {
int n=cin.nextInt();
String a=cin.next();
char[]c=a.toCharArray();
ArrayList<Character>list=new ArrayList<Character>();
int as=0;
int ans=1;
for(int i=0;i<n;i+=2){
if(a.charAt(i)!=a.charAt(i+1)){
as++;
}else{
list.add(c[i]);
list.add(c[i+1]);
}
}
//out.println(list.toString());
for(int i=0;i<list.size()-1;i++){
if(list.get(i)!=list.get(i+1)){
ans++;
}
}
out.print(as+" ");
out.println(ans==0?1:ans);
}
static class Node {
int x, y, k;
Node(){}
public Node(int x, int y, int k) {
this.x = x;
this.y = y;
this.k = k;
}
}
static void csh() {
}
static long cnm(int a, int b) {
long sum = 1;
int i = a, j = 1;
while (j <= b) {
sum = sum * i / j;
i--;
j++;
}
return sum;
}
static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
static void gbSort(int[] a, int l, int r) {
if (l < r) {
int m = (l + r) >> 1;
gbSort(a, l, m);
gbSort(a, m + 1, r);
int[] t = new int[r - l + 1];
int idx = 0, i = l, j = m + 1;
while (i <= m && j <= r){
//cnt++;
if (a[i] <= a[j])
t[idx++] = a[i++];
else{
t[idx++] = a[j++];
// cnt += m -i +1;//nx
}
}
while (i <= m)
t[idx++] = a[i++];
while (j <= r)
t[idx++] = a[j++];
for (int z = 0; z < t.length; z++)
a[l + z] = t[z];
}
}
static FastScanner cin = new FastScanner(System.in);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(in), 16384);
eat("");
}
public void eat(String s) {
st = new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
return null;
}
}
public boolean hasNext() {
while (!st.hasMoreTokens()) {
String s = nextLine();
if (s == null)
return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public Double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
public BigDecimal nextBigDecimal() {
return new BigDecimal(next());
}
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | a63e15d0fac830296b568c1e9dd9d035 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
for (int i = 0; i < N; i++) {
int num = sc.nextInt();
sc.nextLine();
String str = sc.nextLine();
int dif = 0;
int sub = 0;
char last = ' ';
char[] chars = str.toCharArray();
for (int j = 0; j < num - 1; j+=2) {
if (chars[j] != chars[j + 1]){
dif++;
}else {
if (last!=chars[j]){
sub++;
}
last = chars[j];
}
}
System.out.println(dif + " " + Math.max(1,sub));
}
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 50dd85ee2655aa7d12b6d6452d66c87f | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int times = scanner.nextInt();
while (times-- > 0){
int n = scanner.nextInt();
String s = scanner.next();
char last2 = ' ';
int x = 0;
int y = 0;
for (int i = 0; i < s.length(); i += 2){
if (s.charAt(i) != s.charAt(i+1)){
x++;
} else {
if (last2 != s.charAt(i)){
y++;
}
last2 = s.charAt(i);
}
}
System.out.println(x+" "+Math.max(1,y));
}
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | c74b7142ed64a296ce3f3ac8e487298d | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.Scanner;
/**
* @Author LWX
* @date 2022/5/11 23:15
*/
public class Tokitsukaze_and_Good_01_String_hard_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();
int[] curans = minimumPos(len, line);
System.out.println(curans[0] + " " + curans[1]);
}
}
private static int[] minimumPos(int len, String line) {
if (len == 0) return new int[]{0, 0};
int[] ans = new int[2];
int pre = -1;
char[] chars = line.toCharArray();
for (int i = 0; i < len; i+=2) {
int cur = chars[i] - '0';
int next = chars[i + 1] - '0';
if (cur != next) {
ans[0]++;
} else {
if (pre == -1 || cur != pre) {
pre = cur;
ans[1] ++;
}
}
}
if (pre == -1 && ans[1] == 0) {
ans[1] = 1;
}
return ans;
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 4339623c540f0bd07e7a78bbb04709ce | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 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);
CF789B2 solver = new CF789B2();
solver.solve(1, in, out);
out.close();
}
static class CF789B2 {
public void solve(int testNumber, RealFastReader in, RealFastWriter out) {
final int INF = 0x3f3f3f3f;
int cases = in.ni();
while (cases-- > 0) {
int n = in.ni();
char[] chars = in.ns(n);
int ans = 0;
n /= 2;
int[][] dp = new int[2][n + 1];
Arrays.fill(dp[0], INF);
Arrays.fill(dp[1], INF);
dp[0][0] = 0;
dp[1][0] = 0;
for (int i = 0; i < n; i++) {
if (chars[2 * i] != chars[2 * i + 1]) {
ans++;
dp[0][i + 1] = Math.min(dp[0][i], dp[1][i] + 1);
dp[1][i + 1] = Math.min(dp[1][i], dp[0][i] + 1);
} else if (chars[2 * i] == '1') {
dp[1][i + 1] = Math.min(dp[1][i], dp[0][i] + 1);
} else {
dp[0][i + 1] = Math.min(dp[0][i], dp[1][i] + 1);
}
if (dp[1][i + 1] == 0) {
dp[1][i + 1] = 1;
}
if (dp[0][i + 1] == 0) {
dp[0][i + 1] = 1;
}
}
out.println(ans + " " + Math.min(dp[0][n], dp[1][n]));
}
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;
}
public RealFastWriter writeln() {
return write((byte) '\n');
}
public RealFastWriter 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 {
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(String s) {
return writeln(s);
}
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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 9b2e5777686608805c7daea314b67eef | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.rmi.Remote;
import java.util.*;
public class Full {
static BufferedReader bf;
static PrintWriter out;
public static void main (String[] args)throws IOException {
bf = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int t = nextInt();
while(t-->0){
solve();
}
}
public static void solve()throws IOException{
int n = nextInt();
String s = bf.readLine();
char[]arr = s.toCharArray();
int prev = 1;
int sub1 = 1;
int change1 = 0;
for(int i =0;i<n;i+=2){
if(arr[i]!=arr[i+1]){
change1++;
}
else{
if(arr[i]!= prev+'0'){
sub1++;
prev = arr[i]-'0';
}
}
}
prev = 0;
int change2 = 0;
int sub2 = 1;
for(int i =0 ;i<n;i+=2){
if(arr[i]!=arr[i+1]){
change2++;
}
else{
if(arr[i]!= prev+'0'){
sub2++;
prev = arr[i]-'0';
}
}
}
if(sub2 < sub1){
println(change2 + " "+sub2);
}
else{
println(change1 + " " + sub1);
}
}
public static boolean isSorted(int[]arr){
for(int i =0;i<arr.length-1;i++){
if(arr[i]>arr[i+1])return false;
}
return true;
}
public static long gcd(long a,long b){
if(b == 0)return a;
return gcd(b,a%b);
}
// code for input
public static void print(String s ){
System.out.print(s);
}
public static void sort(long[]arr){
List<Long>list = new ArrayList<>();
for(int i =0;i<arr.length;i++){
list.add(arr[i]);
}
Collections.sort(list);
for(int i =0;i<arr.length;i++){
arr[i] = list.get(i);
}
}
public static void print(int num ){
System.out.print(num);
}
public static void print(long num ){
System.out.print(num);
}
public static void println(String s){
System.out.println(s);
}
public static void println(int num){
System.out.println(num);
}
public static void println(long num){
System.out.println(num);
}
public static void println(){
System.out.println();
}
public static int toInt(String s){
return Integer.parseInt(s);
}
public static long toLong(String s){
return Long.parseLong(s);
}
public static String[] nextStringArray()throws IOException{
return bf.readLine().split(" ");
}
public static int nextInt()throws IOException{
return Integer.parseInt(bf.readLine());
}
public static long nextLong()throws IOException{
return Long.parseLong(bf.readLine());
}
public static String nextString()throws IOException{
return bf.readLine();
}
public static int[] nextIntArray(int n)throws IOException{
String[]str = bf.readLine().split(" ");
int[]arr = new int[n];
for(int i =0;i<n;i++){
arr[i] = Integer.parseInt(str[i]);
}
return arr;
}
public static long[] nextLongArray(int n)throws IOException{
String[]str = bf.readLine().split(" ");
long[]arr = new long[n];
for(int i =0;i<n;i++){
arr[i] = Long.parseLong(str[i]);
}
return arr;
}
public static int[][] newIntMatrix(int r,int c)throws IOException{
int[][]arr = new int[r][c];
for(int i =0;i<r;i++){
String[]str = bf.readLine().split(" ");
for(int j =0;j<c;j++){
arr[i][j] = Integer.parseInt(str[j]);
}
}
return arr;
}
public static long[][] newLongMatrix(int r,int c)throws IOException{
long[][]arr = new long[r][c];
for(int i =0;i<r;i++){
String[]str = bf.readLine().split(" ");
for(int j =0;j<c;j++){
arr[i][j] = Long.parseLong(str[j]);
}
}
return arr;
}
}
// if a problem is related to binary string it could also be related to parenthesis
// try to use binary search in the question it might work
// try sorting
// try to think in opposite direction of question it might work in your way
// if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+ or a,b,c,d in general
// gcd(1.p1,2.p2,3.p3,4.p4....n.pn) cannot be greater than 2 it has been proveds | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 9df88db75913c2aad8ce394a654ff474 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class B2_Tokitsukaze_and_Good_01_String_hard_version {
static Scanner in = new Scanner();
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans = new StringBuilder();
static int testCases, n, iterations;
static char a[];
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);
iterations = Math.max(iterations, 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++;
}
}
iterations = 0;
char last = '?';
for(int i = 0; i < n; i += 2) {
if(a[i] == a[i + 1]) {
if(a[i] != last) {
last = a[i];
++iterations;
}
}
}
iterations = Math.max(iterations, 1);
ans.append(ans1).append(" ").append(iterations);
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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 406c9156be20f280bfa3f21cb8658d73 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
// try {
// System.setIn(new FileInputStream("input.txt"));
// System.setOut(new PrintStream(new FileOutputStream("output.txt")));
// } catch (Exception e) {
// System.err.println("Error");
// }
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-- > 0) {
int n = in.nextInt();
in.nextLine();
String s = in.nextLine();
int[] ans = solve(s);
System.out.println(ans[0] + " " + ans[1]);
}
}
static int[] solve(String str) {
int ans = 0;
int ans1 = 0;
char[] s = str.toCharArray();
char ch = '1';
for(int i = 0; i < s.length; i+=2) {
if(s[i] == s[i+1]) {
ch = s[i+1];
break;
}
}
while(true) {
break;
}
for(int i = 0; i < s.length; i+=2) {
if(s[i] == s[i+1]) {
ch = s[i+1];
}
else {
ans++;
s[i] = ch;
s[i+1] = ch;
}
}
ch = 'b';
for(int i = 0; i < s.length; i++) {
if(s[i] != ch) {
ans1++;
ch = s[i];
}
}
return new int[] {ans, ans1};
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | ec5514cd6e30ef86fa04cdaf01fe7992 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) throws IOException {
// Your code goes here
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t-->0){
int n=Integer.parseInt(br.readLine());
String str=br.readLine();
char ch[]=str.toCharArray();
int i;
int ans=0;
int curr=1;
for(i=1;i<n;i++){
if(ch[i]==ch[i-1]){
curr++;
continue;
}
else{
if(curr%2==1){
ans++;
curr=1;
i++;
}
else{
curr=1;
}
}
}
char test[]=new char[n];
for(i=0;i<n;i++)
test[i]=ch[i];
char res='?';
int total=0;
for(i=1;i<n;i+=2){
if(ch[i]==ch[i-1]){
if(res!='?'&&res!=ch[i]){
total++;
res=ch[i];
}
res=ch[i];
}
}
System.out.println(ans+" "+(1+total));
}
}
public static int cot(char a[]){
int i;
int ans=0;
for(i=1;i<a.length;i++){
if(a[i]!=a[i-1]){
ans++;
}
}
return ans+1;
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 54735bb0c4d73d4b56b170d0da634812 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.util.*;
public class CodeForces {
/*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/
public static void solve(int tCase) throws IOException {
int n = sc.nextInt();
char[] str = sc.next().toCharArray();
int cnt = 0;
int prev = '.';
int yoo = 0;
for(int i=0;i<n;i+=2)
if(str[i] != str[i+1])cnt++;
else {
if(str[i] != prev)yoo++;
prev = str[i];
}
out.println(cnt+" "+Math.max(1,yoo));
}
public static void main(String[] args) throws IOException {
openIO();
int testCase = 1;
testCase = sc.nextInt();
for (int i = 1; i <= testCase; i++) solve(i);
closeIO();
}
/*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/
/*--------------------------------------HELPER FUNCTIONS STARTS HERE-----------------------------------------*/
public static int mod = (int) 1e9 + 7;
// public static int mod = 998244353;
public static int inf_int = (int) 1e9;
public static long inf_long = (long)2e18;
public static void _sort(int[] arr, boolean isAscending) {
int n = arr.length;
List<Integer> list = new ArrayList<>();
for (int ele : arr) list.add(ele);
Collections.sort(list);
if (!isAscending) Collections.reverse(list);
for (int i = 0; i < n; i++) arr[i] = list.get(i);
}
public static void _sort(long[] arr, boolean isAscending) {
int n = arr.length;
List<Long> list = new ArrayList<>();
for (long ele : arr) list.add(ele);
Collections.sort(list);
if (!isAscending) Collections.reverse(list);
for (int i = 0; i < n; i++) arr[i] = list.get(i);
}
// time : O(1), space : O(1)
public static int _digitCount(long num,int base){
// this will give the # of digits needed for a number num in format : base
return (int)(1 + Math.log(num)/Math.log(base));
}
// time : O(n), space: O(n)
public static long _fact(int n){
// simple factorial calculator
long ans = 1;
for(int i=2;i<=n;i++) ans = ans * i % mod;
return ans;
}
// time for pre-computation of factorial and inverse-factorial table : O(nlog(mod))
public static long[] factorial , inverseFact;
public static void _ncr_precompute(int n){
factorial = new long[n+1];
inverseFact = new long[n+1];
factorial[0] = inverseFact[0] = 1;
for (int i = 1; i <=n; i++) {
factorial[i] = (factorial[i - 1] * i) % mod;
inverseFact[i] = _modExpo(factorial[i], mod - 2);
}
}
// time of factorial calculation after pre-computation is O(1)
public static int _ncr(int n,int r){
if(r > n)return 0;
return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod);
}
public static int _npr(int n,int r){
if(r > n)return 0;
return (int)(factorial[n] * inverseFact[n - r] % mod);
}
// euclidean algorithm time O(max (loga ,logb))
public static long _gcd(long a, long b) {
while (a>0){
long x = a;
a = b % a;
b = x;
}
return b;
}
// lcm(a,b) * gcd(a,b) = a * b
public static long _lcm(long a, long b) {
return (a / _gcd(a, b)) * b;
}
// binary exponentiation time O(logn)
public static long _modExpo(long x, long n) {
long ans = 1;
while (n > 0) {
if ((n & 1) == 1) {
ans *= x;
ans %= mod;
n--;
} else {
x *= x;
x %= mod;
n >>= 1;
}
}
return ans;
}
// function to find a/b under modulo mod. time : O(logn)
public static long _modInv(long a,long b){
return (a * _modExpo(b,mod-2)) % mod;
}
//sieve or first divisor time : O(mx * log ( log (mx) ) )
public static int[] _seive(int mx){
int[] firstDivisor = new int[mx+1];
for(int i=0;i<=mx;i++)firstDivisor[i] = i;
for(int i=2;i*i<=mx;i++)
if(firstDivisor[i] == i)
for(int j = i*i;j<=mx;j+=i)
if(firstDivisor[j]==j)firstDivisor[j] = i;
return firstDivisor;
}
static class Pair<K, V>{
K ff;
V ss;
public Pair(K ff, V ss) {
this.ff = ff;
this.ss = ss;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || this.getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return ff.equals(pair.ff) && ss.equals(pair.ss);
}
@Override
public int hashCode() {
return Objects.hash(ff, ss);
}
@Override
public String toString(){
return ff.toString()+" "+ss.toString();
}
}
/*--------------------------------------HELPER FUNCTIONS ENDS HERE-----------------------------------------*/
/*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/
static FastestReader sc;
static PrintWriter out;
private static void openIO() throws IOException {
sc = new FastestReader();
out = new PrintWriter(System.out);
}
public static void closeIO() throws IOException {
out.flush();
out.close();
sc.close();
}
private static final class FastestReader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
public FastestReader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public FastestReader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() throws IOException {
int b;
//noinspection StatementWithEmptyBody
while ((b = read()) != -1 && isSpaceChar(b)) {}
return b;
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
final boolean neg = c == '-';
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return neg?-ret:ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
final boolean neg = c == '-';
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return neg?-ret:ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') c = read();
final boolean neg = c == '-';
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.')
while ((c = read()) >= '0' && c <= '9')
ret += (c - '0') / (div *= 10);
return neg?-ret:ret;
}
public String nextLine() throws IOException {
final byte[] buf = new byte[(1<<10)]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
break;
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
din.close();
}
}
/*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/
}
/** Some points to keep in mind :
* 1. don't use Arrays.sort(primitive data type array)
* 2. try to make the parameters of a recursive function as less as possible,
* more use static variables.
* 3. If n = 5000, then O(n^2 logn) need atleast 4 sec to work
* 4. dp[2][n] works faster than dp[n][2]
*
**/ | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 2c61a25eac4f05d98f69cd25dc3dd013 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 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++;
}
}
if (ans * 2 == n) {
out.println(ans + " " + 1);
} else {
int pre = -1;
int cnt = 0;
for (int i = 0; i < n; i += 2) {
if (s.charAt(i) == s.charAt(i + 1)) {
if (s.charAt(i) == '1') {
if (pre != 1) {
pre = 1;
cnt++;
}
} else {
if (pre != 0) {
pre = 0;
cnt++;
}
}
}
}
out.println(ans + " " + cnt);
}
}
// (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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 1c019212b834682ddef5a3bf1036f761 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 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+" "+(res.blocks+1));
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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | f6d782a19a5ce010d95123fcf6d43f04 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.*;
public class cf {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int t=s.nextInt();
for(int i=0;i<t;i++) {
int n=s.nextInt();
String st=s.next();
char[] a=st.toCharArray();
int pref=-1;
int seg=0;
int count=0;
for(int j=0;j<n;j=j+2) {
if(a[j]==a[j+1]) {
if(a[j]-'0' != pref) {
pref=a[j]-'0';
seg++;
}
}
else {
count++;
}
}
System.out.println(count+" "+Math.max(seg,1));
}
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | e06a49f0329c6f0598cffda871935a40 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 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 x=0,y=0,ch=-1;
for(int i=0;i<n;i+=2)
{
if(a[i]!=a[i+1])x++;
else{
if(ch!=a[i]-'0')
y++;
ch=a[i]-'0';
}
}
o.print(x+" "+Math.max(y,1));
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 8ee50e2590aa221ca71a4b03fc58b8b1 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | // Problem: B2. Tokitsukaze and Good 01-String (hard version)
// Contest: Codeforces - Codeforces Round #789 (Div. 2)
// URL: https://codeforces.com/contest/1678/problem/B2
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
import java.util.*;
public class Mytemplate {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t>0){
t--;
int n=sc.nextInt();
String s=sc.next();
int c=1;
int min=0;
int sub=0;
char[] cc=s.toCharArray();
char l='2';
for(int i=0;i<n;i+=2){
if(cc[i+1]==cc[i]){
if(cc[i]!=l){
sub++;
l=cc[i];
}
}else{
min++;
}
}
System.out.println(min+" "+Math.max(1,sub));
}
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | bd72c3bd2cb3497c8eca204c9587bd76 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
public class CodeForces {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
int n = s.nextInt(), count = 0, operations = 0;;
String str = s.next();
char prev = '$';
for (int i = 0; i < n; i += 2) {
if (str.charAt(i) == str.charAt(i + 1)) {
if (prev != str.charAt(i)) {
count++;
prev = str.charAt(i);
}
} else {
operations++;
}
}
sb.append(operations).append(" ").append(Math.max(1, count));
sb.append("\n");
}
System.out.println(sb);
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | ef0a8279a23e972d15cf3bc0e49f2fe9 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 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;
long c=0;
String s=s();
char str[]=s.toCharArray();
char str3[]=s.toCharArray();
char str4[]=s.toCharArray();
int r=0;
int l=0;
while(r<n)
{
char ch=str[r];
while(r<n&&str[r]==ch)
r++;
if((r-l)%2!=0)
{
cnt++;
r++;
}
//c++;
l=r;
}
char str2[]=s.toCharArray();
r=0;
while(r<n)
{
if((str[r]=='0'&&str[r+1]=='1')||(str[r]=='1'&&str[r+1]=='0'))
{
if(r==0)
{
str[r]='1';
str[r+1]='1';
}
else
{
if(str[r-1]=='0')
{
str[r]='0';
str[r+1]='0';
}
else
{
str[r]='1';
str[r+1]='1';
}
}
}
r+=2;
}
r=0;
while(r<n)
{
if((str3[r]=='0'&&str3[r+1]=='1')||(str3[r]=='1'&&str3[r+1]=='0'))
{
if(r==0)
{
str3[r]='0';
str3[r+1]='0';
}
else
{
if(str3[r-1]=='0')
{
str3[r]='0';
str3[r+1]='0';
}
else
{
str3[r]='1';
str3[r+1]='1';
}
}
}
r+=2;
}
r=n-1;
while(r>=0)
{
if((str2[r]=='0'&&str2[r-1]=='1')||(str2[r]=='1'&&str2[r-1]=='0'))
{
if(r==n-1)
{
str2[r]='1';
str2[r-1]='1';
}
else
{
if(str2[r+1]=='0')
{
str2[r]='0';
str2[r-1]='0';
}
else
{
str2[r]='1';
str2[r-1]='1';
}
}
}
r-=2;
}
r=n-1;
while(r>=0)
{
if((str4[r]=='0'&&str4[r-1]=='1')||(str4[r]=='1'&&str4[r-1]=='0'))
{
if(r==n-1)
{
str4[r]='0';
str4[r-1]='0';
}
else
{
if(str4[r+1]=='0')
{
str4[r]='0';
str4[r-1]='0';
}
else
{
str4[r]='1';
str4[r-1]='1';
}
}
}
r-=2;
}
int n1=helper(str);
int n2=helper(str2);
int n3=helper(str3);
int n4=helper(str4);
out.println(cnt+" "+min(n1,n2,n3,n4));
}
out.close();
}
static int helper(char str[])
{
int r=0;
int c=0;
int l=0;
int n=str.length;
while(r<n)
{
char ch=str[r];
while(r<n&&str[r]==ch)
r++;
c++;
l=r;
}
return c;
}
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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | b1d237d44461ff282c4dfcb75b82afa8 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.*;
public class Main {
public static int n,t;
public static void main(String[]args) throws IOException {
Scanner scan = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
n = scan.nextInt();
while (n-- > 0) {
int m = scan.nextInt();
String mid = scan.next();
int res = 0,res1 = 0;
int L = -1;
for(int i = 0;i < mid.length();i += 2){
if(mid.charAt(i) != mid.charAt(i + 1)){
res += 1;
}else{
if(L != mid.charAt(i)){
res1 += 1;
}
L = mid.charAt(i);
}
}
System.out.println(res + " " + Math.max(res1,1));
}
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 11515764fbbcf3ab4fe82baf441a6107 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class B2_Tokitsukaze_and_Good_01_String_hard_version {
static Scanner in = new Scanner();
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans = new StringBuilder();
static int testCases, n, iterations;
static char a[];
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);
iterations = Math.max(iterations, 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++;
}
}
iterations = 0;
char last = '?';
for(int i = 0; i < n; i += 2) {
if(a[i] == a[i + 1]) {
if(a[i] != last) {
last = a[i];
++iterations;
}
}
}
iterations = Math.max(iterations, 1);
ans.append(ans1).append(" ").append(iterations);
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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | a569bf7db11fa4251dc091b834911706 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int w = sc.nextInt();
while (w-- > 0) {
int n = sc.nextInt(), index = 0, len = 0;
char[] c = sc.next().toCharArray();
char last = 'a';
for (int i = 1; i < n; i += 2) {
if (c[i] != c[i - 1]) {
index++;
} else if (last != c[i]) {
last = c[i];
len++;
}
}
sb.append(index).append(" ").append(Math.max(1, len)).append("\n");
}
sc.close();
System.out.println(sb);
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 9419c2ef4d7c78f78718cce81bcff043 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 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();
char[] str=(" "+sc.next()).toCharArray();
int ans1=0,ans2=0;
char last='g';
for(int i=2;i<=n;i+=2) {
if(str[i]!=str[i-1])
ans1++;
else {
if(str[i]!=last) {
ans2++;
last=str[i];
}
}
}
System.out.println(ans1+" "+Math.max(1,ans2));
}
sc.close();
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 1a55ba00b095fbd15314c26e93417a8d | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) {
new B().run();
}
BufferedReader br;
PrintWriter out;
long mod = (long) (1e9 + 7), inf = (long) (3e18);
class pair {
int F, S;
pair(int f, int s) {
F = f; S = s;
}
}
// ---------------------- solve --------------------------
void solve() {
int t=1;
t = ni(); // comment for no test cases
while(t-- > 0) {
//TODO:
int n= ni();
char[] s =ns().toCharArray();
char ch='1';
for(int i=0; i<n; i+=2){
if(s[i]==s[i+1]){
ch=s[i+1];
break;
}
}
int part=0, change=0;
for(int i=0; i<n; i+=2){
if(s[i]==s[i+1]){
ch=s[i+1];
}
else{
change++;
s[i]=ch;
s[i+1]=ch;
}
}
ch='*';
for(int i=0; i<n; i++){
if(ch!=s[i]){
part++;
ch=s[i];
}
}
out.println(change+" "+part);
}
}
// -------- I/O Template -------------
public long pow(long A, long B, long C) {
if(A==0) return 0;
if(B==0) return 1;
long n=pow(A, B/2, C);
if(B%2==0){
return (long)((n*n)%C + C )%C;
}
else{
return (long)(((n*n)%C * A)%C +C)%C;
}
}
char nc() {
return ns().charAt(0);
}
String nLine() {
try {
return br.readLine();
} catch(IOException e) {
return "-1";
}
}
double nd() {
return Double.parseDouble(ns());
}
long nl() {
return Long.parseLong(ns());
}
int ni() {
return Integer.parseInt(ns());
}
int[] na(int n) {
int a[] = new int[n];
for(int i = 0; i < n; i++) a[i] = ni();
return a;
}
long[] nal(int n) {
long a[] = new long[n];
for(int i = 0; i < n; i++) a[i] = nl();
return a;
}
StringTokenizer ip;
String ns() {
if(ip == null || !ip.hasMoreTokens()) {
try {
ip = new StringTokenizer(br.readLine());
if(ip == null || !ip.hasMoreTokens())
ip = new StringTokenizer(br.readLine());
} catch(IOException e) {
throw new InputMismatchException();
}
}
return ip.nextToken();
}
void run() {
try {
if (System.getProperty("ONLINE_JUDGE") == null) {
br = new BufferedReader(new FileReader("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/input.txt"));
out = new PrintWriter("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/output.txt");
} else {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
} catch (FileNotFoundException e) {
System.out.println(e);
}
solve();
out.flush();
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 6c3a6b0d0a1d663ae69fbea2ca12769d | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 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+" ");
int dp[][]=new int[2][n+1];
Arrays.fill(dp[0],-1);
Arrays.fill(dp[1],-1);
sb.append( Math.min( solve(c,0,0,dp),solve(c,1,0,dp))+1 );
sb.append("\n");
}
System.out.println(sb.toString());
}
public static int solve(char c[],int prev,int i,int dp[][])
{
if(i+1>=c.length)
return 0;
if(dp[prev][i]!=-1)
return dp[prev][i];
int res=0;
if(c[i]!=c[i+1])
res=Math.min( solve(c,prev,i+2,dp),1+solve(c,1-prev,i+2,dp));
else if(prev!=c[i]-'0')
res= 1+solve(c,c[i]-'0',i+2,dp);
else
res=solve(c,c[i]-'0',i+2,dp);
dp[prev][i]=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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 350a51a5c1fba8839732936ddacd6dbe | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 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 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 948fdea71b76cc944f9a0f270c7048f5 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
// Map.Entry<Integer,String>dum;
//res=(num1>num2) ? (num1+num2):(num1-num2)
/* Name of the class has to be "Main" only if the class is public. */
public class Pupil
{
static FastReader sc = new FastReader();
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int t=sc.nextInt();
while(t>0){
int n=sc.nextInt();
String s=sc.next();
char ch[]=s.toCharArray();
char arr[]=new char[n];
char c='2';
c=ch[0];
arr[0]=ch[0];
int c1=0;
char pre='2';
int deaf=0;
for(int i=0;i<n;i=i+2)
{
if(ch[i]==ch[i+1])
{
if(ch[i]!=pre)
{
c1++;
pre=ch[i];
}
}
else {
deaf++;
}
}
if(c1==0)
{
c1++;
}
System.out.println(deaf+" "+c1);
// for(int i=0;i<n;i++)
// {
// System.out.print(arr[i]);
// }
//
//
t--;
}
}
// FAST I/O
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static boolean two(int n)//power of two
{
if((n&(n-1))==0)
{
return true;
}
else{
return false;
}
}
public static int factorial(int n)
{
int res = 1, i;
for (i = 2; i <= n; i++)
res *= i;
return res;
}
public static boolean isPrime(long n){
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static int digit(int n)
{
int n1=(int)Math.floor((int)Math.log10(n)) + 1;
return n1;
}
public static long gcd(long a,long b) {
if(b==0)
return a;
return gcd(b,a%b);
}
public static long highestPowerof2(long x)
{
// check for the set bits
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
// Then we remove all but the top bit by xor'ing the
// string of 1's with that string of 1's shifted one to
// the left, and we end up with just the one top bit
// followed by 0's.
return x ^ (x >> 1);
}
public static void yes()
{
System.out.println("YES");
return;
}
public static void no()
{
System.out.println("NO");
return ;
}
public static void al(long arr[],int n)
{
for(int i=0;i<n;i++)
{
arr[i]=sc.nextLong();
}
return ;
}
public static void ai(int arr[],int n)
{
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
return ;
}
}
//CAAL THE BELOW FUNCTION IF PARING PRIORITY IS NEEDED //
// PriorityQueue<pair> pq = new PriorityQueue<>(); **********declare the syntax in the main function******
// pq.add(1,2)///////
// class pair implements Comparable<pair> {
// int value, index;
// pair(int v, int i) { index = i; value = v; }
// @Override
// public int compareTo(pair o) { return o.value - value; }
// }
// User defined Pair class
// class Pair {
// int x;
// int y;
// // Constructor
// public Pair(int x, int y)
// {
// this.x = x;
// this.y = y;
// }
// }
// Arrays.sort(arr, new Comparator<Pair>() {
// @Override public int compare(Pair p1, Pair p2)
// {
// return p1.x - p2.x;
// }
// });
// class Pair {
// int height, id;
//
// public Pair(int i, int s) {
// this.height = s;
// this.id = i;
// }
// }
//Arrays.sort(trips, (a, b) -> Integer.compare(a[1], b[1]));
// ArrayList<ArrayList<Integer>> connections = new ArrayList<ArrayList<Integer>>();
// for(int i = 0; i<n;i++)
// connections.add(new ArrayList<Integer>()); | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | 11e565b8f7c06b0c95e70330a85518f9 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class c731{
public static void main(String[] args) throws IOException{
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
PrintWriter pt = new PrintWriter(System.out);
FastReader sc = new FastReader();
int t = sc.nextInt();
for(int o = 0; o<t;o++) {
int n = sc.nextInt();
String s = sc.next();
int ans = 0;
int max = 1000000000;
int[][] dp = new int[n+1][2];
for(int i = 0 ; i<n-1;i+=2) {
char x = s.charAt(i);
char y = s.charAt(i+1);
if(x != y ) {
ans++;
}
}
for(int i = 0 ; i<n;i++) {
dp[i][0] = max;
dp[i][1] = max;
}
if(s.charAt(0) == '0' && s.charAt(1) == '0') {
dp[0][0] = 1;
dp[1][0] = 1;
}else if(s.charAt(0) == '1' && s.charAt(1) == '1') {
dp[0][1] = 1;
dp[1][1] = 1;
}else {
dp[0][0] = 1;
dp[1][0] = 1;
dp[1][0] = 1;
dp[1][1] = 1;
}
for(int i = 2 ; i<n-1;i+=2) {
char x = s.charAt(i);
char y = s.charAt(i+1);
if(x == y ) {
if(x == '1') {
dp[i][1] = Math.min(dp[i-1][1], dp[i-1][0] + 1);
}else {
dp[i][0] = Math.min(dp[i-1][0], dp[i-1][1] + 1);
}
}else {
dp[i][0] = Math.min(dp[i-1][0], dp[i-1][1] + 1);
dp[i][1] = Math.min(dp[i-1][0]+1, dp[i-1][1]);
}
dp[i+1][0] = dp[i][0];
dp[i+1][1] = dp[i][1];
}
//for(int i = 0 ; i<n;i++) {
// System.out.println(dp[i][0] + " " + dp[i][1]);
//}
System.out.println(ans + " " + Math.min(dp[n-1][0], dp[n-1][1]));
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------
public static boolean chk(char[] arr , int n) {
for(int i = 0 ; i<n;i++) {
if(arr[i]>'a') {
return false;
}
}
return true;
}
public static void dfs(HashMap<Integer, ArrayList<Integer>> map, HashSet<Integer> vis , int s, ArrayList<Integer> al ) {
if(!vis.contains(s)) {
al.add(s);
}
vis.add(s);
if(map.get(s).size() == 0) {
StringBuilder sb = new StringBuilder();
sb.append(al.size() + "\n");
for(int z : al) {
sb.append(z + " ");
}
System.out.println(sb);
al.clear();
}
for(int x : map.get(s)) {
if(vis.contains(x)) {
continue;
}
dfs(map, vis, x,al);
}
}
public static ArrayList<Long> printDivisors(long n)
{
ArrayList<Long> al = new ArrayList<>();
for (long i=1; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
if (n/i == i)al.add(i);
else {
al.add(i);
al.add(n/i);
}
}
}
return al;
}
public static boolean palin(String s) {
int n = s.length();
int i = 0;
int j = n-1;
while(i<=j) {
if(s.charAt(i)!=s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
public static boolean check(int[] arr , int n , int v , int l ) {
int x = v/2;
int y = v/2;
// System.out.println(x + " " + y);
if(v%2 == 1 ) {
x++;
}
for(int i = 0 ; i<n;i++) {
int d = l - arr[i];
int c = Math.min(d/2, y);
y -= c;
arr[i] -= c*2;
if(arr[i] > x) {
return false;
}
x -= arr[i];
}
return true;
}
public static int cnt_set(long x) {
long v = 1l;
int c =0;
int f = 0;
while(v<=x) {
if((v&x)!=0) {
c++;
}
v = v<<1;
}
return c;
}
public static int lis(int[] arr,int[] dp) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
dp[0]= 1;
for(int i = 1 ; i<n;i++) {
int x = al.get(al.size()-1);
if(arr[i]>x) {
al.add(arr[i]);
}else {
int v = lower_bound(al, 0, al.size(), arr[i]);
// System.out.println(v);
al.set(v, arr[i]);
}
dp[i] = al.size();
}
//return al.size();
return al.size();
}
public static int lis2(int[] arr,int[] dp) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(-arr[n-1]);
dp[n-1] = 1;
// System.out.println(al);
for(int i = n-2 ; i>=0;i--) {
int x = al.get(al.size()-1);
// System.out.println(-arr[i] + " " + i + " " + x);
if((-arr[i])>x) {
al.add(-arr[i]);
}else {
int v = lower_bound(al, 0, al.size(), -arr[i]);
// System.out.println(v);
al.set(v, -arr[i]);
}
dp[i] = al.size();
}
//return al.size();
return al.size();
}
static int cntDivisors(int n){
int cnt = 0;
for (int i=1; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
if (n/i == i)
cnt++;
else
cnt+=2;
}
}
return cnt;
}
public static long power(long x, long y, long p){
long res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0){
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static long ncr(long[] fac, int n , int r , long m) {
if(r>n) {
return 0;
}
return fac[n]*(modInverse(fac[r], m))%m *(modInverse(fac[n-r], m))%m;
}
public static int lower_bound(ArrayList<Integer> arr,int lo , int hi, int k)
{
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (arr.get(mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==arr.size())
{
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> arr,int lo , int hi, int k)
{
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (arr.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==arr.size())
{
return -1;
}
return s;
}
// -----------------------------------------------------------------------------------------------------------------------------------------------
public static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b % a, a);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
public static long modInverse(long a, long m){
long m0 = m;
long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
// q is quotient
long q = a / m;
long t = m;
// m is remainder now, process
// same as Euclid's algo
m = a % m;
a = t;
t = y;
// Update x and y
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
//_________________________________________________________________________________________________________________________________________________________________
// private static int[] parent;
// private static int[] size;
public static int find(int[] parent, int u) {
while(u != parent[u]) {
parent[u] = parent[parent[u]];
u = parent[u];
}
return u;
}
private static void union(int[] parent,int[] size,int u, int v) {
int rootU = find(parent,u);
int rootV = find(parent,v);
if(rootU == rootV) {
return;
}
if(size[rootU] < size[rootV]) {
parent[rootU] = rootV;
size[rootV] += size[rootU];
} else {
parent[rootV] = rootU;
size[rootU] += size[rootV];
}
}
//-----------------------------------------------------------------------------------------------------------------------------------
//segment tree
//for finding minimum in range
public static void build(int [] seg,int []arr,int idx, int lo , int hi) {
if(lo == hi) {
seg[idx] = arr[lo];
return;
}
int mid = (lo + hi)/2;
build(seg,arr,2*idx+1, lo, mid);
build(seg,arr,idx*2+2, mid +1, hi);
// seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]);
// seg[idx] = seg[idx*2+1]+ seg[idx*2+2];
// seg[idx] = Math.min(seg[idx*2+1], seg[idx*2+2]);
seg[idx] = seg[idx*2+1] * seg[idx*2+2];
}
//for finding minimum in range
public static int query(int[]seg,int idx , int lo , int hi , int l , int r) {
if(lo>=l && hi<=r) {
return seg[idx];
}
if(hi<l || lo>r) {
return 1;
}
int mid = (lo + hi)/2;
int left = query(seg,idx*2 +1, lo, mid, l, r);
int right = query(seg,idx*2 + 2, mid + 1, hi, l, r);
// return Math.min(left, right);
//return gcd(left, right);
// return Math.min(left, right);
return left * right;
}
// // for sum
//
//public static void update(int[]seg,int idx, int lo , int hi , int node , int val) {
// if(lo == hi) {
// seg[idx] += val;
// }else {
//int mid = (lo + hi )/2;
//if(node<=mid && node>=lo) {
// update(seg, idx * 2 +1, lo, mid, node, val);
//}else {
// update(seg, idx*2 + 2, mid + 1, hi, node, val);
//}
//seg[idx] = seg[idx*2 + 1] + seg[idx*2 + 2];
//
//}
//}
//---------------------------------------------------------------------------------------------------------------------------------------
//
//static void shuffleArray(int[] ar)
//{
// // If running on Java 6 or older, use `new Random()` on RHS here
// Random rnd = ThreadLocalRandom.current();
// for (int i = ar.length - 1; i > 0; i--)
// {
// int index = rnd.nextInt(i + 1);
// // Simple swap
// int a = ar[index];
// ar[index] = ar[i];
// ar[i] = a;
// }
//}
// static void shuffleArray(coup[] ar)
// {
// // If running on Java 6 or older, use `new Random()` on RHS here
// Random rnd = ThreadLocalRandom.current();
// for (int i = ar.length - 1; i > 0; i--)
// {
// int index = rnd.nextInt(i + 1);
// // Simple swap
// coup a = ar[index];
// ar[index] = ar[i];
// ar[i] = a;
// }
// }
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
}
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 coup{
int a;
int b;
public coup(int a , int b) {
this.a = a;
this.b = b;
}
}
class tripp{
int a;
int b;
double c;
public tripp(int a , int b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | c0fc7365894972407167ac8c2e2b29ad | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
Task solver = new Task();
solver.solve(in.nextInt(), in, out);
out.close();
}
static class Task {
public void solve(int testNumber, InputReader in, PrintWriter out) {
for (int i = 0; i < testNumber; i++) {
int n = in.nextInt();
int[] solve = new Solution().solve(in.next());
out.println(solve[0] + " " + solve[1]);
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
class Solution {
public int[] solve(String s) {
int cnt = 0;
char[] chars = s.toCharArray();
int n = chars.length;
int[] arr = new int[n / 2];
for (int i = 1; i < chars.length; i += 2) {
if (chars[i] != chars[i - 1]) {
cnt++;
arr[i / 2] = -1;
} else {
arr[i / 2] = chars[i] - '0';
}
}
int t = 1;
int cur = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == cur) {
continue;
}
if (cur == -1) {
cur = arr[i];
continue;
}
if (arr[i] == -1) {
continue;
}
t++;
cur = arr[i];
}
return new int[]{cnt, t};
}
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | ccb14ee2bd59cb2eca02159371e94153 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
/*
* Author: Atuer
*/
public class Main
{
// ==== Solve Code ====//
static int INF = 2000000010;
public static void csh()
{
}
public static void main(String[] args) throws IOException
{
// csh();
int t = in.nextInt();
while (t-- > 0)
{
solve();
out.flush();
}
out.close();
}
public static void solve()
{
int n = in.nextInt();
char[] s = in.next().toCharArray();
int ans = 0;
int[] qj = new int[n];
Arrays.fill(qj, -2);
int idx = 0;
for (int i = 0; i < n; i += 2)
{
if (s[i] != s[i + 1])
ans++;
else
qj[idx++] = s[i] - '0';
}
int cot = 1;
for (int i = 0; i < idx - 1; i++)
if (qj[i] != qj[i + 1])
cot++;
//out.println(Arrays.toString(qj));
out.println(ans+" "+cot);
}
public static class Node
{
int x, y, k;
public Node(int x, int y, int k)
{
this.x = x;
this.y = y;
this.k = k;
}
}
// ==== Solve Code ====//
// ==== Template ==== //
public static long cnm(int a, int b)
{
long sum = 1;
int i = a, j = 1;
while (j <= b)
{
sum = sum * i / j;
i--;
j++;
}
return sum;
}
public static int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
public static int lcm(int a, int b)
{
return (a * b) / gcd(a, b);
}
public static void gbSort(int[] a, int l, int r)
{
if (l < r)
{
int m = (l + r) >> 1;
gbSort(a, l, m);
gbSort(a, m + 1, r);
int[] t = new int[r - l + 1];
int idx = 0, i = l, j = m + 1;
while (i <= m && j <= r)
if (a[i] <= a[j])
t[idx++] = a[i++];
else
t[idx++] = a[j++];
while (i <= m)
t[idx++] = a[i++];
while (j <= r)
t[idx++] = a[j++];
for (int z = 0; z < t.length; z++)
a[l + z] = t[z];
}
}
// ==== Template ==== //
// ==== IO ==== //
static InputStream inputStream = System.in;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(System.out);
static class InputReader
{
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream)
{
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next()
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
{
try
{
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e)
{
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
boolean hasNext()
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
{
try
{
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e)
{
return false;
// TODO: handle exception
}
}
return true;
}
public String nextLine()
{
String str = null;
try
{
str = reader.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 BigInteger nextBigInteger()
{
return new BigInteger(next());
}
public BigDecimal nextBigDecimal()
{
return new BigDecimal(next());
}
}
// ==== IO ==== //
} | Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 8 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | d57c77ff1a5cb387b8b32c5b456f9989 | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes |
import java.io.*;
import java.util.*;
public class TokitsukazeAndGoodString_Hard {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int testcases = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (testcases-->0){
int n = sc.nextInt();
String s = sc.next();
int ops = 0;
char last = '2';
int c = 0;
for(int i=0;i<n;i+=2){
if(s.charAt(i) != s.charAt(i+1)){
ops++;
}
else{
if(s.charAt(i) != last){
c++;
}
last = s.charAt(i);
}
}
sb.append(ops).append(" ").append(Math.max(c, 1)).append("\n");
}
System.out.println(sb);
sc.close();
}
static class Soumit {
final private int BUFFER_SIZE = 1 << 18;
final private DataInputStream din;
final private byte[] buffer;
private PrintWriter pw;
private int bufferPointer, bytesRead;
StringTokenizer st;
public Soumit() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Soumit(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public void streamOutput(String file) throws IOException {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
pw = new PrintWriter(bw);
}
public void println(String a) {
pw.println(a);
}
public void print(String a) {
pw.print(a);
}
public String readLine() throws IOException {
byte[] buf = new byte[3000064]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public void sort(int[] arr) {
ArrayList<Integer> arlist = new ArrayList<>();
for (int i : arr)
arlist.add(i);
Collections.sort(arlist);
for (int i = 0; i < arr.length; i++)
arr[i] = arlist.get(i);
}
public void sort(long[] arr) {
ArrayList<Long> arlist = new ArrayList<>();
for (long i : arr)
arlist.add(i);
Collections.sort(arlist);
for (int i = 0; i < arr.length; i++)
arr[i] = arlist.get(i);
}
public int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = nextDouble();
}
return arr;
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
/*if (din == null)
return;*/
if (din != null) din.close();
if (pw != null) pw.close();
}
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 17 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | c03ef10b5f146bc0fd8c0dd9b0f7f9fc | train_108.jsonl | 1652020500 | This is the hard 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? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | 256 megabytes |
/**
* @author luke_nguyen
* @link https://codeforces.com/contest/1709/problem/E
*/
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;
public class codeforces_1678B2 {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
for (int numCases = scanner.nextInt(); numCases > 0; --numCases) {
int len = scanner.nextInt();
String s = scanner.next();
int numSeg0 = 1;
int numSeg1 = 1;
int numChanges = 0;
for (int i = 0; i < len / 2; i++) {
int temp0 = numSeg0;
int temp1 = numSeg1;
if (s.charAt(i * 2) != s.charAt(i * 2 + 1)) {
numChanges++;
numSeg0 = Math.min(temp0, temp1 + 1);
numSeg1 = Math.min(temp1, temp0 + 1);
} else if (s.charAt(i * 2) == '0') {
numSeg0 = Math.min(temp0, temp1 + 1);
numSeg1 = len;
} else {
numSeg1 = Math.min(temp1, temp0 + 1);
numSeg0 = len;
}
}
writer.write(String.format("%d %d\n", numChanges, Math.min(numSeg0, numSeg1)));
}
scanner.close();
writer.flush();
}
}
| Java | ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"] | 1 second | ["3 2\n0 3\n0 1\n0 1\n3 1"] | 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, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$.In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | Java 17 | standard input | [
"dp",
"greedy",
"implementation"
] | 8c7844673f2030371cbc0cb19ab99b35 | 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$$$. | 1,800 | For each test case, print a single line with two integers — the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. | standard output | |
PASSED | a065d3512f41f527eadd42dcc1a9a042 | 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 IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = Integer.parseInt(br.readLine());
while(t-->0){
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int[] arr = new int[n];
ArrayList<Integer> seg = new ArrayList<>();
int num = 1;
String s = br.readLine();
arr[0] = Integer.parseInt(String.valueOf(s.charAt(0)));
for(int i=1; i<n; i++){
arr[i] = Integer.parseInt(String.valueOf(s.charAt(i)));
if(arr[i]==arr[i-1]){
num++;
if(i==n-1){
seg.add(num);
num = 1;
}
}
else{
seg.add(num);
num = 1;
if(i==n-1){
seg.add(1);
}
}
}
boolean odd = false;
int ans = 0;
int times = 0;
for(int i:seg){
if(odd&&i%2==0){
times++;
}
else if(odd&&i%2==1){
ans++;
odd=false;
}
else if(i%2==1){
odd=true;
}
else{
odd=false;
}
}
pw.println(ans+times);
}
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 11 | 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 | b38c0c2af7f1dcb9ac47dcd6fc232a9e | 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 IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = Integer.parseInt(br.readLine());
while(t-->0){
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int[] arr = new int[n];
ArrayList<Integer> seg = new ArrayList<>();
int num = 1;
String s = br.readLine();
arr[0] = Integer.parseInt(String.valueOf(s.charAt(0)));
for(int i=1; i<n; i++){
arr[i] = Integer.parseInt(String.valueOf(s.charAt(i)));
if(arr[i]==arr[i-1]){
num++;
if(i==n-1){
seg.add(num);
num = 1;
}
}
else{
seg.add(num);
num = 1;
if(i==n-1){
seg.add(1);
}
}
}
boolean odd = false;
int ans = 0;
int old = 0;
int even = 0;
int times = 0;
boolean tran = false;
for(int i:seg){
if(odd&&i%2==0){
times++;
if(tran){
even+=i;
tran = false;
}
else{
old+=i;
tran = true;
}
}
else if(odd&&i%2==1){
ans+=times+1;
even=0; old=0;
odd=false;
times = 0;
}
else if(i%2==1){
odd=true;
}
else{
odd=false;
}
}
pw.println(ans);
}
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 11 | 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 | aa4217b4b94e8a7543f5c9fd85cef23e | 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();
int[] arr = new int[n];
for (int i = 0 ; i < n ; i++) {
arr[i] = s.charAt(i);
}
solve(n, arr);
tc--;
}
}
public static void solve(int n, int[] arr) {
ArrayList<Integer> arrlist = new ArrayList<>();
int i=0;
while(i<arr.length){
int ele = arr[i];
int count=0;
while(i<arr.length && arr[i]==ele){
count++;
i++;
}
arrlist.add(count);
}
int count=0;
i=1;
while(i<arrlist.size()){
if (arrlist.get(i-1) % 2 == 0 && arrlist.get(i) % 2 == 0) {
i++;
continue;
}if (arrlist.get(i-1) % 2 == 0 && arrlist.get(i) % 2 != 0) {
i++;
continue;
}
else {
arrlist.set(i,arrlist.get(i)+1);
count++;
i++;
}
}
System.out.println(count);
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 11 | 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 | 22b820c9a471ddd29a8ac321e0117b36 | 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 extends PrintWriter {
Main() { super(System.out); }
static boolean cases = true;
// Solution
void solve(int t) {
int n = sc.nextInt();
char a[] = sc.readCharArray(n);
ArrayList<Integer> al = new ArrayList<>();
int size = 1;
for (int i = 1; i < n; i++) {
if (a[i] == a[i - 1]) size++;
else {
al.add(size);
size = 1;
}
}
al.add(size);
int ans = 0;
int prev = al.get(0);
for (int i = 1; i < al.size(); i++) {
if (prev % 2 != 0) {
prev = al.get(i) - 1;
ans++;
} else prev = al.get(i);
}
System.out.println(ans);
}
public static void main(String[] args) {
Main obj = new Main();
int c = 1;
for (int t = (cases ? sc.nextInt() : 0); t > 1; t--, c++) obj.solve(c);
obj.solve(c);
obj.flush();
}
void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
int[] readIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
char[] readCharArray(int n) {
char a[] = new char[n];
String s = sc.next();
for (int i = 0; i < n; i++) { a[i] = s.charAt(i); }
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() { return Long.parseLong(next()); }
}
private static final FastScanner sc = new FastScanner();
private PrintWriter out = 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 11 | 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 | bf6f444e62ce677ffc3d2a4599498d01 | 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 extends PrintWriter {
Main() { super(System.out); }
static boolean cases = true;
// Solution
void solve(int t) {
int n = sc.nextInt();
char a[] = sc.readCharArray(n);
int size = 1;
int ans = 0;
int c[] = { 0, 0 };
for (char ch : a) c[ch - '0']++;
for (int i = 0; i < n - 1; i++) {
if (a[i] == a[i + 1]) size++;
else {
if (size % 2 == 1) {
ans++;
size = 1;
i++;
} else size = 1;
}
}
System.out.println(Math.min(ans, Math.min(c[0], c[1])));
}
public static void main(String[] args) {
Main obj = new Main();
int c = 1;
for (int t = (cases ? sc.nextInt() : 0); t > 1; t--, c++) obj.solve(c);
obj.solve(c);
obj.flush();
}
void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
int[] readIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
char[] readCharArray(int n) {
char a[] = new char[n];
String s = sc.next();
for (int i = 0; i < n; i++) { a[i] = s.charAt(i); }
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() { return Long.parseLong(next()); }
}
private static final FastScanner sc = new FastScanner();
private PrintWriter out = 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 11 | 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 | 1903e0fc0392ca039e129d4317fd9224 | 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{
public 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 swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static class Pair{
int sib;
boolean inj;
Pair(int x, boolean y) {
sib=x;
inj=y;
}
}
static long mod = 1000000007;
static boolean ans= false;
static StringBuffer sb = new StringBuffer("");
public static void main(String[] args) throws Exception
{
//Read input from user
//Scanner scn = new Scanner(System.in);
FastReader scn = new FastReader();
int t = scn.nextInt();
while(t>0) {
int n = scn.nextInt();
String s = scn.next();
int ans=0;
int i=0;
int ss=0;
int eve=0;
int o=0;
while(i<n) {
int j=i;
int c=0;
while(j<n && s.charAt(j)==s.charAt(i)) {
c++;
j++;
}
if(c%2!=0 ) {
o++;
if(ss!=0) {
// System.out.println(eve);
ans+=eve;
ss=0;
}else {
ss++;
}
eve=0;
}else {
if(ss!=0) {
eve++;
}
}
i=j;
}
ans= ans+(o/2);
System.out.println(ans);
t--;
}
}
public static long gcd(long a,long b){
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static long lcm( long a,long b)
{
long g = gcd(a,b);
return (a*b)/g;
}
public static void find(long a, List<Long> div) {
for(long i=1;i<=Math.sqrt(a);i++) {
if(a%i==0) {
div.add(i);
if((a/i)!=i) {
div.add(a/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 11 | 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 | ed89d34e291be8e42eebdd08dbccbb8e | 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 MyProgram
{
public static void main(String[] args) throws FileNotFoundException
{
//Scanner sc = new Scanner(new File("text.txt"));
Scanner sc = new Scanner(System.in);
int counter = sc.nextInt();
for(int i = 0; i < counter; i++)
{
ArrayList<Integer> List = new ArrayList<>();
int loop = sc.nextInt();
String word = sc.next();
int count = 0;
char index = word.charAt(0);
int mainCount = 0;
for(int j = 0; j < loop; j++)
{
if(index == word.charAt(j))
{
count++;
}
else
{
mainCount++;
index = word.charAt(j);
if(count%2 != 0)
{
List.add(mainCount-1);
}
count = 1;
}
}
if(count%2 != 0)
{
mainCount++;
List.add(mainCount-1);
}
System.out.println(solver(List));
}
}
public static Long solver(ArrayList<Integer> oddList)
{
long counter = 0L;
for(int i = 0; i < oddList.size(); i+=2)
{
counter+=(Math.abs(oddList.get(i)-oddList.get(i+1)));
}
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 11 | 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 | 3257d98501a8990dd34cee304e9389ef | 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.OutputStream;
import java.io.PrintWriter;
import java.util.*;
public class coding {
public static void main(String args[] ) throws Exception {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int arr[]=new int[n];
String s=sc.next();
for(int i=0;i<n;i++)
{
arr[i]=s.charAt(i);
}
int c=1;
int x=0;
for(int i=0;i<n-1;i++)
{
if(arr[i+1]==arr[i])
{
c++;
}
else
{
if(c%2==1)
{
arr[i+1]=1-arr[i+1];
x++;
i++;
}
c=1;
}
}
// int c1=1;
// for(int i=0;i<n-1;i++)
// {
// if(arr[i+1]==arr[i])
// {
// c1++;
// }
// else
// {
// if(c1%2==1)
// {
// arr[i+1]=1-arr[i+1];
// x1++;
// }
// c1=1;
// }
// }
// System.out.println(Math.min(x1, x));
System.out.println(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 11 | 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 | d80418a51e714f88efb8749d7cfa41bf | 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;
/**
* BadAndGoodStrings
*/
public class BadAndGoodStrings {
public static List<Integer> divider(String test_case) {
List<Integer> res = new ArrayList<>();
int current = -1;
int currentLen = 0;
for (char item: test_case.toCharArray()) {
int value = Character.getNumericValue(item);
if (current == -1) {
current = value;
currentLen += 1;
} else if (value == 1 || value == 0) {
if (value == current) {
currentLen += 1;
} else {
current = value;
res.add(currentLen);
currentLen = 1;
}
} else {
System.out.println(item + " " + value);
throw new IllegalArgumentException(test_case);
}
}
return res;
}
public static boolean isGood(List<Integer> current_splitting) {
for (Integer integer : current_splitting) {
if (integer % 2 != 0) {
return false;
}
}
return true;
}
public static int sorted(List<Integer> current_splitting) {
// (a, b) -> (a+1, b-1)
// OO -> EE, EE -> OO, OE -> EO, EO -> OE
// OEO -> EOO -> EEE, OEEO -> EOEO -> EOOE -> EEEE
// OEEEO -> EOEEO -> EOOOO -> EEEEE
int number_of_swaps = 0;
for (int k = 0; k < 20; k++) {
int currentLen = 0;
// Even = 0, Odd = 1
int prevType = -1;
for (int i = 0; i < current_splitting.size(); i++) {
int item = current_splitting.get(i);
int currentType = item % 2;
if (prevType == -1) {
currentLen += 1;
} else {
if (prevType == currentType) {
currentLen+=1;
} else {
if (currentLen % 2 != 0) {
int prevItem = current_splitting.get(i-1);
if (item!=0) {
current_splitting.set(i-1, prevItem+1);
current_splitting.set(i, item-1);
} else {
current_splitting.set(i-1, prevItem-1);
current_splitting.set(i, item+1);
}
number_of_swaps+=2;
} else if (prevType == 1 && currentLen > 0) {
current_splitting.set(i-2, current_splitting.get(i-2)+1);
current_splitting.set(i-1, current_splitting.get(i-1)-1);
number_of_swaps+=2;
}
currentLen=1;
}
}
prevType = currentType;
}
if (isGood(current_splitting)) {
System.out.println(current_splitting);
return number_of_swaps;
} else {
System.out.println(current_splitting);
}
}
return number_of_swaps;
}
public static int[] stringToArray(String test_case) {
int[] arr = new int[test_case.length()];
int i = 0;
for (char ch : test_case.toCharArray()) {
arr[i]=Character.getNumericValue(ch);
i+=1;
}
return arr;
}
public static int solve(int n, int[] test_case) {
int swaps = 0;
int group_length = 1;
for (int i = 1; i < n; i++) {
if (test_case[i-1] == test_case[i]) {
group_length+=1;
}
else if (group_length % 2 != 0) {
test_case[i]=test_case[i-1];
swaps+=1;
group_length=0;
}
else {
group_length=1;
}
}
return swaps;
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int num_cases=input.nextInt();
for (int i = 0; i < num_cases-1; i++) {
int n =input.nextInt();
String test_case=input.next();
int[] test_arr = stringToArray(test_case);
System.out.println(solve(n, test_arr));
}
int n =input.nextInt();
String test_case=input.next();
int[] test_arr = stringToArray(test_case);
System.out.print(solve(n, test_arr));
input.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 11 | 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 | f10507449d23b00774172c745dd77fcd | 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 obj = new Scanner(System.in);
int t = obj.nextInt();
while(t-->0){
int n = obj.nextInt();
String str = obj.next();
int count = 0;
for(int i = 0;i<n;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 11 | 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 | c18ee664d488551c9115c510a1ed63fe | 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 s = new Scanner(System.in);
int T = s.nextInt();
for(int t=0;t<T;t++) {
String ip;
int n, r = 0;
n = s.nextInt();
ip = s.next();
for(int i=0;i<n;i+=2) {
if(ip.charAt(i+1) == ip.charAt(i)) {
continue;
}
r += 1;
}
System.out.println(r);
}
}
} | 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 11 | 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 | e2a63606b833964757bc41f8e23faede | 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 E {
// public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
// {
//
// List<Map.Entry<Integer, Integer> > list =
// new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
//
// Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
// public int compare(Map.Entry<Integer, Integer> o1,
// Map.Entry<Integer, Integer> o2)
// {
// return (o2.getValue()).compareTo(o1.getValue());
// }
// });
//
// HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
// for (Map.Entry<Integer, Integer> aa : list) {
// temp.put(aa.getKey(), aa.getValue());
// }
// return temp;
// }
// static int binarySearch(ArrayList<Integer> arr, int l, int r, int x)
// {
// while (l <= r) {
// int m = l + (r - l) / 2;
//
// // Check if x is present at mid
// if (arr.get(m) == x)
// return m;
//
// // If x greater, ignore left half
// if (arr.get(m) < x)
// l = m + 1;
//
// // If x is smaller, ignore right half
// else
// r = m - 1;
// }
//
// // if we reach here, then element was
// // not present
// return l;
// }
// static int binarySearch1(ArrayList<Integer> arr, int l, int r, int x)
// {
// while (l <= r) {
// int m = l + (r - l) / 2;
//
// // Check if x is present at mid
// if (arr.get(m) == x)
// return m;
//
// // If x greater, ignore left half
// if (arr.get(m) < x)
// l = m + 1;
//
// // If x is smaller, ignore right half
// else
// r = m - 1;
// }
//
// // if we reach here, then element was
// // not present
// return l-1;
// }
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
String s = sc.next();
int ans = 0;
ArrayList<String> list = new ArrayList<String> ();
int count = 1;
for(int i=1;i<n;i++) {
if(s.charAt(i)!=s.charAt(i-1)) {
if(count%2!=0) {
ans++;
count=2;
}
else {
count=1;
}
}
else {
count++;
}
}
if(count%2!=0)
ans++;
sb.append(ans +"\n");
}
System.out.print(sb.toString());
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 11 | 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 | 22371aaa890f1434dd2d35b470cdde18 | 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 pw = new PrintWriter(System.out);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
test(sc, pw);
}
pw.flush();
sc.close();
}
public static void test(Scanner sc, PrintWriter pw) throws IOException {
int n = sc.nextInt();
char[] chars = sc.next().toCharArray();
int count = 1;
boolean isBeauty = true;
List<Integer> segs = new ArrayList<>();
for (int i = 0; i < n - 1; i++) {
if (chars[i] == chars[i + 1]) {
count++;
} else {
if (count % 2 != 0) {
isBeauty = false;
}
segs.add(count);
count = 1;
}
}
segs.add(count);
if (isBeauty) {
pw.println(0);
} else {
// 11 000 11 000
// 3 2 2 3
// oe....eo -> eoeo -> eeoo -> eeee (3)
//
// oeeooeeo
// 0 3 4 7 8 10 12
// oeo -> eoo -> eeee (2)
// oe oe oeo -> ooeeoeo -> ooeeeoo -> 4
// oe oe oe eo
// 3 3 2 2 ooee -> ee
// eooe 11 001 111 00
int ans = 0;
List<Integer> pos = new ArrayList<>();
for (int i = 0; i < segs.size(); i++) {
if (segs.get(i) % 2 != 0) {
pos.add(i);
}
}
for (int i = 0; i < pos.size(); i += 2) {
ans += pos.get(i + 1) - pos.get(i);
}
pw.println(ans);
}
}
public static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String s) throws FileNotFoundException {
br = new BufferedReader(new InputStreamReader(new FileInputStream(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 void close() throws IOException {
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 11 | 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 | c8a1350e52f21c58bc50f50e2fcc8fd3 | 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 B2 {
static class Scan {
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(int arr[],int l1,int r1,int l2,int r2) {
int tmp[]=new int[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(long arr[],int l1,int r1,int l2,int r2) {
long tmp[]=new long[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void main(String args[]) throws IOException {
Scan input=new Scan();
StringBuilder ans=new StringBuilder("");
int test=input.scanInt();
for(int tt=1;tt<=test;tt++) {
int n=input.scanInt();
StringBuilder str=new StringBuilder(input.scanString());
ArrayList<Integer> sec=new ArrayList<>();
for(int i=0;i<str.length();i++) {
int j=i;
int cnt=0;
while(j<n && str.charAt(j)==str.charAt(i)) {
cnt++;
j++;
}
sec.add(cnt);
i=j-1;
}
int cnt=0;
int prev=-1;
for(int i=0;i<sec.size();i++) {
if(sec.get(i)%2==0) {
continue;
}
if(prev==-1) {
prev=i;
continue;
}
int mid=(prev+i)/2;
sec.set(prev, sec.get(prev)-1);
sec.set(i, sec.get(i)-1);
sec.set(mid, sec.get(mid)+1+1);
cnt+=i-prev;
prev=-1;
}
int sec_cnt=sec.size();
for(int i=0;i<sec.size();i++) {
if(sec.get(i)==0) {
sec_cnt-=2;
}
}
ans.append(cnt+" "+"\n");
}
System.out.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 11 | 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 | 5d61613a37a9f576db86085bb76a2c0d | 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 ProgramJava {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int m = 1; m<=T; m++){
int n = sc.nextInt();
String str = sc.next();
int ls = 1;
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i<str.length()-1; i++){
char init = str.charAt(i);
if (init == str.charAt(i+1)){
ls++;
}
else {
arr.add(ls);
ls = 1;
}
}
arr.add(ls);
int ops = 0;
for (int i = 0; i<arr.size()-1; i++){
if (arr.get(i)%2!=0){
for (int j = i+1; j<arr.size(); j++){
if (arr.get(j)%2!=0){
ops = ops + j-i;
if (j<arr.size()-1) {
i = j;
}
else {
i = j-1;
}
break;
}
}
}
}
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 11 | 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 | fb956b43d162cabd0014710f1b7bf10b | 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 practice {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new PrintWriter(System.out)));
StringBuilder sb = new StringBuilder();
int t = Integer.parseInt(br.readLine());
while (t --> 0) {
int n = Integer.parseInt(br.readLine());
String s = br.readLine();
int tot = 0;
for (int i = 0; i < n; i += 2) {
if (s.charAt(i) != s.charAt(i + 1)) tot++;
}
sb.append(tot).append("\n");
}
pw.println(sb.toString().trim());
pw.close();
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 11 | 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 | 1ac8e27a6b7a4142e51a4dfe91835568 | 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 prefixremoval {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
int t = Integer.parseInt(s);
StringBuilder ans = new StringBuilder();
for (int i = 0; i < t; i++) {
int n=Integer.parseInt(in.nextLine());
s=in.nextLine();
int c=1;
int x=0;
for(int j=1; j<n; j++)
{
if (s.charAt(j)!=s.charAt(j-1))
{
if (c%2==1)
{
x++;
c=0;
}
else c=1;
}
else c++;
}
ans.append(x+"\n");
}
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 11 | 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 | 3c2bec81fe611122053f78603386a830 | 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.nio.file.FileAlreadyExistsException;
import java.util.*;
public class roughCodeforces {
public static boolean isok(long x, long h, long k){
long sum = 0;
if(h > k){
long t1 = h - k;
long t = t1 * k;
sum += (k * (k + 1)) / 2;
sum += t - (t1 * (t1 + 1) / 2);
}else{
sum += (h * (h + 1)) / 2;
}
if(sum < x){
return true;
}
return false;
}
public static boolean binary_search(long[] a, long k){
long low = 0;
long high = a.length - 1;
long mid = 0;
while(low <= high){
mid = low + (high - low) / 2;
if(a[(int)mid] == k){
return true;
}else if(a[(int)mid] < k){
low = mid + 1;
}else{
high = mid - 1;
}
}
return false;
}
public static long lowerbound(long a[], long ddp){
long low = 0;
long high = a.length;
long mid = 0;
while(low < high){
mid = low + (high - low)/2;
if(a[(int)mid] == ddp){
return mid;
}
if(a[(int)mid] < ddp){
low = mid + 1;
}else{
high = mid;
}
}
// if(low + 1 < a.length && a[(int)low + 1] <= ddp){
// low++;
// }
if(low == a.length && low != 0){
low--;
return low;
}
if(a[(int)low] > ddp && low != 0){
low--;
}
return low;
}
public static long upperbound(long a[], long ddp){
long low = 0;
long high = a.length;
long mid = 0;
while(low < high){
mid = low + (high - low) / 2;
if(a[(int)mid] < ddp){
low = mid + 1;;
}else{
high = mid;
}
}
if(low == a.length){
return a.length - 1;
}
return low;
}
public static class pair{
long w;
long h;
public pair(long w, long h){
this.w = w;
this.h = h;
}
}
public static class trinary{
long a;
long b;
long c;
public trinary(long a, long b, long c){
this.a = a;
this.b = b;
this.c = c;
}
}
public static long lowerboundforpairs(pair a[], long pr){
long low = 0;
long high = a.length;
long mid = 0;
while(low < high){
mid = low + (high - low)/2;
if(a[(int)mid].w <= pr){
low = mid + 1;
}else{
high = mid;
}
}
// if(low + 1 < a.length && a[(int)low + 1] <= ddp){
// low++;
// }
// if(low == a.length && low != 0){
// low--;
// return low;
// }
// if(a[(int)low].w > pr && low != 0){
// low--;
// }
return low;
}
public static pair[] sortpair(pair[] a){
Arrays.sort(a, new Comparator<pair>() {
public int compare(pair p1, pair p2){
return (int)p1.w - (int)p2.w;
}
});
return a;
}
public static boolean ispalindrome(String s){
long i = 0;
long j = s.length() - 1;
boolean is = false;
while(i < j){
if(s.charAt((int)i) == s.charAt((int)j)){
is = true;
i++;
j--;
}else{
is = false;
return is;
}
}
return is;
}
public static void sort(long[] arr) {
ArrayList<Long> a = new ArrayList<>();
for (long i : arr) {
a.add(i);
}
Collections.sort(a);
for (int i = 0; i < a.size(); i++) {
arr[i] = a.get(i);
}
}
public static void sortForObjecttypes(Long[] arr) {
ArrayList<Long> a = new ArrayList<>();
for (Long i : arr) {
a.add(i);
}
Collections.sort(a);
for (int i = 0; i < a.size(); i++) {
arr[i] = a.get(i);
}
}
public static void main(String[] args) throws java.lang.Exception{
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
long o = sc.nextLong();
while(o > 0){
long n = sc.nextLong();
sc.nextLine();
String s = sc.nextLine();
Vector<Long> v = new Vector<>();
long ones = -1;
long zeros = -1;
long onesc = 0;
long zerosc = 0;
for(long i = 0;i < n;i++){
if(s.charAt((int)i) == '0'){
zerosc++;
if(ones != -1 && ones != 0){
v.add(ones);
ones = 0;
}
if(zeros == -1){
zeros = 0;
}
zeros++;
}else{
onesc++;
if(zeros != -1 && zeros != 0){
v.add(zeros);
zeros = 0;
}
if(ones == -1){
ones = 0;
}
ones++;
}
}
if(zeros != 0 && zeros != -1){
v.add(zeros);
}
if(ones != 0 && ones != -1){
v.add(ones);
}
long a[] = new long[v.size()];
long id = 0;
for(Long k : v){
// System.out.print(k + " ");
a[(int)id] = k;
id++;
}
// System.out.println();
boolean is = false;
for(long i = 0;i < v.size();i++){
if(a[(int)i] % 2 == 0){
is = true;
}else{
is = false;
break;
}
}
if(!is){
if(v.size() == 1){
if(a[0] % 2 == 0){
System.out.println(0);
}
}else{
long ans = 0;
for(long i = 0;i < v.size() - 1;i++){
if(a[(int)i] % 2 != 0){
ans++;
a[(int)i] = a[(int)i] + 1;
a[(int)i + 1] = a[(int)i + 1] - 1;
}
// System.out.print(a[(int)i] + " ");
}
// System.out.print(a[v.size() - 1] + " ");
// System.out.println();
if(a[v.size() - 2] % 2 != 0 || a[v.size() - 1] % 2 != 0){
ans++;
}
System.out.println(Math.min(ans, Math.min(zerosc, onesc)));
}
}else{
System.out.println(0);
}
o--;
}
System.out.println(sb.toString());
}
} | 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 11 | 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 | 8235f8a3a6842b35ffad4727d557745d | 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 <Sting> void main(String args[]) {
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
while(test != 0){
int len = sc.nextInt();
String str = sc.next();
ArrayList<Integer> ans = new ArrayList<>();
int low = 0;
int high = 0;
while(high < str.length()) {
if(str.charAt(low) == str.charAt(high)) high++;
else {
ans.add((high)-low);
low = high;
}
}
ans.add((high)-low);
int min = 0;
for(int i =0;i<ans.size()-1;i++){
if(ans.get(i) == 0) continue;
if(ans.get(i)%2 != 0) {
ans.set(i+1,ans.get(i+1)-1);
min++;
}
}
System.out.println(min);
test--;
}
}
}
| 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 11 | 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 | 6a1dc072833db85e456292fc271ecb56 | 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 | /**
* @author egaeus
* @mail sebegaeusprogram@gmail.com
* @veredict Not sended
* @problemId 1678B1
* @problemName Tokitsukaze and Good 01-String (easy version)
* @judge http://codeforces.com/
* @category adhoc
* @level easy
* @date Thu May 12 2022
*/
import java.io.*;
import static java.lang.Integer.*;
import java.util.*;
public class CF1678B1 {
static ArrayList<Integer> list;
public static void main(String args[]) throws Throwable {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = parseInt(in.readLine());
for(int t = 0; t < T; t++ ) {
int N = parseInt(in.readLine());
char[] arr = in.readLine().toCharArray();
list = new ArrayList<>();
int last = 0;
for(int i=1;i<arr.length;i++) {
if(arr[i]!=arr[i-1]) {
list.add(i-last);
last = i;
}
}
list.add(N-last);
System.out.println(f(0,0));
}
System.out.print(new String(sb));
}
static int f(int p, int add) {
if(p==list.size())return 0;
if((list.get(p)+add)%2==1) {
return f(p+1, 1) + 1;
}
return f(p+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 11 | 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 | 6f0b06363e90200ec4f1c803132133ea | 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{
public 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 swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static class Pair{
int sib;
boolean inj;
Pair(int x, boolean y) {
sib=x;
inj=y;
}
}
static long mod = 1000000007;
static boolean ans= false;
static StringBuffer sb = new StringBuffer("");
public static void main(String[] args) throws Exception
{
//Read input from user
//Scanner scn = new Scanner(System.in);
FastReader scn = new FastReader();
int t = scn.nextInt();
while(t>0) {
int n = scn.nextInt();
String s = scn.next();
int ans=0;
int i=0;
int ss=0;
int eve=0;
int o=0;
while(i<n) {
int j=i;
int c=0;
while(j<n && s.charAt(j)==s.charAt(i)) {
c++;
j++;
}
if(c%2!=0 ) {
o++;
if(ss!=0) {
// System.out.println(eve);
ans+=eve;
ss=0;
}else {
ss++;
}
eve=0;
}else {
if(ss!=0) {
eve++;
}
}
i=j;
}
ans= ans+(o/2);
System.out.println(ans);
t--;
}
}
public static long gcd(long a,long b){
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static long lcm( long a,long b)
{
long g = gcd(a,b);
return (a*b)/g;
}
public static void find(long a, List<Long> div) {
for(long i=1;i<=Math.sqrt(a);i++) {
if(a%i==0) {
div.add(i);
if((a/i)!=i) {
div.add(a/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 11 | 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 | 39ba3847b3a197f80fc93bb88db818ce | 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 int mod = (int)1e9+7;
static boolean[] prime = new boolean[1];
static int[][] dir1 = new int[][] {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
static int[][] dir2 = new int[][] {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
static {
for (int i = 2; i < prime.length; i++)
prime[i] = true;
for (int i = 2; i < prime.length; i++) {
if (prime[i]) {
for (int k = 2; i * k < prime.length; k++) {
prime[i * k] = false;
}
}
}
}
static class JoinSet {
int[] fa;
JoinSet(int n) {
fa = new int[n];
for (int i = 0; i < n; i++)
fa[i] = i;
}
int find(int t) {
if (t != fa[t])
fa[t] = find(fa[t]);
return fa[t];
}
void join(int x, int y) {
x = find(x);
y = find(y);
fa[x] = y;
}
}
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static int get() throws Exception {
String ss = bf.readLine();
if (ss.contains(" "))
ss = ss.substring(0, ss.indexOf(" "));
return Integer.parseInt(ss);
}
static long getx() throws Exception {
String ss = bf.readLine();
if (ss.contains(" "))
ss = ss.substring(0, ss.indexOf(" "));
return Long.parseLong(ss);
}
static int[] getint() throws Exception {
String[] s = bf.readLine().split(" ");
int[] a = new int[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(s[i]);
}
return a;
}
static long[] getlong() throws Exception {
String[] s = bf.readLine().split(" ");
long[] a = new long[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Long.parseLong(s[i]);
}
return a;
}
static String getstr() throws Exception {
return bf.readLine();
}
static void println() throws Exception {
bw.write("\n");
}
static void print(int a) throws Exception {
bw.write(a + "\n");
}
static void print(long a) throws Exception {
bw.write(a + "\n");
}
static void print(String a) throws Exception {
bw.write(a + "\n");
}
static void print(int[] a) throws Exception {
for (int i : a) {
bw.write(i + " ");
}
println();
}
static void print(long[] a) throws Exception {
for (long i : a) {
bw.write(i + " ");
}
println();
}
static void print(int[][] a) throws Exception {
for (int i[] : a)
print(i);
}
static void print(long[][] a) throws Exception {
for (long[] i : a)
print(i);
}
static void print(char[] a) throws Exception {
for (char i : a) {
bw.write(i + "");
}
println();
}
static long pow(long a, long b) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans *= a;
}
a *= a;
b >>= 1;
}
return ans;
}
static int powmod(long a, long b, int mod) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return (int)ans;
}
static void sort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[i];
}
static void sort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[i];
}
static void resort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[n - 1 - i];
}
static void resort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[n - 1 - i];
}
static int max(int a, int b) {
return Math.max(a, b);
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long max(long a, long b) {
return Math.max(a, b);
}
static long min(long a, long b) {
return Math.min(a, b);
}
static int max(int[] a) {
int max = a[0];
for (int i : a)
max = max(max, i);
return max;
}
static int min(int[] a) {
int min = a[0];
for (int i : a)
min = min(min, i);
return min;
}
static long max(long[] a) {
long max = a[0];
for (long i : a)
max = max(max, i);
return max;
}
static long min(long[] a) {
long min = a[0];
for (long i : a)
min = min(min, i);
return min;
}
static int abs(int a) {
return Math.abs(a);
}
static long abs(long a) {
return Math.abs(a);
}
static void yes() throws Exception {
print("Yes");
}
static void no() throws Exception {
print("No");
}
static int[] getarr(List<Integer> list) {
int n = list.size();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = list.get(i);
return a;
}
public static void main(String[] args) throws Exception {
int t = get();
while (t-- > 0){
int n = get(), ans = 0;
String s = getstr();
for(int i = 0;i < n;i += 2){
if(s.charAt(i) != s.charAt(i+1)) ans ++;
}
print(ans);
}
bw.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 11 | 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 | 968dcb8b4b3e85c8b766222880e42b23 | 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 int mod = (int)1e9+7;
static boolean[] prime = new boolean[1];
static int[][] dir1 = new int[][] {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
static int[][] dir2 = new int[][] {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
static {
for (int i = 2; i < prime.length; i++)
prime[i] = true;
for (int i = 2; i < prime.length; i++) {
if (prime[i]) {
for (int k = 2; i * k < prime.length; k++) {
prime[i * k] = false;
}
}
}
}
static class JoinSet {
int[] fa;
JoinSet(int n) {
fa = new int[n];
for (int i = 0; i < n; i++)
fa[i] = i;
}
int find(int t) {
if (t != fa[t])
fa[t] = find(fa[t]);
return fa[t];
}
void join(int x, int y) {
x = find(x);
y = find(y);
fa[x] = y;
}
}
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static int get() throws Exception {
String ss = bf.readLine();
if (ss.contains(" "))
ss = ss.substring(0, ss.indexOf(" "));
return Integer.parseInt(ss);
}
static long getx() throws Exception {
String ss = bf.readLine();
if (ss.contains(" "))
ss = ss.substring(0, ss.indexOf(" "));
return Long.parseLong(ss);
}
static int[] getint() throws Exception {
String[] s = bf.readLine().split(" ");
int[] a = new int[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(s[i]);
}
return a;
}
static long[] getlong() throws Exception {
String[] s = bf.readLine().split(" ");
long[] a = new long[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Long.parseLong(s[i]);
}
return a;
}
static String getstr() throws Exception {
return bf.readLine();
}
static void println() throws Exception {
bw.write("\n");
}
static void print(int a) throws Exception {
bw.write(a + "\n");
}
static void print(long a) throws Exception {
bw.write(a + "\n");
}
static void print(String a) throws Exception {
bw.write(a + "\n");
}
static void print(int[] a) throws Exception {
for (int i : a) {
bw.write(i + " ");
}
println();
}
static void print(long[] a) throws Exception {
for (long i : a) {
bw.write(i + " ");
}
println();
}
static void print(int[][] a) throws Exception {
for (int i[] : a)
print(i);
}
static void print(long[][] a) throws Exception {
for (long[] i : a)
print(i);
}
static void print(char[] a) throws Exception {
for (char i : a) {
bw.write(i + "");
}
println();
}
static long pow(long a, long b) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans *= a;
}
a *= a;
b >>= 1;
}
return ans;
}
static int powmod(long a, long b, int mod) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return (int)ans;
}
static void sort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[i];
}
static void sort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[i];
}
static void resort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[n - 1 - i];
}
static void resort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[n - 1 - i];
}
static int max(int a, int b) {
return Math.max(a, b);
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long max(long a, long b) {
return Math.max(a, b);
}
static long min(long a, long b) {
return Math.min(a, b);
}
static int max(int[] a) {
int max = a[0];
for (int i : a)
max = max(max, i);
return max;
}
static int min(int[] a) {
int min = a[0];
for (int i : a)
min = min(min, i);
return min;
}
static long max(long[] a) {
long max = a[0];
for (long i : a)
max = max(max, i);
return max;
}
static long min(long[] a) {
long min = a[0];
for (long i : a)
min = min(min, i);
return min;
}
static int abs(int a) {
return Math.abs(a);
}
static long abs(long a) {
return Math.abs(a);
}
static void yes() throws Exception {
print("Yes");
}
static void no() throws Exception {
print("No");
}
static int[] getarr(List<Integer> list) {
int n = list.size();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = list.get(i);
return a;
}
public static void main(String[] args) throws Exception {
int t = get();
while (t-- > 0){
int n = get();
String s = getstr();
int ans = 0, pre = 0;
for(int i = 0;i < n;i++){
char c = s.charAt(i);
int x = pre;
while(i < n && s.charAt(i) == c){
i++;
x++;
}
i--;
if(x%2==0) {
pre = 0;
continue;
}
pre = 1;
ans++;
}
print(ans);
}
bw.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 11 | 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 | a6ec05f79dbac56b728ec0eff6146ca2 | 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.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
in = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
try {
int t = in.nextInt(); while(t-- > 0) { solve(); out.println();}
// solve();
} finally {
out.close();
}
return;
}
public static void solve() {
int n = in.nextInt();
char[] s = fillArray();
int ans = 0;
for(int i = 0; i + 1 < n; i +=2) {
if(s[i] != s[i+1]) {
ans++;
}
}
out.print(ans);
}
//-------------- Helper methods-------------------
public static int[] fillArray(int n) {
int[] array = new int[n];
for(int i = 0; i < n; i++) {
array[i] = in.nextInt();
}
return array;
}
public static char[] fillArray() {
char[] array = in.next().toCharArray();
return array;
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
public static MyScanner in;
//-----------MyScanner class for faster input----------x§x
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;
}
}
static void shuffleArray(int[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
//--------------------------------------------------------
}
| 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 11 | 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 | 2bdfc8a7ce63b1bfc0515ff2c3460096 | 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.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
in = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
try {
int t = in.nextInt(); while(t-- > 0) { solve(); out.println();}
// solve();
} finally {
out.close();
}
return;
}
public static void solve() {
int n = in.nextInt();
char[] s = fillArray();
Vector<Integer> vec = new Vector<Integer>();
for(int i = 0; i < n; i++) {
int cnt = 0;
int j = i;
while(j < n && s[i] == s[j]) {
cnt++;
j++;
}
// either j == n or a[j] != a[i];
vec.add(cnt);
i = j - 1;
}
int ans = 0;
int carry = 0;
for(int i = 0; i < vec.size(); i++) {
int el = vec.get(i);
if( (el+carry) %2 == 0) {
carry = 0;
continue;
}
ans++;
carry = 1;
}
out.print(ans);
}
//-------------- Helper methods-------------------
public static int[] fillArray(int n) {
int[] array = new int[n];
for(int i = 0; i < n; i++) {
array[i] = in.nextInt();
}
return array;
}
public static char[] fillArray() {
char[] array = in.next().toCharArray();
return array;
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
public static MyScanner in;
//-----------MyScanner class for faster input----------x§x
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;
}
}
static void shuffleArray(int[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
//--------------------------------------------------------
}
| 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 11 | 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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.